]> git.lizzy.rs Git - rust.git/blob - src/libstd/process.rs
skip check for DYLD envars in child proc
[rust.git] / src / libstd / process.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Working with processes.
12
13 #![stable(feature = "process", since = "1.0.0")]
14 #![allow(non_upper_case_globals)]
15
16 use prelude::v1::*;
17 use io::prelude::*;
18
19 use ffi::OsStr;
20 use fmt;
21 use io::{self, Error, ErrorKind};
22 use path;
23 use sys::pipe::{self, AnonPipe};
24 use sys::process as imp;
25 use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
26 use thread::{self, JoinHandle};
27
28 /// Representation of a running or exited child process.
29 ///
30 /// This structure is used to represent and manage child processes. A child
31 /// process is created via the `Command` struct, which configures the spawning
32 /// process and can itself be constructed using a builder-style interface.
33 ///
34 /// # Examples
35 ///
36 /// ```should_panic
37 /// use std::process::Command;
38 ///
39 /// let mut child = Command::new("/bin/cat")
40 ///                         .arg("file.txt")
41 ///                         .spawn()
42 ///                         .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) });
43 ///
44 /// let ecode = child.wait()
45 ///                  .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) });
46 ///
47 /// assert!(ecode.success());
48 /// ```
49 #[stable(feature = "process", since = "1.0.0")]
50 pub struct Child {
51     handle: imp::Process,
52
53     /// None until wait() or wait_with_output() is called.
54     status: Option<imp::ExitStatus>,
55
56     /// The handle for writing to the child's stdin, if it has been captured
57     #[stable(feature = "process", since = "1.0.0")]
58     pub stdin: Option<ChildStdin>,
59
60     /// The handle for reading from the child's stdout, if it has been captured
61     #[stable(feature = "process", since = "1.0.0")]
62     pub stdout: Option<ChildStdout>,
63
64     /// The handle for reading from the child's stderr, if it has been captured
65     #[stable(feature = "process", since = "1.0.0")]
66     pub stderr: Option<ChildStderr>,
67 }
68
69 impl AsInner<imp::Process> for Child {
70     fn as_inner(&self) -> &imp::Process { &self.handle }
71 }
72
73 impl IntoInner<imp::Process> for Child {
74     fn into_inner(self) -> imp::Process { self.handle }
75 }
76
77 /// A handle to a child process's stdin
78 #[stable(feature = "process", since = "1.0.0")]
79 pub struct ChildStdin {
80     inner: AnonPipe
81 }
82
83 #[stable(feature = "process", since = "1.0.0")]
84 impl Write for ChildStdin {
85     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
86         self.inner.write(buf)
87     }
88
89     fn flush(&mut self) -> io::Result<()> {
90         Ok(())
91     }
92 }
93
94 impl AsInner<AnonPipe> for ChildStdin {
95     fn as_inner(&self) -> &AnonPipe { &self.inner }
96 }
97
98 impl IntoInner<AnonPipe> for ChildStdin {
99     fn into_inner(self) -> AnonPipe { self.inner }
100 }
101
102 /// A handle to a child process's stdout
103 #[stable(feature = "process", since = "1.0.0")]
104 pub struct ChildStdout {
105     inner: AnonPipe
106 }
107
108 #[stable(feature = "process", since = "1.0.0")]
109 impl Read for ChildStdout {
110     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
111         self.inner.read(buf)
112     }
113 }
114
115 impl AsInner<AnonPipe> for ChildStdout {
116     fn as_inner(&self) -> &AnonPipe { &self.inner }
117 }
118
119 impl IntoInner<AnonPipe> for ChildStdout {
120     fn into_inner(self) -> AnonPipe { self.inner }
121 }
122
123 /// A handle to a child process's stderr
124 #[stable(feature = "process", since = "1.0.0")]
125 pub struct ChildStderr {
126     inner: AnonPipe
127 }
128
129 #[stable(feature = "process", since = "1.0.0")]
130 impl Read for ChildStderr {
131     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
132         self.inner.read(buf)
133     }
134 }
135
136 impl AsInner<AnonPipe> for ChildStderr {
137     fn as_inner(&self) -> &AnonPipe { &self.inner }
138 }
139
140 impl IntoInner<AnonPipe> for ChildStderr {
141     fn into_inner(self) -> AnonPipe { self.inner }
142 }
143
144 /// The `Command` type acts as a process builder, providing fine-grained control
145 /// over how a new process should be spawned. A default configuration can be
146 /// generated using `Command::new(program)`, where `program` gives a path to the
147 /// program to be executed. Additional builder methods allow the configuration
148 /// to be changed (for example, by adding arguments) prior to spawning:
149 ///
150 /// ```
151 /// use std::process::Command;
152 ///
153 /// let output = Command::new("sh")
154 ///                      .arg("-c")
155 ///                      .arg("echo hello")
156 ///                      .output()
157 ///                      .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
158 /// let hello = output.stdout;
159 /// ```
160 #[stable(feature = "process", since = "1.0.0")]
161 pub struct Command {
162     inner: imp::Command,
163
164     // Details explained in the builder methods
165     stdin: Option<Stdio>,
166     stdout: Option<Stdio>,
167     stderr: Option<Stdio>,
168 }
169
170 impl Command {
171     /// Constructs a new `Command` for launching the program at
172     /// path `program`, with the following default configuration:
173     ///
174     /// * No arguments to the program
175     /// * Inherit the current process's environment
176     /// * Inherit the current process's working directory
177     /// * Inherit stdin/stdout/stderr for `spawn` or `status`, but create pipes for `output`
178     ///
179     /// Builder methods are provided to change these defaults and
180     /// otherwise configure the process.
181     #[stable(feature = "process", since = "1.0.0")]
182     pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
183         Command {
184             inner: imp::Command::new(program.as_ref()),
185             stdin: None,
186             stdout: None,
187             stderr: None,
188         }
189     }
190
191     /// Add an argument to pass to the program.
192     #[stable(feature = "process", since = "1.0.0")]
193     pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
194         self.inner.arg(arg.as_ref());
195         self
196     }
197
198     /// Add multiple arguments to pass to the program.
199     #[stable(feature = "process", since = "1.0.0")]
200     pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command {
201         self.inner.args(args.iter().map(AsRef::as_ref));
202         self
203     }
204
205     /// Inserts or updates an environment variable mapping.
206     ///
207     /// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
208     /// and case-sensitive on all other platforms.
209     #[stable(feature = "process", since = "1.0.0")]
210     pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
211         where K: AsRef<OsStr>, V: AsRef<OsStr>
212     {
213         self.inner.env(key.as_ref(), val.as_ref());
214         self
215     }
216
217     /// Removes an environment variable mapping.
218     #[stable(feature = "process", since = "1.0.0")]
219     pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
220         self.inner.env_remove(key.as_ref());
221         self
222     }
223
224     /// Clears the entire environment map for the child process.
225     #[stable(feature = "process", since = "1.0.0")]
226     pub fn env_clear(&mut self) -> &mut Command {
227         self.inner.env_clear();
228         self
229     }
230
231     /// Sets the working directory for the child process.
232     #[stable(feature = "process", since = "1.0.0")]
233     pub fn current_dir<P: AsRef<path::Path>>(&mut self, dir: P) -> &mut Command {
234         self.inner.cwd(dir.as_ref().as_ref());
235         self
236     }
237
238     /// Configuration for the child process's stdin handle (file descriptor 0).
239     #[stable(feature = "process", since = "1.0.0")]
240     pub fn stdin(&mut self, cfg: Stdio) -> &mut Command {
241         self.stdin = Some(cfg);
242         self
243     }
244
245     /// Configuration for the child process's stdout handle (file descriptor 1).
246     #[stable(feature = "process", since = "1.0.0")]
247     pub fn stdout(&mut self, cfg: Stdio) -> &mut Command {
248         self.stdout = Some(cfg);
249         self
250     }
251
252     /// Configuration for the child process's stderr handle (file descriptor 2).
253     #[stable(feature = "process", since = "1.0.0")]
254     pub fn stderr(&mut self, cfg: Stdio) -> &mut Command {
255         self.stderr = Some(cfg);
256         self
257     }
258
259     fn spawn_inner(&self, default_io: StdioImp) -> io::Result<Child> {
260         let default_io = Stdio(default_io);
261
262         // See comment on `setup_io` for what `_drop_later` is.
263         let (their_stdin, our_stdin, _drop_later) = try!(
264             setup_io(self.stdin.as_ref().unwrap_or(&default_io), true)
265         );
266         let (their_stdout, our_stdout, _drop_later) = try!(
267             setup_io(self.stdout.as_ref().unwrap_or(&default_io), false)
268         );
269         let (their_stderr, our_stderr, _drop_later) = try!(
270             setup_io(self.stderr.as_ref().unwrap_or(&default_io), false)
271         );
272
273         match imp::Process::spawn(&self.inner, their_stdin, their_stdout,
274                                   their_stderr) {
275             Err(e) => Err(e),
276             Ok(handle) => Ok(Child {
277                 handle: handle,
278                 status: None,
279                 stdin: our_stdin.map(|fd| ChildStdin { inner: fd }),
280                 stdout: our_stdout.map(|fd| ChildStdout { inner: fd }),
281                 stderr: our_stderr.map(|fd| ChildStderr { inner: fd }),
282             })
283         }
284     }
285
286     /// Executes the command as a child process, returning a handle to it.
287     ///
288     /// By default, stdin, stdout and stderr are inherited from the parent.
289     #[stable(feature = "process", since = "1.0.0")]
290     pub fn spawn(&mut self) -> io::Result<Child> {
291         self.spawn_inner(StdioImp::Inherit)
292     }
293
294     /// Executes the command as a child process, waiting for it to finish and
295     /// collecting all of its output.
296     ///
297     /// By default, stdin, stdout and stderr are captured (and used to
298     /// provide the resulting output).
299     ///
300     /// # Examples
301     ///
302     /// ```
303     /// use std::process::Command;
304     /// let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| {
305     ///     panic!("failed to execute process: {}", e)
306     /// });
307     ///
308     /// println!("status: {}", output.status);
309     /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
310     /// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
311     /// ```
312     #[stable(feature = "process", since = "1.0.0")]
313     pub fn output(&mut self) -> io::Result<Output> {
314         self.spawn_inner(StdioImp::MakePipe).and_then(|p| p.wait_with_output())
315     }
316
317     /// Executes a command as a child process, waiting for it to finish and
318     /// collecting its exit status.
319     ///
320     /// By default, stdin, stdout and stderr are inherited from the parent.
321     ///
322     /// # Examples
323     ///
324     /// ```
325     /// use std::process::Command;
326     ///
327     /// let status = Command::new("ls").status().unwrap_or_else(|e| {
328     ///     panic!("failed to execute process: {}", e)
329     /// });
330     ///
331     /// println!("process exited with: {}", status);
332     /// ```
333     #[stable(feature = "process", since = "1.0.0")]
334     pub fn status(&mut self) -> io::Result<ExitStatus> {
335         self.spawn().and_then(|mut p| p.wait())
336     }
337 }
338
339 #[stable(feature = "rust1", since = "1.0.0")]
340 impl fmt::Debug for Command {
341     /// Format the program and arguments of a Command for display. Any
342     /// non-utf8 data is lossily converted using the utf8 replacement
343     /// character.
344     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
345         try!(write!(f, "{:?}", self.inner.program));
346         for arg in &self.inner.args {
347             try!(write!(f, " {:?}", arg));
348         }
349         Ok(())
350     }
351 }
352
353 impl AsInner<imp::Command> for Command {
354     fn as_inner(&self) -> &imp::Command { &self.inner }
355 }
356
357 impl AsInnerMut<imp::Command> for Command {
358     fn as_inner_mut(&mut self) -> &mut imp::Command { &mut self.inner }
359 }
360
361 // Takes a `Stdio` configuration (this module) and whether the to-be-owned
362 // handle will be readable.
363 //
364 // Returns a triple of (stdio to spawn with, stdio to store, stdio to drop). The
365 // stdio to spawn with is passed down to the `sys` module and indicates how the
366 // stdio stream should be set up. The "stdio to store" is an object which
367 // should be returned in the `Child` that makes its way out. The "stdio to drop"
368 // represents the raw value of "stdio to spawn with", but is the owned variant
369 // for it. This needs to be dropped after the child spawns
370 fn setup_io(io: &Stdio, readable: bool)
371             -> io::Result<(imp::Stdio, Option<AnonPipe>, Option<AnonPipe>)>
372 {
373     Ok(match io.0 {
374         StdioImp::MakePipe => {
375             let (reader, writer) = try!(pipe::anon_pipe());
376             if readable {
377                 (imp::Stdio::Raw(reader.raw()), Some(writer), Some(reader))
378             } else {
379                 (imp::Stdio::Raw(writer.raw()), Some(reader), Some(writer))
380             }
381         }
382         StdioImp::Raw(ref owned) => (imp::Stdio::Raw(owned.raw()), None, None),
383         StdioImp::Inherit => (imp::Stdio::Inherit, None, None),
384         StdioImp::None => (imp::Stdio::None, None, None),
385     })
386 }
387
388 /// The output of a finished process.
389 #[derive(PartialEq, Eq, Clone)]
390 #[stable(feature = "process", since = "1.0.0")]
391 pub struct Output {
392     /// The status (exit code) of the process.
393     #[stable(feature = "process", since = "1.0.0")]
394     pub status: ExitStatus,
395     /// The data that the process wrote to stdout.
396     #[stable(feature = "process", since = "1.0.0")]
397     pub stdout: Vec<u8>,
398     /// The data that the process wrote to stderr.
399     #[stable(feature = "process", since = "1.0.0")]
400     pub stderr: Vec<u8>,
401 }
402
403 /// Describes what to do with a standard I/O stream for a child process.
404 #[stable(feature = "process", since = "1.0.0")]
405 pub struct Stdio(StdioImp);
406
407 // The internal enum for stdio setup; see below for descriptions.
408 enum StdioImp {
409     MakePipe,
410     Raw(imp::RawStdio),
411     Inherit,
412     None,
413 }
414
415 impl Stdio {
416     /// A new pipe should be arranged to connect the parent and child processes.
417     #[stable(feature = "process", since = "1.0.0")]
418     pub fn piped() -> Stdio { Stdio(StdioImp::MakePipe) }
419
420     /// The child inherits from the corresponding parent descriptor.
421     #[stable(feature = "process", since = "1.0.0")]
422     pub fn inherit() -> Stdio { Stdio(StdioImp::Inherit) }
423
424     /// This stream will be ignored. This is the equivalent of attaching the
425     /// stream to `/dev/null`
426     #[stable(feature = "process", since = "1.0.0")]
427     pub fn null() -> Stdio { Stdio(StdioImp::None) }
428 }
429
430 impl FromInner<imp::RawStdio> for Stdio {
431     fn from_inner(inner: imp::RawStdio) -> Stdio {
432         Stdio(StdioImp::Raw(inner))
433     }
434 }
435
436 /// Describes the result of a process after it has terminated.
437 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
438 #[stable(feature = "process", since = "1.0.0")]
439 pub struct ExitStatus(imp::ExitStatus);
440
441 impl ExitStatus {
442     /// Was termination successful? Signal termination not considered a success,
443     /// and success is defined as a zero exit status.
444     #[stable(feature = "process", since = "1.0.0")]
445     pub fn success(&self) -> bool {
446         self.0.success()
447     }
448
449     /// Returns the exit code of the process, if any.
450     ///
451     /// On Unix, this will return `None` if the process was terminated
452     /// by a signal; `std::os::unix` provides an extension trait for
453     /// extracting the signal and other details from the `ExitStatus`.
454     #[stable(feature = "process", since = "1.0.0")]
455     pub fn code(&self) -> Option<i32> {
456         self.0.code()
457     }
458 }
459
460 impl AsInner<imp::ExitStatus> for ExitStatus {
461     fn as_inner(&self) -> &imp::ExitStatus { &self.0 }
462 }
463
464 #[stable(feature = "process", since = "1.0.0")]
465 impl fmt::Display for ExitStatus {
466     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
467         self.0.fmt(f)
468     }
469 }
470
471 impl Child {
472     /// Forces the child to exit. This is equivalent to sending a
473     /// SIGKILL on unix platforms.
474     #[stable(feature = "process", since = "1.0.0")]
475     pub fn kill(&mut self) -> io::Result<()> {
476         #[cfg(unix)] fn collect_status(p: &mut Child) {
477             // On Linux (and possibly other unices), a process that has exited will
478             // continue to accept signals because it is "defunct". The delivery of
479             // signals will only fail once the child has been reaped. For this
480             // reason, if the process hasn't exited yet, then we attempt to collect
481             // their status with WNOHANG.
482             if p.status.is_none() {
483                 match p.handle.try_wait() {
484                     Some(status) => { p.status = Some(status); }
485                     None => {}
486                 }
487             }
488         }
489         #[cfg(windows)] fn collect_status(_p: &mut Child) {}
490
491         collect_status(self);
492
493         // if the process has finished, and therefore had waitpid called,
494         // and we kill it, then on unix we might ending up killing a
495         // newer process that happens to have the same (re-used) id
496         if self.status.is_some() {
497             return Err(Error::new(
498                 ErrorKind::InvalidInput,
499                 "invalid argument: can't kill an exited process",
500             ))
501         }
502
503         unsafe { self.handle.kill() }
504     }
505
506     /// Returns the OS-assigned process identifier associated with this child.
507     #[stable(feature = "process_id", since = "1.3.0")]
508     pub fn id(&self) -> u32 {
509         self.handle.id()
510     }
511
512     /// Waits for the child to exit completely, returning the status that it
513     /// exited with. This function will continue to have the same return value
514     /// after it has been called at least once.
515     ///
516     /// The stdin handle to the child process, if any, will be closed
517     /// before waiting. This helps avoid deadlock: it ensures that the
518     /// child does not block waiting for input from the parent, while
519     /// the parent waits for the child to exit.
520     #[stable(feature = "process", since = "1.0.0")]
521     pub fn wait(&mut self) -> io::Result<ExitStatus> {
522         drop(self.stdin.take());
523         match self.status {
524             Some(code) => Ok(ExitStatus(code)),
525             None => {
526                 let status = try!(self.handle.wait());
527                 self.status = Some(status);
528                 Ok(ExitStatus(status))
529             }
530         }
531     }
532
533     /// Simultaneously waits for the child to exit and collect all remaining
534     /// output on the stdout/stderr handles, returning a `Output`
535     /// instance.
536     ///
537     /// The stdin handle to the child process, if any, will be closed
538     /// before waiting. This helps avoid deadlock: it ensures that the
539     /// child does not block waiting for input from the parent, while
540     /// the parent waits for the child to exit.
541     #[stable(feature = "process", since = "1.0.0")]
542     pub fn wait_with_output(mut self) -> io::Result<Output> {
543         drop(self.stdin.take());
544         fn read<R>(mut input: R) -> JoinHandle<io::Result<Vec<u8>>>
545             where R: Read + Send + 'static
546         {
547             thread::spawn(move || {
548                 let mut ret = Vec::new();
549                 input.read_to_end(&mut ret).map(|_| ret)
550             })
551         }
552         let stdout = self.stdout.take().map(read);
553         let stderr = self.stderr.take().map(read);
554         let status = try!(self.wait());
555         let stdout = stdout.and_then(|t| t.join().unwrap().ok());
556         let stderr = stderr.and_then(|t| t.join().unwrap().ok());
557
558         Ok(Output {
559             status: status,
560             stdout: stdout.unwrap_or(Vec::new()),
561             stderr: stderr.unwrap_or(Vec::new()),
562         })
563     }
564 }
565
566 /// Terminates the current process with the specified exit code.
567 ///
568 /// This function will never return and will immediately terminate the current
569 /// process. The exit code is passed through to the underlying OS and will be
570 /// available for consumption by another process.
571 ///
572 /// Note that because this function never returns, and that it terminates the
573 /// process, no destructors on the current stack or any other thread's stack
574 /// will be run. If a clean shutdown is needed it is recommended to only call
575 /// this function at a known point where there are no more destructors left
576 /// to run.
577 #[stable(feature = "rust1", since = "1.0.0")]
578 pub fn exit(code: i32) -> ! {
579     ::sys_common::cleanup();
580     ::sys::os::exit(code)
581 }
582
583 #[cfg(test)]
584 mod tests {
585     use prelude::v1::*;
586     use io::prelude::*;
587
588     use io::ErrorKind;
589     use str;
590     use super::{Command, Output, Stdio};
591
592     // FIXME(#10380) these tests should not all be ignored on android.
593
594     #[cfg(not(target_os="android"))]
595     #[test]
596     fn smoke() {
597         let p = Command::new("true").spawn();
598         assert!(p.is_ok());
599         let mut p = p.unwrap();
600         assert!(p.wait().unwrap().success());
601     }
602
603     #[cfg(not(target_os="android"))]
604     #[test]
605     fn smoke_failure() {
606         match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
607             Ok(..) => panic!(),
608             Err(..) => {}
609         }
610     }
611
612     #[cfg(not(target_os="android"))]
613     #[test]
614     fn exit_reported_right() {
615         let p = Command::new("false").spawn();
616         assert!(p.is_ok());
617         let mut p = p.unwrap();
618         assert!(p.wait().unwrap().code() == Some(1));
619         drop(p.wait());
620     }
621
622     #[cfg(all(unix, not(target_os="android")))]
623     #[test]
624     fn signal_reported_right() {
625         use os::unix::process::ExitStatusExt;
626
627         let mut p = Command::new("/bin/sh")
628                             .arg("-c").arg("read a")
629                             .stdin(Stdio::piped())
630                             .spawn().unwrap();
631         p.kill().unwrap();
632         match p.wait().unwrap().signal() {
633             Some(9) => {},
634             result => panic!("not terminated by signal 9 (instead, {:?})",
635                              result),
636         }
637     }
638
639     pub fn run_output(mut cmd: Command) -> String {
640         let p = cmd.spawn();
641         assert!(p.is_ok());
642         let mut p = p.unwrap();
643         assert!(p.stdout.is_some());
644         let mut ret = String::new();
645         p.stdout.as_mut().unwrap().read_to_string(&mut ret).unwrap();
646         assert!(p.wait().unwrap().success());
647         return ret;
648     }
649
650     #[cfg(not(target_os="android"))]
651     #[test]
652     fn stdout_works() {
653         let mut cmd = Command::new("echo");
654         cmd.arg("foobar").stdout(Stdio::piped());
655         assert_eq!(run_output(cmd), "foobar\n");
656     }
657
658     #[cfg(all(unix, not(target_os="android")))]
659     #[test]
660     fn set_current_dir_works() {
661         let mut cmd = Command::new("/bin/sh");
662         cmd.arg("-c").arg("pwd")
663            .current_dir("/")
664            .stdout(Stdio::piped());
665         assert_eq!(run_output(cmd), "/\n");
666     }
667
668     #[cfg(all(unix, not(target_os="android")))]
669     #[test]
670     fn stdin_works() {
671         let mut p = Command::new("/bin/sh")
672                             .arg("-c").arg("read line; echo $line")
673                             .stdin(Stdio::piped())
674                             .stdout(Stdio::piped())
675                             .spawn().unwrap();
676         p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
677         drop(p.stdin.take());
678         let mut out = String::new();
679         p.stdout.as_mut().unwrap().read_to_string(&mut out).unwrap();
680         assert!(p.wait().unwrap().success());
681         assert_eq!(out, "foobar\n");
682     }
683
684
685     #[cfg(all(unix, not(target_os="android")))]
686     #[test]
687     fn uid_works() {
688         use os::unix::prelude::*;
689         use libc;
690         let mut p = Command::new("/bin/sh")
691                             .arg("-c").arg("true")
692                             .uid(unsafe { libc::getuid() })
693                             .gid(unsafe { libc::getgid() })
694                             .spawn().unwrap();
695         assert!(p.wait().unwrap().success());
696     }
697
698     #[cfg(all(unix, not(target_os="android")))]
699     #[test]
700     fn uid_to_root_fails() {
701         use os::unix::prelude::*;
702         use libc;
703
704         // if we're already root, this isn't a valid test. Most of the bots run
705         // as non-root though (android is an exception).
706         if unsafe { libc::getuid() == 0 } { return }
707         assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err());
708     }
709
710     #[cfg(not(target_os="android"))]
711     #[test]
712     fn test_process_status() {
713         let mut status = Command::new("false").status().unwrap();
714         assert!(status.code() == Some(1));
715
716         status = Command::new("true").status().unwrap();
717         assert!(status.success());
718     }
719
720     #[test]
721     fn test_process_output_fail_to_start() {
722         match Command::new("/no-binary-by-this-name-should-exist").output() {
723             Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound),
724             Ok(..) => panic!()
725         }
726     }
727
728     #[cfg(not(target_os="android"))]
729     #[test]
730     fn test_process_output_output() {
731         let Output {status, stdout, stderr}
732              = Command::new("echo").arg("hello").output().unwrap();
733         let output_str = str::from_utf8(&stdout).unwrap();
734
735         assert!(status.success());
736         assert_eq!(output_str.trim().to_string(), "hello");
737         assert_eq!(stderr, Vec::new());
738     }
739
740     #[cfg(not(target_os="android"))]
741     #[test]
742     fn test_process_output_error() {
743         let Output {status, stdout, stderr}
744              = Command::new("mkdir").arg(".").output().unwrap();
745
746         assert!(status.code() == Some(1));
747         assert_eq!(stdout, Vec::new());
748         assert!(!stderr.is_empty());
749     }
750
751     #[cfg(not(target_os="android"))]
752     #[test]
753     fn test_finish_once() {
754         let mut prog = Command::new("false").spawn().unwrap();
755         assert!(prog.wait().unwrap().code() == Some(1));
756     }
757
758     #[cfg(not(target_os="android"))]
759     #[test]
760     fn test_finish_twice() {
761         let mut prog = Command::new("false").spawn().unwrap();
762         assert!(prog.wait().unwrap().code() == Some(1));
763         assert!(prog.wait().unwrap().code() == Some(1));
764     }
765
766     #[cfg(not(target_os="android"))]
767     #[test]
768     fn test_wait_with_output_once() {
769         let prog = Command::new("echo").arg("hello").stdout(Stdio::piped())
770             .spawn().unwrap();
771         let Output {status, stdout, stderr} = prog.wait_with_output().unwrap();
772         let output_str = str::from_utf8(&stdout).unwrap();
773
774         assert!(status.success());
775         assert_eq!(output_str.trim().to_string(), "hello");
776         assert_eq!(stderr, Vec::new());
777     }
778
779     #[cfg(all(unix, not(target_os="android")))]
780     pub fn env_cmd() -> Command {
781         Command::new("env")
782     }
783     #[cfg(target_os="android")]
784     pub fn env_cmd() -> Command {
785         let mut cmd = Command::new("/system/bin/sh");
786         cmd.arg("-c").arg("set");
787         cmd
788     }
789
790     #[cfg(windows)]
791     pub fn env_cmd() -> Command {
792         let mut cmd = Command::new("cmd");
793         cmd.arg("/c").arg("set");
794         cmd
795     }
796
797     #[cfg(not(target_os="android"))]
798     #[test]
799     fn test_inherit_env() {
800         use env;
801
802         let result = env_cmd().output().unwrap();
803         let output = String::from_utf8(result.stdout).unwrap();
804
805         for (ref k, ref v) in env::vars() {
806             // Windows has hidden environment variables whose names start with
807             // equals signs (`=`). Those do not show up in the output of the
808             // `set` command.
809             assert!((cfg!(windows) && k.starts_with("=")) ||
810                     k.starts_with("DYLD") ||
811                     output.contains(&format!("{}={}", *k, *v)),
812                     "output doesn't contain `{}={}`\n{}",
813                     k, v, output);
814         }
815     }
816     #[cfg(target_os="android")]
817     #[test]
818     fn test_inherit_env() {
819         use env;
820
821         let mut result = env_cmd().output().unwrap();
822         let output = String::from_utf8(result.stdout).unwrap();
823
824         for (ref k, ref v) in env::vars() {
825             // don't check android RANDOM variables
826             if *k != "RANDOM".to_string() {
827                 assert!(output.contains(&format!("{}={}",
828                                                  *k,
829                                                  *v)) ||
830                         output.contains(&format!("{}=\'{}\'",
831                                                  *k,
832                                                  *v)));
833             }
834         }
835     }
836
837     #[test]
838     fn test_override_env() {
839         use env;
840
841         // In some build environments (such as chrooted Nix builds), `env` can
842         // only be found in the explicitly-provided PATH env variable, not in
843         // default places such as /bin or /usr/bin. So we need to pass through
844         // PATH to our sub-process.
845         let mut cmd = env_cmd();
846         cmd.env_clear().env("RUN_TEST_NEW_ENV", "123");
847         if let Some(p) = env::var_os("PATH") {
848             cmd.env("PATH", &p);
849         }
850         let result = cmd.output().unwrap();
851         let output = String::from_utf8_lossy(&result.stdout).to_string();
852
853         assert!(output.contains("RUN_TEST_NEW_ENV=123"),
854                 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
855     }
856
857     #[test]
858     fn test_add_to_env() {
859         let result = env_cmd().env("RUN_TEST_NEW_ENV", "123").output().unwrap();
860         let output = String::from_utf8_lossy(&result.stdout).to_string();
861
862         assert!(output.contains("RUN_TEST_NEW_ENV=123"),
863                 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
864     }
865 }