22436
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
12890
|
3 |
"{ Package: 'stx:libbasic' }"
|
|
4 |
|
19286
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
12890
|
7 |
Object subclass:#OSProcess
|
21182
|
8 |
instanceVariableNames:'pid command environment directory inStream outStream errorStream
|
|
9 |
auxStream showWindow lineWise newPgrp exitStatus finishSema
|
21206
|
10 |
shufflerProcesses streamsToClose terminateActionBlock'
|
12890
|
11 |
classVariableNames:''
|
|
12 |
poolDictionaries:''
|
|
13 |
category:'System-Support'
|
|
14 |
!
|
|
15 |
|
22198
|
16 |
OSProcess subclass:#RemoteOSProcess
|
|
17 |
instanceVariableNames:'host'
|
|
18 |
classVariableNames:'MethodPerHost'
|
|
19 |
poolDictionaries:''
|
|
20 |
privateIn:OSProcess
|
|
21 |
!
|
|
22 |
|
12890
|
23 |
!OSProcess class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
documentation
|
|
26 |
"
|
21552
|
27 |
Instances of OSProcess represent operating system processes that can be executed.
|
12890
|
28 |
(as opposed to Smalltalk processes).
|
22439
|
29 |
Both local and remote processes are suported.
|
|
30 |
|
|
31 |
commandString:
|
|
32 |
If command is a String, the commandString is passed to a shell for execution
|
|
33 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
|
34 |
Regular input/output redirection and pipes etc. are supported as supported by
|
|
35 |
the underlying OS's command interpreter (i.e. some limits apply to MSDOS)
|
|
36 |
|
|
37 |
With an empty command, the shell will read commands from the passed input stream.
|
|
38 |
|
|
39 |
If command is an Array, the first element is the command to be executed,
|
|
40 |
and the other elements are the arguments to the command.
|
|
41 |
No shell is invoked in this case. Any redirection or piping can then be done
|
|
42 |
by Smalltalk code (including piping through smalltalk streams & filters).
|
12890
|
43 |
|
|
44 |
[author:]
|
|
45 |
Stefan Vogel (stefan@zwerg)
|
|
46 |
|
|
47 |
[instance variables:]
|
|
48 |
pid SmallInteger the process id
|
21182
|
49 |
command String the command line of the running command.
|
12890
|
50 |
|
|
51 |
[class variables:]
|
|
52 |
|
|
53 |
[see also:]
|
|
54 |
Process
|
|
55 |
|
|
56 |
"
|
21181
|
57 |
!
|
|
58 |
|
|
59 |
examples
|
21512
|
60 |
"
|
22439
|
61 |
send command's output to the Transcript:
|
|
62 |
[exBegin]
|
|
63 |
OSProcess new
|
|
64 |
command:'ls -l';
|
|
65 |
outStream:Transcript;
|
|
66 |
lineWise:true;
|
|
67 |
execute.
|
|
68 |
[exEnd]
|
|
69 |
|
|
70 |
send command's output to my stdout (see console):
|
|
71 |
[exBegin]
|
|
72 |
OSProcess new
|
|
73 |
command:'ls -l';
|
|
74 |
outStream:Stdout;
|
|
75 |
execute.
|
|
76 |
[exEnd]
|
|
77 |
|
|
78 |
send command's output to a Smalltalk stream:
|
22197
|
79 |
[exBegin]
|
22198
|
80 |
|outStream|
|
21181
|
81 |
|
22198
|
82 |
outStream := '' writeStream.
|
21181
|
83 |
|
22198
|
84 |
OSProcess new
|
|
85 |
command:'ls -l';
|
|
86 |
outStream:outStream;
|
|
87 |
execute.
|
21181
|
88 |
|
22439
|
89 |
outStream contents inspect.
|
22197
|
90 |
[exEnd]
|
21184
|
91 |
|
22439
|
92 |
feed it from a smalltalk stream, get command's output into a Smalltalk stream:
|
|
93 |
[exBegin]
|
|
94 |
|inStream outStream|
|
|
95 |
|
|
96 |
inStream := 'hello world' readStream.
|
|
97 |
outStream := '' writeStream.
|
|
98 |
|
|
99 |
OSProcess new
|
|
100 |
command:'tr ''a-z'' ''A-Z''';
|
|
101 |
inStream:inStream;
|
|
102 |
outStream:outStream;
|
|
103 |
execute.
|
|
104 |
|
|
105 |
outStream contents inspect.
|
|
106 |
[exEnd]
|
|
107 |
|
|
108 |
again, send output to a Smalltalk stream;
|
|
109 |
watch the stream getting filled after 10 seconds:
|
22197
|
110 |
[exBegin]
|
22198
|
111 |
|outStream|
|
21184
|
112 |
|
22198
|
113 |
outStream := '' writeStream.
|
21184
|
114 |
|
22198
|
115 |
OSProcess new
|
|
116 |
command:'ls -l; sleep 10; echo =================================; echo hallo after 10s; echo >&2 +++++++++++++++++++++; cat >&2';
|
|
117 |
outStream:outStream;
|
|
118 |
errorStream:outStream;
|
|
119 |
startProcess.
|
21184
|
120 |
|
22198
|
121 |
outStream inspect
|
22197
|
122 |
[exEnd]
|
21184
|
123 |
|
22439
|
124 |
separate stdout and stderr:
|
22197
|
125 |
[exBegin]
|
22439
|
126 |
|outStream errStream|
|
21184
|
127 |
|
22198
|
128 |
outStream := '' writeStream.
|
22439
|
129 |
errStream := '' writeStream.
|
21184
|
130 |
|
22198
|
131 |
OSProcess new
|
22439
|
132 |
command:'ls -l && ls >&2';
|
22198
|
133 |
outStream:outStream;
|
22439
|
134 |
errorStream:errStream;
|
22198
|
135 |
startProcess.
|
21184
|
136 |
|
22439
|
137 |
outStream inspect.
|
|
138 |
errStream inspect.
|
22197
|
139 |
[exEnd]
|
21512
|
140 |
|
22439
|
141 |
Execute commands in shell/cmd.exe and read them from stdin:
|
22197
|
142 |
[exBegin]
|
22198
|
143 |
|outStream|
|
21512
|
144 |
|
22198
|
145 |
outStream := '' writeStream.
|
21512
|
146 |
|
22198
|
147 |
OSProcess new
|
|
148 |
command:'';
|
|
149 |
inStream:'ls -l' readStream;
|
|
150 |
outStream:outStream;
|
|
151 |
lineWise:true;
|
|
152 |
execute.
|
21512
|
153 |
|
22198
|
154 |
outStream contents inspect
|
22197
|
155 |
[exEnd]
|
22439
|
156 |
|
|
157 |
Execute on a remote host (you must have the appropriate ssh setup):
|
|
158 |
[exBegin]
|
|
159 |
|outStream|
|
|
160 |
|
|
161 |
outStream := '' writeStream.
|
|
162 |
|
|
163 |
(OSProcess onHost:'exeptn')
|
|
164 |
command:'ls -l';
|
|
165 |
outStream:outStream;
|
|
166 |
lineWise:true;
|
|
167 |
execute.
|
|
168 |
|
|
169 |
outStream contents inspect
|
|
170 |
[exEnd]
|
21512
|
171 |
"
|
21181
|
172 |
! !
|
|
173 |
|
|
174 |
!OSProcess class methodsFor:'instance creation'!
|
|
175 |
|
22437
|
176 |
command:aCommandString
|
22439
|
177 |
"return an initialized instance to execute aCommandString
|
|
178 |
in the current directory (for a local process)"
|
22437
|
179 |
|
|
180 |
^ self new command:aCommandString.
|
|
181 |
!
|
|
182 |
|
22438
|
183 |
command:aCommandString directory:aStringOrFilename
|
22439
|
184 |
"return an initialized instance to execute aCommandString
|
|
185 |
in a given directory (for a local process)"
|
22437
|
186 |
|
22438
|
187 |
^ self new command:aCommandString directory:aStringOrFilename.
|
22437
|
188 |
!
|
|
189 |
|
21181
|
190 |
new
|
22198
|
191 |
"return an initialized instance for a local process"
|
21181
|
192 |
|
|
193 |
^ self basicNew initialize.
|
22198
|
194 |
!
|
|
195 |
|
|
196 |
onHost:aHost
|
|
197 |
"return an initialized instance for a remote process running on another host"
|
|
198 |
|
|
199 |
(SocketAddress hostName:aHost) isLocal ifTrue:[
|
|
200 |
^ self basicNew initialize.
|
|
201 |
].
|
|
202 |
^ RemoteOSProcess basicNew host:aHost; initialize.
|
|
203 |
|
|
204 |
"
|
|
205 |
OSProcess onHost:'localhost'
|
|
206 |
OSProcess onHost:'exeptn'
|
22439
|
207 |
|
|
208 |
(OSProcess new) command:'ls'
|
|
209 |
(OSProcess onHost:'exeptn') command:'ls'
|
22198
|
210 |
"
|
12890
|
211 |
! !
|
|
212 |
|
22439
|
213 |
!OSProcess class methodsFor:'class initialization'!
|
21172
|
214 |
|
|
215 |
initialize
|
|
216 |
"Backward compatibility"
|
|
217 |
Win32Process := self.
|
|
218 |
! !
|
|
219 |
|
12890
|
220 |
!OSProcess methodsFor:'accessing'!
|
|
221 |
|
21181
|
222 |
auxStream
|
|
223 |
^ auxStream
|
|
224 |
!
|
|
225 |
|
|
226 |
auxStream:something
|
21552
|
227 |
"set an auxiliary input stream that will be available to the command as
|
21208
|
228 |
file descriptor 3"
|
|
229 |
|
21181
|
230 |
auxStream := something.
|
21552
|
231 |
|
|
232 |
"Modified (comment): / 23-02-2017 / 10:56:57 / Maren"
|
21181
|
233 |
!
|
|
234 |
|
21172
|
235 |
command
|
22438
|
236 |
"the OS (shell-) command"
|
21172
|
237 |
|
|
238 |
^ command
|
|
239 |
|
|
240 |
"Created: / 10.11.1998 / 21:27:07 / cg"
|
|
241 |
!
|
|
242 |
|
22437
|
243 |
command:aStringOrArray
|
21208
|
244 |
"set the command to be executed.
|
22439
|
245 |
|
21208
|
246 |
If aStringOrArray is a String, the commandString is passed to a shell for execution
|
|
247 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
22439
|
248 |
With an empty command, the shell will read commands from the passed input stream.
|
|
249 |
|
21208
|
250 |
If aCommandString is an Array, the first element is the command to be executed,
|
22439
|
251 |
and the other elements are the arguments to the command.
|
|
252 |
No shell is invoked in this case."
|
22437
|
253 |
|
21208
|
254 |
command := aStringOrArray.
|
21172
|
255 |
!
|
|
256 |
|
22436
|
257 |
command:commandStringArg directory:stringOrFilenameArg
|
22438
|
258 |
"set the command to be executed and directory, where to execute.
|
|
259 |
If aStringOrArray is a String, the commandString is passed to a shell for execution
|
|
260 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
|
261 |
If aCommandString is an Array, the first element is the command to be executed,
|
|
262 |
and the other elements are the arguments to the command. No shell is invoked in this case."
|
|
263 |
|
22436
|
264 |
command := commandStringArg.
|
|
265 |
directory := stringOrFilenameArg.
|
|
266 |
!
|
|
267 |
|
22181
|
268 |
command:commandArg environment:environmentArg directory:directoryArg inStream:inStreamArg outStream:outStreamArg errorStream:errorStreamArg auxStream:auxStreamArg showWindow:showWindowArg lineWise:lineWiseArg
|
22438
|
269 |
"set the command to be executed & directory, where to execute.
|
|
270 |
and input/output streams.
|
|
271 |
See comments in individual setters for more info"
|
|
272 |
|
22181
|
273 |
command := commandArg.
|
|
274 |
environment := environmentArg.
|
|
275 |
directory := directoryArg.
|
|
276 |
inStream := inStreamArg.
|
|
277 |
outStream := outStreamArg.
|
|
278 |
errorStream := errorStreamArg.
|
|
279 |
auxStream := auxStreamArg.
|
|
280 |
showWindow := showWindowArg.
|
|
281 |
lineWise := lineWiseArg.
|
|
282 |
!
|
|
283 |
|
21172
|
284 |
directory
|
22438
|
285 |
"the directory where executed"
|
21172
|
286 |
|
|
287 |
^ directory
|
|
288 |
|
|
289 |
"Created: / 10.11.1998 / 21:21:52 / cg"
|
|
290 |
!
|
|
291 |
|
22436
|
292 |
directory:aStringOrFilename
|
21208
|
293 |
"set the directory that will be set as the current directory of the command to be executed"
|
21172
|
294 |
|
22436
|
295 |
directory := aStringOrFilename.
|
21172
|
296 |
!
|
|
297 |
|
|
298 |
environment
|
22438
|
299 |
"the shell environment"
|
21172
|
300 |
|
|
301 |
^ environment
|
|
302 |
|
|
303 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
304 |
!
|
|
305 |
|
21208
|
306 |
environment:aDictionary
|
|
307 |
"set the environment variables of the command to be executed"
|
21172
|
308 |
|
21208
|
309 |
environment := aDictionary.
|
21172
|
310 |
!
|
|
311 |
|
|
312 |
errorStream
|
|
313 |
^ errorStream
|
|
314 |
|
|
315 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
12890
|
316 |
!
|
|
317 |
|
21208
|
318 |
errorStream:aStream
|
|
319 |
"set the stream where the stderr output of the command is directed to"
|
21172
|
320 |
|
21208
|
321 |
errorStream := aStream.
|
21172
|
322 |
!
|
|
323 |
|
|
324 |
exitStatus
|
21208
|
325 |
"answer the exit status of the command or nil, if the command has not yet been finished"
|
21172
|
326 |
|
|
327 |
^ exitStatus
|
|
328 |
!
|
|
329 |
|
|
330 |
exitStatus:something
|
|
331 |
"set the value of the instance variable 'exitStatus' (automatically generated)"
|
|
332 |
|
|
333 |
exitStatus := something.
|
|
334 |
|
|
335 |
"Created: / 10.11.1998 / 21:24:55 / cg"
|
|
336 |
!
|
|
337 |
|
|
338 |
finishSema
|
21552
|
339 |
"wait on this semaphore if you want to wait until the OS process has finished.
|
21184
|
340 |
There may be multiple waiters, so it is a good idea to do a #waitUncounted"
|
21172
|
341 |
|
|
342 |
^ finishSema
|
21552
|
343 |
|
|
344 |
"Modified (comment): / 23-02-2017 / 10:53:17 / Maren"
|
21172
|
345 |
!
|
|
346 |
|
|
347 |
inStream
|
|
348 |
^ inStream
|
|
349 |
|
|
350 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
351 |
!
|
|
352 |
|
21208
|
353 |
inStream:aStream
|
|
354 |
"set the stream where the stdin input of the command is read from"
|
21172
|
355 |
|
21208
|
356 |
inStream := aStream.
|
21172
|
357 |
!
|
|
358 |
|
21181
|
359 |
lineWise
|
|
360 |
^ lineWise
|
|
361 |
!
|
|
362 |
|
21208
|
363 |
lineWise:aBoolean
|
21209
|
364 |
"When setting to true, read linewise from the command's output and error.
|
|
365 |
This is a bit slower than lineWise = false.
|
21208
|
366 |
|
|
367 |
You may use it also when streaming to e.g. Transcript"
|
|
368 |
|
|
369 |
lineWise := aBoolean.
|
21181
|
370 |
!
|
|
371 |
|
21182
|
372 |
newPgrp
|
|
373 |
^ newPgrp
|
|
374 |
!
|
|
375 |
|
21208
|
376 |
newPgrp:aBoolean
|
|
377 |
"if aBoolean is true, a new process group will be created for the command and its subprocesses"
|
|
378 |
|
|
379 |
newPgrp := aBoolean.
|
21182
|
380 |
!
|
|
381 |
|
21172
|
382 |
outStream
|
|
383 |
^ outStream
|
|
384 |
|
|
385 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
386 |
!
|
|
387 |
|
21208
|
388 |
outStream:aStream
|
|
389 |
"set the stream where the stdout output of the command is directed to"
|
21172
|
390 |
|
21208
|
391 |
outStream := aStream.
|
12890
|
392 |
!
|
|
393 |
|
|
394 |
pid
|
21208
|
395 |
"answer the pid of the process the command is running in or nil, if the command has not yet been started."
|
|
396 |
|
12890
|
397 |
^ pid
|
|
398 |
!
|
|
399 |
|
|
400 |
pid:something
|
|
401 |
pid := something.
|
21181
|
402 |
!
|
|
403 |
|
|
404 |
showWindow
|
|
405 |
^ showWindow
|
|
406 |
!
|
|
407 |
|
21208
|
408 |
showWindow:aBooleanOrNil
|
|
409 |
"This parameter is ignored on Unix systems.
|
|
410 |
|
|
411 |
You can control (have to - sigh) if a window should be shown for the command or not.
|
|
412 |
This is the OS's H_SHOWWINDOW argument.
|
|
413 |
If you pass nil as showWindow-argument, the OS's default is used for the particular
|
|
414 |
command, which is correct most of the time: i.e. a notepad will open its window, other (non-UI)
|
|
415 |
executables will not.
|
|
416 |
However, some command-line executables show a window, even if they should not.
|
|
417 |
(and also, there seems to be an inconsistency between windows7 and newer windows: in newer,
|
|
418 |
a shell command opens a cmd-window, whereas in windows7 it did not)
|
|
419 |
In this case, pass an explicit false argument to suppress it."
|
|
420 |
|
|
421 |
showWindow := aBooleanOrNil.
|
21206
|
422 |
!
|
|
423 |
|
|
424 |
terminateActionBlock
|
|
425 |
^ terminateActionBlock
|
|
426 |
!
|
|
427 |
|
21208
|
428 |
terminateActionBlock:aBlock
|
|
429 |
"set the block that will be executed when the command has been finished or terminated."
|
|
430 |
|
|
431 |
terminateActionBlock := aBlock.
|
12890
|
432 |
! !
|
|
433 |
|
21181
|
434 |
!OSProcess methodsFor:'initialization'!
|
|
435 |
|
|
436 |
initialize
|
|
437 |
"Invoked when a new instance is created."
|
|
438 |
|
|
439 |
showWindow := false. "/ for backward compatibility
|
21183
|
440 |
lineWise := false.
|
|
441 |
newPgrp := true.
|
21181
|
442 |
! !
|
|
443 |
|
13313
|
444 |
!OSProcess methodsFor:'printing'!
|
|
445 |
|
|
446 |
printOn:aStream
|
|
447 |
aStream
|
|
448 |
nextPutAll:self className;
|
|
449 |
nextPut:$(.
|
|
450 |
pid printOn:aStream.
|
|
451 |
aStream space.
|
21172
|
452 |
command printOn:aStream.
|
13313
|
453 |
aStream nextPut:$)
|
|
454 |
! !
|
|
455 |
|
21181
|
456 |
!OSProcess methodsFor:'private'!
|
|
457 |
|
|
458 |
setupShufflerForInput:aStream
|
21182
|
459 |
"if aStream is an internal Stream, set up a pipe for the command input.
|
21552
|
460 |
Start a process that shuffles the data from the internal stream into the pipe
|
21182
|
461 |
(and into the command's input)."
|
|
462 |
|
21181
|
463 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
464 |
|
|
465 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
466 |
^ aStream.
|
|
467 |
].
|
|
468 |
|
|
469 |
pipe := NonPositionableExternalStream makePipe.
|
|
470 |
externalStream := pipe at:1.
|
|
471 |
shuffledStream := pipe at:2.
|
|
472 |
lineWise ifFalse:[
|
|
473 |
shuffledStream blocking:false.
|
|
474 |
].
|
|
475 |
aStream isBinary ifTrue:[
|
|
476 |
shuffledStream binary.
|
|
477 |
].
|
|
478 |
|
|
479 |
"/ start a reader process, shuffling data from the given
|
|
480 |
"/ inStream to the pipe (which is connected to the commands input)
|
22198
|
481 |
shufflerProcess :=
|
|
482 |
[
|
21181
|
483 |
[
|
21332
|
484 |
lineWise ifTrue:[
|
|
485 |
"shuffle until end-of-input"
|
|
486 |
[aStream atEnd] whileFalse:[
|
|
487 |
|data|
|
21184
|
488 |
|
21332
|
489 |
data := aStream nextLine.
|
|
490 |
data notNil ifTrue:[
|
|
491 |
shuffledStream nextPutLine:data.
|
|
492 |
shuffledStream flush.
|
21184
|
493 |
].
|
|
494 |
].
|
21332
|
495 |
] ifFalse:[
|
|
496 |
aStream copyToEndInto:shuffledStream.
|
21184
|
497 |
].
|
21332
|
498 |
] on:WriteError do:[:ex|
|
|
499 |
"ignore"
|
21181
|
500 |
] ensure:[
|
|
501 |
shuffledStream close.
|
|
502 |
]
|
|
503 |
] newProcess
|
|
504 |
name:'OSProcess input shuffler';
|
|
505 |
"/ beSystemProcess;
|
|
506 |
resume.
|
|
507 |
|
|
508 |
shufflerProcesses add:shufflerProcess.
|
21184
|
509 |
streamsToClose add:externalStream.
|
|
510 |
|
21181
|
511 |
^ externalStream
|
21332
|
512 |
|
|
513 |
"Modified: / 31-01-2017 / 16:50:39 / stefan"
|
21552
|
514 |
"Modified (comment): / 23-02-2017 / 10:51:34 / Maren"
|
21181
|
515 |
!
|
|
516 |
|
|
517 |
setupShufflerForOutput:aStream
|
21182
|
518 |
"if aStream is an internal Stream, set up a pipe for the command output.
|
|
519 |
Start a process that shuffles the data fron the pipe into the internal stream."
|
|
520 |
|
21181
|
521 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
522 |
|
|
523 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
524 |
^ aStream.
|
|
525 |
].
|
|
526 |
|
|
527 |
pipe := NonPositionableExternalStream makePipe.
|
|
528 |
externalStream := pipe at:2.
|
|
529 |
shuffledStream := pipe at:1.
|
|
530 |
aStream isBinary ifTrue:[
|
|
531 |
shuffledStream binary.
|
|
532 |
].
|
|
533 |
|
22198
|
534 |
shufflerProcess :=
|
|
535 |
[
|
21332
|
536 |
[
|
|
537 |
"shuffle until the pipe closes"
|
|
538 |
lineWise ifTrue:[
|
|
539 |
[shuffledStream atEnd] whileFalse:[
|
|
540 |
|data|
|
21184
|
541 |
|
21332
|
542 |
data := shuffledStream nextLine.
|
|
543 |
data notNil ifTrue:[
|
|
544 |
aStream nextPutLine:data
|
21184
|
545 |
].
|
|
546 |
].
|
21332
|
547 |
] ifFalse:[
|
|
548 |
shuffledStream copyToEndInto:aStream.
|
21181
|
549 |
].
|
21332
|
550 |
] on:WriteError do:[:ex |
|
|
551 |
"ignore"
|
|
552 |
] ensure:[
|
|
553 |
shuffledStream close.
|
21181
|
554 |
].
|
|
555 |
] newProcess
|
|
556 |
priority:(Processor userSchedulingPriority "+ 1");
|
|
557 |
name:'OSProcess output shuffler';
|
|
558 |
"/ beSystemProcess;
|
|
559 |
resume.
|
|
560 |
|
21223
|
561 |
shufflerProcesses add:shufflerProcess.
|
21184
|
562 |
streamsToClose add:externalStream.
|
|
563 |
|
21181
|
564 |
^ externalStream
|
21332
|
565 |
|
|
566 |
"Modified: / 31-01-2017 / 16:57:25 / stefan"
|
22198
|
567 |
!
|
|
568 |
|
|
569 |
startCommand
|
|
570 |
"the 'real' command to be executed.
|
|
571 |
Redefined for remote processes (eg. to construct a remote command string)"
|
|
572 |
|
|
573 |
^ command
|
21181
|
574 |
! !
|
|
575 |
|
12890
|
576 |
!OSProcess methodsFor:'queries'!
|
|
577 |
|
21208
|
578 |
finishedWithSuccess
|
|
579 |
^ exitStatus notNil and:[exitStatus success].
|
|
580 |
!
|
|
581 |
|
12890
|
582 |
isAlive
|
21552
|
583 |
"answer true if the process is still alive"
|
12890
|
584 |
|
21205
|
585 |
^ pid notNil and:[exitStatus isNil]
|
21552
|
586 |
|
|
587 |
"Modified (comment): / 23-02-2017 / 10:52:37 / Maren"
|
12890
|
588 |
!
|
|
589 |
|
|
590 |
isDead
|
21552
|
591 |
"answer true if the process is no longer alive"
|
12890
|
592 |
|
|
593 |
^ self isAlive not
|
21552
|
594 |
|
|
595 |
"Modified (comment): / 23-02-2017 / 10:52:31 / Maren"
|
12890
|
596 |
! !
|
|
597 |
|
21172
|
598 |
!OSProcess methodsFor:'starting'!
|
|
599 |
|
21181
|
600 |
execute
|
21184
|
601 |
"execute the command.
|
21552
|
602 |
Wait until it is finished.
|
21184
|
603 |
Abort the execution if I am interrupted.
|
21552
|
604 |
Answer true if it terminated successfully,
|
21184
|
605 |
false if it could not be started or terminated with error."
|
21181
|
606 |
|
21184
|
607 |
|ok|
|
21181
|
608 |
|
|
609 |
[
|
21185
|
610 |
ok := self startProcess.
|
|
611 |
ok ifTrue:[
|
21208
|
612 |
self waitUntilFinished.
|
|
613 |
ok := self finishedWithSuccess.
|
21185
|
614 |
].
|
21184
|
615 |
] ifCurtailed:[
|
|
616 |
"/ we were interrupted -
|
|
617 |
"/ terminate the os-command (and all of its forked commands)
|
21185
|
618 |
pid notNil ifTrue:[
|
|
619 |
self terminateGroup.
|
|
620 |
].
|
21181
|
621 |
].
|
21184
|
622 |
^ ok.
|
21552
|
623 |
|
|
624 |
"Modified (comment): / 23-02-2017 / 10:54:34 / Maren"
|
21181
|
625 |
!
|
|
626 |
|
21172
|
627 |
startProcess
|
21184
|
628 |
"If there are non-external streams, setup transfer (shuffler) processes
|
|
629 |
to transfer data from a pipe to the internal stream.
|
|
630 |
Start the command.
|
|
631 |
Answer true if the command could be started, false if not.
|
|
632 |
Return immediately (do not wait until the command is finished)."
|
|
633 |
|
21200
|
634 |
|externalInStream externalAuxStream externalErrorStream externalOutStream|
|
21184
|
635 |
|
21200
|
636 |
shufflerProcesses := OrderedCollection new:4.
|
|
637 |
streamsToClose := OrderedCollection new:2.
|
21184
|
638 |
|
|
639 |
externalInStream := self setupShufflerForInput:inStream.
|
|
640 |
externalAuxStream := self setupShufflerForInput:auxStream.
|
21226
|
641 |
externalOutStream := self setupShufflerForOutput:outStream.
|
22198
|
642 |
|
21184
|
643 |
errorStream == outStream ifTrue:[
|
21226
|
644 |
externalErrorStream := externalOutStream.
|
21184
|
645 |
] ifFalse:[
|
|
646 |
externalErrorStream := self setupShufflerForOutput:errorStream.
|
|
647 |
].
|
|
648 |
|
|
649 |
"start the command"
|
21200
|
650 |
finishSema := EventSemaphore new.
|
21172
|
651 |
|
|
652 |
Processor
|
|
653 |
monitor:[
|
|
654 |
pid := OperatingSystem
|
22198
|
655 |
startProcess:(self startCommand)
|
21200
|
656 |
inputFrom:externalInStream
|
|
657 |
outputTo:externalOutStream
|
|
658 |
errorTo:externalErrorStream
|
|
659 |
auxFrom:externalAuxStream
|
|
660 |
environment:environment
|
|
661 |
inDirectory:directory
|
|
662 |
newPgrp:newPgrp
|
|
663 |
showWindow:showWindow.
|
21172
|
664 |
]
|
|
665 |
action:[:status |
|
|
666 |
status stillAlive ifFalse:[
|
|
667 |
exitStatus := status.
|
|
668 |
pid notNil ifTrue:[
|
|
669 |
OperatingSystem closePid:pid.
|
|
670 |
].
|
22198
|
671 |
"/ shufflerProcesses do:[:eachProcess|
|
|
672 |
"/ "terminate the shuffler processes.
|
|
673 |
"/ They close the local side of the pipe when being terminated"
|
|
674 |
"/ eachProcess terminate.
|
|
675 |
"/ ].
|
|
676 |
|
21223
|
677 |
finishSema signal.
|
21206
|
678 |
terminateActionBlock value.
|
21172
|
679 |
].
|
|
680 |
].
|
|
681 |
|
21184
|
682 |
"we can close the remote side of the pipes after the os process has been started (after the fork)"
|
|
683 |
streamsToClose do:[:eachStream | eachStream close].
|
|
684 |
streamsToClose := nil.
|
|
685 |
|
21172
|
686 |
pid isNil ifTrue:[
|
21184
|
687 |
"process could not be started - terminate shufflers"
|
|
688 |
shufflerProcesses do:[:eachProcess|
|
|
689 |
eachProcess terminate.
|
|
690 |
].
|
|
691 |
shufflerProcesses := nil.
|
21172
|
692 |
exitStatus := OperatingSystem osProcessStatusClass processCreationFailure.
|
21208
|
693 |
finishSema signal.
|
21184
|
694 |
^ false.
|
21172
|
695 |
].
|
|
696 |
|
|
697 |
^ true.
|
|
698 |
! !
|
|
699 |
|
12890
|
700 |
!OSProcess methodsFor:'terminating'!
|
|
701 |
|
|
702 |
kill
|
|
703 |
"kill the process - the process does not get the chance to clean up"
|
|
704 |
|
21208
|
705 |
pid notNil ifTrue:[
|
|
706 |
OperatingSystem killProcess:pid.
|
|
707 |
].
|
12890
|
708 |
!
|
|
709 |
|
|
710 |
killGroup
|
20379
|
711 |
"kill the processGroup - the processes do not get the chance to clean up"
|
|
712 |
|
21208
|
713 |
pid notNil ifTrue:[
|
|
714 |
OperatingSystem
|
|
715 |
killProcessGroup:pid;
|
|
716 |
killProcess:pid.
|
|
717 |
].
|
12890
|
718 |
!
|
|
719 |
|
|
720 |
terminate
|
|
721 |
"terminate the process gracefully"
|
|
722 |
|
21208
|
723 |
pid notNil ifTrue:[
|
|
724 |
OperatingSystem terminateProcess:pid.
|
|
725 |
].
|
12890
|
726 |
!
|
|
727 |
|
|
728 |
terminateGroup
|
|
729 |
"terminate the process group.
|
21552
|
730 |
Under Windows, this is the same as terminateWithAllChildren,
|
12890
|
731 |
under unix, this terminates a subset of all children"
|
|
732 |
|
21208
|
733 |
pid notNil ifTrue:[
|
|
734 |
OperatingSystem
|
21632
|
735 |
terminateProcess:pid;
|
|
736 |
terminateProcessGroup:pid.
|
21208
|
737 |
].
|
21551
|
738 |
|
21552
|
739 |
"Modified (comment): / 23-02-2017 / 10:50:13 / Maren"
|
21632
|
740 |
"Modified: / 09-03-2017 / 15:00:58 / stefan"
|
21184
|
741 |
! !
|
|
742 |
|
|
743 |
!OSProcess methodsFor:'waiting'!
|
|
744 |
|
|
745 |
waitUntilFinished
|
22148
|
746 |
|
|
747 |
"wait with a veryy long timeout,
|
|
748 |
in order that ProcessorScheduler>>#checkForEndOfDispatch recogizes
|
|
749 |
this waiting process as user process which is still alive.
|
|
750 |
The timout is meant to never occur!!"
|
|
751 |
|
|
752 |
^ self waitUntilFinishedWithTimeout:60*60*24000
|
|
753 |
|
|
754 |
"Modified: / 01-08-2017 / 14:41:10 / stefan"
|
|
755 |
"Modified (comment): / 01-08-2017 / 17:15:55 / stefan"
|
21208
|
756 |
!
|
|
757 |
|
|
758 |
waitUntilFinishedWithTimeout:timeout
|
21184
|
759 |
|processList|
|
|
760 |
|
21208
|
761 |
(finishSema waitWithTimeout:timeout) isNil ifTrue:[
|
|
762 |
"timed out"
|
|
763 |
^ nil.
|
|
764 |
].
|
21184
|
765 |
|
|
766 |
"have to wait until the shufflers have finished their work"
|
21208
|
767 |
processList := shufflerProcesses.
|
|
768 |
processList notEmptyOrNil ifTrue:[
|
21184
|
769 |
processList do:[:eachProcess |
|
|
770 |
eachProcess waitUntilTerminated.
|
|
771 |
].
|
|
772 |
shufflerProcesses := nil.
|
|
773 |
].
|
12890
|
774 |
! !
|
|
775 |
|
22198
|
776 |
!OSProcess::RemoteOSProcess class methodsFor:'documentation'!
|
|
777 |
|
|
778 |
documentation
|
|
779 |
"
|
|
780 |
Instances of OSProcess represent operating system processes that can be executed.
|
|
781 |
(as opposed to Smalltalk processes).
|
|
782 |
|
|
783 |
[author:]
|
|
784 |
cg
|
|
785 |
|
|
786 |
[instance variables:]
|
|
787 |
host String the host on which to execute the command
|
|
788 |
|
|
789 |
[class variables:]
|
|
790 |
|
|
791 |
[see also:]
|
|
792 |
Process
|
|
793 |
"
|
|
794 |
!
|
|
795 |
|
|
796 |
examples
|
|
797 |
"
|
|
798 |
[exBegin]
|
|
799 |
|outStream|
|
|
800 |
|
|
801 |
outStream := '' writeStream.
|
|
802 |
|
|
803 |
(OSProcess onHost:'exeptn')
|
|
804 |
command:'ls -l';
|
|
805 |
inStream:'abc' readStream;
|
|
806 |
outStream:outStream;
|
|
807 |
lineWise:true;
|
|
808 |
execute.
|
|
809 |
|
|
810 |
outStream contents
|
|
811 |
[exEnd]
|
|
812 |
"
|
|
813 |
! !
|
|
814 |
|
|
815 |
!OSProcess::RemoteOSProcess methodsFor:'accessing'!
|
|
816 |
|
|
817 |
host
|
|
818 |
^ host
|
|
819 |
!
|
|
820 |
|
|
821 |
host:aHostName
|
|
822 |
host := aHostName.
|
|
823 |
! !
|
|
824 |
|
|
825 |
!OSProcess::RemoteOSProcess methodsFor:'private'!
|
|
826 |
|
|
827 |
startCommand
|
|
828 |
"the 'real' command"
|
22199
|
829 |
|
|
830 |
"for now, always use ssh;
|
|
831 |
later, this should be configurable,
|
|
832 |
which remote mechanism is to be used per host"
|
|
833 |
|
22198
|
834 |
^ 'ssh %1 "%2"' bindWith:host with:command
|
|
835 |
! !
|
|
836 |
|
12890
|
837 |
!OSProcess class methodsFor:'documentation'!
|
|
838 |
|
19298
|
839 |
version
|
|
840 |
^ '$Header$'
|
|
841 |
!
|
|
842 |
|
12890
|
843 |
version_CVS
|
19286
|
844 |
^ '$Header$'
|
12890
|
845 |
! !
|
19286
|
846 |
|
21172
|
847 |
|
|
848 |
OSProcess initialize!
|