|
10 | 10 |
|
11 | 11 | use prelude::v1::*; |
12 | 12 |
|
13 | | -use collections::hash_map::Hasher; |
| 13 | +#[cfg(stage0)] use collections::hash_map::Hasher; |
14 | 14 | use collections; |
15 | 15 | use env; |
16 | 16 | use ffi::CString; |
@@ -106,6 +106,7 @@ impl Process { |
106 | 106 | } |
107 | 107 |
|
108 | 108 | #[allow(deprecated)] |
| 109 | + #[cfg(stage0)] |
109 | 110 | pub fn spawn<K, V, C, P>(cfg: &C, in_fd: Option<P>, |
110 | 111 | out_fd: Option<P>, err_fd: Option<P>) |
111 | 112 | -> IoResult<Process> |
@@ -267,6 +268,169 @@ impl Process { |
267 | 268 | }) |
268 | 269 | } |
269 | 270 | } |
| 271 | + #[allow(deprecated)] |
| 272 | + #[cfg(not(stage0))] |
| 273 | + pub fn spawn<K, V, C, P>(cfg: &C, in_fd: Option<P>, |
| 274 | + out_fd: Option<P>, err_fd: Option<P>) |
| 275 | + -> IoResult<Process> |
| 276 | + where C: ProcessConfig<K, V>, P: AsInner<FileDesc>, |
| 277 | + K: BytesContainer + Eq + Hash, V: BytesContainer |
| 278 | + { |
| 279 | + use libc::types::os::arch::extra::{DWORD, HANDLE, STARTUPINFO}; |
| 280 | + use libc::consts::os::extra::{ |
| 281 | + TRUE, FALSE, |
| 282 | + STARTF_USESTDHANDLES, |
| 283 | + INVALID_HANDLE_VALUE, |
| 284 | + DUPLICATE_SAME_ACCESS |
| 285 | + }; |
| 286 | + use libc::funcs::extra::kernel32::{ |
| 287 | + GetCurrentProcess, |
| 288 | + DuplicateHandle, |
| 289 | + CloseHandle, |
| 290 | + CreateProcessW |
| 291 | + }; |
| 292 | + use libc::funcs::extra::msvcrt::get_osfhandle; |
| 293 | + |
| 294 | + use mem; |
| 295 | + use iter::IteratorExt; |
| 296 | + use str::StrExt; |
| 297 | + |
| 298 | + if cfg.gid().is_some() || cfg.uid().is_some() { |
| 299 | + return Err(IoError { |
| 300 | + kind: old_io::IoUnavailable, |
| 301 | + desc: "unsupported gid/uid requested on windows", |
| 302 | + detail: None, |
| 303 | + }) |
| 304 | + } |
| 305 | + |
| 306 | + // To have the spawning semantics of unix/windows stay the same, we need to |
| 307 | + // read the *child's* PATH if one is provided. See #15149 for more details. |
| 308 | + let program = cfg.env().and_then(|env| { |
| 309 | + for (key, v) in env { |
| 310 | + if b"PATH" != key.container_as_bytes() { continue } |
| 311 | + |
| 312 | + // Split the value and test each path to see if the |
| 313 | + // program exists. |
| 314 | + for path in os::split_paths(v.container_as_bytes()) { |
| 315 | + let path = path.join(cfg.program().as_bytes()) |
| 316 | + .with_extension(env::consts::EXE_EXTENSION); |
| 317 | + if path.exists() { |
| 318 | + return Some(CString::from_slice(path.as_vec())) |
| 319 | + } |
| 320 | + } |
| 321 | + break |
| 322 | + } |
| 323 | + None |
| 324 | + }); |
| 325 | + |
| 326 | + unsafe { |
| 327 | + let mut si = zeroed_startupinfo(); |
| 328 | + si.cb = mem::size_of::<STARTUPINFO>() as DWORD; |
| 329 | + si.dwFlags = STARTF_USESTDHANDLES; |
| 330 | + |
| 331 | + let cur_proc = GetCurrentProcess(); |
| 332 | + |
| 333 | + // Similarly to unix, we don't actually leave holes for the stdio file |
| 334 | + // descriptors, but rather open up /dev/null equivalents. These |
| 335 | + // equivalents are drawn from libuv's windows process spawning. |
| 336 | + let set_fd = |fd: &Option<P>, slot: &mut HANDLE, |
| 337 | + is_stdin: bool| { |
| 338 | + match *fd { |
| 339 | + None => { |
| 340 | + let access = if is_stdin { |
| 341 | + libc::FILE_GENERIC_READ |
| 342 | + } else { |
| 343 | + libc::FILE_GENERIC_WRITE | libc::FILE_READ_ATTRIBUTES |
| 344 | + }; |
| 345 | + let size = mem::size_of::<libc::SECURITY_ATTRIBUTES>(); |
| 346 | + let mut sa = libc::SECURITY_ATTRIBUTES { |
| 347 | + nLength: size as libc::DWORD, |
| 348 | + lpSecurityDescriptor: ptr::null_mut(), |
| 349 | + bInheritHandle: 1, |
| 350 | + }; |
| 351 | + let mut filename: Vec<u16> = "NUL".utf16_units().collect(); |
| 352 | + filename.push(0); |
| 353 | + *slot = libc::CreateFileW(filename.as_ptr(), |
| 354 | + access, |
| 355 | + libc::FILE_SHARE_READ | |
| 356 | + libc::FILE_SHARE_WRITE, |
| 357 | + &mut sa, |
| 358 | + libc::OPEN_EXISTING, |
| 359 | + 0, |
| 360 | + ptr::null_mut()); |
| 361 | + if *slot == INVALID_HANDLE_VALUE { |
| 362 | + return Err(super::last_error()) |
| 363 | + } |
| 364 | + } |
| 365 | + Some(ref fd) => { |
| 366 | + let orig = get_osfhandle(fd.as_inner().fd()) as HANDLE; |
| 367 | + if orig == INVALID_HANDLE_VALUE { |
| 368 | + return Err(super::last_error()) |
| 369 | + } |
| 370 | + if DuplicateHandle(cur_proc, orig, cur_proc, slot, |
| 371 | + 0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE { |
| 372 | + return Err(super::last_error()) |
| 373 | + } |
| 374 | + } |
| 375 | + } |
| 376 | + Ok(()) |
| 377 | + }; |
| 378 | + |
| 379 | + try!(set_fd(&in_fd, &mut si.hStdInput, true)); |
| 380 | + try!(set_fd(&out_fd, &mut si.hStdOutput, false)); |
| 381 | + try!(set_fd(&err_fd, &mut si.hStdError, false)); |
| 382 | + |
| 383 | + let cmd_str = make_command_line(program.as_ref().unwrap_or(cfg.program()), |
| 384 | + cfg.args()); |
| 385 | + let mut pi = zeroed_process_information(); |
| 386 | + let mut create_err = None; |
| 387 | + |
| 388 | + // stolen from the libuv code. |
| 389 | + let mut flags = libc::CREATE_UNICODE_ENVIRONMENT; |
| 390 | + if cfg.detach() { |
| 391 | + flags |= libc::DETACHED_PROCESS | libc::CREATE_NEW_PROCESS_GROUP; |
| 392 | + } |
| 393 | + |
| 394 | + with_envp(cfg.env(), |envp| { |
| 395 | + with_dirp(cfg.cwd(), |dirp| { |
| 396 | + let mut cmd_str: Vec<u16> = cmd_str.utf16_units().collect(); |
| 397 | + cmd_str.push(0); |
| 398 | + let _lock = CREATE_PROCESS_LOCK.lock().unwrap(); |
| 399 | + let created = CreateProcessW(ptr::null(), |
| 400 | + cmd_str.as_mut_ptr(), |
| 401 | + ptr::null_mut(), |
| 402 | + ptr::null_mut(), |
| 403 | + TRUE, |
| 404 | + flags, envp, dirp, |
| 405 | + &mut si, &mut pi); |
| 406 | + if created == FALSE { |
| 407 | + create_err = Some(super::last_error()); |
| 408 | + } |
| 409 | + }) |
| 410 | + }); |
| 411 | + |
| 412 | + assert!(CloseHandle(si.hStdInput) != 0); |
| 413 | + assert!(CloseHandle(si.hStdOutput) != 0); |
| 414 | + assert!(CloseHandle(si.hStdError) != 0); |
| 415 | + |
| 416 | + match create_err { |
| 417 | + Some(err) => return Err(err), |
| 418 | + None => {} |
| 419 | + } |
| 420 | + |
| 421 | + // We close the thread handle because we don't care about keeping the |
| 422 | + // thread id valid, and we aren't keeping the thread handle around to be |
| 423 | + // able to close it later. We don't close the process handle however |
| 424 | + // because std::we want the process id to stay valid at least until the |
| 425 | + // calling code closes the process handle. |
| 426 | + assert!(CloseHandle(pi.hThread) != 0); |
| 427 | + |
| 428 | + Ok(Process { |
| 429 | + pid: pi.dwProcessId as pid_t, |
| 430 | + handle: pi.hProcess as *mut () |
| 431 | + }) |
| 432 | + } |
| 433 | + } |
270 | 434 |
|
271 | 435 | /// Waits for a process to exit and returns the exit code, failing |
272 | 436 | /// if there is no process with the specified id. |
|
0 commit comments