OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 // TODO(ager): The only reason for this class is that we | 7 // TODO(ager): The only reason for this class is that we |
8 // cannot patch a top-level at this point. | 8 // cannot patch a top-level at this point. |
9 class _ProcessUtils { | 9 class _ProcessUtils { |
10 external static void _exit(int status); | 10 external static void _exit(int status); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 * Returns the PID of the current process. | 83 * Returns the PID of the current process. |
84 */ | 84 */ |
85 int get pid => _ProcessUtils._pid(null); | 85 int get pid => _ProcessUtils._pid(null); |
86 | 86 |
87 /** | 87 /** |
88 * [Process] is used to start new processes using the static | 88 * [Process] is used to start new processes using the static |
89 * [start] and [run] methods. | 89 * [start] and [run] methods. |
90 */ | 90 */ |
91 abstract class Process { | 91 abstract class Process { |
92 /** | 92 /** |
| 93 * Returns a [:Future:] which completes with the exit code of the process |
| 94 * when the process completes. |
| 95 * |
| 96 * The handling of exit codes is platform specific. |
| 97 * |
| 98 * On Linux and Mac a normal exit code will be a positive value in |
| 99 * the range [0..255]. If the process was terminated due to a signal |
| 100 * the exit code will be a negative value in the range [-255..0[, |
| 101 * where the absolute value of the exit code is the signal |
| 102 * number. For example, if a process crashes due to a segmentation |
| 103 * violation the exit code will be -11, as the signal SIGSEGV has the |
| 104 * number 11. |
| 105 * |
| 106 * On Windows a process can report any 32-bit value as an exit |
| 107 * code. When returning the exit code this exit code is turned into |
| 108 * a signed value. Some special values are used to report |
| 109 * termination due to some system event. E.g. if a process crashes |
| 110 * due to an access violation the 32-bit exit code is `0xc0000005`, |
| 111 * which will be returned as the negative number `-1073741819`. To |
| 112 * get the original 32-bit value use `(0x100000000 + exitCode) & |
| 113 * 0xffffffff`. |
| 114 */ |
| 115 Future<int> exitCode; |
| 116 |
| 117 /** |
93 * Starts a process running the [executable] with the specified | 118 * Starts a process running the [executable] with the specified |
94 * [arguments]. Returns a [:Future<Process>:] that completes with a | 119 * [arguments]. Returns a [:Future<Process>:] that completes with a |
95 * Process instance when the process has been successfully | 120 * Process instance when the process has been successfully |
96 * started. That [Process] object can be used to interact with the | 121 * started. That [Process] object can be used to interact with the |
97 * process. If the process cannot be started the returned [Future] | 122 * process. If the process cannot be started the returned [Future] |
98 * completes with an exception. | 123 * completes with an exception. |
99 * | 124 * |
100 * Use [workingDirectory] to set the working directory for the process. Note | 125 * Use [workingDirectory] to set the working directory for the process. Note |
101 * that the change of directory occurs before executing the process on some | 126 * that the change of directory occurs before executing the process on some |
102 * platforms, which may have impact when using relative paths for the | 127 * platforms, which may have impact when using relative paths for the |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 * Returns the standard input stream of the process as an [IOSink]. | 247 * Returns the standard input stream of the process as an [IOSink]. |
223 */ | 248 */ |
224 IOSink get stdin; | 249 IOSink get stdin; |
225 | 250 |
226 /** | 251 /** |
227 * Returns the process id of the process. | 252 * Returns the process id of the process. |
228 */ | 253 */ |
229 int get pid; | 254 int get pid; |
230 | 255 |
231 /** | 256 /** |
232 * Returns a [:Future:] which completes with the exit code of the process | |
233 * when the process completes. | |
234 * | |
235 * The handling of exit codes is platform specific. | |
236 * | |
237 * On Linux and Mac a normal exit code will be a positive value in | |
238 * the range [0..255]. If the process was terminated due to a signal | |
239 * the exit code will be a negative value in the range [-255..0[, | |
240 * where the absolute value of the exit code is the signal | |
241 * number. For example, if a process crashes due to a segmentation | |
242 * violation the exit code will be -11, as the signal SIGSEGV has the | |
243 * number 11. | |
244 * | |
245 * On Windows a process can report any 32-bit value as an exit | |
246 * code. When returning the exit code this exit code is turned into | |
247 * a signed value. Some special values are used to report | |
248 * termination due to some system event. E.g. if a process crashes | |
249 * due to an access violation the 32-bit exit code is `0xc0000005`, | |
250 * which will be returned as the negative number `-1073741819`. To | |
251 * get the original 32-bit value use `(0x100000000 + exitCode) & | |
252 * 0xffffffff`. | |
253 */ | |
254 Future<int> exitCode; | |
255 | |
256 /** | |
257 * On Windows, [kill] kills the process, ignoring the [signal] | 257 * On Windows, [kill] kills the process, ignoring the [signal] |
258 * flag. On Posix systems, [kill] sends [signal] to the | 258 * flag. On Posix systems, [kill] sends [signal] to the |
259 * process. Depending on the signal giving, it'll have different | 259 * process. Depending on the signal giving, it'll have different |
260 * meanings. When the process terminates as a result of calling | 260 * meanings. When the process terminates as a result of calling |
261 * [kill] [onExit] is called. | 261 * [kill] [onExit] is called. |
262 * | 262 * |
263 * Returns [:true:] if the process is successfully killed (the | 263 * Returns [:true:] if the process is successfully killed (the |
264 * signal is successfully sent). Returns [:false:] if the process | 264 * signal is successfully sent). Returns [:false:] if the process |
265 * could not be killed (the signal could not be sent). Usually, | 265 * could not be killed (the signal could not be sent). Usually, |
266 * a [:false:] return value from kill means that the process is | 266 * a [:false:] return value from kill means that the process is |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 String toString() => _name; | 349 String toString() => _name; |
350 | 350 |
351 Stream<ProcessSignal> watch() => _ProcessUtils._watchSignal(this); | 351 Stream<ProcessSignal> watch() => _ProcessUtils._watchSignal(this); |
352 } | 352 } |
353 | 353 |
354 | 354 |
355 class SignalException implements IOException { | 355 class SignalException implements IOException { |
356 final String message; | 356 final String message; |
357 final osError; | 357 final osError; |
358 | 358 |
359 const SignalException(String this.message, [this.osError = null]); | 359 const SignalException(this.message, [this.osError = null]); |
360 | 360 |
361 String toString() { | 361 String toString() { |
362 var msg = ""; | 362 var msg = ""; |
363 if (osError != null) { | 363 if (osError != null) { |
364 msg = ", osError: $osError"; | 364 msg = ", osError: $osError"; |
365 } | 365 } |
366 return "SignalException: $message$msg"; | 366 return "SignalException: $message$msg"; |
367 } | 367 } |
368 } | 368 } |
369 | 369 |
370 | 370 |
371 class ProcessException implements IOException { | 371 class ProcessException implements IOException { |
372 const ProcessException(String this.executable, | |
373 List<String> this.arguments, | |
374 [String this.message = "", | |
375 int this.errorCode = 0]); | |
376 String toString() { | |
377 var msg = (message == null) ? 'OS error code: $errorCode' : message; | |
378 var args = arguments.join(' '); | |
379 return "ProcessException: $msg\n Command: $executable $args"; | |
380 } | |
381 | |
382 /** | 372 /** |
383 * Contains the executable provided for the process. | 373 * Contains the executable provided for the process. |
384 */ | 374 */ |
385 final String executable; | 375 final String executable; |
386 | 376 |
387 /** | 377 /** |
388 * Contains the arguments provided for the process. | 378 * Contains the arguments provided for the process. |
389 */ | 379 */ |
390 final List<String> arguments; | 380 final List<String> arguments; |
391 | 381 |
392 /** | 382 /** |
393 * Contains the system message for the process exception if any. | 383 * Contains the system message for the process exception if any. |
394 */ | 384 */ |
395 final String message; | 385 final String message; |
396 | 386 |
397 /** | 387 /** |
398 * Contains the OS error code for the process exception if any. | 388 * Contains the OS error code for the process exception if any. |
399 */ | 389 */ |
400 final int errorCode; | 390 final int errorCode; |
| 391 |
| 392 const ProcessException(this.executable, this.arguments, [this.message = "", |
| 393 this.errorCode = 0]); |
| 394 String toString() { |
| 395 var msg = (message == null) ? 'OS error code: $errorCode' : message; |
| 396 var args = arguments.join(' '); |
| 397 return "ProcessException: $msg\n Command: $executable $args"; |
| 398 } |
401 } | 399 } |
OLD | NEW |