| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package XAS::Lib::Spawn; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
my $mixin; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
BEGIN { |
|
8
|
1
|
|
|
1
|
|
735
|
$mixin = 'XAS::Lib::Spawn::Unix'; |
|
9
|
1
|
50
|
|
|
|
25
|
$mixin = 'XAS::Lib::Spawn::Win32' if ($^O eq 'MSWin32'); |
|
10
|
|
|
|
|
|
|
} |
|
11
|
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
5
|
use Hash::Merge; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
31
|
|
|
13
|
1
|
|
|
1
|
|
4
|
use Badger::Filesystem 'Cwd File'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
6
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use XAS::Class |
|
16
|
1
|
|
|
|
|
10
|
debug => 0, |
|
17
|
|
|
|
|
|
|
version => $VERSION, |
|
18
|
|
|
|
|
|
|
base => 'XAS::Base', |
|
19
|
|
|
|
|
|
|
mixin => "XAS::Lib::Mixins::Process $mixin", |
|
20
|
|
|
|
|
|
|
utils => 'dotid trim', |
|
21
|
|
|
|
|
|
|
accessors => 'merger', |
|
22
|
|
|
|
|
|
|
vars => { |
|
23
|
|
|
|
|
|
|
PARAMS => { |
|
24
|
|
|
|
|
|
|
-command => 1, |
|
25
|
|
|
|
|
|
|
-priority => { optional => 1, default => 0 }, |
|
26
|
|
|
|
|
|
|
-environment => { optional => 1, default => {} }, |
|
27
|
|
|
|
|
|
|
-umask => { optional => 1, default => '0022' }, |
|
28
|
|
|
|
|
|
|
-group => { optional => 1, default => 'nobody' }, |
|
29
|
|
|
|
|
|
|
-user => { optional => 1, default => 'nobody' }, |
|
30
|
|
|
|
|
|
|
-directory => { optional => 1, default => Cwd, isa => 'Badger::Filesystem::Directory' }, |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
} |
|
33
|
1
|
|
|
1
|
|
172
|
; |
|
|
1
|
|
|
|
|
2
|
|
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#use Data::Dumper; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
38
|
|
|
|
|
|
|
# Public Methods |
|
39
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
42
|
|
|
|
|
|
|
# Private Methods |
|
43
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub init { |
|
46
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
47
|
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $self = $class->SUPER::init(@_); |
|
49
|
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
$self->{'merger'} = Hash::Merge->new('RIGHT_PRECEDENT'); |
|
51
|
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return $self; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _resolve_path { |
|
57
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
58
|
0
|
|
|
|
|
|
my $command = shift; |
|
59
|
0
|
|
|
|
|
|
my $extensions = shift; |
|
60
|
0
|
|
|
|
|
|
my $xpaths = shift; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Make the path to the progam absolute if it isn't already. If the |
|
63
|
|
|
|
|
|
|
# path is not absolute and if the path contains a directory element |
|
64
|
|
|
|
|
|
|
# separator, then only prepend the current working to it. If the |
|
65
|
|
|
|
|
|
|
# path is not absolute, then look through the PATH environment to |
|
66
|
|
|
|
|
|
|
# find the executable. |
|
67
|
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $path = File($command); |
|
69
|
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
if ($path->is_absolute) { |
|
|
|
0
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
if ($path->exists) { |
|
73
|
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return $path->absolute; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} elsif ($path->is_relative) { |
|
79
|
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
if ($path->name eq $path) { |
|
81
|
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
foreach my $xpath (@$xpaths) { |
|
83
|
|
|
|
|
|
|
|
|
84
|
0
|
0
|
|
|
|
|
next if ($xpath eq ''); |
|
85
|
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if ($path->extension) { |
|
87
|
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my $p = File($xpath, $path->name); |
|
89
|
|
|
|
|
|
|
|
|
90
|
0
|
0
|
|
|
|
|
if ($p->exists) { |
|
91
|
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return $p->absolute; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
} else { |
|
97
|
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
foreach my $ext (@$extensions) { |
|
99
|
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my $p = File($xpath, $path->basename . $ext); |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if ($p->exists) { |
|
103
|
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
return $p->absolute; |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
} else { |
|
115
|
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $p = File($path->absoulte); |
|
117
|
|
|
|
|
|
|
|
|
118
|
0
|
0
|
|
|
|
|
if ($p->exists) { |
|
119
|
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
return $p->absolute; |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$self->throw_msg( |
|
129
|
0
|
|
|
|
|
|
dotid($self->class) . '.resolve_path.path', |
|
130
|
|
|
|
|
|
|
'process_location', |
|
131
|
|
|
|
|
|
|
$command |
|
132
|
|
|
|
|
|
|
); |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
1; |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
__END__ |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 NAME |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
XAS::Lib::Spawn - A class to spawn detached processes within the XAS environment |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
use XAS::Lib::Spawn; |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $process = XAS::Lib::Spawn->new( |
|
149
|
|
|
|
|
|
|
-command => 'perl test.pl' |
|
150
|
|
|
|
|
|
|
); |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$process->run(); |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
This class spawns a process to run in the back ground in a platform |
|
157
|
|
|
|
|
|
|
independent way. Mixins are loaded to handle the differences between |
|
158
|
|
|
|
|
|
|
Unix/Linux and Windows. |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 METHODS |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 new |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This method initialized the module and takes the following parameters: |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=over 4 |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item B<-command> |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The command to run. |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item B<-directory> |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The optional directory to start the process in. Defaults to the current |
|
175
|
|
|
|
|
|
|
directory of the parent process. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item B<-environment> |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Optional, addtional environmnet variables to provide to the process. |
|
180
|
|
|
|
|
|
|
The default is none. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item B<-group> |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
The group to run the process under. Defaults to 'nobody'. This group |
|
185
|
|
|
|
|
|
|
may not be defined on your system. This option is not implemented on Windows. |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item B<-priority> |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
The optional priority to run the process at. Defaults to 0. This option |
|
190
|
|
|
|
|
|
|
is not implemented on Windows. |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item B<-umask> |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
The optional protection mask for the process. Defaults to '0022'. This |
|
195
|
|
|
|
|
|
|
option is not implemented on Windows. |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item B<-user> |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
The optional user to run the process under. Defaults to 'nobody'. This user |
|
200
|
|
|
|
|
|
|
may not be defined on your system. This option is not implemented on Windows. |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=back |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 run |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Start the process. It returns the pid of that process. |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head2 status |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Returns the status of the process. The status could be one of the follow: |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=over 4 |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item 6 - suspended ready |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item 5 - suspended blocked |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item 4 - blocked |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item 3 - running |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item 2 - ready |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item 1 - other |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=item 0 - unknown |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=back |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head2 pause |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Pause the process, returns 1 on success. |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 resume |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Resume the process, returns 1 on success. |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head2 stop |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Stop the process, returns 1 on success. |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head2 kill |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
Kill the process, returns 1 on success. |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head2 wait |
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Waits for the process to finish, returns 0 when the process is done. |
|
249
|
|
|
|
|
|
|
This method may return a -1 if the processes was reaped before the |
|
250
|
|
|
|
|
|
|
wait is called. |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head2 errorlevel |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
Returns the exit code of the process, or a -1 if the exit code is not |
|
255
|
|
|
|
|
|
|
available. |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=over 4 |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item L<XAS::Lib::Spawn::Unix|XAS::Lib::Spawn::Unix> |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=item L<XAS::Lib::Spawn::Win32|XAS::Lib::Spawn::Win32> |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item L<XAS::Lib::Process|XAS::Lib::Process> |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=item L<XAS|XAS> |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=back |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 AUTHOR |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
Copyright (c) 2012-2016 Kevin L. Esteb |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
280
|
|
|
|
|
|
|
the terms of the Artistic License 2.0. For details, see the full text |
|
281
|
|
|
|
|
|
|
of the license at http://www.perlfoundation.org/artistic_license_2_0. |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=cut |