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