24011
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
25120
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
12890
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
19286
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
12890
|
18 |
Object subclass:#OSProcess
|
23035
|
19 |
instanceVariableNames:'pid command environment directory inStream outStream errorStream
|
24724
|
20 |
auxInStream auxOutStream showWindow lineWise newPgrp exitStatus
|
|
21 |
finishSema inputShufflerProcesses outputShufflerProcesses
|
|
22 |
streamsToClose terminateActionBlock'
|
23793
|
23 |
classVariableNames:'Instances'
|
23035
|
24 |
poolDictionaries:''
|
|
25 |
category:'System-Support'
|
12890
|
26 |
!
|
|
27 |
|
23201
|
28 |
Object subclass:#ProcessListEntry
|
23204
|
29 |
instanceVariableNames:'executableName processID sessionName userName'
|
23201
|
30 |
classVariableNames:''
|
|
31 |
poolDictionaries:''
|
|
32 |
privateIn:OSProcess
|
|
33 |
!
|
|
34 |
|
22198
|
35 |
OSProcess subclass:#RemoteOSProcess
|
23035
|
36 |
instanceVariableNames:'host'
|
|
37 |
classVariableNames:'MethodPerHost'
|
|
38 |
poolDictionaries:''
|
|
39 |
privateIn:OSProcess
|
22198
|
40 |
!
|
|
41 |
|
12890
|
42 |
!OSProcess class methodsFor:'documentation'!
|
|
43 |
|
25120
|
44 |
copyright
|
|
45 |
"
|
|
46 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
47 |
All Rights Reserved
|
|
48 |
|
|
49 |
This software is furnished under a license and may be used
|
|
50 |
only in accordance with the terms of that license and with the
|
|
51 |
inclusion of the above copyright notice. This software may not
|
|
52 |
be provided or otherwise made available to, or used by, any
|
|
53 |
other person. No title to or ownership of the software is
|
|
54 |
hereby transferred.
|
|
55 |
"
|
|
56 |
!
|
|
57 |
|
12890
|
58 |
documentation
|
|
59 |
"
|
21552
|
60 |
Instances of OSProcess represent operating system processes that can be executed.
|
12890
|
61 |
(as opposed to Smalltalk processes).
|
24290
|
62 |
Both local and remote processes are supported.
|
22439
|
63 |
|
|
64 |
commandString:
|
|
65 |
If command is a String, the commandString is passed to a shell for execution
|
|
66 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
|
67 |
Regular input/output redirection and pipes etc. are supported as supported by
|
|
68 |
the underlying OS's command interpreter (i.e. some limits apply to MSDOS)
|
|
69 |
|
|
70 |
With an empty command, the shell will read commands from the passed input stream.
|
|
71 |
|
|
72 |
If command is an Array, the first element is the command to be executed,
|
|
73 |
and the other elements are the arguments to the command.
|
|
74 |
No shell is invoked in this case. Any redirection or piping can then be done
|
|
75 |
by Smalltalk code (including piping through smalltalk streams & filters).
|
12890
|
76 |
|
|
77 |
[author:]
|
|
78 |
Stefan Vogel (stefan@zwerg)
|
|
79 |
|
|
80 |
[instance variables:]
|
|
81 |
pid SmallInteger the process id
|
21182
|
82 |
command String the command line of the running command.
|
12890
|
83 |
|
|
84 |
[class variables:]
|
|
85 |
|
|
86 |
[see also:]
|
|
87 |
Process
|
|
88 |
|
|
89 |
"
|
21181
|
90 |
!
|
|
91 |
|
|
92 |
examples
|
21512
|
93 |
"
|
22439
|
94 |
send command's output to the Transcript:
|
|
95 |
[exBegin]
|
|
96 |
OSProcess new
|
22986
|
97 |
terminateActionBlock:[:status | Transcript showCR:status. Transcript showCR:status isError];
|
|
98 |
command:'lsxxxx -l';
|
22439
|
99 |
outStream:Transcript;
|
|
100 |
lineWise:true;
|
22986
|
101 |
startProcess.
|
22439
|
102 |
[exEnd]
|
|
103 |
|
|
104 |
send command's output to my stdout (see console):
|
|
105 |
[exBegin]
|
|
106 |
OSProcess new
|
|
107 |
command:'ls -l';
|
|
108 |
outStream:Stdout;
|
|
109 |
execute.
|
|
110 |
[exEnd]
|
|
111 |
|
|
112 |
send command's output to a Smalltalk stream:
|
22197
|
113 |
[exBegin]
|
22198
|
114 |
|outStream|
|
21181
|
115 |
|
22198
|
116 |
outStream := '' writeStream.
|
21181
|
117 |
|
22198
|
118 |
OSProcess new
|
|
119 |
command:'ls -l';
|
|
120 |
outStream:outStream;
|
|
121 |
execute.
|
21181
|
122 |
|
22439
|
123 |
outStream contents inspect.
|
22197
|
124 |
[exEnd]
|
21184
|
125 |
|
22439
|
126 |
feed it from a smalltalk stream, get command's output into a Smalltalk stream:
|
|
127 |
[exBegin]
|
|
128 |
|inStream outStream|
|
|
129 |
|
|
130 |
inStream := 'hello world' readStream.
|
|
131 |
outStream := '' writeStream.
|
|
132 |
|
|
133 |
OSProcess new
|
|
134 |
command:'tr ''a-z'' ''A-Z''';
|
|
135 |
inStream:inStream;
|
|
136 |
outStream:outStream;
|
|
137 |
execute.
|
|
138 |
|
|
139 |
outStream contents inspect.
|
|
140 |
[exEnd]
|
|
141 |
|
|
142 |
again, send output to a Smalltalk stream;
|
|
143 |
watch the stream getting filled after 10 seconds:
|
22197
|
144 |
[exBegin]
|
22198
|
145 |
|outStream|
|
21184
|
146 |
|
22198
|
147 |
outStream := '' writeStream.
|
21184
|
148 |
|
22198
|
149 |
OSProcess new
|
|
150 |
command:'ls -l; sleep 10; echo =================================; echo hallo after 10s; echo >&2 +++++++++++++++++++++; cat >&2';
|
|
151 |
outStream:outStream;
|
|
152 |
errorStream:outStream;
|
|
153 |
startProcess.
|
21184
|
154 |
|
22198
|
155 |
outStream inspect
|
22197
|
156 |
[exEnd]
|
21184
|
157 |
|
22439
|
158 |
separate stdout and stderr:
|
22197
|
159 |
[exBegin]
|
22439
|
160 |
|outStream errStream|
|
21184
|
161 |
|
22198
|
162 |
outStream := '' writeStream.
|
22439
|
163 |
errStream := '' writeStream.
|
21184
|
164 |
|
22198
|
165 |
OSProcess new
|
22439
|
166 |
command:'ls -l && ls >&2';
|
22198
|
167 |
outStream:outStream;
|
22439
|
168 |
errorStream:errStream;
|
22198
|
169 |
startProcess.
|
21184
|
170 |
|
22439
|
171 |
outStream inspect.
|
|
172 |
errStream inspect.
|
22197
|
173 |
[exEnd]
|
21512
|
174 |
|
22439
|
175 |
Execute commands in shell/cmd.exe and read them from stdin:
|
22197
|
176 |
[exBegin]
|
22198
|
177 |
|outStream|
|
21512
|
178 |
|
22198
|
179 |
outStream := '' writeStream.
|
21512
|
180 |
|
22198
|
181 |
OSProcess new
|
|
182 |
command:'';
|
|
183 |
inStream:'ls -l' readStream;
|
|
184 |
outStream:outStream;
|
|
185 |
lineWise:true;
|
|
186 |
execute.
|
21512
|
187 |
|
22198
|
188 |
outStream contents inspect
|
22197
|
189 |
[exEnd]
|
22439
|
190 |
|
|
191 |
Execute on a remote host (you must have the appropriate ssh setup):
|
|
192 |
[exBegin]
|
|
193 |
|outStream|
|
|
194 |
|
|
195 |
outStream := '' writeStream.
|
|
196 |
|
|
197 |
(OSProcess onHost:'exeptn')
|
|
198 |
command:'ls -l';
|
|
199 |
outStream:outStream;
|
|
200 |
lineWise:true;
|
|
201 |
execute.
|
|
202 |
|
|
203 |
outStream contents inspect
|
|
204 |
[exEnd]
|
21512
|
205 |
"
|
21181
|
206 |
! !
|
|
207 |
|
|
208 |
!OSProcess class methodsFor:'instance creation'!
|
|
209 |
|
23793
|
210 |
allInstancesDo:aBlock
|
|
211 |
"return an initialized instance for a local process.
|
|
212 |
Keep track of the new instance (so we can quickly enumerate all of them)"
|
|
213 |
|
|
214 |
Instances notNil ifTrue:[
|
|
215 |
Instances do:aBlock
|
|
216 |
].
|
|
217 |
|
|
218 |
"
|
|
219 |
super allInstances
|
|
220 |
self allInstances
|
|
221 |
"
|
|
222 |
|
|
223 |
"Created: / 02-03-2019 / 19:19:41 / Claus Gittinger"
|
|
224 |
!
|
|
225 |
|
22437
|
226 |
command:aCommandString
|
22439
|
227 |
"return an initialized instance to execute aCommandString
|
|
228 |
in the current directory (for a local process)"
|
22437
|
229 |
|
|
230 |
^ self new command:aCommandString.
|
|
231 |
!
|
|
232 |
|
22438
|
233 |
command:aCommandString directory:aStringOrFilename
|
22439
|
234 |
"return an initialized instance to execute aCommandString
|
|
235 |
in a given directory (for a local process)"
|
22437
|
236 |
|
22438
|
237 |
^ self new command:aCommandString directory:aStringOrFilename.
|
22437
|
238 |
!
|
|
239 |
|
22568
|
240 |
commandStringForProgramName:executableFile arguments:arrayOfStrings
|
23184
|
241 |
"generate a command line string from appending args (possibley quoted) to the exe-name (with spaces)"
|
|
242 |
|
22568
|
243 |
^ String streamContents:[:s |
|
|
244 |
s nextPutAll:(self possiblyQuoted:executableFile asFilename pathName).
|
23182
|
245 |
arrayOfStrings notNil ifTrue:[
|
|
246 |
arrayOfStrings do:[:eachArg |
|
23183
|
247 |
s space.
|
23182
|
248 |
s nextPutAll:(self possiblyQuoted:eachArg).
|
|
249 |
].
|
|
250 |
]
|
22568
|
251 |
]
|
23182
|
252 |
|
23184
|
253 |
"Modified (comment): / 02-07-2018 / 20:50:29 / cg"
|
22568
|
254 |
!
|
|
255 |
|
21181
|
256 |
new
|
23793
|
257 |
"return an initialized instance for a local process.
|
|
258 |
Keep track of the new instance (so we can quickly enumerate all of them)"
|
21181
|
259 |
|
23793
|
260 |
|newInst|
|
|
261 |
|
|
262 |
Instances isNil ifTrue:[
|
|
263 |
Instances := WeakIdentitySet new
|
|
264 |
].
|
|
265 |
newInst := self basicNew initialize.
|
|
266 |
Instances add:newInst.
|
|
267 |
^ newInst
|
|
268 |
|
|
269 |
"Modified (comment): / 02-03-2019 / 19:15:25 / Claus Gittinger"
|
22198
|
270 |
!
|
|
271 |
|
|
272 |
onHost:aHost
|
|
273 |
"return an initialized instance for a remote process running on another host"
|
|
274 |
|
|
275 |
(SocketAddress hostName:aHost) isLocal ifTrue:[
|
|
276 |
^ self basicNew initialize.
|
|
277 |
].
|
|
278 |
^ RemoteOSProcess basicNew host:aHost; initialize.
|
|
279 |
|
|
280 |
"
|
|
281 |
OSProcess onHost:'localhost'
|
|
282 |
OSProcess onHost:'exeptn'
|
22439
|
283 |
|
|
284 |
(OSProcess new) command:'ls'
|
|
285 |
(OSProcess onHost:'exeptn') command:'ls'
|
22198
|
286 |
"
|
22568
|
287 |
!
|
|
288 |
|
|
289 |
possiblyQuoted:aString
|
|
290 |
"should we quote or escape?"
|
|
291 |
|
24018
|
292 |
"/ better quote - qorks on Windows AND Unix
|
|
293 |
^ Filename possiblyQuotedPathname:aString.
|
|
294 |
|
|
295 |
"/ ^ String streamContents:[:s |
|
|
296 |
"/ aString do:[:ch |
|
|
297 |
"/ (ch isSeparator or:[ch = $\]) ifTrue:[
|
|
298 |
"/ s nextPut:$\
|
|
299 |
"/ ].
|
|
300 |
"/ s nextPut:ch
|
|
301 |
"/ ]
|
|
302 |
"/ ]
|
22568
|
303 |
|
|
304 |
"
|
|
305 |
self possiblyQuoted:'foo bar'
|
|
306 |
self possiblyQuoted:'foo\bar'
|
|
307 |
"
|
24018
|
308 |
|
|
309 |
"Modified: / 28-03-2019 / 16:19:41 / Claus Gittinger"
|
22568
|
310 |
!
|
|
311 |
|
|
312 |
programName:executableFile arguments:arrayOfStrings initialEnvironment:stringDictionary
|
|
313 |
"similar to command:, but with separate command and arguments"
|
|
314 |
|
|
315 |
^ self new
|
|
316 |
command:(self commandStringForProgramName:executableFile arguments:arrayOfStrings);
|
|
317 |
environment:stringDictionary;
|
|
318 |
yourself
|
23182
|
319 |
|
|
320 |
"
|
|
321 |
(self programName:'notepad.exe' arguments:#() initialEnvironment:nil)
|
|
322 |
showWindow:true;
|
|
323 |
startProcess
|
|
324 |
"
|
|
325 |
|
|
326 |
"Modified (comment): / 02-07-2018 / 20:33:02 / cg"
|
12890
|
327 |
! !
|
|
328 |
|
22439
|
329 |
!OSProcess class methodsFor:'class initialization'!
|
21172
|
330 |
|
|
331 |
initialize
|
22568
|
332 |
"Backward compatibility: Win32Process is an alias for OSProcess"
|
|
333 |
|
21172
|
334 |
Win32Process := self.
|
|
335 |
! !
|
|
336 |
|
22568
|
337 |
!OSProcess class methodsFor:'queries'!
|
|
338 |
|
|
339 |
defaultShellPath
|
|
340 |
OperatingSystem isUNIXlike ifTrue:[
|
|
341 |
^ OperatingSystem getEnvironment:'SHELL'
|
|
342 |
].
|
|
343 |
^ 'cmd'
|
|
344 |
! !
|
|
345 |
|
23201
|
346 |
!OSProcess class methodsFor:'utilities'!
|
|
347 |
|
|
348 |
getTaskList
|
|
349 |
"get a tasklist (Windows OS only)"
|
|
350 |
|
|
351 |
|lines sepIdx entries|
|
|
352 |
|
|
353 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
354 |
lines := (PipeStream outputFromCommand:'tasklist') asStringCollection.
|
|
355 |
sepIdx := lines findFirst:[:line | line startsWith:'=='].
|
|
356 |
sepIdx == 0 ifTrue:[ ^ #() ].
|
|
357 |
entries := (lines from:(sepIdx + 1))
|
|
358 |
collect:[:line |
|
|
359 |
|s idx executableName processID userName|
|
|
360 |
|
|
361 |
s := line readStream.
|
|
362 |
executableName := (s upToMatching:[:ch | ch isDigit]) withoutSeparators.
|
|
363 |
processID := Integer readFrom:s.
|
|
364 |
s skipSeparators.
|
|
365 |
userName := s nextAlphaNumericWord.
|
|
366 |
|
23204
|
367 |
ProcessListEntry executableName:executableName processID:processID sessionName:userName
|
23202
|
368 |
]
|
|
369 |
as:OrderedCollection.
|
23201
|
370 |
] ifFalse:[
|
23213
|
371 |
lines := (PipeStream outputFromCommand:'ps') asStringCollection.
|
|
372 |
sepIdx := lines findFirst:[:line | line withoutSeparators first isDigit].
|
|
373 |
sepIdx == 0 ifTrue:[ ^ #() ].
|
|
374 |
entries := (lines from:(sepIdx + 1))
|
|
375 |
collect:[:line |
|
|
376 |
|s idx executableName processID tty time|
|
|
377 |
|
|
378 |
s := line readStream.
|
|
379 |
processID := Integer readFrom:s.
|
|
380 |
s skipSeparators.
|
|
381 |
tty := s upToSeparator.
|
|
382 |
s skipSeparators.
|
|
383 |
time := s upToSeparator.
|
|
384 |
s skipSeparators.
|
|
385 |
executableName := s upToEnd.
|
|
386 |
(executableName startsWith:'-') ifTrue:[
|
|
387 |
executableName := executableName copyFrom:2.
|
|
388 |
].
|
|
389 |
ProcessListEntry executableName:executableName processID:processID sessionName:nil
|
|
390 |
]
|
|
391 |
as:OrderedCollection.
|
23201
|
392 |
].
|
23213
|
393 |
^ entries
|
23201
|
394 |
|
|
395 |
"
|
|
396 |
self getTaskList
|
|
397 |
"
|
23213
|
398 |
|
|
399 |
"Modified: / 16-07-2018 / 23:11:05 / Claus Gittinger"
|
23201
|
400 |
! !
|
|
401 |
|
12890
|
402 |
!OSProcess methodsFor:'accessing'!
|
|
403 |
|
24724
|
404 |
auxInStream
|
|
405 |
^ auxInStream
|
21181
|
406 |
!
|
|
407 |
|
24724
|
408 |
auxInStream:aStream
|
21552
|
409 |
"set an auxiliary input stream that will be available to the command as
|
21208
|
410 |
file descriptor 3"
|
|
411 |
|
24724
|
412 |
auxInStream := aStream.
|
|
413 |
|
|
414 |
"Modified (comment): / 23-02-2017 / 10:56:57 / Maren"
|
|
415 |
!
|
|
416 |
|
|
417 |
auxOutStream
|
|
418 |
^ auxOutStream
|
|
419 |
!
|
|
420 |
|
|
421 |
auxOutStream:aStream
|
|
422 |
"set an auxiliary output stream that will be available to the command as
|
|
423 |
file descriptor 4"
|
|
424 |
|
|
425 |
auxOutStream := aStream.
|
|
426 |
|
|
427 |
"Modified (comment): / 23-02-2017 / 10:56:57 / Maren"
|
|
428 |
!
|
|
429 |
|
|
430 |
auxStream
|
|
431 |
<resource: #obsolete>
|
|
432 |
^ self auxInStream
|
|
433 |
!
|
|
434 |
|
|
435 |
auxStream:aStream
|
|
436 |
<resource: #obsolete>
|
|
437 |
"set an auxiliary input stream that will be available to the command as
|
|
438 |
file descriptor 3"
|
|
439 |
|
|
440 |
self auxInStream:aStream.
|
21552
|
441 |
|
|
442 |
"Modified (comment): / 23-02-2017 / 10:56:57 / Maren"
|
21181
|
443 |
!
|
|
444 |
|
21172
|
445 |
command
|
24682
|
446 |
"the OS (shell-) command, as string or string-array"
|
21172
|
447 |
|
|
448 |
^ command
|
|
449 |
|
|
450 |
"Created: / 10.11.1998 / 21:27:07 / cg"
|
|
451 |
!
|
|
452 |
|
22437
|
453 |
command:aStringOrArray
|
21208
|
454 |
"set the command to be executed.
|
22439
|
455 |
|
21208
|
456 |
If aStringOrArray is a String, the commandString is passed to a shell for execution
|
|
457 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
22439
|
458 |
With an empty command, the shell will read commands from the passed input stream.
|
|
459 |
|
21208
|
460 |
If aCommandString is an Array, the first element is the command to be executed,
|
22439
|
461 |
and the other elements are the arguments to the command.
|
|
462 |
No shell is invoked in this case."
|
22437
|
463 |
|
21208
|
464 |
command := aStringOrArray.
|
21172
|
465 |
!
|
|
466 |
|
22436
|
467 |
command:commandStringArg directory:stringOrFilenameArg
|
22438
|
468 |
"set the command to be executed and directory, where to execute.
|
|
469 |
If aStringOrArray is a String, the commandString is passed to a shell for execution
|
|
470 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
|
471 |
If aCommandString is an Array, the first element is the command to be executed,
|
|
472 |
and the other elements are the arguments to the command. No shell is invoked in this case."
|
|
473 |
|
22436
|
474 |
command := commandStringArg.
|
|
475 |
directory := stringOrFilenameArg.
|
|
476 |
!
|
|
477 |
|
24822
|
478 |
command:commandArg directory:directoryArg outStream:outStreamArg errorStream:errorStreamArg
|
|
479 |
"set the command to be executed & directory, where to execute.
|
|
480 |
and output/error streams.
|
|
481 |
See comments in individual setters for more info"
|
|
482 |
|
|
483 |
command := commandArg.
|
|
484 |
directory := directoryArg.
|
|
485 |
outStream := outStreamArg.
|
|
486 |
errorStream := errorStreamArg.
|
|
487 |
|
|
488 |
|
|
489 |
!
|
|
490 |
|
23505
|
491 |
command:commandArg environment:environmentArg directory:directoryArg inStream:inStreamArg outStream:outStreamArg errorStream:errorStreamArg
|
|
492 |
"set the command to be executed & directory, where to execute.
|
|
493 |
and input/output streams.
|
|
494 |
See comments in individual setters for more info"
|
|
495 |
|
|
496 |
command := commandArg.
|
|
497 |
environment := environmentArg.
|
|
498 |
directory := directoryArg.
|
|
499 |
inStream := inStreamArg.
|
|
500 |
outStream := outStreamArg.
|
|
501 |
errorStream := errorStreamArg.
|
|
502 |
|
|
503 |
"Created: / 04-11-2018 / 18:28:24 / Claus Gittinger"
|
|
504 |
!
|
|
505 |
|
24724
|
506 |
command:commandArg environment:environmentArg directory:directoryArg inStream:inStreamArg outStream:outStreamArg errorStream:errorStreamArg auxInStream:auxInStreamArg auxOutStream:auxOutStreamArg showWindow:showWindowArg lineWise:lineWiseArg
|
22438
|
507 |
"set the command to be executed & directory, where to execute.
|
|
508 |
and input/output streams.
|
|
509 |
See comments in individual setters for more info"
|
|
510 |
|
22181
|
511 |
command := commandArg.
|
|
512 |
environment := environmentArg.
|
|
513 |
directory := directoryArg.
|
|
514 |
inStream := inStreamArg.
|
|
515 |
outStream := outStreamArg.
|
|
516 |
errorStream := errorStreamArg.
|
24724
|
517 |
auxInStream := auxInStreamArg.
|
|
518 |
auxOutStream := auxOutStreamArg.
|
|
519 |
showWindow := showWindowArg.
|
|
520 |
lineWise := lineWiseArg.
|
|
521 |
!
|
|
522 |
|
|
523 |
command:commandArg environment:environmentArg directory:directoryArg inStream:inStreamArg outStream:outStreamArg errorStream:errorStreamArg auxStream:auxInStreamArg showWindow:showWindowArg lineWise:lineWiseArg
|
|
524 |
"set the command to be executed & directory, where to execute.
|
|
525 |
and input/output streams.
|
|
526 |
See comments in individual setters for more info"
|
|
527 |
|
|
528 |
command := commandArg.
|
|
529 |
environment := environmentArg.
|
|
530 |
directory := directoryArg.
|
|
531 |
inStream := inStreamArg.
|
|
532 |
outStream := outStreamArg.
|
|
533 |
errorStream := errorStreamArg.
|
|
534 |
auxInStream := auxInStreamArg.
|
22181
|
535 |
showWindow := showWindowArg.
|
|
536 |
lineWise := lineWiseArg.
|
|
537 |
!
|
|
538 |
|
24820
|
539 |
command:commandArg outStream:outStreamArg errorStream:errorStreamArg
|
24821
|
540 |
"set the command to be executed and output+error streams.
|
24820
|
541 |
See comments in individual setters for more info"
|
|
542 |
|
|
543 |
command := commandArg.
|
|
544 |
outStream := outStreamArg.
|
|
545 |
errorStream := errorStreamArg.
|
|
546 |
!
|
|
547 |
|
24682
|
548 |
commandString
|
|
549 |
"the OS (shell-) command, as string"
|
|
550 |
|
|
551 |
command isString ifTrue:[^ command].
|
|
552 |
^ command asStringWith:' '
|
|
553 |
|
|
554 |
"Created: / 10.11.1998 / 21:27:07 / cg"
|
|
555 |
!
|
|
556 |
|
21172
|
557 |
directory
|
22438
|
558 |
"the directory where executed"
|
21172
|
559 |
|
|
560 |
^ directory
|
|
561 |
|
|
562 |
"Created: / 10.11.1998 / 21:21:52 / cg"
|
|
563 |
!
|
|
564 |
|
22436
|
565 |
directory:aStringOrFilename
|
21208
|
566 |
"set the directory that will be set as the current directory of the command to be executed"
|
21172
|
567 |
|
22436
|
568 |
directory := aStringOrFilename.
|
21172
|
569 |
!
|
|
570 |
|
|
571 |
environment
|
22438
|
572 |
"the shell environment"
|
21172
|
573 |
|
|
574 |
^ environment
|
|
575 |
|
|
576 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
577 |
!
|
|
578 |
|
21208
|
579 |
environment:aDictionary
|
|
580 |
"set the environment variables of the command to be executed"
|
21172
|
581 |
|
21208
|
582 |
environment := aDictionary.
|
21172
|
583 |
!
|
|
584 |
|
|
585 |
errorStream
|
|
586 |
^ errorStream
|
|
587 |
|
|
588 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
12890
|
589 |
!
|
|
590 |
|
21208
|
591 |
errorStream:aStream
|
|
592 |
"set the stream where the stderr output of the command is directed to"
|
21172
|
593 |
|
21208
|
594 |
errorStream := aStream.
|
21172
|
595 |
!
|
|
596 |
|
|
597 |
exitStatus
|
24681
|
598 |
"answer the exit status of the command (instance of OSProcessStatus).
|
|
599 |
or nil if the command has not yet been finished."
|
21172
|
600 |
|
|
601 |
^ exitStatus
|
|
602 |
!
|
|
603 |
|
|
604 |
finishSema
|
21552
|
605 |
"wait on this semaphore if you want to wait until the OS process has finished.
|
21184
|
606 |
There may be multiple waiters, so it is a good idea to do a #waitUncounted"
|
21172
|
607 |
|
|
608 |
^ finishSema
|
21552
|
609 |
|
|
610 |
"Modified (comment): / 23-02-2017 / 10:53:17 / Maren"
|
21172
|
611 |
!
|
|
612 |
|
|
613 |
inStream
|
|
614 |
^ inStream
|
|
615 |
|
|
616 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
617 |
!
|
|
618 |
|
21208
|
619 |
inStream:aStream
|
|
620 |
"set the stream where the stdin input of the command is read from"
|
21172
|
621 |
|
21208
|
622 |
inStream := aStream.
|
21172
|
623 |
!
|
|
624 |
|
21181
|
625 |
lineWise
|
|
626 |
^ lineWise
|
|
627 |
!
|
|
628 |
|
21208
|
629 |
lineWise:aBoolean
|
21209
|
630 |
"When setting to true, read linewise from the command's output and error.
|
|
631 |
This is a bit slower than lineWise = false.
|
21208
|
632 |
|
|
633 |
You may use it also when streaming to e.g. Transcript"
|
|
634 |
|
|
635 |
lineWise := aBoolean.
|
21181
|
636 |
!
|
|
637 |
|
21182
|
638 |
newPgrp
|
|
639 |
^ newPgrp
|
|
640 |
!
|
|
641 |
|
21208
|
642 |
newPgrp:aBoolean
|
|
643 |
"if aBoolean is true, a new process group will be created for the command and its subprocesses"
|
|
644 |
|
|
645 |
newPgrp := aBoolean.
|
21182
|
646 |
!
|
|
647 |
|
23223
|
648 |
numericPid
|
|
649 |
"answer the pid of the process the command is running in,
|
|
650 |
or nil if the command has not yet been started.
|
23537
|
651 |
Always return an integer, even in windows where pid is a handle-"
|
23223
|
652 |
|
23635
|
653 |
pid isOsHandle ifTrue:[
|
|
654 |
^ pid pid.
|
23223
|
655 |
].
|
23635
|
656 |
^ pid. "Integer or nil"
|
23223
|
657 |
|
|
658 |
"Created: / 19-07-2018 / 16:21:25 / Stefan Vogel"
|
23537
|
659 |
"Modified (comment): / 20-11-2018 / 16:50:26 / Stefan Vogel"
|
23223
|
660 |
!
|
|
661 |
|
21172
|
662 |
outStream
|
22985
|
663 |
"the stream where the stdout output of the command is directed to"
|
|
664 |
|
21172
|
665 |
^ outStream
|
|
666 |
|
|
667 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
668 |
!
|
|
669 |
|
21208
|
670 |
outStream:aStream
|
|
671 |
"set the stream where the stdout output of the command is directed to"
|
21172
|
672 |
|
21208
|
673 |
outStream := aStream.
|
12890
|
674 |
!
|
|
675 |
|
|
676 |
pid
|
23204
|
677 |
"answer the pid of the process the command is running in,
|
|
678 |
or nil if the command has not yet been started.
|
|
679 |
Notice: on Unix, the pid is an integer;
|
|
680 |
on Windows, it is a handle."
|
21208
|
681 |
|
12890
|
682 |
^ pid
|
|
683 |
!
|
|
684 |
|
21181
|
685 |
showWindow
|
|
686 |
^ showWindow
|
|
687 |
!
|
|
688 |
|
21208
|
689 |
showWindow:aBooleanOrNil
|
|
690 |
"This parameter is ignored on Unix systems.
|
|
691 |
|
|
692 |
You can control (have to - sigh) if a window should be shown for the command or not.
|
|
693 |
This is the OS's H_SHOWWINDOW argument.
|
|
694 |
If you pass nil as showWindow-argument, the OS's default is used for the particular
|
|
695 |
command, which is correct most of the time: i.e. a notepad will open its window, other (non-UI)
|
|
696 |
executables will not.
|
|
697 |
However, some command-line executables show a window, even if they should not.
|
|
698 |
(and also, there seems to be an inconsistency between windows7 and newer windows: in newer,
|
|
699 |
a shell command opens a cmd-window, whereas in windows7 it did not)
|
|
700 |
In this case, pass an explicit false argument to suppress it."
|
|
701 |
|
|
702 |
showWindow := aBooleanOrNil.
|
21206
|
703 |
!
|
|
704 |
|
|
705 |
terminateActionBlock
|
22985
|
706 |
"the (user provided) callback block,
|
|
707 |
that will be executed when the command has finished or was terminated.
|
|
708 |
If non-nil, it will be called with optional argument:status and:self, the OSProcess."
|
|
709 |
|
21206
|
710 |
^ terminateActionBlock
|
22985
|
711 |
|
|
712 |
"Modified (comment): / 22-05-2018 / 12:21:24 / Claus Gittinger"
|
21206
|
713 |
!
|
|
714 |
|
21208
|
715 |
terminateActionBlock:aBlock
|
22985
|
716 |
"set the callback block,
|
|
717 |
that will be called when the command has finished or was terminated.
|
22986
|
718 |
If non-nil, it will be called with optional argument:status and:self, the OSProcess.
|
|
719 |
WARNING:
|
|
720 |
if the active-process calling #startProcess is already dead (i.e. terminated),
|
|
721 |
this callback will be called by the scheduler;
|
|
722 |
otherwise, the calling process will be interrupted to perform this callback.
|
|
723 |
Therefore: do not open any GUI or other blocking actions in it."
|
21208
|
724 |
|
|
725 |
terminateActionBlock := aBlock.
|
22985
|
726 |
|
22986
|
727 |
"Modified (comment): / 22-05-2018 / 12:42:21 / Claus Gittinger"
|
12890
|
728 |
! !
|
|
729 |
|
21181
|
730 |
!OSProcess methodsFor:'initialization'!
|
|
731 |
|
|
732 |
initialize
|
|
733 |
"Invoked when a new instance is created."
|
|
734 |
|
|
735 |
showWindow := false. "/ for backward compatibility
|
21183
|
736 |
lineWise := false.
|
|
737 |
newPgrp := true.
|
21181
|
738 |
! !
|
|
739 |
|
23002
|
740 |
|
13313
|
741 |
!OSProcess methodsFor:'printing'!
|
|
742 |
|
|
743 |
printOn:aStream
|
|
744 |
aStream
|
|
745 |
nextPutAll:self className;
|
|
746 |
nextPut:$(.
|
23641
|
747 |
exitStatus notNil ifTrue:[
|
|
748 |
exitStatus printOn:aStream.
|
|
749 |
] ifFalse:[
|
|
750 |
pid printOn:aStream.
|
|
751 |
].
|
13313
|
752 |
aStream space.
|
21172
|
753 |
command printOn:aStream.
|
13313
|
754 |
aStream nextPut:$)
|
23641
|
755 |
|
|
756 |
"Modified: / 23-01-2019 / 13:55:08 / Claus Gittinger"
|
13313
|
757 |
! !
|
|
758 |
|
21181
|
759 |
!OSProcess methodsFor:'private'!
|
|
760 |
|
24471
|
761 |
closeHelperStreams
|
|
762 |
"close the helper streams that have been opened for shuffler processes
|
|
763 |
(remote side of pipe)"
|
|
764 |
|
|
765 |
|streams|
|
|
766 |
|
|
767 |
streams := streamsToClose.
|
|
768 |
streams notNil ifTrue:[
|
24496
|
769 |
Logger debug:'OSProcess: closing leftover pipe streams for "%1"' with:self.
|
24471
|
770 |
streamsToClose := nil.
|
|
771 |
streams do:[:eachStream | eachStream close].
|
|
772 |
].
|
|
773 |
!
|
|
774 |
|
24681
|
775 |
exitStatus:something
|
|
776 |
"not used and should not be"
|
|
777 |
|
|
778 |
exitStatus := something.
|
|
779 |
|
|
780 |
"Created: / 10.11.1998 / 21:24:55 / cg"
|
|
781 |
!
|
|
782 |
|
23635
|
783 |
releasePid
|
|
784 |
"only used in Windows, where pid is a handle that must be released"
|
|
785 |
|
|
786 |
|pidHandle|
|
|
787 |
|
|
788 |
pidHandle := pid.
|
|
789 |
pidHandle isOsHandle ifFalse:[
|
|
790 |
^ self.
|
|
791 |
].
|
|
792 |
|
|
793 |
pid := pidHandle pid. "remember the numeric value"
|
|
794 |
pidHandle close.
|
|
795 |
!
|
|
796 |
|
21181
|
797 |
setupShufflerForInput:aStream
|
21182
|
798 |
"if aStream is an internal Stream, set up a pipe for the command input.
|
21552
|
799 |
Start a process that shuffles the data from the internal stream into the pipe
|
23501
|
800 |
(and into the command's input).
|
|
801 |
Return the ExternalStream that should be passed to the OS process."
|
21182
|
802 |
|
21181
|
803 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
804 |
|
|
805 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
806 |
^ aStream.
|
|
807 |
].
|
|
808 |
|
23486
|
809 |
pipe := PipeStream makePipe.
|
21181
|
810 |
externalStream := pipe at:1.
|
|
811 |
shuffledStream := pipe at:2.
|
24011
|
812 |
shuffledStream setCommandString:('Stdin of: ', command printString).
|
21181
|
813 |
lineWise ifFalse:[
|
|
814 |
shuffledStream blocking:false.
|
|
815 |
].
|
|
816 |
aStream isBinary ifTrue:[
|
|
817 |
shuffledStream binary.
|
|
818 |
].
|
|
819 |
|
|
820 |
"/ start a reader process, shuffling data from the given
|
|
821 |
"/ inStream to the pipe (which is connected to the commands input)
|
22198
|
822 |
shufflerProcess :=
|
|
823 |
[
|
21181
|
824 |
[
|
21332
|
825 |
lineWise ifTrue:[
|
|
826 |
"shuffle until end-of-input"
|
|
827 |
[aStream atEnd] whileFalse:[
|
|
828 |
|data|
|
21184
|
829 |
|
21332
|
830 |
data := aStream nextLine.
|
|
831 |
data notNil ifTrue:[
|
|
832 |
shuffledStream nextPutLine:data.
|
|
833 |
shuffledStream flush.
|
21184
|
834 |
].
|
|
835 |
].
|
21332
|
836 |
] ifFalse:[
|
|
837 |
aStream copyToEndInto:shuffledStream.
|
21184
|
838 |
].
|
21332
|
839 |
] on:WriteError do:[:ex|
|
|
840 |
"ignore"
|
21181
|
841 |
] ensure:[
|
|
842 |
shuffledStream close.
|
|
843 |
]
|
|
844 |
] newProcess
|
23280
|
845 |
name:'OSProcess: input shuffler';
|
21181
|
846 |
"/ beSystemProcess;
|
|
847 |
resume.
|
|
848 |
|
23501
|
849 |
inputShufflerProcesses isNil ifTrue:[
|
|
850 |
inputShufflerProcesses := OrderedCollection new:2.
|
|
851 |
].
|
|
852 |
inputShufflerProcesses add:shufflerProcess.
|
24471
|
853 |
streamsToClose isNil ifTrue:[
|
|
854 |
streamsToClose := OrderedCollection new:4.
|
|
855 |
].
|
21184
|
856 |
streamsToClose add:externalStream.
|
|
857 |
|
21181
|
858 |
^ externalStream
|
21332
|
859 |
|
|
860 |
"Modified: / 31-01-2017 / 16:50:39 / stefan"
|
21552
|
861 |
"Modified (comment): / 23-02-2017 / 10:51:34 / Maren"
|
23486
|
862 |
"Modified: / 29-10-2018 / 15:35:01 / Claus Gittinger"
|
23501
|
863 |
"Modified: / 01-11-2018 / 14:40:12 / Stefan Vogel"
|
21181
|
864 |
!
|
|
865 |
|
|
866 |
setupShufflerForOutput:aStream
|
21182
|
867 |
"if aStream is an internal Stream, set up a pipe for the command output.
|
23486
|
868 |
Start a process that shuffles the data from the pipe's output
|
23501
|
869 |
into the internal stream.
|
|
870 |
Return the ExternalStream that should be passed to the OS process."
|
21182
|
871 |
|
21181
|
872 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
873 |
|
|
874 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
875 |
^ aStream.
|
|
876 |
].
|
|
877 |
|
23486
|
878 |
pipe := PipeStream makePipe.
|
21181
|
879 |
externalStream := pipe at:2.
|
|
880 |
shuffledStream := pipe at:1.
|
24011
|
881 |
shuffledStream setCommandString:('Stdout/err of: ', command printString).
|
21181
|
882 |
aStream isBinary ifTrue:[
|
|
883 |
shuffledStream binary.
|
|
884 |
].
|
|
885 |
|
22198
|
886 |
shufflerProcess :=
|
|
887 |
[
|
21332
|
888 |
[
|
|
889 |
"shuffle until the pipe closes"
|
|
890 |
lineWise ifTrue:[
|
|
891 |
[shuffledStream atEnd] whileFalse:[
|
|
892 |
|data|
|
21184
|
893 |
|
21332
|
894 |
data := shuffledStream nextLine.
|
24429
|
895 |
"/ Transcript showCR:'got: ',data printString.
|
21332
|
896 |
data notNil ifTrue:[
|
24429
|
897 |
"/ Transcript showCR:'for: ',aStream printString.
|
21332
|
898 |
aStream nextPutLine:data
|
21184
|
899 |
].
|
|
900 |
].
|
21332
|
901 |
] ifFalse:[
|
|
902 |
shuffledStream copyToEndInto:aStream.
|
21181
|
903 |
].
|
21332
|
904 |
] on:WriteError do:[:ex |
|
|
905 |
"ignore"
|
|
906 |
] ensure:[
|
|
907 |
shuffledStream close.
|
21181
|
908 |
].
|
|
909 |
] newProcess
|
|
910 |
priority:(Processor userSchedulingPriority "+ 1");
|
23280
|
911 |
name:'OSProcess: output shuffler';
|
21181
|
912 |
"/ beSystemProcess;
|
|
913 |
resume.
|
|
914 |
|
23501
|
915 |
outputShufflerProcesses isNil ifTrue:[
|
|
916 |
outputShufflerProcesses := OrderedCollection new:2.
|
|
917 |
].
|
|
918 |
outputShufflerProcesses add:shufflerProcess.
|
24471
|
919 |
streamsToClose isNil ifTrue:[
|
|
920 |
streamsToClose := OrderedCollection new:4.
|
|
921 |
].
|
21184
|
922 |
streamsToClose add:externalStream.
|
|
923 |
|
21181
|
924 |
^ externalStream
|
21332
|
925 |
|
|
926 |
"Modified: / 31-01-2017 / 16:57:25 / stefan"
|
23501
|
927 |
"Modified: / 01-11-2018 / 14:40:24 / Stefan Vogel"
|
24429
|
928 |
"Modified: / 17-07-2019 / 19:53:26 / Claus Gittinger"
|
22198
|
929 |
!
|
|
930 |
|
|
931 |
startCommand
|
|
932 |
"the 'real' command to be executed.
|
22987
|
933 |
Redefined for remote processes (eg. to construct a remote command string).
|
|
934 |
Command may be both an Array or a String"
|
22198
|
935 |
|
|
936 |
^ command
|
22987
|
937 |
|
|
938 |
"Modified (comment): / 22-05-2018 / 12:37:38 / Stefan Vogel"
|
23501
|
939 |
!
|
|
940 |
|
|
941 |
terminateShufflerProcesses
|
|
942 |
"terminate all the running shuffler processes"
|
|
943 |
|
|
944 |
self
|
|
945 |
terminateShufflerProcesses:inputShufflerProcesses;
|
|
946 |
terminateShufflerProcesses:outputShufflerProcesses.
|
|
947 |
|
|
948 |
inputShufflerProcesses := outputShufflerProcesses := nil.
|
|
949 |
|
|
950 |
"Created: / 01-11-2018 / 14:16:51 / Stefan Vogel"
|
|
951 |
"Modified: / 01-11-2018 / 15:37:16 / Stefan Vogel"
|
|
952 |
!
|
|
953 |
|
|
954 |
terminateShufflerProcesses:aProcessCollection
|
|
955 |
"terminate the running shuffler processes contained in aProcessCollection"
|
|
956 |
|
|
957 |
|procs|
|
|
958 |
|
|
959 |
procs := aProcessCollection.
|
|
960 |
procs notNil ifTrue:[
|
24724
|
961 |
procs copy do:[:eachProc | eachProc terminate].
|
23501
|
962 |
procs removeAll.
|
|
963 |
].
|
|
964 |
|
|
965 |
"Created: / 01-11-2018 / 14:35:51 / Stefan Vogel"
|
|
966 |
"Modified: / 01-11-2018 / 15:36:12 / Stefan Vogel"
|
21181
|
967 |
! !
|
|
968 |
|
12890
|
969 |
!OSProcess methodsFor:'queries'!
|
|
970 |
|
21208
|
971 |
finishedWithSuccess
|
|
972 |
^ exitStatus notNil and:[exitStatus success].
|
|
973 |
!
|
|
974 |
|
12890
|
975 |
isAlive
|
21552
|
976 |
"answer true if the process is still alive"
|
12890
|
977 |
|
21205
|
978 |
^ pid notNil and:[exitStatus isNil]
|
21552
|
979 |
|
|
980 |
"Modified (comment): / 23-02-2017 / 10:52:37 / Maren"
|
12890
|
981 |
!
|
|
982 |
|
|
983 |
isDead
|
21552
|
984 |
"answer true if the process is no longer alive"
|
12890
|
985 |
|
|
986 |
^ self isAlive not
|
21552
|
987 |
|
|
988 |
"Modified (comment): / 23-02-2017 / 10:52:31 / Maren"
|
12890
|
989 |
! !
|
|
990 |
|
21172
|
991 |
!OSProcess methodsFor:'starting'!
|
|
992 |
|
21181
|
993 |
execute
|
21184
|
994 |
"execute the command.
|
21552
|
995 |
Wait until it is finished.
|
21184
|
996 |
Abort the execution if I am interrupted.
|
24547
|
997 |
Answer true if it successfully terminated,
|
21184
|
998 |
false if it could not be started or terminated with error."
|
21181
|
999 |
|
24471
|
1000 |
^ [
|
|
1001 |
self startProcess
|
|
1002 |
and:[
|
23501
|
1003 |
self waitUntilFinished.
|
24471
|
1004 |
self finishedWithSuccess.
|
21185
|
1005 |
].
|
23501
|
1006 |
"if we come here, all streamsToClose have been closed
|
|
1007 |
and all shuffler processses are terminated"
|
|
1008 |
] ifCurtailed:[
|
|
1009 |
"we have been interrupted -
|
|
1010 |
terminate the os-command (and all of its forked commands), and clean up"
|
|
1011 |
pid notNil ifTrue:[
|
|
1012 |
self terminateGroup.
|
|
1013 |
].
|
24471
|
1014 |
self
|
|
1015 |
closeHelperStreams;
|
|
1016 |
terminateShufflerProcesses.
|
21181
|
1017 |
].
|
23501
|
1018 |
|
24471
|
1019 |
"Modified: / 29-10-2018 / 15:59:33 / Claus Gittinger"
|
|
1020 |
"Modified: / 31-10-2018 / 12:31:42 / Maren"
|
|
1021 |
"Modified (format): / 01-11-2018 / 15:00:01 / Stefan Vogel"
|
|
1022 |
!
|
|
1023 |
|
|
1024 |
executeKeepingProcess
|
|
1025 |
"execute the command.
|
|
1026 |
If I am interrupted keep the process as background process.
|
|
1027 |
Wait until it is finished.
|
24547
|
1028 |
Answer true if it successfully terminated,
|
24471
|
1029 |
false if it could not be started or terminated with error."
|
|
1030 |
|
|
1031 |
^ [
|
|
1032 |
self startProcess
|
|
1033 |
and:[
|
|
1034 |
self waitUntilFinished.
|
|
1035 |
self finishedWithSuccess.
|
|
1036 |
].
|
|
1037 |
"if we come here, all streamsToClose have been closed
|
|
1038 |
and all shuffler processses are terminated"
|
|
1039 |
] ifCurtailed:[
|
24495
|
1040 |
"we have been interrupted - clean up.
|
|
1041 |
Different from #execute - here we do NOT terminate the OS-command"
|
24471
|
1042 |
self
|
|
1043 |
closeHelperStreams;
|
|
1044 |
terminateShufflerProcesses;
|
|
1045 |
releasePid.
|
|
1046 |
].
|
21552
|
1047 |
|
23486
|
1048 |
"Modified: / 29-10-2018 / 15:59:33 / Claus Gittinger"
|
23493
|
1049 |
"Modified: / 31-10-2018 / 12:31:42 / Maren"
|
24495
|
1050 |
"Modified (format): / 30-07-2019 / 12:00:12 / Stefan Vogel"
|
21181
|
1051 |
!
|
|
1052 |
|
21172
|
1053 |
startProcess
|
22987
|
1054 |
"Start the command asynchronously (i.e. don't wait until is has finished).
|
|
1055 |
If there are non-external streams, setup transfer (shuffler) processes
|
21184
|
1056 |
to transfer data from a pipe to the internal stream.
|
22987
|
1057 |
|
|
1058 |
Answer true if the command could started succesfully, false if not.
|
|
1059 |
|
|
1060 |
NOTE: under normal circumstances, even if the command cannot be found,
|
|
1061 |
the exit fom the command interpreter/shell is done some time later
|
|
1062 |
So you have to check later or set #terminateActionBlock: and check there."
|
21184
|
1063 |
|
24724
|
1064 |
|externalInStream externalAuxInStream externalErrorStream externalOutStream
|
|
1065 |
externalAuxOutStream callingProcess|
|
21184
|
1066 |
|
|
1067 |
externalInStream := self setupShufflerForInput:inStream.
|
24724
|
1068 |
auxInStream notNil ifTrue:[
|
|
1069 |
externalAuxInStream := self setupShufflerForInput:auxInStream.
|
|
1070 |
].
|
|
1071 |
auxOutStream notNil ifTrue:[
|
|
1072 |
externalAuxOutStream := self setupShufflerForOutput:auxOutStream.
|
|
1073 |
].
|
21226
|
1074 |
externalOutStream := self setupShufflerForOutput:outStream.
|
22198
|
1075 |
|
21184
|
1076 |
errorStream == outStream ifTrue:[
|
21226
|
1077 |
externalErrorStream := externalOutStream.
|
21184
|
1078 |
] ifFalse:[
|
|
1079 |
externalErrorStream := self setupShufflerForOutput:errorStream.
|
|
1080 |
].
|
|
1081 |
|
|
1082 |
"start the command"
|
21200
|
1083 |
finishSema := EventSemaphore new.
|
21172
|
1084 |
|
22983
|
1085 |
"/ UserPreferences current logExecutedOSCommands:true
|
|
1086 |
UserPreferences current logExecutedOSCommands ifTrue:[
|
23002
|
1087 |
Transcript showCR:(('OS command: ', self startCommand printString) withColor:Color brown).
|
22983
|
1088 |
].
|
22986
|
1089 |
|
|
1090 |
callingProcess := Processor activeProcess.
|
22983
|
1091 |
|
25070
|
1092 |
pid := Processor
|
21172
|
1093 |
monitor:[
|
25070
|
1094 |
OperatingSystem
|
22198
|
1095 |
startProcess:(self startCommand)
|
21200
|
1096 |
inputFrom:externalInStream
|
|
1097 |
outputTo:externalOutStream
|
|
1098 |
errorTo:externalErrorStream
|
24724
|
1099 |
auxFrom:externalAuxInStream
|
|
1100 |
aux2To:externalAuxOutStream
|
21200
|
1101 |
environment:environment
|
|
1102 |
inDirectory:directory
|
|
1103 |
newPgrp:newPgrp
|
|
1104 |
showWindow:showWindow.
|
21172
|
1105 |
]
|
|
1106 |
action:[:status |
|
|
1107 |
status stillAlive ifFalse:[
|
|
1108 |
exitStatus := status.
|
24429
|
1109 |
"/ cg: why this? Isn't the process already dead?
|
23006
|
1110 |
self terminate.
|
|
1111 |
|
21223
|
1112 |
finishSema signal.
|
22985
|
1113 |
terminateActionBlock notNil ifTrue:[
|
23391
|
1114 |
callingProcess isDead ifTrue:[
|
|
1115 |
Error catch:[
|
|
1116 |
"don't bother ProcessorScheduler with errors from terminateActionBlock!!"
|
|
1117 |
terminateActionBlock valueWithOptionalArgument:status and:self.
|
|
1118 |
].
|
22986
|
1119 |
] ifFalse:[
|
|
1120 |
callingProcess
|
|
1121 |
interruptWith:[
|
|
1122 |
terminateActionBlock valueWithOptionalArgument:status and:self.
|
|
1123 |
]
|
|
1124 |
].
|
22985
|
1125 |
].
|
23635
|
1126 |
|
|
1127 |
self releasePid.
|
|
1128 |
|
23126
|
1129 |
UserPreferences current logExecutedOSCommands ifTrue:[
|
|
1130 |
Transcript
|
|
1131 |
showCR:(('OS command finished: %1 status: %2'
|
|
1132 |
bindWith: self startCommand
|
|
1133 |
with:status code)
|
|
1134 |
withColor:(
|
|
1135 |
status success
|
23907
|
1136 |
ifTrue:[Color darkGreen]
|
23126
|
1137 |
ifFalse:[Color red])).
|
|
1138 |
].
|
21172
|
1139 |
].
|
|
1140 |
].
|
|
1141 |
|
21184
|
1142 |
"we can close the remote side of the pipes after the os process has been started (after the fork)"
|
24471
|
1143 |
self closeHelperStreams.
|
21184
|
1144 |
|
23488
|
1145 |
"/ nil those references here, because there is still a home-context reference from the
|
|
1146 |
"/ monitor-pid action block. Otheriwse, the garbage collector might not collect them.
|
24724
|
1147 |
externalInStream := externalAuxInStream := externalAuxOutStream := externalOutStream := externalErrorStream := nil.
|
23488
|
1148 |
|
21172
|
1149 |
pid isNil ifTrue:[
|
25070
|
1150 |
"process could not be started!!"
|
23501
|
1151 |
self terminateShufflerProcesses.
|
|
1152 |
|
21172
|
1153 |
exitStatus := OperatingSystem osProcessStatusClass processCreationFailure.
|
21208
|
1154 |
finishSema signal.
|
23006
|
1155 |
terminateActionBlock notNil ifTrue:[
|
|
1156 |
terminateActionBlock valueWithOptionalArgument:exitStatus and:self.
|
|
1157 |
].
|
25070
|
1158 |
UserPreferences current logExecutedOSCommands ifTrue:[
|
|
1159 |
Transcript
|
|
1160 |
showCR:(('OS command failed: %1'bindWith: self startCommand) allRed).
|
|
1161 |
].
|
|
1162 |
|
21184
|
1163 |
^ false.
|
21172
|
1164 |
].
|
|
1165 |
|
|
1166 |
^ true.
|
22985
|
1167 |
|
23501
|
1168 |
"Modified: / 01-11-2018 / 14:41:12 / Stefan Vogel"
|
23907
|
1169 |
"Modified: / 13-03-2019 / 21:13:05 / Claus Gittinger"
|
24429
|
1170 |
"Modified (comment): / 17-07-2019 / 19:33:43 / Claus Gittinger"
|
21172
|
1171 |
! !
|
|
1172 |
|
12890
|
1173 |
!OSProcess methodsFor:'terminating'!
|
|
1174 |
|
|
1175 |
kill
|
|
1176 |
"kill the process - the process does not get the chance to clean up"
|
|
1177 |
|
21208
|
1178 |
pid notNil ifTrue:[
|
|
1179 |
OperatingSystem killProcess:pid.
|
25072
|
1180 |
"do not release the pid here. The release is done,
|
|
1181 |
after the process reported its termination!!"
|
21208
|
1182 |
].
|
12890
|
1183 |
!
|
|
1184 |
|
|
1185 |
killGroup
|
20379
|
1186 |
"kill the processGroup - the processes do not get the chance to clean up"
|
|
1187 |
|
21208
|
1188 |
pid notNil ifTrue:[
|
|
1189 |
OperatingSystem
|
|
1190 |
killProcessGroup:pid;
|
|
1191 |
killProcess:pid.
|
25072
|
1192 |
"do not release the pid here. The release is done,
|
|
1193 |
after the process reported its termination!!"
|
21208
|
1194 |
].
|
12890
|
1195 |
!
|
|
1196 |
|
|
1197 |
terminate
|
|
1198 |
"terminate the process gracefully"
|
|
1199 |
|
21208
|
1200 |
pid notNil ifTrue:[
|
|
1201 |
OperatingSystem terminateProcess:pid.
|
25072
|
1202 |
"do not release the pid here. The release is done,
|
|
1203 |
after the process reported its termination!!"
|
21208
|
1204 |
].
|
12890
|
1205 |
!
|
|
1206 |
|
|
1207 |
terminateGroup
|
|
1208 |
"terminate the process group.
|
21552
|
1209 |
Under Windows, this is the same as terminateWithAllChildren,
|
12890
|
1210 |
under unix, this terminates a subset of all children"
|
|
1211 |
|
21208
|
1212 |
pid notNil ifTrue:[
|
|
1213 |
OperatingSystem
|
21632
|
1214 |
terminateProcess:pid;
|
|
1215 |
terminateProcessGroup:pid.
|
25072
|
1216 |
"do not release the pid here. The release is done,
|
|
1217 |
after the process reported its termination!!"
|
21208
|
1218 |
].
|
21551
|
1219 |
|
21552
|
1220 |
"Modified (comment): / 23-02-2017 / 10:50:13 / Maren"
|
21632
|
1221 |
"Modified: / 09-03-2017 / 15:00:58 / stefan"
|
21184
|
1222 |
! !
|
|
1223 |
|
|
1224 |
!OSProcess methodsFor:'waiting'!
|
|
1225 |
|
|
1226 |
waitUntilFinished
|
23035
|
1227 |
<resource: #skipInDebuggersWalkBack>
|
22148
|
1228 |
|
|
1229 |
"wait with a veryy long timeout,
|
23501
|
1230 |
in order that ProcessorScheduler>>#checkForEndOfDispatch recognizes
|
|
1231 |
this waiting process as an user process which is still alive.
|
|
1232 |
The timeout is meant to never occur!!"
|
22148
|
1233 |
|
|
1234 |
^ self waitUntilFinishedWithTimeout:60*60*24000
|
|
1235 |
|
|
1236 |
"Modified: / 01-08-2017 / 14:41:10 / stefan"
|
|
1237 |
"Modified (comment): / 01-08-2017 / 17:15:55 / stefan"
|
23035
|
1238 |
"Modified: / 30-05-2018 / 13:57:42 / Claus Gittinger"
|
23501
|
1239 |
"Modified (comment): / 01-11-2018 / 14:43:46 / Stefan Vogel"
|
21208
|
1240 |
!
|
|
1241 |
|
|
1242 |
waitUntilFinishedWithTimeout:timeout
|
23035
|
1243 |
<resource: #skipInDebuggersWalkBack>
|
|
1244 |
|
21184
|
1245 |
|processList|
|
|
1246 |
|
21208
|
1247 |
(finishSema waitWithTimeout:timeout) isNil ifTrue:[
|
|
1248 |
"timed out"
|
|
1249 |
^ nil.
|
|
1250 |
].
|
21184
|
1251 |
|
23501
|
1252 |
"have to wait until the output shufflers shuffling command's outputs have finished their work"
|
|
1253 |
processList := outputShufflerProcesses.
|
|
1254 |
processList notNil ifTrue:[
|
21184
|
1255 |
processList do:[:eachProcess |
|
|
1256 |
eachProcess waitUntilTerminated.
|
|
1257 |
].
|
23501
|
1258 |
outputShufflerProcesses := nil.
|
|
1259 |
].
|
|
1260 |
|
|
1261 |
"command has finished and is no longer reading from its inputs"
|
|
1262 |
inputShufflerProcesses notNil ifTrue:[
|
|
1263 |
self terminateShufflerProcesses:inputShufflerProcesses.
|
|
1264 |
inputShufflerProcesses := nil.
|
21184
|
1265 |
].
|
23035
|
1266 |
|
|
1267 |
"Modified: / 30-05-2018 / 13:57:39 / Claus Gittinger"
|
23501
|
1268 |
"Modified: / 01-11-2018 / 15:53:09 / Stefan Vogel"
|
12890
|
1269 |
! !
|
|
1270 |
|
23201
|
1271 |
!OSProcess::ProcessListEntry class methodsFor:'documentation'!
|
|
1272 |
|
|
1273 |
documentation
|
|
1274 |
"
|
|
1275 |
documentation to be added.
|
|
1276 |
|
|
1277 |
[author:]
|
|
1278 |
anharman
|
|
1279 |
|
|
1280 |
[instance variables:]
|
|
1281 |
|
|
1282 |
[class variables:]
|
|
1283 |
|
|
1284 |
[see also:]
|
|
1285 |
|
|
1286 |
"
|
|
1287 |
! !
|
|
1288 |
|
|
1289 |
!OSProcess::ProcessListEntry class methodsFor:'instance creation'!
|
|
1290 |
|
23204
|
1291 |
executableName:executableNameArg processID:processIDArg sessionName:sessionNameArg
|
|
1292 |
^ self new executableName:executableNameArg processID:processIDArg sessionName:sessionNameArg
|
|
1293 |
!
|
|
1294 |
|
23201
|
1295 |
executableName:executableNameArg processID:processIDArg userName:userNameArg
|
|
1296 |
^ self new executableName:executableNameArg processID:processIDArg userName:userNameArg
|
|
1297 |
! !
|
|
1298 |
|
|
1299 |
!OSProcess::ProcessListEntry methodsFor:'accessing'!
|
|
1300 |
|
|
1301 |
executableName
|
|
1302 |
^ executableName
|
|
1303 |
!
|
|
1304 |
|
23204
|
1305 |
executableName:executableNameArg processID:processIDArg sessionName:sessionNameArg
|
|
1306 |
executableName := executableNameArg.
|
|
1307 |
processID := processIDArg.
|
|
1308 |
sessionName := sessionNameArg.
|
|
1309 |
!
|
|
1310 |
|
23201
|
1311 |
executableName:executableNameArg processID:processIDArg userName:userNameArg
|
|
1312 |
executableName := executableNameArg.
|
|
1313 |
processID := processIDArg.
|
|
1314 |
userName := userNameArg.
|
|
1315 |
!
|
|
1316 |
|
|
1317 |
processID
|
|
1318 |
^ processID
|
|
1319 |
!
|
|
1320 |
|
|
1321 |
userName
|
|
1322 |
^ userName
|
|
1323 |
! !
|
|
1324 |
|
23204
|
1325 |
!OSProcess::ProcessListEntry methodsFor:'printing & storing'!
|
|
1326 |
|
|
1327 |
printOn:aStream
|
|
1328 |
aStream nextPutAll:('(process: %1 pid: %2)' bindWith:executableName with:processID)
|
|
1329 |
! !
|
|
1330 |
|
22198
|
1331 |
!OSProcess::RemoteOSProcess class methodsFor:'documentation'!
|
|
1332 |
|
|
1333 |
documentation
|
|
1334 |
"
|
|
1335 |
Instances of OSProcess represent operating system processes that can be executed.
|
|
1336 |
(as opposed to Smalltalk processes).
|
|
1337 |
|
|
1338 |
[author:]
|
|
1339 |
cg
|
|
1340 |
|
|
1341 |
[instance variables:]
|
|
1342 |
host String the host on which to execute the command
|
|
1343 |
|
|
1344 |
[class variables:]
|
|
1345 |
|
|
1346 |
[see also:]
|
|
1347 |
Process
|
|
1348 |
"
|
|
1349 |
!
|
|
1350 |
|
|
1351 |
examples
|
|
1352 |
"
|
|
1353 |
[exBegin]
|
|
1354 |
|outStream|
|
|
1355 |
|
|
1356 |
outStream := '' writeStream.
|
|
1357 |
|
|
1358 |
(OSProcess onHost:'exeptn')
|
|
1359 |
command:'ls -l';
|
|
1360 |
inStream:'abc' readStream;
|
|
1361 |
outStream:outStream;
|
|
1362 |
lineWise:true;
|
|
1363 |
execute.
|
|
1364 |
|
|
1365 |
outStream contents
|
|
1366 |
[exEnd]
|
|
1367 |
"
|
|
1368 |
! !
|
|
1369 |
|
|
1370 |
!OSProcess::RemoteOSProcess methodsFor:'accessing'!
|
|
1371 |
|
|
1372 |
host
|
|
1373 |
^ host
|
|
1374 |
!
|
|
1375 |
|
|
1376 |
host:aHostName
|
|
1377 |
host := aHostName.
|
|
1378 |
! !
|
|
1379 |
|
|
1380 |
!OSProcess::RemoteOSProcess methodsFor:'private'!
|
|
1381 |
|
|
1382 |
startCommand
|
|
1383 |
"the 'real' command"
|
22199
|
1384 |
|
|
1385 |
"for now, always use ssh;
|
|
1386 |
later, this should be configurable,
|
|
1387 |
which remote mechanism is to be used per host"
|
|
1388 |
|
22198
|
1389 |
^ 'ssh %1 "%2"' bindWith:host with:command
|
|
1390 |
! !
|
|
1391 |
|
12890
|
1392 |
!OSProcess class methodsFor:'documentation'!
|
|
1393 |
|
19298
|
1394 |
version
|
|
1395 |
^ '$Header$'
|
|
1396 |
!
|
|
1397 |
|
12890
|
1398 |
version_CVS
|
19286
|
1399 |
^ '$Header$'
|
12890
|
1400 |
! !
|
19286
|
1401 |
|
21172
|
1402 |
|
|
1403 |
OSProcess initialize!
|