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