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