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 class _WindowsCodePageDecoder { | 5 @patch |
6 @patch static String _decodeBytes(List<int> bytes) | 6 class _WindowsCodePageDecoder { |
7 native "SystemEncodingToString"; | 7 @patch |
| 8 static String _decodeBytes(List<int> bytes) native "SystemEncodingToString"; |
8 } | 9 } |
9 | 10 |
10 | 11 @patch |
11 @patch class _WindowsCodePageEncoder { | 12 class _WindowsCodePageEncoder { |
12 @patch static List<int> _encodeString(String string) | 13 @patch |
13 native "StringToSystemEncoding"; | 14 static List<int> _encodeString(String string) native "StringToSystemEncoding"; |
14 } | 15 } |
15 | 16 |
16 | 17 @patch |
17 @patch class Process { | 18 class Process { |
18 @patch static Future<Process> start( | 19 @patch |
19 String executable, | 20 static Future<Process> start(String executable, List<String> arguments, |
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(executable, | 26 _ProcessImpl process = new _ProcessImpl( |
27 arguments, | 27 executable, |
28 workingDirectory, | 28 arguments, |
29 environment, | 29 workingDirectory, |
30 includeParentEnvironment, | 30 environment, |
31 runInShell, | 31 includeParentEnvironment, |
32 mode); | 32 runInShell, |
| 33 mode); |
33 return process._start(); | 34 return process._start(); |
34 } | 35 } |
35 | 36 |
36 @patch static Future<ProcessResult> run( | 37 @patch |
37 String executable, | 38 static Future<ProcessResult> run(String executable, List<String> arguments, |
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(executable, | 45 return _runNonInteractiveProcess( |
46 arguments, | 46 executable, |
47 workingDirectory, | 47 arguments, |
48 environment, | 48 workingDirectory, |
49 includeParentEnvironment, | 49 environment, |
50 runInShell, | 50 includeParentEnvironment, |
51 stdoutEncoding, | 51 runInShell, |
52 stderrEncoding); | 52 stdoutEncoding, |
| 53 stderrEncoding); |
53 } | 54 } |
54 | 55 |
55 @patch static ProcessResult runSync( | 56 @patch |
56 String executable, | 57 static ProcessResult runSync(String executable, List<String> arguments, |
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(executable, | 64 return _runNonInteractiveProcessSync( |
65 arguments, | 65 executable, |
66 workingDirectory, | 66 arguments, |
67 environment, | 67 workingDirectory, |
68 includeParentEnvironment, | 68 environment, |
69 runInShell, | 69 includeParentEnvironment, |
70 stdoutEncoding, | 70 runInShell, |
71 stderrEncoding); | 71 stdoutEncoding, |
| 72 stderrEncoding); |
72 } | 73 } |
73 | 74 |
74 @patch static bool killPid( | 75 @patch |
75 int pid, [ProcessSignal signal = ProcessSignal.SIGTERM]) { | 76 static bool killPid(int pid, [ProcessSignal signal = ProcessSignal.SIGTERM]) { |
76 if (signal is! ProcessSignal) { | 77 if (signal is! ProcessSignal) { |
77 throw new ArgumentError( | 78 throw new ArgumentError("Argument 'signal' must be a ProcessSignal"); |
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 | |
85 List<_SignalController> _signalControllers = new List(32); | 84 List<_SignalController> _signalControllers = new List(32); |
86 | 85 |
87 | |
88 class _SignalController { | 86 class _SignalController { |
89 final ProcessSignal signal; | 87 final ProcessSignal signal; |
90 | 88 |
91 StreamController _controller; | 89 StreamController _controller; |
92 var _id; | 90 var _id; |
93 | 91 |
94 _SignalController(this.signal) { | 92 _SignalController(this.signal) { |
95 _controller = new StreamController.broadcast( | 93 _controller = |
96 onListen: _listen, | 94 new StreamController.broadcast(onListen: _listen, onCancel: _cancel); |
97 onCancel: _cancel); | |
98 } | 95 } |
99 | 96 |
100 Stream<ProcessSignal> get stream => _controller.stream; | 97 Stream<ProcessSignal> get stream => _controller.stream; |
101 | 98 |
102 void _listen() { | 99 void _listen() { |
103 var id = _setSignalHandler(signal._signalNumber); | 100 var id = _setSignalHandler(signal._signalNumber); |
104 if (id is! int) { | 101 if (id is! int) { |
105 _controller.addError( | 102 _controller |
106 new SignalException("Failed to listen for $signal", id)); | 103 .addError(new SignalException("Failed to listen for $signal", id)); |
107 return; | 104 return; |
108 } | 105 } |
109 _id = id; | 106 _id = id; |
110 var socket = new _RawSocket(new _NativeSocket.watch(id)); | 107 var socket = new _RawSocket(new _NativeSocket.watch(id)); |
111 socket.listen((event) { | 108 socket.listen((event) { |
112 if (event == RawSocketEvent.READ) { | 109 if (event == RawSocketEvent.READ) { |
113 var bytes = socket.read(); | 110 var bytes = socket.read(); |
114 for (int i = 0; i < bytes.length; i++) { | 111 for (int i = 0; i < bytes.length; i++) { |
115 _controller.add(signal); | 112 _controller.add(signal); |
116 } | 113 } |
117 } | 114 } |
118 }); | 115 }); |
119 } | 116 } |
120 | 117 |
121 void _cancel() { | 118 void _cancel() { |
122 if (_id != null) { | 119 if (_id != null) { |
123 _clearSignalHandler(signal._signalNumber); | 120 _clearSignalHandler(signal._signalNumber); |
124 _id = null; | 121 _id = null; |
125 } | 122 } |
126 } | 123 } |
127 | 124 |
128 static _setSignalHandler(int signal) | 125 static _setSignalHandler(int signal) native "Process_SetSignalHandler"; |
129 native "Process_SetSignalHandler"; | |
130 static int _clearSignalHandler(int signal) | 126 static int _clearSignalHandler(int signal) |
131 native "Process_ClearSignalHandler"; | 127 native "Process_ClearSignalHandler"; |
132 } | 128 } |
133 | 129 |
134 Function _getWatchSignalInternal() => _ProcessUtils._watchSignalInternal; | 130 Function _getWatchSignalInternal() => _ProcessUtils._watchSignalInternal; |
135 | 131 |
136 | 132 @patch |
137 @patch class _ProcessUtils { | 133 class _ProcessUtils { |
138 @patch static void _exit(int status) native "Process_Exit"; | 134 @patch |
139 @patch static void _setExitCode(int status) | 135 static void _exit(int status) native "Process_Exit"; |
140 native "Process_SetExitCode"; | 136 @patch |
141 @patch static int _getExitCode() native "Process_GetExitCode"; | 137 static void _setExitCode(int status) native "Process_SetExitCode"; |
142 @patch static void _sleep(int millis) native "Process_Sleep"; | 138 @patch |
143 @patch static int _pid(Process process) native "Process_Pid"; | 139 static int _getExitCode() native "Process_GetExitCode"; |
144 static bool _killPid(int pid, int signal) | 140 @patch |
145 native "Process_KillPid"; | 141 static void _sleep(int millis) native "Process_Sleep"; |
146 @patch static Stream<ProcessSignal> _watchSignal(ProcessSignal signal) { | 142 @patch |
| 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 | |
169 class _ProcessStartStatus { | 168 class _ProcessStartStatus { |
170 int _errorCode; // Set to OS error code if process start failed. | 169 int _errorCode; // Set to OS error code if process start failed. |
171 String _errorMessage; // Set to OS error message if process start failed. | 170 String _errorMessage; // Set to OS error message if process start failed. |
172 } | 171 } |
173 | 172 |
174 | |
175 // The NativeFieldWrapperClass1 can not be used with a mixin, due to missing | 173 // The NativeFieldWrapperClass1 can not be used with a mixin, due to missing |
176 // implicit constructor. | 174 // implicit constructor. |
177 class _ProcessImplNativeWrapper extends NativeFieldWrapperClass1 {} | 175 class _ProcessImplNativeWrapper extends NativeFieldWrapperClass1 {} |
178 | 176 |
179 class _ProcessImpl extends _ProcessImplNativeWrapper implements Process { | 177 class _ProcessImpl extends _ProcessImplNativeWrapper implements Process { |
180 _ProcessResourceInfo _resourceInfo; | 178 _ProcessResourceInfo _resourceInfo; |
181 static bool connectedResourceHandler = false; | 179 static bool connectedResourceHandler = false; |
182 | 180 |
183 _ProcessImpl(String path, | 181 _ProcessImpl( |
184 List<String> arguments, | 182 String path, |
185 this._workingDirectory, | 183 List<String> arguments, |
186 Map<String, String> environment, | 184 this._workingDirectory, |
187 bool includeParentEnvironment, | 185 Map<String, String> environment, |
188 bool runInShell, | 186 bool includeParentEnvironment, |
189 ProcessStartMode mode) : super() { | 187 bool runInShell, |
| 188 ProcessStartMode mode) |
| 189 : super() { |
190 if (!connectedResourceHandler) { | 190 if (!connectedResourceHandler) { |
191 registerExtension('ext.dart.io.getProcesses', | 191 registerExtension( |
192 _ProcessResourceInfo.getStartedProcesses); | 192 'ext.dart.io.getProcesses', _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(String executable, | 283 static List<String> _getShellArguments( |
284 List<String> arguments) { | 284 String executable, 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 = | 370 bool success = _startNative( |
371 _startNative(_path, | 371 _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 : _stdin._sink._nativeSocket, | 377 ? null |
378 _mode == ProcessStartMode.DETACHED | 378 : _stdin._sink._nativeSocket, |
379 ? null : _stdout._stream._nativeSocket, | 379 _mode == ProcessStartMode.DETACHED |
380 _mode == ProcessStartMode.DETACHED | 380 ? null |
381 ? null : _stderr._stream._nativeSocket, | 381 : _stdout._stream._nativeSocket, |
382 _mode != ProcessStartMode.NORMAL | 382 _mode == ProcessStartMode.DETACHED |
383 ? null : _exitHandler._nativeSocket, | 383 ? null |
384 status); | 384 : _stderr._stream._nativeSocket, |
| 385 _mode != ProcessStartMode.NORMAL ? null : _exitHandler._nativeSocket, |
| 386 status); |
385 if (!success) { | 387 if (!success) { |
386 completer.completeError( | 388 completer.completeError(new ProcessException( |
387 new ProcessException(_path, | 389 _path, _arguments, status._errorMessage, status._errorCode)); |
388 _arguments, | |
389 status._errorMessage, | |
390 status._errorCode)); | |
391 return; | 390 return; |
392 } | 391 } |
393 | 392 |
394 _started = true; | 393 _started = true; |
395 _resourceInfo = new _ProcessResourceInfo(this); | 394 _resourceInfo = new _ProcessResourceInfo(this); |
396 | 395 |
397 // Setup an exit handler to handle internal cleanup and possible | 396 // Setup an exit handler to handle internal cleanup and possible |
398 // callback when a process terminates. | 397 // callback when a process terminates. |
399 if (_mode == ProcessStartMode.NORMAL) { | 398 if (_mode == ProcessStartMode.NORMAL) { |
400 int exitDataRead = 0; | 399 int exitDataRead = 0; |
401 final int EXIT_DATA_SIZE = 8; | 400 final int EXIT_DATA_SIZE = 8; |
402 List<int> exitDataBuffer = new List<int>(EXIT_DATA_SIZE); | 401 List<int> exitDataBuffer = new List<int>(EXIT_DATA_SIZE); |
403 _exitHandler.listen((data) { | 402 _exitHandler.listen((data) { |
404 | |
405 int exitCode(List<int> ints) { | 403 int exitCode(List<int> ints) { |
406 var code = _intFromBytes(ints, 0); | 404 var code = _intFromBytes(ints, 0); |
407 var negative = _intFromBytes(ints, 4); | 405 var negative = _intFromBytes(ints, 4); |
408 assert(negative == 0 || negative == 1); | 406 assert(negative == 0 || negative == 1); |
409 return (negative == 0) ? code : -code; | 407 return (negative == 0) ? code : -code; |
410 } | 408 } |
411 | 409 |
412 void handleExit() { | 410 void handleExit() { |
413 _ended = true; | 411 _ended = true; |
414 _exitCode.complete(exitCode(exitDataBuffer)); | 412 _exitCode.complete(exitCode(exitDataBuffer)); |
415 // Kill stdin, helping hand if the user forgot to do it. | 413 // Kill stdin, helping hand if the user forgot to do it. |
416 _stdin._sink.destroy(); | 414 _stdin._sink.destroy(); |
417 _resourceInfo.stopped(); | 415 _resourceInfo.stopped(); |
418 } | 416 } |
419 | 417 |
420 exitDataBuffer.setRange( | 418 exitDataBuffer.setRange( |
421 exitDataRead, exitDataRead + data.length, data); | 419 exitDataRead, exitDataRead + data.length, data); |
422 exitDataRead += data.length; | 420 exitDataRead += data.length; |
423 if (exitDataRead == EXIT_DATA_SIZE) { | 421 if (exitDataRead == EXIT_DATA_SIZE) { |
424 handleExit(); | 422 handleExit(); |
425 } | 423 } |
426 }); | 424 }); |
427 } | 425 } |
428 | 426 |
429 completer.complete(this); | 427 completer.complete(this); |
430 }); | 428 }); |
431 return completer.future; | 429 return completer.future; |
432 } | 430 } |
433 | 431 |
434 ProcessResult _runAndWait(Encoding stdoutEncoding, | 432 ProcessResult _runAndWait(Encoding stdoutEncoding, Encoding stderrEncoding) { |
435 Encoding stderrEncoding) { | |
436 var status = new _ProcessStartStatus(); | 433 var status = new _ProcessStartStatus(); |
437 _exitCode = new Completer<int>(); | 434 _exitCode = new Completer<int>(); |
438 bool success = _startNative(_path, | 435 bool success = _startNative( |
439 _arguments, | 436 _path, |
440 _workingDirectory, | 437 _arguments, |
441 _environment, | 438 _workingDirectory, |
442 ProcessStartMode.NORMAL.index, | 439 _environment, |
443 _stdin._sink._nativeSocket, | 440 ProcessStartMode.NORMAL.index, |
444 _stdout._stream._nativeSocket, | 441 _stdin._sink._nativeSocket, |
445 _stderr._stream._nativeSocket, | 442 _stdout._stream._nativeSocket, |
446 _exitHandler._nativeSocket, | 443 _stderr._stream._nativeSocket, |
447 status); | 444 _exitHandler._nativeSocket, |
| 445 status); |
448 if (!success) { | 446 if (!success) { |
449 throw new ProcessException(_path, | 447 throw new ProcessException( |
450 _arguments, | 448 _path, _arguments, status._errorMessage, status._errorCode); |
451 status._errorMessage, | |
452 status._errorCode); | |
453 } | 449 } |
454 | 450 |
455 _resourceInfo = new _ProcessResourceInfo(this); | 451 _resourceInfo = new _ProcessResourceInfo(this); |
456 | 452 |
457 var result = _wait( | 453 var result = _wait( |
458 _stdin._sink._nativeSocket, | 454 _stdin._sink._nativeSocket, |
459 _stdout._stream._nativeSocket, | 455 _stdout._stream._nativeSocket, |
460 _stderr._stream._nativeSocket, | 456 _stderr._stream._nativeSocket, |
461 _exitHandler._nativeSocket); | 457 _exitHandler._nativeSocket); |
462 | 458 |
463 getOutput(output, encoding) { | 459 getOutput(output, encoding) { |
464 if (encoding == null) return output; | 460 if (encoding == null) return output; |
465 return encoding.decode(output); | 461 return encoding.decode(output); |
466 } | 462 } |
467 | 463 |
468 _resourceInfo.stopped(); | 464 _resourceInfo.stopped(); |
469 | 465 |
470 return new ProcessResult( | 466 return new ProcessResult( |
471 result[0], | 467 result[0], |
472 result[1], | 468 result[1], |
473 getOutput(result[2], stdoutEncoding), | 469 getOutput(result[2], stdoutEncoding), |
474 getOutput(result[3], stderrEncoding)); | 470 getOutput(result[3], stderrEncoding)); |
475 } | 471 } |
476 | 472 |
477 bool _startNative(String path, | 473 bool _startNative( |
478 List<String> arguments, | 474 String path, |
479 String workingDirectory, | 475 List<String> arguments, |
480 List<String> environment, | 476 String workingDirectory, |
481 int mode, | 477 List<String> environment, |
482 _NativeSocket stdin, | 478 int mode, |
483 _NativeSocket stdout, | 479 _NativeSocket stdin, |
484 _NativeSocket stderr, | 480 _NativeSocket stdout, |
485 _NativeSocket exitHandler, | 481 _NativeSocket stderr, |
486 _ProcessStartStatus status) native "Process_Start"; | 482 _NativeSocket exitHandler, |
| 483 _ProcessStartStatus status) native "Process_Start"; |
487 | 484 |
488 _wait(_NativeSocket stdin, | 485 _wait(_NativeSocket stdin, _NativeSocket stdout, _NativeSocket stderr, |
489 _NativeSocket stdout, | 486 _NativeSocket exitHandler) native "Process_Wait"; |
490 _NativeSocket stderr, | |
491 _NativeSocket exitHandler) native "Process_Wait"; | |
492 | 487 |
493 Stream<List<int>> get stdout { | 488 Stream<List<int>> get stdout { |
494 return _stdout; | 489 return _stdout; |
495 } | 490 } |
496 | 491 |
497 Stream<List<int>> get stderr { | 492 Stream<List<int>> get stderr { |
498 return _stderr; | 493 return _stderr; |
499 } | 494 } |
500 | 495 |
501 IOSink get stdin { | 496 IOSink get stdin { |
502 return _stdin; | 497 return _stdin; |
503 } | 498 } |
504 | 499 |
505 Future<int> get exitCode => _exitCode != null ? _exitCode.future : null; | 500 Future<int> get exitCode => _exitCode != null ? _exitCode.future : null; |
506 | 501 |
507 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]) { | 502 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]) { |
508 if (signal is! ProcessSignal) { | 503 if (signal is! ProcessSignal) { |
509 throw new ArgumentError( | 504 throw new ArgumentError("Argument 'signal' must be a ProcessSignal"); |
510 "Argument 'signal' must be a ProcessSignal"); | |
511 } | 505 } |
512 assert(_started); | 506 assert(_started); |
513 if (_ended) return false; | 507 if (_ended) return false; |
514 return _ProcessUtils._killPid(pid, signal._signalNumber); | 508 return _ProcessUtils._killPid(pid, signal._signalNumber); |
515 } | 509 } |
516 | 510 |
517 int get pid => _ProcessUtils._pid(this); | 511 int get pid => _ProcessUtils._pid(this); |
518 | 512 |
519 String _path; | 513 String _path; |
520 List<String> _arguments; | 514 List<String> _arguments; |
521 String _workingDirectory; | 515 String _workingDirectory; |
522 List<String> _environment; | 516 List<String> _environment; |
523 ProcessStartMode _mode; | 517 ProcessStartMode _mode; |
524 // Private methods of Socket are used by _in, _out, and _err. | 518 // Private methods of Socket are used by _in, _out, and _err. |
525 _StdSocketSink _stdin; | 519 _StdSocketSink _stdin; |
526 _StdStream _stdout; | 520 _StdStream _stdout; |
527 _StdStream _stderr; | 521 _StdStream _stderr; |
528 Socket _exitHandler; | 522 Socket _exitHandler; |
529 bool _ended; | 523 bool _ended; |
530 bool _started; | 524 bool _started; |
531 Completer<int> _exitCode; | 525 Completer<int> _exitCode; |
532 } | 526 } |
533 | 527 |
534 | |
535 // _NonInteractiveProcess is a wrapper around an interactive process | 528 // _NonInteractiveProcess is a wrapper around an interactive process |
536 // that buffers output so it can be delivered when the process exits. | 529 // that buffers output so it can be delivered when the process exits. |
537 // _NonInteractiveProcess is used to implement the Process.run | 530 // _NonInteractiveProcess is used to implement the Process.run |
538 // method. | 531 // method. |
539 Future<ProcessResult> _runNonInteractiveProcess(String path, | 532 Future<ProcessResult> _runNonInteractiveProcess( |
540 List<String> arguments, | 533 String path, |
541 String workingDirectory, | 534 List<String> arguments, |
542 Map<String, String> environment, | 535 String workingDirectory, |
543 bool includeParentEnvironment, | 536 Map<String, String> environment, |
544 bool runInShell, | 537 bool includeParentEnvironment, |
545 Encoding stdoutEncoding, | 538 bool runInShell, |
546 Encoding stderrEncoding) { | 539 Encoding stdoutEncoding, |
| 540 Encoding stderrEncoding) { |
547 // Start the underlying process. | 541 // Start the underlying process. |
548 return Process.start(path, | 542 return Process |
549 arguments, | 543 .start(path, arguments, |
550 workingDirectory: workingDirectory, | 544 workingDirectory: workingDirectory, |
551 environment: environment, | 545 environment: environment, |
552 includeParentEnvironment: includeParentEnvironment, | 546 includeParentEnvironment: includeParentEnvironment, |
553 runInShell: runInShell).then((Process p) { | 547 runInShell: runInShell) |
| 548 .then((Process p) { |
554 int pid = p.pid; | 549 int pid = p.pid; |
555 | 550 |
556 // Make sure the process stdin is closed. | 551 // Make sure the process stdin is closed. |
557 p.stdin.close(); | 552 p.stdin.close(); |
558 | 553 |
559 // Setup stdout and stderr handling. | 554 // Setup stdout and stderr handling. |
560 Future foldStream(Stream<List<int>> stream, Encoding encoding) { | 555 Future foldStream(Stream<List<int>> stream, Encoding encoding) { |
561 if (encoding == null) { | 556 if (encoding == null) { |
562 return stream | 557 return stream |
563 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) | 558 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) |
564 .then((builder) => builder.takeBytes()); | 559 .then((builder) => builder.takeBytes()); |
565 } else { | 560 } else { |
566 return stream | 561 return stream.transform(encoding.decoder).fold(new StringBuffer(), |
567 .transform(encoding.decoder) | 562 (buf, data) { |
568 .fold( | 563 buf.write(data); |
569 new StringBuffer(), | 564 return buf; |
570 (buf, data) { | 565 }).then((sb) => sb.toString()); |
571 buf.write(data); | |
572 return buf; | |
573 }) | |
574 .then((sb) => sb.toString()); | |
575 } | 566 } |
576 } | 567 } |
577 | 568 |
578 Future stdout = foldStream(p.stdout, stdoutEncoding); | 569 Future stdout = foldStream(p.stdout, stdoutEncoding); |
579 Future stderr = foldStream(p.stderr, stderrEncoding); | 570 Future stderr = foldStream(p.stderr, stderrEncoding); |
580 | 571 |
581 return Future.wait([p.exitCode, stdout, stderr]).then((result) { | 572 return Future.wait([p.exitCode, stdout, stderr]).then((result) { |
582 return new ProcessResult(pid, result[0], result[1], result[2]); | 573 return new ProcessResult(pid, result[0], result[1], result[2]); |
583 }); | 574 }); |
584 }); | 575 }); |
585 } | 576 } |
586 | 577 |
587 ProcessResult _runNonInteractiveProcessSync( | 578 ProcessResult _runNonInteractiveProcessSync( |
588 String executable, | 579 String executable, |
589 List<String> arguments, | 580 List<String> arguments, |
590 String workingDirectory, | 581 String workingDirectory, |
591 Map<String, String> environment, | 582 Map<String, String> environment, |
592 bool includeParentEnvironment, | 583 bool includeParentEnvironment, |
593 bool runInShell, | 584 bool runInShell, |
594 Encoding stdoutEncoding, | 585 Encoding stdoutEncoding, |
595 Encoding stderrEncoding) { | 586 Encoding stderrEncoding) { |
596 var process = new _ProcessImpl(executable, | 587 var process = new _ProcessImpl( |
597 arguments, | 588 executable, |
598 workingDirectory, | 589 arguments, |
599 environment, | 590 workingDirectory, |
600 includeParentEnvironment, | 591 environment, |
601 runInShell, | 592 includeParentEnvironment, |
602 ProcessStartMode.NORMAL); | 593 runInShell, |
| 594 ProcessStartMode.NORMAL); |
603 return process._runAndWait(stdoutEncoding, stderrEncoding); | 595 return process._runAndWait(stdoutEncoding, stderrEncoding); |
604 } | 596 } |
OLD | NEW |