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