21182
|
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
|
21184
|
10 |
shufflerProcesses streamsToClose'
|
12890
|
11 |
classVariableNames:''
|
|
12 |
poolDictionaries:''
|
|
13 |
category:'System-Support'
|
|
14 |
!
|
|
15 |
|
|
16 |
!OSProcess class methodsFor:'documentation'!
|
|
17 |
|
|
18 |
documentation
|
|
19 |
"
|
21182
|
20 |
Instances of OSProcess represent operating system processes thatr can be executed.
|
12890
|
21 |
(as opposed to Smalltalk processes).
|
|
22 |
|
|
23 |
[author:]
|
|
24 |
Stefan Vogel (stefan@zwerg)
|
|
25 |
|
|
26 |
[instance variables:]
|
|
27 |
pid SmallInteger the process id
|
21182
|
28 |
command String the command line of the running command.
|
12890
|
29 |
|
|
30 |
[class variables:]
|
|
31 |
|
|
32 |
[see also:]
|
|
33 |
Process
|
|
34 |
|
|
35 |
"
|
21181
|
36 |
!
|
|
37 |
|
|
38 |
examples
|
|
39 |
"
|
|
40 |
|outStream|
|
|
41 |
|
|
42 |
outStream := '' writeStream.
|
|
43 |
|
|
44 |
self new
|
21182
|
45 |
command:'ls -l';
|
21181
|
46 |
inStream:'abc' readStream;
|
|
47 |
outStream:outStream;
|
|
48 |
lineWise:true;
|
|
49 |
execute.
|
|
50 |
|
|
51 |
outStream contents
|
|
52 |
"
|
21184
|
53 |
|
|
54 |
"
|
|
55 |
|outStream|
|
|
56 |
|
|
57 |
outStream := '' writeStream.
|
|
58 |
|
|
59 |
self new
|
|
60 |
command:'ls -l; sleep 10; echo =================================; echo hallo after 10s; echo >&2 +++++++++++++++++++++; cat >&2';
|
|
61 |
inStream:'abc' readStream;
|
|
62 |
outStream:outStream;
|
|
63 |
errorStream:outStream;
|
|
64 |
startProcess.
|
|
65 |
|
|
66 |
outStream inspect
|
|
67 |
"
|
|
68 |
|
|
69 |
"
|
|
70 |
|outStream|
|
|
71 |
|
|
72 |
outStream := '' writeStream.
|
|
73 |
|
|
74 |
self new
|
|
75 |
command:'ls -l & ls -l >&2';
|
|
76 |
inStream:'abc' readStream;
|
|
77 |
outStream:outStream;
|
|
78 |
errorStream:outStream;
|
|
79 |
startProcess.
|
|
80 |
|
|
81 |
outStream inspect
|
|
82 |
"
|
21181
|
83 |
! !
|
|
84 |
|
|
85 |
!OSProcess class methodsFor:'instance creation'!
|
|
86 |
|
|
87 |
new
|
|
88 |
"return an initialized instance"
|
|
89 |
|
|
90 |
^ self basicNew initialize.
|
12890
|
91 |
! !
|
|
92 |
|
21172
|
93 |
!OSProcess class methodsFor:'initialize'!
|
|
94 |
|
|
95 |
initialize
|
|
96 |
"Backward compatibility"
|
|
97 |
Win32Process := self.
|
|
98 |
! !
|
|
99 |
|
12890
|
100 |
!OSProcess methodsFor:'accessing'!
|
|
101 |
|
19298
|
102 |
accessor
|
|
103 |
^ self
|
|
104 |
!
|
|
105 |
|
21181
|
106 |
auxStream
|
|
107 |
^ auxStream
|
|
108 |
!
|
|
109 |
|
|
110 |
auxStream:something
|
|
111 |
auxStream := something.
|
|
112 |
!
|
|
113 |
|
21172
|
114 |
command
|
|
115 |
"return the value of the instance variable 'command' (automatically generated)"
|
|
116 |
|
|
117 |
^ command
|
|
118 |
|
|
119 |
"Created: / 10.11.1998 / 21:27:07 / cg"
|
|
120 |
!
|
|
121 |
|
|
122 |
command:something
|
|
123 |
"set the value of the instance variable 'command' (automatically generated)"
|
|
124 |
|
|
125 |
command := something.
|
|
126 |
|
|
127 |
"Created: / 10.11.1998 / 21:27:07 / cg"
|
|
128 |
!
|
|
129 |
|
|
130 |
directory
|
|
131 |
"return the value of the instance variable 'directory' (automatically generated)"
|
|
132 |
|
|
133 |
^ directory
|
|
134 |
|
|
135 |
"Created: / 10.11.1998 / 21:21:52 / cg"
|
|
136 |
!
|
|
137 |
|
|
138 |
directory:something
|
|
139 |
"set the value of the instance variable 'directory' (automatically generated)"
|
|
140 |
|
|
141 |
directory := something.
|
|
142 |
|
|
143 |
"Created: / 10.11.1998 / 21:21:52 / cg"
|
|
144 |
!
|
|
145 |
|
|
146 |
environment
|
|
147 |
"return the value of the instance variable 'environment' (automatically generated)"
|
|
148 |
|
|
149 |
^ environment
|
|
150 |
|
|
151 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
152 |
!
|
|
153 |
|
|
154 |
environment:something
|
|
155 |
"set the value of the instance variable 'environment' (automatically generated)"
|
|
156 |
|
|
157 |
environment := something.
|
|
158 |
|
|
159 |
"Created: / 10.11.1998 / 21:27:07 / cg"
|
|
160 |
!
|
|
161 |
|
|
162 |
errorStream
|
|
163 |
"return the value of the instance variable 'errorStream' (automatically generated)"
|
|
164 |
|
|
165 |
^ errorStream
|
|
166 |
|
|
167 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
12890
|
168 |
!
|
|
169 |
|
21172
|
170 |
errorStream:something
|
|
171 |
"set the value of the instance variable 'errorStream' (automatically generated)"
|
|
172 |
|
|
173 |
errorStream := something.
|
|
174 |
|
|
175 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
176 |
!
|
|
177 |
|
|
178 |
exitStatus
|
|
179 |
"return the value of the instance variable 'exitStatus' (automatically generated)"
|
|
180 |
|
|
181 |
^ exitStatus
|
|
182 |
|
|
183 |
"Created: / 10.11.1998 / 21:24:55 / cg"
|
|
184 |
!
|
|
185 |
|
|
186 |
exitStatus:something
|
|
187 |
"set the value of the instance variable 'exitStatus' (automatically generated)"
|
|
188 |
|
|
189 |
exitStatus := something.
|
|
190 |
|
|
191 |
"Created: / 10.11.1998 / 21:24:55 / cg"
|
|
192 |
!
|
|
193 |
|
|
194 |
finishSema
|
21184
|
195 |
"wait on this semaphore if yo want to wait until the os process has finished.
|
|
196 |
There may be multiple waiters, so it is a good idea to do a #waitUncounted"
|
21172
|
197 |
|
|
198 |
^ finishSema
|
|
199 |
!
|
|
200 |
|
|
201 |
inStream
|
|
202 |
"return the value of the instance variable 'inStream' (automatically generated)"
|
|
203 |
|
|
204 |
^ inStream
|
|
205 |
|
|
206 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
207 |
!
|
|
208 |
|
|
209 |
inStream:something
|
|
210 |
"set the value of the instance variable 'inStream' (automatically generated)"
|
|
211 |
|
|
212 |
inStream := something.
|
|
213 |
|
|
214 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
215 |
!
|
|
216 |
|
21181
|
217 |
lineWise
|
|
218 |
^ lineWise
|
|
219 |
!
|
|
220 |
|
|
221 |
lineWise:something
|
|
222 |
lineWise := something.
|
|
223 |
!
|
|
224 |
|
21182
|
225 |
newPgrp
|
|
226 |
^ newPgrp
|
|
227 |
!
|
|
228 |
|
|
229 |
newPgrp:something
|
|
230 |
newPgrp := something.
|
|
231 |
!
|
|
232 |
|
21172
|
233 |
outStream
|
|
234 |
"return the value of the instance variable 'outStream' (automatically generated)"
|
|
235 |
|
|
236 |
^ outStream
|
|
237 |
|
|
238 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
239 |
!
|
|
240 |
|
|
241 |
outStream:something
|
|
242 |
"set the value of the instance variable 'outStream' (automatically generated)"
|
|
243 |
|
|
244 |
outStream := something.
|
|
245 |
|
|
246 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
12890
|
247 |
!
|
|
248 |
|
|
249 |
pid
|
|
250 |
^ pid
|
|
251 |
!
|
|
252 |
|
|
253 |
pid:something
|
|
254 |
pid := something.
|
21181
|
255 |
!
|
|
256 |
|
|
257 |
showWindow
|
|
258 |
^ showWindow
|
|
259 |
!
|
|
260 |
|
|
261 |
showWindow:something
|
|
262 |
showWindow := something.
|
12890
|
263 |
! !
|
|
264 |
|
21181
|
265 |
!OSProcess methodsFor:'initialization'!
|
|
266 |
|
|
267 |
initialize
|
|
268 |
"Invoked when a new instance is created."
|
|
269 |
|
|
270 |
showWindow := false. "/ for backward compatibility
|
21183
|
271 |
lineWise := false.
|
|
272 |
newPgrp := true.
|
21181
|
273 |
! !
|
|
274 |
|
13313
|
275 |
!OSProcess methodsFor:'printing'!
|
|
276 |
|
|
277 |
printOn:aStream
|
|
278 |
aStream
|
|
279 |
nextPutAll:self className;
|
|
280 |
nextPut:$(.
|
|
281 |
pid printOn:aStream.
|
|
282 |
aStream space.
|
21172
|
283 |
command printOn:aStream.
|
13313
|
284 |
aStream nextPut:$)
|
|
285 |
! !
|
|
286 |
|
21181
|
287 |
!OSProcess methodsFor:'private'!
|
|
288 |
|
|
289 |
setupShufflerForInput:aStream
|
21182
|
290 |
"if aStream is an internal Stream, set up a pipe for the command input.
|
|
291 |
Start a process that shuffles tha data fron the internal stream into the pipe
|
|
292 |
(and into the command's input)."
|
|
293 |
|
21181
|
294 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
295 |
|
|
296 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
297 |
^ aStream.
|
|
298 |
].
|
|
299 |
|
|
300 |
pipe := NonPositionableExternalStream makePipe.
|
|
301 |
externalStream := pipe at:1.
|
|
302 |
shuffledStream := pipe at:2.
|
|
303 |
lineWise ifFalse:[
|
|
304 |
shuffledStream blocking:false.
|
|
305 |
].
|
|
306 |
aStream isBinary ifTrue:[
|
|
307 |
shuffledStream binary.
|
|
308 |
].
|
|
309 |
|
|
310 |
"/ start a reader process, shuffling data from the given
|
|
311 |
"/ inStream to the pipe (which is connected to the commands input)
|
21182
|
312 |
shufflerProcess := [
|
21181
|
313 |
[
|
21184
|
314 |
WriteError handle:[:ex |"ignored" ] do:[
|
|
315 |
lineWise ifTrue:[
|
|
316 |
"shuffle until end-of-input"
|
|
317 |
[aStream atEnd] whileFalse:[
|
|
318 |
|data|
|
|
319 |
|
|
320 |
data := aStream nextLine.
|
|
321 |
data notNil ifTrue:[
|
|
322 |
shuffledStream nextPutLine:data.
|
|
323 |
shuffledStream flush.
|
|
324 |
].
|
|
325 |
].
|
|
326 |
] ifFalse:[
|
|
327 |
aStream copyToEndInto:shuffledStream.
|
|
328 |
].
|
|
329 |
].
|
21181
|
330 |
] ensure:[
|
|
331 |
shuffledStream close.
|
|
332 |
]
|
|
333 |
] newProcess
|
|
334 |
name:'OSProcess input shuffler';
|
|
335 |
"/ beSystemProcess;
|
|
336 |
resume.
|
|
337 |
|
|
338 |
shufflerProcesses add:shufflerProcess.
|
21184
|
339 |
streamsToClose add:externalStream.
|
|
340 |
|
21181
|
341 |
^ externalStream
|
|
342 |
!
|
|
343 |
|
|
344 |
setupShufflerForOutput:aStream
|
21182
|
345 |
"if aStream is an internal Stream, set up a pipe for the command output.
|
|
346 |
Start a process that shuffles the data fron the pipe into the internal stream."
|
|
347 |
|
21181
|
348 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
349 |
|
|
350 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
351 |
^ aStream.
|
|
352 |
].
|
|
353 |
|
|
354 |
pipe := NonPositionableExternalStream makePipe.
|
|
355 |
externalStream := pipe at:2.
|
|
356 |
shuffledStream := pipe at:1.
|
|
357 |
aStream isBinary ifTrue:[
|
|
358 |
shuffledStream binary.
|
|
359 |
].
|
|
360 |
|
21182
|
361 |
shufflerProcess := [
|
|
362 |
WriteError handle:[:ex |"ignored" ] do:[
|
21181
|
363 |
[
|
21184
|
364 |
"shuffle until the pipe closes"
|
|
365 |
lineWise ifTrue:[
|
|
366 |
[shuffledStream atEnd] whileFalse:[
|
|
367 |
|data|
|
|
368 |
|
|
369 |
data := shuffledStream nextLine.
|
|
370 |
data notNil ifTrue:[
|
|
371 |
aStream nextPutLine:data
|
|
372 |
] .
|
|
373 |
].
|
|
374 |
] ifFalse:[
|
|
375 |
shuffledStream copyToEndInto:aStream.
|
|
376 |
].
|
21181
|
377 |
] ensure:[
|
21182
|
378 |
shuffledStream close.
|
21181
|
379 |
].
|
|
380 |
].
|
|
381 |
] newProcess
|
|
382 |
priority:(Processor userSchedulingPriority "+ 1");
|
|
383 |
name:'OSProcess output shuffler';
|
|
384 |
"/ beSystemProcess;
|
|
385 |
resume.
|
|
386 |
|
21184
|
387 |
streamsToClose add:externalStream.
|
|
388 |
|
21181
|
389 |
^ externalStream
|
|
390 |
! !
|
|
391 |
|
12890
|
392 |
!OSProcess methodsFor:'queries'!
|
|
393 |
|
|
394 |
isAlive
|
|
395 |
"answer true, if the process is still alive"
|
|
396 |
|
21181
|
397 |
^ pid notNil and:[exitStatus notNil]
|
12890
|
398 |
!
|
|
399 |
|
|
400 |
isDead
|
|
401 |
"answer true, if the process is no longer alive"
|
|
402 |
|
|
403 |
^ self isAlive not
|
|
404 |
! !
|
|
405 |
|
21172
|
406 |
!OSProcess methodsFor:'starting'!
|
|
407 |
|
21181
|
408 |
execute
|
21184
|
409 |
"execute the command.
|
|
410 |
Wait until is is finished.
|
|
411 |
Abort the execution if I am interrupted.
|
|
412 |
Answer true if the terminated succesfully,
|
|
413 |
false if it could not be started or terminated with error."
|
21181
|
414 |
|
21184
|
415 |
|ok|
|
21181
|
416 |
|
|
417 |
[
|
21185
|
418 |
ok := self startProcess.
|
|
419 |
ok ifTrue:[
|
|
420 |
ok := self waitUntilFinished.
|
|
421 |
].
|
21184
|
422 |
] ifCurtailed:[
|
|
423 |
"/ we were interrupted -
|
|
424 |
"/ terminate the os-command (and all of its forked commands)
|
21185
|
425 |
pid notNil ifTrue:[
|
|
426 |
self terminateGroup.
|
|
427 |
].
|
21181
|
428 |
].
|
21184
|
429 |
^ ok.
|
21181
|
430 |
!
|
|
431 |
|
21172
|
432 |
startProcess
|
21184
|
433 |
"If there are non-external streams, setup transfer (shuffler) processes
|
|
434 |
to transfer data from a pipe to the internal stream.
|
|
435 |
Start the command.
|
|
436 |
Answer true if the command could be started, false if not.
|
|
437 |
Return immediately (do not wait until the command is finished)."
|
|
438 |
|
21200
|
439 |
|externalInStream externalAuxStream externalErrorStream externalOutStream|
|
21184
|
440 |
|
21200
|
441 |
shufflerProcesses := OrderedCollection new:4.
|
|
442 |
streamsToClose := OrderedCollection new:2.
|
21184
|
443 |
|
|
444 |
externalInStream := self setupShufflerForInput:inStream.
|
|
445 |
externalAuxStream := self setupShufflerForInput:auxStream.
|
|
446 |
errorStream == outStream ifTrue:[
|
|
447 |
externalErrorStream := externalOutStream := self setupShufflerForOutput:errorStream.
|
|
448 |
] ifFalse:[
|
|
449 |
externalErrorStream := self setupShufflerForOutput:errorStream.
|
|
450 |
externalOutStream := self setupShufflerForOutput:outStream.
|
|
451 |
].
|
|
452 |
|
|
453 |
"start the command"
|
21200
|
454 |
finishSema := EventSemaphore new.
|
21172
|
455 |
|
|
456 |
Processor
|
|
457 |
monitor:[
|
|
458 |
pid := OperatingSystem
|
21200
|
459 |
startProcess:command
|
|
460 |
inputFrom:externalInStream
|
|
461 |
outputTo:externalOutStream
|
|
462 |
errorTo:externalErrorStream
|
|
463 |
auxFrom:externalAuxStream
|
|
464 |
environment:environment
|
|
465 |
inDirectory:directory
|
|
466 |
newPgrp:newPgrp
|
|
467 |
showWindow:showWindow.
|
21172
|
468 |
]
|
|
469 |
action:[:status |
|
|
470 |
status stillAlive ifFalse:[
|
|
471 |
exitStatus := status.
|
|
472 |
pid notNil ifTrue:[
|
|
473 |
OperatingSystem closePid:pid.
|
|
474 |
].
|
21184
|
475 |
shufflerProcesses do:[:eachProcess|
|
|
476 |
"terminate the shuffler processes.
|
|
477 |
They close the local side of the pipe when being terminated"
|
|
478 |
eachProcess terminate.
|
|
479 |
].
|
|
480 |
finishSema signal.
|
21172
|
481 |
].
|
|
482 |
].
|
|
483 |
|
21184
|
484 |
"we can close the remote side of the pipes after the os process has been started (after the fork)"
|
|
485 |
streamsToClose do:[:eachStream | eachStream close].
|
|
486 |
streamsToClose := nil.
|
|
487 |
|
21172
|
488 |
pid isNil ifTrue:[
|
21184
|
489 |
"process could not be started - terminate shufflers"
|
|
490 |
shufflerProcesses do:[:eachProcess|
|
|
491 |
eachProcess terminate.
|
|
492 |
].
|
|
493 |
shufflerProcesses := nil.
|
21172
|
494 |
exitStatus := OperatingSystem osProcessStatusClass processCreationFailure.
|
21184
|
495 |
^ false.
|
21172
|
496 |
].
|
|
497 |
|
|
498 |
^ true.
|
|
499 |
! !
|
|
500 |
|
12890
|
501 |
!OSProcess methodsFor:'terminating'!
|
|
502 |
|
|
503 |
kill
|
|
504 |
"kill the process - the process does not get the chance to clean up"
|
|
505 |
|
21181
|
506 |
OperatingSystem killProcess:pid.
|
12890
|
507 |
!
|
|
508 |
|
|
509 |
killGroup
|
20379
|
510 |
"kill the processGroup - the processes do not get the chance to clean up"
|
|
511 |
|
21184
|
512 |
OperatingSystem
|
|
513 |
killProcessGroup:pid;
|
|
514 |
killProcess:pid.
|
12890
|
515 |
!
|
|
516 |
|
|
517 |
terminate
|
|
518 |
"terminate the process gracefully"
|
|
519 |
|
21181
|
520 |
OperatingSystem terminateProcess:pid.
|
12890
|
521 |
!
|
|
522 |
|
|
523 |
terminateGroup
|
|
524 |
"terminate the process group.
|
|
525 |
Under Windows, these is the same as terminateWithhAllChildren,
|
|
526 |
under unix, this terminates a subset of all children"
|
|
527 |
|
21184
|
528 |
OperatingSystem
|
|
529 |
terminateProcessGroup:pid;
|
|
530 |
terminateProcess:pid.
|
|
531 |
! !
|
|
532 |
|
|
533 |
!OSProcess methodsFor:'waiting'!
|
|
534 |
|
|
535 |
waitUntilFinished
|
|
536 |
|processList|
|
|
537 |
|
|
538 |
finishSema waitUncounted.
|
|
539 |
|
|
540 |
"have to wait until the shufflers have finished their work"
|
|
541 |
shufflerProcesses notEmptyOrNil ifTrue:[
|
|
542 |
processList := shufflerProcesses.
|
|
543 |
processList do:[:eachProcess |
|
|
544 |
eachProcess waitUntilTerminated.
|
|
545 |
].
|
|
546 |
shufflerProcesses := nil.
|
|
547 |
].
|
|
548 |
|
|
549 |
^ exitStatus notNil and:[exitStatus success].
|
12890
|
550 |
! !
|
|
551 |
|
|
552 |
!OSProcess class methodsFor:'documentation'!
|
|
553 |
|
19298
|
554 |
version
|
|
555 |
^ '$Header$'
|
|
556 |
!
|
|
557 |
|
12890
|
558 |
version_CVS
|
19286
|
559 |
^ '$Header$'
|
12890
|
560 |
! !
|
19286
|
561 |
|
21172
|
562 |
|
|
563 |
OSProcess initialize!
|