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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 _ProcessUtils._sleep(milliseconds); | 94 _ProcessUtils._sleep(milliseconds); |
95 } | 95 } |
96 | 96 |
97 /** | 97 /** |
98 * Returns the PID of the current process. | 98 * Returns the PID of the current process. |
99 */ | 99 */ |
100 int get pid => _ProcessUtils._pid(null); | 100 int get pid => _ProcessUtils._pid(null); |
101 | 101 |
102 /** | 102 /** |
103 * The means to execute a program. | 103 * The means to execute a program. |
104 * | 104 * |
105 * Use the static [start] and [run] methods to start a new process. | 105 * Use the static [start] and [run] methods to start a new process. |
106 * The run method executes the process non-interactively to completion. | 106 * The run method executes the process non-interactively to completion. |
107 * In contrast, the start method allows your code to interact with the | 107 * In contrast, the start method allows your code to interact with the |
108 * running process. | 108 * running process. |
109 * | 109 * |
110 * ## Start a process with the run method | 110 * ## Start a process with the run method |
111 * | 111 * |
112 * The following code sample uses the run method to create a process | 112 * The following code sample uses the run method to create a process |
113 * that runs the UNIX command `ls`, which lists the contents of a directory. | 113 * that runs the UNIX command `ls`, which lists the contents of a directory. |
114 * The run method completes with a [ProcessResult] object when the process | 114 * The run method completes with a [ProcessResult] object when the process |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 * process.stdout | 146 * process.stdout |
147 * .transform(UTF8.decoder) | 147 * .transform(UTF8.decoder) |
148 * .listen((data) { print(data); }); | 148 * .listen((data) { print(data); }); |
149 * process.stdin.writeln('Hello, world!'); | 149 * process.stdin.writeln('Hello, world!'); |
150 * process.stdin.writeln('Hello, galaxy!'); | 150 * process.stdin.writeln('Hello, galaxy!'); |
151 * process.stdin.writeln('Hello, universe!'); | 151 * process.stdin.writeln('Hello, universe!'); |
152 * }); | 152 * }); |
153 * } | 153 * } |
154 * | 154 * |
155 * ## Standard I/O streams | 155 * ## Standard I/O streams |
156 * | 156 * |
157 * As seen in the previous code sample, you can interact with the Process's | 157 * As seen in the previous code sample, you can interact with the Process's |
158 * standard output stream through the getter [stdout], | 158 * standard output stream through the getter [stdout], |
159 * and you can interact with the Process's standard input stream through | 159 * and you can interact with the Process's standard input stream through |
160 * the getter [stdin]. | 160 * the getter [stdin]. |
161 * In addition, Process provides a getter [stderr] for using the Process's | 161 * In addition, Process provides a getter [stderr] for using the Process's |
162 * standard error stream. | 162 * standard error stream. |
163 * | 163 * |
164 * A Process's streams are distinct from the top-level streams | 164 * A Process's streams are distinct from the top-level streams |
165 * for the current program. | 165 * for the current program. |
166 * | 166 * |
167 * ## Exit codes | 167 * ## Exit codes |
168 * | 168 * |
169 * Call the [exitCode] method to get the exit code of the process. | 169 * Call the [exitCode] method to get the exit code of the process. |
(...skipping 12 matching lines...) Expand all Loading... |
182 * // Get the exit code from the new process. | 182 * // Get the exit code from the new process. |
183 * process.exitCode.then((exitCode) { | 183 * process.exitCode.then((exitCode) { |
184 * print('exit code: $exitCode'); | 184 * print('exit code: $exitCode'); |
185 * }); | 185 * }); |
186 * }); | 186 * }); |
187 * } | 187 * } |
188 * | 188 * |
189 * ## Other resources | 189 * ## Other resources |
190 * | 190 * |
191 * [Dart by Example](https://www.dartlang.org/dart-by-example/#dart-io-and-comma
nd-line-apps) | 191 * [Dart by Example](https://www.dartlang.org/dart-by-example/#dart-io-and-comma
nd-line-apps) |
192 * provides additional task-oriented code samples that show how to use | 192 * provides additional task-oriented code samples that show how to use |
193 * various API from the [dart:io] library. | 193 * various API from the [dart:io] library. |
194 */ | 194 */ |
195 abstract class Process { | 195 abstract class Process { |
196 /** | 196 /** |
197 * Returns a [:Future:] which completes with the exit code of the process | 197 * Returns a [:Future:] which completes with the exit code of the process |
198 * when the process completes. | 198 * when the process completes. |
199 * | 199 * |
200 * The handling of exit codes is platform specific. | 200 * The handling of exit codes is platform specific. |
201 * | 201 * |
202 * On Linux and Mac a normal exit code will be a positive value in | 202 * On Linux and Mac a normal exit code will be a positive value in |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 /** | 404 /** |
405 * Process id from the process. | 405 * Process id from the process. |
406 */ | 406 */ |
407 int get pid; | 407 int get pid; |
408 } | 408 } |
409 | 409 |
410 | 410 |
411 /** | 411 /** |
412 * On Posix systems, [ProcessSignal] is used to send a specific signal | 412 * On Posix systems, [ProcessSignal] is used to send a specific signal |
413 * to a child process, see [:Process.kill:]. | 413 * to a child process, see [:Process.kill:]. |
| 414 * |
| 415 * Some [ProcessSignal]s can also be watched, as a way to intercept the default |
| 416 * signal handler and implement another. See [ProcessSignal.watch] for more |
| 417 * information. |
414 */ | 418 */ |
415 class ProcessSignal { | 419 class ProcessSignal { |
416 static const ProcessSignal SIGHUP = const ProcessSignal._(1, "SIGHUP"); | 420 static const ProcessSignal SIGHUP = const ProcessSignal._(1, "SIGHUP"); |
417 static const ProcessSignal SIGINT = const ProcessSignal._(2, "SIGINT"); | 421 static const ProcessSignal SIGINT = const ProcessSignal._(2, "SIGINT"); |
418 static const ProcessSignal SIGQUIT = const ProcessSignal._(3, "SIGQUIT"); | 422 static const ProcessSignal SIGQUIT = const ProcessSignal._(3, "SIGQUIT"); |
419 static const ProcessSignal SIGILL = const ProcessSignal._(4, "SIGILL"); | 423 static const ProcessSignal SIGILL = const ProcessSignal._(4, "SIGILL"); |
420 static const ProcessSignal SIGTRAP = const ProcessSignal._(5, "SIGTRAP"); | 424 static const ProcessSignal SIGTRAP = const ProcessSignal._(5, "SIGTRAP"); |
421 static const ProcessSignal SIGABRT = const ProcessSignal._(6, "SIGABRT"); | 425 static const ProcessSignal SIGABRT = const ProcessSignal._(6, "SIGABRT"); |
422 static const ProcessSignal SIGBUS = const ProcessSignal._(7, "SIGBUS"); | 426 static const ProcessSignal SIGBUS = const ProcessSignal._(7, "SIGBUS"); |
423 static const ProcessSignal SIGFPE = const ProcessSignal._(8, "SIGFPE"); | 427 static const ProcessSignal SIGFPE = const ProcessSignal._(8, "SIGFPE"); |
(...skipping 25 matching lines...) Expand all Loading... |
449 const ProcessSignal._(this._signalNumber, this._name); | 453 const ProcessSignal._(this._signalNumber, this._name); |
450 | 454 |
451 String toString() => _name; | 455 String toString() => _name; |
452 | 456 |
453 /** | 457 /** |
454 * Watch for process signals. | 458 * Watch for process signals. |
455 * | 459 * |
456 * The following [ProcessSignal]s can be listened to: | 460 * The following [ProcessSignal]s can be listened to: |
457 * | 461 * |
458 * * [ProcessSignal.SIGHUP]. | 462 * * [ProcessSignal.SIGHUP]. |
459 * * [ProcessSignal.SIGINT]. | 463 * * [ProcessSignal.SIGINT]. Signal sent by e.g. CTRL-C. |
460 * * [ProcessSignal.SIGTERM]. Not available on Windows. | 464 * * [ProcessSignal.SIGTERM]. Not available on Windows. |
461 * * [ProcessSignal.SIGUSR1]. Not available on Windows. | 465 * * [ProcessSignal.SIGUSR1]. Not available on Windows. |
462 * * [ProcessSignal.SIGUSR2]. Not available on Windows. | 466 * * [ProcessSignal.SIGUSR2]. Not available on Windows. |
463 * * [ProcessSignal.SIGWINCH]. Not available on Windows. | 467 * * [ProcessSignal.SIGWINCH]. Not available on Windows. |
464 * | 468 * |
465 * Other signals are disallowed, as they may be used by the VM. | 469 * Other signals are disallowed, as they may be used by the VM. |
| 470 * |
| 471 * A signal can be watched multiple times, from multiple isolates, where all |
| 472 * callbacks are invoked when signaled, in no specific order. |
466 */ | 473 */ |
467 Stream<ProcessSignal> watch() => _ProcessUtils._watchSignal(this); | 474 Stream<ProcessSignal> watch() => _ProcessUtils._watchSignal(this); |
468 } | 475 } |
469 | 476 |
470 | 477 |
471 class SignalException implements IOException { | 478 class SignalException implements IOException { |
472 final String message; | 479 final String message; |
473 final osError; | 480 final osError; |
474 | 481 |
475 const SignalException(this.message, [this.osError = null]); | 482 const SignalException(this.message, [this.osError = null]); |
(...skipping 30 matching lines...) Expand all Loading... |
506 final int errorCode; | 513 final int errorCode; |
507 | 514 |
508 const ProcessException(this.executable, this.arguments, [this.message = "", | 515 const ProcessException(this.executable, this.arguments, [this.message = "", |
509 this.errorCode = 0]); | 516 this.errorCode = 0]); |
510 String toString() { | 517 String toString() { |
511 var msg = (message == null) ? 'OS error code: $errorCode' : message; | 518 var msg = (message == null) ? 'OS error code: $errorCode' : message; |
512 var args = arguments.join(' '); | 519 var args = arguments.join(' '); |
513 return "ProcessException: $msg\n Command: $executable $args"; | 520 return "ProcessException: $msg\n Command: $executable $args"; |
514 } | 521 } |
515 } | 522 } |
OLD | NEW |