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 @patch | 5 @patch class _WindowsCodePageDecoder { |
6 class _WindowsCodePageDecoder { | 6 @patch static String _decodeBytes(List<int> bytes) |
7 @patch | 7 native "SystemEncodingToString"; |
8 static String _decodeBytes(List<int> bytes) native "SystemEncodingToString"; | |
9 } | 8 } |
10 | 9 |
11 @patch | 10 |
12 class _WindowsCodePageEncoder { | 11 @patch class _WindowsCodePageEncoder { |
13 @patch | 12 @patch static List<int> _encodeString(String string) |
14 static List<int> _encodeString(String string) native "StringToSystemEncoding"; | 13 native "StringToSystemEncoding"; |
15 } | 14 } |
16 | 15 |
17 @patch | 16 |
18 class Process { | 17 @patch class Process { |
19 @patch | 18 @patch static Future<Process> start( |
20 static Future<Process> start(String executable, List<String> arguments, | 19 String executable, |
| 20 List<String> arguments, |
21 {String workingDirectory, | 21 {String workingDirectory, |
22 Map<String, String> environment, | 22 Map<String, String> environment, |
23 bool includeParentEnvironment: true, | 23 bool includeParentEnvironment: true, |
24 bool runInShell: false, | 24 bool runInShell: false, |
25 ProcessStartMode mode: ProcessStartMode.NORMAL}) { | 25 ProcessStartMode mode: ProcessStartMode.NORMAL}) { |
26 _ProcessImpl process = new _ProcessImpl( | 26 _ProcessImpl process = new _ProcessImpl(executable, |
27 executable, | 27 arguments, |
28 arguments, | 28 workingDirectory, |
29 workingDirectory, | 29 environment, |
30 environment, | 30 includeParentEnvironment, |
31 includeParentEnvironment, | 31 runInShell, |
32 runInShell, | 32 mode); |
33 mode); | |
34 return process._start(); | 33 return process._start(); |
35 } | 34 } |
36 | 35 |
37 @patch | 36 @patch static Future<ProcessResult> run( |
38 static Future<ProcessResult> run(String executable, List<String> arguments, | 37 String executable, |
| 38 List<String> arguments, |
39 {String workingDirectory, | 39 {String workingDirectory, |
40 Map<String, String> environment, | 40 Map<String, String> environment, |
41 bool includeParentEnvironment: true, | 41 bool includeParentEnvironment: true, |
42 bool runInShell: false, | 42 bool runInShell: false, |
43 Encoding stdoutEncoding: SYSTEM_ENCODING, | 43 Encoding stdoutEncoding: SYSTEM_ENCODING, |
44 Encoding stderrEncoding: SYSTEM_ENCODING}) { | 44 Encoding stderrEncoding: SYSTEM_ENCODING}) { |
45 return _runNonInteractiveProcess( | 45 return _runNonInteractiveProcess(executable, |
46 executable, | 46 arguments, |
47 arguments, | 47 workingDirectory, |
48 workingDirectory, | 48 environment, |
49 environment, | 49 includeParentEnvironment, |
50 includeParentEnvironment, | 50 runInShell, |
51 runInShell, | 51 stdoutEncoding, |
52 stdoutEncoding, | 52 stderrEncoding); |
53 stderrEncoding); | |
54 } | 53 } |
55 | 54 |
56 @patch | 55 @patch static ProcessResult runSync( |
57 static ProcessResult runSync(String executable, List<String> arguments, | 56 String executable, |
| 57 List<String> arguments, |
58 {String workingDirectory, | 58 {String workingDirectory, |
59 Map<String, String> environment, | 59 Map<String, String> environment, |
60 bool includeParentEnvironment: true, | 60 bool includeParentEnvironment: true, |
61 bool runInShell: false, | 61 bool runInShell: false, |
62 Encoding stdoutEncoding: SYSTEM_ENCODING, | 62 Encoding stdoutEncoding: SYSTEM_ENCODING, |
63 Encoding stderrEncoding: SYSTEM_ENCODING}) { | 63 Encoding stderrEncoding: SYSTEM_ENCODING}) { |
64 return _runNonInteractiveProcessSync( | 64 return _runNonInteractiveProcessSync(executable, |
65 executable, | 65 arguments, |
66 arguments, | 66 workingDirectory, |
67 workingDirectory, | 67 environment, |
68 environment, | 68 includeParentEnvironment, |
69 includeParentEnvironment, | 69 runInShell, |
70 runInShell, | 70 stdoutEncoding, |
71 stdoutEncoding, | 71 stderrEncoding); |
72 stderrEncoding); | |
73 } | 72 } |
74 | 73 |
75 @patch | 74 @patch static bool killPid( |
76 static bool killPid(int pid, [ProcessSignal signal = ProcessSignal.SIGTERM]) { | 75 int pid, [ProcessSignal signal = ProcessSignal.SIGTERM]) { |
77 if (signal is! ProcessSignal) { | 76 if (signal is! ProcessSignal) { |
78 throw new ArgumentError("Argument 'signal' must be a ProcessSignal"); | 77 throw new ArgumentError( |
| 78 "Argument 'signal' must be a ProcessSignal"); |
79 } | 79 } |
80 return _ProcessUtils._killPid(pid, signal._signalNumber); | 80 return _ProcessUtils._killPid(pid, signal._signalNumber); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
| 84 |
84 List<_SignalController> _signalControllers = new List(32); | 85 List<_SignalController> _signalControllers = new List(32); |
85 | 86 |
| 87 |
86 class _SignalController { | 88 class _SignalController { |
87 final ProcessSignal signal; | 89 final ProcessSignal signal; |
88 | 90 |
89 StreamController _controller; | 91 StreamController _controller; |
90 var _id; | 92 var _id; |
91 | 93 |
92 _SignalController(this.signal) { | 94 _SignalController(this.signal) { |
93 _controller = | 95 _controller = new StreamController.broadcast( |
94 new StreamController.broadcast(onListen: _listen, onCancel: _cancel); | 96 onListen: _listen, |
| 97 onCancel: _cancel); |
95 } | 98 } |
96 | 99 |
97 Stream<ProcessSignal> get stream => _controller.stream; | 100 Stream<ProcessSignal> get stream => _controller.stream; |
98 | 101 |
99 void _listen() { | 102 void _listen() { |
100 var id = _setSignalHandler(signal._signalNumber); | 103 var id = _setSignalHandler(signal._signalNumber); |
101 if (id is! int) { | 104 if (id is! int) { |
102 _controller | 105 _controller.addError( |
103 .addError(new SignalException("Failed to listen for $signal", id)); | 106 new SignalException("Failed to listen for $signal", id)); |
104 return; | 107 return; |
105 } | 108 } |
106 _id = id; | 109 _id = id; |
107 var socket = new _RawSocket(new _NativeSocket.watch(id)); | 110 var socket = new _RawSocket(new _NativeSocket.watch(id)); |
108 socket.listen((event) { | 111 socket.listen((event) { |
109 if (event == RawSocketEvent.READ) { | 112 if (event == RawSocketEvent.READ) { |
110 var bytes = socket.read(); | 113 var bytes = socket.read(); |
111 for (int i = 0; i < bytes.length; i++) { | 114 for (int i = 0; i < bytes.length; i++) { |
112 _controller.add(signal); | 115 _controller.add(signal); |
113 } | 116 } |
114 } | 117 } |
115 }); | 118 }); |
116 } | 119 } |
117 | 120 |
118 void _cancel() { | 121 void _cancel() { |
119 if (_id != null) { | 122 if (_id != null) { |
120 _clearSignalHandler(signal._signalNumber); | 123 _clearSignalHandler(signal._signalNumber); |
121 _id = null; | 124 _id = null; |
122 } | 125 } |
123 } | 126 } |
124 | 127 |
125 static _setSignalHandler(int signal) native "Process_SetSignalHandler"; | 128 static _setSignalHandler(int signal) |
| 129 native "Process_SetSignalHandler"; |
126 static int _clearSignalHandler(int signal) | 130 static int _clearSignalHandler(int signal) |
127 native "Process_ClearSignalHandler"; | 131 native "Process_ClearSignalHandler"; |
128 } | 132 } |
129 | 133 |
130 Function _getWatchSignalInternal() => _ProcessUtils._watchSignalInternal; | 134 Function _getWatchSignalInternal() => _ProcessUtils._watchSignalInternal; |
131 | 135 |
132 @patch | 136 |
133 class _ProcessUtils { | 137 @patch class _ProcessUtils { |
134 @patch | 138 @patch static void _exit(int status) native "Process_Exit"; |
135 static void _exit(int status) native "Process_Exit"; | 139 @patch static void _setExitCode(int status) |
136 @patch | 140 native "Process_SetExitCode"; |
137 static void _setExitCode(int status) native "Process_SetExitCode"; | 141 @patch static int _getExitCode() native "Process_GetExitCode"; |
138 @patch | 142 @patch static void _sleep(int millis) native "Process_Sleep"; |
139 static int _getExitCode() native "Process_GetExitCode"; | 143 @patch static int _pid(Process process) native "Process_Pid"; |
140 @patch | 144 static bool _killPid(int pid, int signal) |
141 static void _sleep(int millis) native "Process_Sleep"; | 145 native "Process_KillPid"; |
142 @patch | 146 @patch static Stream<ProcessSignal> _watchSignal(ProcessSignal signal) { |
143 static int _pid(Process process) native "Process_Pid"; | |
144 static bool _killPid(int pid, int signal) native "Process_KillPid"; | |
145 @patch | |
146 static Stream<ProcessSignal> _watchSignal(ProcessSignal signal) { | |
147 if (signal != ProcessSignal.SIGHUP && | 147 if (signal != ProcessSignal.SIGHUP && |
148 signal != ProcessSignal.SIGINT && | 148 signal != ProcessSignal.SIGINT && |
149 signal != ProcessSignal.SIGTERM && | 149 signal != ProcessSignal.SIGTERM && |
150 (Platform.isWindows || | 150 (Platform.isWindows || |
151 (signal != ProcessSignal.SIGUSR1 && | 151 (signal != ProcessSignal.SIGUSR1 && |
152 signal != ProcessSignal.SIGUSR2 && | 152 signal != ProcessSignal.SIGUSR2 && |
153 signal != ProcessSignal.SIGWINCH))) { | 153 signal != ProcessSignal.SIGWINCH))) { |
154 throw new SignalException( | 154 throw new SignalException( |
155 "Listening for signal $signal is not supported"); | 155 "Listening for signal $signal is not supported"); |
156 } | 156 } |
157 return _watchSignalInternal(signal); | 157 return _watchSignalInternal(signal); |
158 } | 158 } |
159 | 159 |
160 static Stream<ProcessSignal> _watchSignalInternal(ProcessSignal signal) { | 160 static Stream<ProcessSignal> _watchSignalInternal(ProcessSignal signal) { |
161 if (_signalControllers[signal._signalNumber] == null) { | 161 if (_signalControllers[signal._signalNumber] == null) { |
162 _signalControllers[signal._signalNumber] = new _SignalController(signal); | 162 _signalControllers[signal._signalNumber] = new _SignalController(signal); |
163 } | 163 } |
164 return _signalControllers[signal._signalNumber].stream; | 164 return _signalControllers[signal._signalNumber].stream; |
165 } | 165 } |
166 } | 166 } |
167 | 167 |
| 168 |
168 class _ProcessStartStatus { | 169 class _ProcessStartStatus { |
169 int _errorCode; // Set to OS error code if process start failed. | 170 int _errorCode; // Set to OS error code if process start failed. |
170 String _errorMessage; // Set to OS error message if process start failed. | 171 String _errorMessage; // Set to OS error message if process start failed. |
171 } | 172 } |
172 | 173 |
| 174 |
173 // The NativeFieldWrapperClass1 can not be used with a mixin, due to missing | 175 // The NativeFieldWrapperClass1 can not be used with a mixin, due to missing |
174 // implicit constructor. | 176 // implicit constructor. |
175 class _ProcessImplNativeWrapper extends NativeFieldWrapperClass1 {} | 177 class _ProcessImplNativeWrapper extends NativeFieldWrapperClass1 {} |
176 | 178 |
177 class _ProcessImpl extends _ProcessImplNativeWrapper implements Process { | 179 class _ProcessImpl extends _ProcessImplNativeWrapper implements Process { |
178 _ProcessResourceInfo _resourceInfo; | 180 _ProcessResourceInfo _resourceInfo; |
179 static bool connectedResourceHandler = false; | 181 static bool connectedResourceHandler = false; |
180 | 182 |
181 _ProcessImpl( | 183 _ProcessImpl(String path, |
182 String path, | 184 List<String> arguments, |
183 List<String> arguments, | 185 this._workingDirectory, |
184 this._workingDirectory, | 186 Map<String, String> environment, |
185 Map<String, String> environment, | 187 bool includeParentEnvironment, |
186 bool includeParentEnvironment, | 188 bool runInShell, |
187 bool runInShell, | 189 ProcessStartMode mode) : super() { |
188 ProcessStartMode mode) | |
189 : super() { | |
190 if (!connectedResourceHandler) { | 190 if (!connectedResourceHandler) { |
191 registerExtension( | 191 registerExtension('ext.dart.io.getProcesses', |
192 'ext.dart.io.getProcesses', _ProcessResourceInfo.getStartedProcesses); | 192 _ProcessResourceInfo.getStartedProcesses); |
193 registerExtension('ext.dart.io.getProcessById', | 193 registerExtension('ext.dart.io.getProcessById', |
194 _ProcessResourceInfo.getProcessInfoMapById); | 194 _ProcessResourceInfo.getProcessInfoMapById); |
195 connectedResourceHandler = true; | 195 connectedResourceHandler = true; |
196 } | 196 } |
197 | 197 |
198 if (runInShell) { | 198 if (runInShell) { |
199 arguments = _getShellArguments(path, arguments); | 199 arguments = _getShellArguments(path, arguments); |
200 path = _getShellCommand(); | 200 path = _getShellCommand(); |
201 } | 201 } |
202 | 202 |
203 if (path is! String) { | 203 if (path is !String) { |
204 throw new ArgumentError("Path is not a String: $path"); | 204 throw new ArgumentError("Path is not a String: $path"); |
205 } | 205 } |
206 _path = path; | 206 _path = path; |
207 | 207 |
208 if (arguments is! List) { | 208 if (arguments is !List) { |
209 throw new ArgumentError("Arguments is not a List: $arguments"); | 209 throw new ArgumentError("Arguments is not a List: $arguments"); |
210 } | 210 } |
211 int len = arguments.length; | 211 int len = arguments.length; |
212 _arguments = new List<String>(len); | 212 _arguments = new List<String>(len); |
213 for (int i = 0; i < len; i++) { | 213 for (int i = 0; i < len; i++) { |
214 var arg = arguments[i]; | 214 var arg = arguments[i]; |
215 if (arg is! String) { | 215 if (arg is !String) { |
216 throw new ArgumentError("Non-string argument: $arg"); | 216 throw new ArgumentError("Non-string argument: $arg"); |
217 } | 217 } |
218 _arguments[i] = arguments[i]; | 218 _arguments[i] = arguments[i]; |
219 if (Platform.isWindows) { | 219 if (Platform.isWindows) { |
220 _arguments[i] = _windowsArgumentEscape(_arguments[i]); | 220 _arguments[i] = _windowsArgumentEscape(_arguments[i]); |
221 } | 221 } |
222 } | 222 } |
223 | 223 |
224 if (_workingDirectory != null && _workingDirectory is! String) { | 224 if (_workingDirectory != null && _workingDirectory is !String) { |
225 throw new ArgumentError( | 225 throw new ArgumentError( |
226 "WorkingDirectory is not a String: $_workingDirectory"); | 226 "WorkingDirectory is not a String: $_workingDirectory"); |
227 } | 227 } |
228 | 228 |
229 _environment = []; | 229 _environment = []; |
230 // Ensure that we have a non-null environment. | 230 // Ensure that we have a non-null environment. |
231 environment = (environment == null) ? (const {}) : environment; | 231 environment = (environment == null) ? (const {}) : environment; |
232 if (environment is! Map) { | 232 if (environment is !Map) { |
233 throw new ArgumentError("Environment is not a map: $environment"); | 233 throw new ArgumentError("Environment is not a map: $environment"); |
234 } | 234 } |
235 environment.forEach((key, value) { | 235 environment.forEach((key, value) { |
236 if (key is! String || value is! String) { | 236 if (key is !String || value is !String) { |
237 throw new ArgumentError( | 237 throw new ArgumentError( |
238 "Environment key or value is not a string: ($key, $value)"); | 238 "Environment key or value is not a string: ($key, $value)"); |
239 } | 239 } |
240 _environment.add('$key=$value'); | 240 _environment.add('$key=$value'); |
241 }); | 241 }); |
242 if (includeParentEnvironment) { | 242 if (includeParentEnvironment) { |
243 Platform.environment.forEach((key, value) { | 243 Platform.environment.forEach((key, value) { |
244 assert(key is String); | 244 assert(key is String); |
245 assert(value is String); | 245 assert(value is String); |
246 // Do not override keys already set as part of environment. | 246 // Do not override keys already set as part of environment. |
247 if (!environment.containsKey(key)) { | 247 if (!environment.containsKey(key)) { |
248 _environment.add('$key=$value'); | 248 _environment.add('$key=$value'); |
249 } | 249 } |
250 }); | 250 }); |
251 } | 251 } |
252 | 252 |
253 if (mode is! ProcessStartMode) { | 253 if (mode is !ProcessStartMode) { |
254 throw new ArgumentError("Mode is not a ProcessStartMode: $mode"); | 254 throw new ArgumentError("Mode is not a ProcessStartMode: $mode"); |
255 } | 255 } |
256 _mode = mode; | 256 _mode = mode; |
257 | 257 |
258 if (mode != ProcessStartMode.DETACHED) { | 258 if (mode != ProcessStartMode.DETACHED) { |
259 // stdin going to process. | 259 // stdin going to process. |
260 _stdin = new _StdSocketSink(new _Socket._writePipe()); | 260 _stdin = new _StdSocketSink(new _Socket._writePipe()); |
261 _stdin._sink._owner = this; | 261 _stdin._sink._owner = this; |
262 // stdout coming from process. | 262 // stdout coming from process. |
263 _stdout = new _StdStream(new _Socket._readPipe()); | 263 _stdout = new _StdStream(new _Socket._readPipe()); |
264 _stdout._stream._owner = this; | 264 _stdout._stream._owner = this; |
265 // stderr coming from process. | 265 // stderr coming from process. |
266 _stderr = new _StdStream(new _Socket._readPipe()); | 266 _stderr = new _StdStream(new _Socket._readPipe()); |
267 _stderr._stream._owner = this; | 267 _stderr._stream._owner = this; |
268 } | 268 } |
269 if (mode == ProcessStartMode.NORMAL) { | 269 if (mode == ProcessStartMode.NORMAL) { |
270 _exitHandler = new _Socket._readPipe(); | 270 _exitHandler = new _Socket._readPipe(); |
271 } | 271 } |
272 _ended = false; | 272 _ended = false; |
273 _started = false; | 273 _started = false; |
274 } | 274 } |
275 | 275 |
276 static String _getShellCommand() { | 276 static String _getShellCommand() { |
277 if (Platform.isWindows) { | 277 if (Platform.isWindows) { |
278 return 'cmd.exe'; | 278 return 'cmd.exe'; |
279 } | 279 } |
280 return '/bin/sh'; | 280 return '/bin/sh'; |
281 } | 281 } |
282 | 282 |
283 static List<String> _getShellArguments( | 283 static List<String> _getShellArguments(String executable, |
284 String executable, List<String> arguments) { | 284 List<String> arguments) { |
285 List<String> shellArguments = []; | 285 List<String> shellArguments = []; |
286 if (Platform.isWindows) { | 286 if (Platform.isWindows) { |
287 shellArguments.add('/c'); | 287 shellArguments.add('/c'); |
288 shellArguments.add(executable); | 288 shellArguments.add(executable); |
289 for (var arg in arguments) { | 289 for (var arg in arguments) { |
290 shellArguments.add(arg); | 290 shellArguments.add(arg); |
291 } | 291 } |
292 } else { | 292 } else { |
293 var commandLine = new StringBuffer(); | 293 var commandLine = new StringBuffer(); |
294 executable = executable.replaceAll("'", "'\"'\"'"); | 294 executable = executable.replaceAll("'", "'\"'\"'"); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 } | 346 } |
347 sb.write('"'); | 347 sb.write('"'); |
348 result = sb.toString(); | 348 result = sb.toString(); |
349 } | 349 } |
350 | 350 |
351 return result; | 351 return result; |
352 } | 352 } |
353 | 353 |
354 int _intFromBytes(List<int> bytes, int offset) { | 354 int _intFromBytes(List<int> bytes, int offset) { |
355 return (bytes[offset] + | 355 return (bytes[offset] + |
356 (bytes[offset + 1] << 8) + | 356 (bytes[offset + 1] << 8) + |
357 (bytes[offset + 2] << 16) + | 357 (bytes[offset + 2] << 16) + |
358 (bytes[offset + 3] << 24)); | 358 (bytes[offset + 3] << 24)); |
359 } | 359 } |
360 | 360 |
361 Future<Process> _start() { | 361 Future<Process> _start() { |
362 var completer = new Completer(); | 362 var completer = new Completer(); |
363 if (_mode == ProcessStartMode.NORMAL) { | 363 if (_mode == ProcessStartMode.NORMAL) { |
364 _exitCode = new Completer<int>(); | 364 _exitCode = new Completer<int>(); |
365 } | 365 } |
366 // TODO(ager): Make the actual process starting really async instead of | 366 // TODO(ager): Make the actual process starting really async instead of |
367 // simulating it with a timer. | 367 // simulating it with a timer. |
368 Timer.run(() { | 368 Timer.run(() { |
369 var status = new _ProcessStartStatus(); | 369 var status = new _ProcessStartStatus(); |
370 bool success = _startNative( | 370 bool success = |
371 _path, | 371 _startNative(_path, |
372 _arguments, | 372 _arguments, |
373 _workingDirectory, | 373 _workingDirectory, |
374 _environment, | 374 _environment, |
375 _mode.index, | 375 _mode.index, |
376 _mode == ProcessStartMode.DETACHED | 376 _mode == ProcessStartMode.DETACHED |
377 ? null | 377 ? null : _stdin._sink._nativeSocket, |
378 : _stdin._sink._nativeSocket, | 378 _mode == ProcessStartMode.DETACHED |
379 _mode == ProcessStartMode.DETACHED | 379 ? null : _stdout._stream._nativeSocket, |
380 ? null | 380 _mode == ProcessStartMode.DETACHED |
381 : _stdout._stream._nativeSocket, | 381 ? null : _stderr._stream._nativeSocket, |
382 _mode == ProcessStartMode.DETACHED | 382 _mode != ProcessStartMode.NORMAL |
383 ? null | 383 ? null : _exitHandler._nativeSocket, |
384 : _stderr._stream._nativeSocket, | 384 status); |
385 _mode != ProcessStartMode.NORMAL ? null : _exitHandler._nativeSocket, | |
386 status); | |
387 if (!success) { | 385 if (!success) { |
388 completer.completeError(new ProcessException( | 386 completer.completeError( |
389 _path, _arguments, status._errorMessage, status._errorCode)); | 387 new ProcessException(_path, |
| 388 _arguments, |
| 389 status._errorMessage, |
| 390 status._errorCode)); |
390 return; | 391 return; |
391 } | 392 } |
392 | 393 |
393 _started = true; | 394 _started = true; |
394 _resourceInfo = new _ProcessResourceInfo(this); | 395 _resourceInfo = new _ProcessResourceInfo(this); |
395 | 396 |
396 // Setup an exit handler to handle internal cleanup and possible | 397 // Setup an exit handler to handle internal cleanup and possible |
397 // callback when a process terminates. | 398 // callback when a process terminates. |
398 if (_mode == ProcessStartMode.NORMAL) { | 399 if (_mode == ProcessStartMode.NORMAL) { |
399 int exitDataRead = 0; | 400 int exitDataRead = 0; |
400 final int EXIT_DATA_SIZE = 8; | 401 final int EXIT_DATA_SIZE = 8; |
401 List<int> exitDataBuffer = new List<int>(EXIT_DATA_SIZE); | 402 List<int> exitDataBuffer = new List<int>(EXIT_DATA_SIZE); |
402 _exitHandler.listen((data) { | 403 _exitHandler.listen((data) { |
| 404 |
403 int exitCode(List<int> ints) { | 405 int exitCode(List<int> ints) { |
404 var code = _intFromBytes(ints, 0); | 406 var code = _intFromBytes(ints, 0); |
405 var negative = _intFromBytes(ints, 4); | 407 var negative = _intFromBytes(ints, 4); |
406 assert(negative == 0 || negative == 1); | 408 assert(negative == 0 || negative == 1); |
407 return (negative == 0) ? code : -code; | 409 return (negative == 0) ? code : -code; |
408 } | 410 } |
409 | 411 |
410 void handleExit() { | 412 void handleExit() { |
411 _ended = true; | 413 _ended = true; |
412 _exitCode.complete(exitCode(exitDataBuffer)); | 414 _exitCode.complete(exitCode(exitDataBuffer)); |
413 // Kill stdin, helping hand if the user forgot to do it. | 415 // Kill stdin, helping hand if the user forgot to do it. |
414 _stdin._sink.destroy(); | 416 _stdin._sink.destroy(); |
415 _resourceInfo.stopped(); | 417 _resourceInfo.stopped(); |
416 } | 418 } |
417 | 419 |
418 exitDataBuffer.setRange( | 420 exitDataBuffer.setRange( |
419 exitDataRead, exitDataRead + data.length, data); | 421 exitDataRead, exitDataRead + data.length, data); |
420 exitDataRead += data.length; | 422 exitDataRead += data.length; |
421 if (exitDataRead == EXIT_DATA_SIZE) { | 423 if (exitDataRead == EXIT_DATA_SIZE) { |
422 handleExit(); | 424 handleExit(); |
423 } | 425 } |
424 }); | 426 }); |
425 } | 427 } |
426 | 428 |
427 completer.complete(this); | 429 completer.complete(this); |
428 }); | 430 }); |
429 return completer.future; | 431 return completer.future; |
430 } | 432 } |
431 | 433 |
432 ProcessResult _runAndWait(Encoding stdoutEncoding, Encoding stderrEncoding) { | 434 ProcessResult _runAndWait(Encoding stdoutEncoding, |
| 435 Encoding stderrEncoding) { |
433 var status = new _ProcessStartStatus(); | 436 var status = new _ProcessStartStatus(); |
434 _exitCode = new Completer<int>(); | 437 _exitCode = new Completer<int>(); |
435 bool success = _startNative( | 438 bool success = _startNative(_path, |
436 _path, | 439 _arguments, |
437 _arguments, | 440 _workingDirectory, |
438 _workingDirectory, | 441 _environment, |
439 _environment, | 442 ProcessStartMode.NORMAL.index, |
440 ProcessStartMode.NORMAL.index, | 443 _stdin._sink._nativeSocket, |
441 _stdin._sink._nativeSocket, | 444 _stdout._stream._nativeSocket, |
442 _stdout._stream._nativeSocket, | 445 _stderr._stream._nativeSocket, |
443 _stderr._stream._nativeSocket, | 446 _exitHandler._nativeSocket, |
444 _exitHandler._nativeSocket, | 447 status); |
445 status); | |
446 if (!success) { | 448 if (!success) { |
447 throw new ProcessException( | 449 throw new ProcessException(_path, |
448 _path, _arguments, status._errorMessage, status._errorCode); | 450 _arguments, |
| 451 status._errorMessage, |
| 452 status._errorCode); |
449 } | 453 } |
450 | 454 |
451 _resourceInfo = new _ProcessResourceInfo(this); | 455 _resourceInfo = new _ProcessResourceInfo(this); |
452 | 456 |
453 var result = _wait( | 457 var result = _wait( |
454 _stdin._sink._nativeSocket, | 458 _stdin._sink._nativeSocket, |
455 _stdout._stream._nativeSocket, | 459 _stdout._stream._nativeSocket, |
456 _stderr._stream._nativeSocket, | 460 _stderr._stream._nativeSocket, |
457 _exitHandler._nativeSocket); | 461 _exitHandler._nativeSocket); |
458 | 462 |
459 getOutput(output, encoding) { | 463 getOutput(output, encoding) { |
460 if (encoding == null) return output; | 464 if (encoding == null) return output; |
461 return encoding.decode(output); | 465 return encoding.decode(output); |
462 } | 466 } |
463 | 467 |
464 _resourceInfo.stopped(); | 468 _resourceInfo.stopped(); |
465 | 469 |
466 return new ProcessResult( | 470 return new ProcessResult( |
467 result[0], | 471 result[0], |
468 result[1], | 472 result[1], |
469 getOutput(result[2], stdoutEncoding), | 473 getOutput(result[2], stdoutEncoding), |
470 getOutput(result[3], stderrEncoding)); | 474 getOutput(result[3], stderrEncoding)); |
471 } | 475 } |
472 | 476 |
473 bool _startNative( | 477 bool _startNative(String path, |
474 String path, | 478 List<String> arguments, |
475 List<String> arguments, | 479 String workingDirectory, |
476 String workingDirectory, | 480 List<String> environment, |
477 List<String> environment, | 481 int mode, |
478 int mode, | 482 _NativeSocket stdin, |
479 _NativeSocket stdin, | 483 _NativeSocket stdout, |
480 _NativeSocket stdout, | 484 _NativeSocket stderr, |
481 _NativeSocket stderr, | 485 _NativeSocket exitHandler, |
482 _NativeSocket exitHandler, | 486 _ProcessStartStatus status) native "Process_Start"; |
483 _ProcessStartStatus status) native "Process_Start"; | |
484 | 487 |
485 _wait(_NativeSocket stdin, _NativeSocket stdout, _NativeSocket stderr, | 488 _wait(_NativeSocket stdin, |
486 _NativeSocket exitHandler) native "Process_Wait"; | 489 _NativeSocket stdout, |
| 490 _NativeSocket stderr, |
| 491 _NativeSocket exitHandler) native "Process_Wait"; |
487 | 492 |
488 Stream<List<int>> get stdout { | 493 Stream<List<int>> get stdout { |
489 return _stdout; | 494 return _stdout; |
490 } | 495 } |
491 | 496 |
492 Stream<List<int>> get stderr { | 497 Stream<List<int>> get stderr { |
493 return _stderr; | 498 return _stderr; |
494 } | 499 } |
495 | 500 |
496 IOSink get stdin { | 501 IOSink get stdin { |
497 return _stdin; | 502 return _stdin; |
498 } | 503 } |
499 | 504 |
500 Future<int> get exitCode => _exitCode != null ? _exitCode.future : null; | 505 Future<int> get exitCode => _exitCode != null ? _exitCode.future : null; |
501 | 506 |
502 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]) { | 507 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]) { |
503 if (signal is! ProcessSignal) { | 508 if (signal is! ProcessSignal) { |
504 throw new ArgumentError("Argument 'signal' must be a ProcessSignal"); | 509 throw new ArgumentError( |
| 510 "Argument 'signal' must be a ProcessSignal"); |
505 } | 511 } |
506 assert(_started); | 512 assert(_started); |
507 if (_ended) return false; | 513 if (_ended) return false; |
508 return _ProcessUtils._killPid(pid, signal._signalNumber); | 514 return _ProcessUtils._killPid(pid, signal._signalNumber); |
509 } | 515 } |
510 | 516 |
511 int get pid => _ProcessUtils._pid(this); | 517 int get pid => _ProcessUtils._pid(this); |
512 | 518 |
513 String _path; | 519 String _path; |
514 List<String> _arguments; | 520 List<String> _arguments; |
515 String _workingDirectory; | 521 String _workingDirectory; |
516 List<String> _environment; | 522 List<String> _environment; |
517 ProcessStartMode _mode; | 523 ProcessStartMode _mode; |
518 // Private methods of Socket are used by _in, _out, and _err. | 524 // Private methods of Socket are used by _in, _out, and _err. |
519 _StdSocketSink _stdin; | 525 _StdSocketSink _stdin; |
520 _StdStream _stdout; | 526 _StdStream _stdout; |
521 _StdStream _stderr; | 527 _StdStream _stderr; |
522 Socket _exitHandler; | 528 Socket _exitHandler; |
523 bool _ended; | 529 bool _ended; |
524 bool _started; | 530 bool _started; |
525 Completer<int> _exitCode; | 531 Completer<int> _exitCode; |
526 } | 532 } |
527 | 533 |
| 534 |
528 // _NonInteractiveProcess is a wrapper around an interactive process | 535 // _NonInteractiveProcess is a wrapper around an interactive process |
529 // that buffers output so it can be delivered when the process exits. | 536 // that buffers output so it can be delivered when the process exits. |
530 // _NonInteractiveProcess is used to implement the Process.run | 537 // _NonInteractiveProcess is used to implement the Process.run |
531 // method. | 538 // method. |
532 Future<ProcessResult> _runNonInteractiveProcess( | 539 Future<ProcessResult> _runNonInteractiveProcess(String path, |
533 String path, | 540 List<String> arguments, |
534 List<String> arguments, | 541 String workingDirectory, |
535 String workingDirectory, | 542 Map<String, String> environment, |
536 Map<String, String> environment, | 543 bool includeParentEnvironment, |
537 bool includeParentEnvironment, | 544 bool runInShell, |
538 bool runInShell, | 545 Encoding stdoutEncoding, |
539 Encoding stdoutEncoding, | 546 Encoding stderrEncoding) { |
540 Encoding stderrEncoding) { | |
541 // Start the underlying process. | 547 // Start the underlying process. |
542 return Process | 548 return Process.start(path, |
543 .start(path, arguments, | 549 arguments, |
544 workingDirectory: workingDirectory, | 550 workingDirectory: workingDirectory, |
545 environment: environment, | 551 environment: environment, |
546 includeParentEnvironment: includeParentEnvironment, | 552 includeParentEnvironment: includeParentEnvironment, |
547 runInShell: runInShell) | 553 runInShell: runInShell).then((Process p) { |
548 .then((Process p) { | |
549 int pid = p.pid; | 554 int pid = p.pid; |
550 | 555 |
551 // Make sure the process stdin is closed. | 556 // Make sure the process stdin is closed. |
552 p.stdin.close(); | 557 p.stdin.close(); |
553 | 558 |
554 // Setup stdout and stderr handling. | 559 // Setup stdout and stderr handling. |
555 Future foldStream(Stream<List<int>> stream, Encoding encoding) { | 560 Future foldStream(Stream<List<int>> stream, Encoding encoding) { |
556 if (encoding == null) { | 561 if (encoding == null) { |
557 return stream | 562 return stream |
558 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) | 563 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) |
559 .then((builder) => builder.takeBytes()); | 564 .then((builder) => builder.takeBytes()); |
560 } else { | 565 } else { |
561 return stream.transform(encoding.decoder).fold(new StringBuffer(), | 566 return stream |
562 (buf, data) { | 567 .transform(encoding.decoder) |
563 buf.write(data); | 568 .fold( |
564 return buf; | 569 new StringBuffer(), |
565 }).then((sb) => sb.toString()); | 570 (buf, data) { |
| 571 buf.write(data); |
| 572 return buf; |
| 573 }) |
| 574 .then((sb) => sb.toString()); |
566 } | 575 } |
567 } | 576 } |
568 | 577 |
569 Future stdout = foldStream(p.stdout, stdoutEncoding); | 578 Future stdout = foldStream(p.stdout, stdoutEncoding); |
570 Future stderr = foldStream(p.stderr, stderrEncoding); | 579 Future stderr = foldStream(p.stderr, stderrEncoding); |
571 | 580 |
572 return Future.wait([p.exitCode, stdout, stderr]).then((result) { | 581 return Future.wait([p.exitCode, stdout, stderr]).then((result) { |
573 return new ProcessResult(pid, result[0], result[1], result[2]); | 582 return new ProcessResult(pid, result[0], result[1], result[2]); |
574 }); | 583 }); |
575 }); | 584 }); |
576 } | 585 } |
577 | 586 |
578 ProcessResult _runNonInteractiveProcessSync( | 587 ProcessResult _runNonInteractiveProcessSync( |
579 String executable, | 588 String executable, |
580 List<String> arguments, | 589 List<String> arguments, |
581 String workingDirectory, | 590 String workingDirectory, |
582 Map<String, String> environment, | 591 Map<String, String> environment, |
583 bool includeParentEnvironment, | 592 bool includeParentEnvironment, |
584 bool runInShell, | 593 bool runInShell, |
585 Encoding stdoutEncoding, | 594 Encoding stdoutEncoding, |
586 Encoding stderrEncoding) { | 595 Encoding stderrEncoding) { |
587 var process = new _ProcessImpl( | 596 var process = new _ProcessImpl(executable, |
588 executable, | 597 arguments, |
589 arguments, | 598 workingDirectory, |
590 workingDirectory, | 599 environment, |
591 environment, | 600 includeParentEnvironment, |
592 includeParentEnvironment, | 601 runInShell, |
593 runInShell, | 602 ProcessStartMode.NORMAL); |
594 ProcessStartMode.NORMAL); | |
595 return process._runAndWait(stdoutEncoding, stderrEncoding); | 603 return process._runAndWait(stdoutEncoding, stderrEncoding); |
596 } | 604 } |
OLD | NEW |