]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/process.rs
use exhaustive_patterns to be able to use `?`
[rust.git] / src / libstd / sys / redox / process.rs
1 use crate::env::{self, split_paths};
2 use crate::ffi::{CStr, OsStr};
3 use crate::fmt;
4 use crate::fs::File;
5 use crate::io::{self, prelude::*, BufReader, Error, ErrorKind, SeekFrom};
6 use crate::os::unix::ffi::OsStrExt;
7 use crate::path::{Path, PathBuf};
8 use crate::ptr;
9 use crate::sys::ext::fs::MetadataExt;
10 use crate::sys::ext::io::AsRawFd;
11 use crate::sys::fd::FileDesc;
12 use crate::sys::fs::{File as SysFile, OpenOptions};
13 use crate::sys::os::{ENV_LOCK, environ};
14 use crate::sys::pipe::{self, AnonPipe};
15 use crate::sys::{cvt, syscall};
16 use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
17
18 use libc::{EXIT_SUCCESS, EXIT_FAILURE};
19
20 ////////////////////////////////////////////////////////////////////////////////
21 // Command
22 ////////////////////////////////////////////////////////////////////////////////
23
24 pub struct Command {
25     // Currently we try hard to ensure that the call to `.exec()` doesn't
26     // actually allocate any memory. While many platforms try to ensure that
27     // memory allocation works after a fork in a multithreaded process, it's
28     // been observed to be buggy and somewhat unreliable, so we do our best to
29     // just not do it at all!
30     //
31     // Along those lines, the `argv` and `envp` raw pointers here are exactly
32     // what's gonna get passed to `execvp`. The `argv` array starts with the
33     // `program` and ends with a NULL, and the `envp` pointer, if present, is
34     // also null-terminated.
35     //
36     // Right now we don't support removing arguments, so there's no much fancy
37     // support there, but we support adding and removing environment variables,
38     // so a side table is used to track where in the `envp` array each key is
39     // located. Whenever we add a key we update it in place if it's already
40     // present, and whenever we remove a key we update the locations of all
41     // other keys.
42     program: String,
43     args: Vec<String>,
44     env: CommandEnv<DefaultEnvKey>,
45
46     cwd: Option<String>,
47     uid: Option<u32>,
48     gid: Option<u32>,
49     saw_nul: bool,
50     closures: Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>>,
51     stdin: Option<Stdio>,
52     stdout: Option<Stdio>,
53     stderr: Option<Stdio>,
54 }
55
56 // passed back to std::process with the pipes connected to the child, if any
57 // were requested
58 pub struct StdioPipes {
59     pub stdin: Option<AnonPipe>,
60     pub stdout: Option<AnonPipe>,
61     pub stderr: Option<AnonPipe>,
62 }
63
64 // passed to do_exec() with configuration of what the child stdio should look
65 // like
66 struct ChildPipes {
67     stdin: ChildStdio,
68     stdout: ChildStdio,
69     stderr: ChildStdio,
70 }
71
72 enum ChildStdio {
73     Inherit,
74     Explicit(usize),
75     Owned(FileDesc),
76 }
77
78 pub enum Stdio {
79     Inherit,
80     Null,
81     MakePipe,
82     Fd(FileDesc),
83 }
84
85 impl Command {
86     pub fn new(program: &OsStr) -> Command {
87         Command {
88             program: program.to_str().unwrap().to_owned(),
89             args: Vec::new(),
90             env: Default::default(),
91             cwd: None,
92             uid: None,
93             gid: None,
94             saw_nul: false,
95             closures: Vec::new(),
96             stdin: None,
97             stdout: None,
98             stderr: None,
99         }
100     }
101
102     pub fn arg(&mut self, arg: &OsStr) {
103         self.args.push(arg.to_str().unwrap().to_owned());
104     }
105
106     pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
107         &mut self.env
108     }
109
110     pub fn cwd(&mut self, dir: &OsStr) {
111         self.cwd = Some(dir.to_str().unwrap().to_owned());
112     }
113     pub fn uid(&mut self, id: u32) {
114         self.uid = Some(id);
115     }
116     pub fn gid(&mut self, id: u32) {
117         self.gid = Some(id);
118     }
119
120     pub unsafe fn pre_exec(
121         &mut self,
122         f: Box<dyn FnMut() -> io::Result<()> + Send + Sync>,
123     ) {
124         self.closures.push(f);
125     }
126
127     pub fn stdin(&mut self, stdin: Stdio) {
128         self.stdin = Some(stdin);
129     }
130     pub fn stdout(&mut self, stdout: Stdio) {
131         self.stdout = Some(stdout);
132     }
133     pub fn stderr(&mut self, stderr: Stdio) {
134         self.stderr = Some(stderr);
135     }
136
137     pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
138                  -> io::Result<(Process, StdioPipes)> {
139          const CLOEXEC_MSG_FOOTER: &[u8] = b"NOEX";
140
141          if self.saw_nul {
142              return Err(io::Error::new(ErrorKind::InvalidInput,
143                                        "nul byte found in provided data"));
144          }
145
146          let (ours, theirs) = self.setup_io(default, needs_stdin)?;
147          let (input, output) = pipe::anon_pipe()?;
148
149          let pid = unsafe {
150              match cvt(syscall::clone(0))? {
151                  0 => {
152                      drop(input);
153                      let Err(err) = self.do_exec(theirs);
154                      let errno = err.raw_os_error().unwrap_or(syscall::EINVAL) as u32;
155                      let bytes = [
156                          (errno >> 24) as u8,
157                          (errno >> 16) as u8,
158                          (errno >>  8) as u8,
159                          (errno >>  0) as u8,
160                          CLOEXEC_MSG_FOOTER[0], CLOEXEC_MSG_FOOTER[1],
161                          CLOEXEC_MSG_FOOTER[2], CLOEXEC_MSG_FOOTER[3]
162                      ];
163                      // pipe I/O up to PIPE_BUF bytes should be atomic, and then
164                      // we want to be sure we *don't* run at_exit destructors as
165                      // we're being torn down regardless
166                      assert!(output.write(&bytes).is_ok());
167                      let _ = syscall::exit(1);
168                      panic!("failed to exit");
169                  }
170                  n => n,
171              }
172          };
173
174          let mut p = Process { pid: pid, status: None };
175          drop(output);
176          let mut bytes = [0; 8];
177
178          // loop to handle EINTR
179          loop {
180              match input.read(&mut bytes) {
181                  Ok(0) => return Ok((p, ours)),
182                  Ok(8) => {
183                      assert!(combine(CLOEXEC_MSG_FOOTER) == combine(&bytes[4.. 8]),
184                              "Validation on the CLOEXEC pipe failed: {:?}", bytes);
185                      let errno = combine(&bytes[0.. 4]);
186                      assert!(p.wait().is_ok(),
187                              "wait() should either return Ok or panic");
188                      return Err(Error::from_raw_os_error(errno))
189                  }
190                  Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
191                  Err(e) => {
192                      assert!(p.wait().is_ok(),
193                              "wait() should either return Ok or panic");
194                      panic!("the CLOEXEC pipe failed: {:?}", e)
195                  },
196                  Ok(..) => { // pipe I/O up to PIPE_BUF bytes should be atomic
197                      assert!(p.wait().is_ok(),
198                              "wait() should either return Ok or panic");
199                      panic!("short read on the CLOEXEC pipe")
200                  }
201              }
202          }
203
204          fn combine(arr: &[u8]) -> i32 {
205              let a = arr[0] as u32;
206              let b = arr[1] as u32;
207              let c = arr[2] as u32;
208              let d = arr[3] as u32;
209
210              ((a << 24) | (b << 16) | (c << 8) | (d << 0)) as i32
211          }
212     }
213
214     pub fn exec(&mut self, default: Stdio) -> io::Error {
215         if self.saw_nul {
216             return io::Error::new(ErrorKind::InvalidInput,
217                                   "nul byte found in provided data")
218         }
219
220         match self.setup_io(default, true) {
221             Ok((_, theirs)) => unsafe {
222                 let Err(e) = self.do_exec(theirs);
223                 e
224             },
225             Err(e) => e,
226         }
227     }
228
229     // And at this point we've reached a special time in the life of the
230     // child. The child must now be considered hamstrung and unable to
231     // do anything other than syscalls really. Consider the following
232     // scenario:
233     //
234     //      1. Thread A of process 1 grabs the malloc() mutex
235     //      2. Thread B of process 1 forks(), creating thread C
236     //      3. Thread C of process 2 then attempts to malloc()
237     //      4. The memory of process 2 is the same as the memory of
238     //         process 1, so the mutex is locked.
239     //
240     // This situation looks a lot like deadlock, right? It turns out
241     // that this is what pthread_atfork() takes care of, which is
242     // presumably implemented across platforms. The first thing that
243     // threads to *before* forking is to do things like grab the malloc
244     // mutex, and then after the fork they unlock it.
245     //
246     // Despite this information, libnative's spawn has been witnessed to
247     // deadlock on both macOS and FreeBSD. I'm not entirely sure why, but
248     // all collected backtraces point at malloc/free traffic in the
249     // child spawned process.
250     //
251     // For this reason, the block of code below should contain 0
252     // invocations of either malloc of free (or their related friends).
253     //
254     // As an example of not having malloc/free traffic, we don't close
255     // this file descriptor by dropping the FileDesc (which contains an
256     // allocation). Instead we just close it manually. This will never
257     // have the drop glue anyway because this code never returns (the
258     // child will either exec() or invoke syscall::exit)
259     unsafe fn do_exec(&mut self, stdio: ChildPipes) -> Result<!, io::Error> {
260         if let Some(fd) = stdio.stderr.fd() {
261             cvt(syscall::dup2(fd, 2, &[]))?;
262             let mut flags = cvt(syscall::fcntl(2, syscall::F_GETFD, 0))?;
263             flags &= ! syscall::O_CLOEXEC;
264             cvt(syscall::fcntl(2, syscall::F_SETFD, flags))?;
265         }
266         if let Some(fd) = stdio.stdout.fd() {
267             cvt(syscall::dup2(fd, 1, &[]))?;
268             let mut flags = cvt(syscall::fcntl(1, syscall::F_GETFD, 0))?;
269             flags &= ! syscall::O_CLOEXEC;
270             cvt(syscall::fcntl(1, syscall::F_SETFD, flags))?;
271         }
272         if let Some(fd) = stdio.stdin.fd() {
273             cvt(syscall::dup2(fd, 0, &[]))?;
274             let mut flags = cvt(syscall::fcntl(0, syscall::F_GETFD, 0))?;
275             flags &= ! syscall::O_CLOEXEC;
276             cvt(syscall::fcntl(0, syscall::F_SETFD, flags))?;
277         }
278
279         if let Some(g) = self.gid {
280             cvt(syscall::setregid(g as usize, g as usize))?;
281         }
282         if let Some(u) = self.uid {
283             cvt(syscall::setreuid(u as usize, u as usize))?;
284         }
285         if let Some(ref cwd) = self.cwd {
286             cvt(syscall::chdir(cwd))?;
287         }
288
289         for callback in self.closures.iter_mut() {
290             callback()?;
291         }
292
293         self.env.apply();
294
295         let program = if self.program.contains(':') || self.program.contains('/') {
296             Some(PathBuf::from(&self.program))
297         } else if let Ok(path_env) = env::var("PATH") {
298             let mut program = None;
299             for mut path in split_paths(&path_env) {
300                 path.push(&self.program);
301                 if path.exists() {
302                     program = Some(path);
303                     break;
304                 }
305             }
306             program
307         } else {
308             None
309         };
310
311         let mut file = if let Some(program) = program {
312             File::open(program.as_os_str())?
313         } else {
314             return Err(io::Error::from_raw_os_error(syscall::ENOENT));
315         };
316
317         // Push all the arguments
318         let mut args: Vec<[usize; 2]> = Vec::with_capacity(1 + self.args.len());
319
320         let interpreter = {
321             let mut reader = BufReader::new(&file);
322
323             let mut shebang = [0; 2];
324             let mut read = 0;
325             loop {
326                 match reader.read(&mut shebang[read..])? {
327                     0 => break,
328                     n => read += n,
329                 }
330             }
331
332             if &shebang == b"#!" {
333                 // This is an interpreted script.
334                 // First of all, since we'll be passing another file to
335                 // fexec(), we need to manually check that we have permission
336                 // to execute this file:
337                 let uid = cvt(syscall::getuid())?;
338                 let gid = cvt(syscall::getgid())?;
339                 let meta = file.metadata()?;
340
341                 let mode = if uid == meta.uid() as usize {
342                     meta.mode() >> 3*2 & 0o7
343                 } else if gid == meta.gid() as usize {
344                     meta.mode() >> 3*1 & 0o7
345                 } else {
346                     meta.mode() & 0o7
347                 };
348                 if mode & 1 == 0 {
349                     return Err(io::Error::from_raw_os_error(syscall::EPERM));
350                 }
351
352                 // Second of all, we need to actually read which interpreter it wants
353                 let mut interpreter = Vec::new();
354                 reader.read_until(b'\n', &mut interpreter)?;
355                 // Pop one trailing newline, if any
356                 if interpreter.ends_with(&[b'\n']) {
357                     interpreter.pop().unwrap();
358                 }
359
360                 // FIXME: Here we could just reassign `file` directly, if it
361                 // wasn't for lexical lifetimes. Remove the whole `let
362                 // interpreter = { ... };` hack once NLL lands.
363                 // NOTE: Although DO REMEMBER to make sure the interpreter path
364                 // still lives long enough to reach fexec.
365                 Some(interpreter)
366             } else {
367                 None
368             }
369         };
370         if let Some(ref interpreter) = interpreter {
371             let path: &OsStr = OsStr::from_bytes(&interpreter);
372             file = File::open(path)?;
373
374             args.push([interpreter.as_ptr() as usize, interpreter.len()]);
375         } else {
376             file.seek(SeekFrom::Start(0))?;
377         }
378
379         args.push([self.program.as_ptr() as usize, self.program.len()]);
380         args.extend(self.args.iter().map(|arg| [arg.as_ptr() as usize, arg.len()]));
381
382         // Push all the variables
383         let mut vars: Vec<[usize; 2]> = Vec::new();
384         {
385             let _guard = ENV_LOCK.lock();
386             let mut environ = *environ();
387             while *environ != ptr::null() {
388                 let var = CStr::from_ptr(*environ).to_bytes();
389                 vars.push([var.as_ptr() as usize, var.len()]);
390                 environ = environ.offset(1);
391             }
392         }
393
394         if let Err(err) = syscall::fexec(file.as_raw_fd(), &args, &vars) {
395             Err(io::Error::from_raw_os_error(err.errno as i32))
396         } else {
397             panic!("return from exec without err");
398         }
399     }
400
401     fn setup_io(&self, default: Stdio, needs_stdin: bool)
402                 -> io::Result<(StdioPipes, ChildPipes)> {
403         let null = Stdio::Null;
404         let default_stdin = if needs_stdin {&default} else {&null};
405         let stdin = self.stdin.as_ref().unwrap_or(default_stdin);
406         let stdout = self.stdout.as_ref().unwrap_or(&default);
407         let stderr = self.stderr.as_ref().unwrap_or(&default);
408         let (their_stdin, our_stdin) = stdin.to_child_stdio(true)?;
409         let (their_stdout, our_stdout) = stdout.to_child_stdio(false)?;
410         let (their_stderr, our_stderr) = stderr.to_child_stdio(false)?;
411         let ours = StdioPipes {
412             stdin: our_stdin,
413             stdout: our_stdout,
414             stderr: our_stderr,
415         };
416         let theirs = ChildPipes {
417             stdin: their_stdin,
418             stdout: their_stdout,
419             stderr: their_stderr,
420         };
421         Ok((ours, theirs))
422     }
423 }
424
425 impl Stdio {
426     fn to_child_stdio(&self, readable: bool)
427                       -> io::Result<(ChildStdio, Option<AnonPipe>)> {
428         match *self {
429             Stdio::Inherit => Ok((ChildStdio::Inherit, None)),
430
431             // Make sure that the source descriptors are not an stdio
432             // descriptor, otherwise the order which we set the child's
433             // descriptors may blow away a descriptor which we are hoping to
434             // save. For example, suppose we want the child's stderr to be the
435             // parent's stdout, and the child's stdout to be the parent's
436             // stderr. No matter which we dup first, the second will get
437             // overwritten prematurely.
438             Stdio::Fd(ref fd) => {
439                 if fd.raw() <= 2 {
440                     Ok((ChildStdio::Owned(fd.duplicate()?), None))
441                 } else {
442                     Ok((ChildStdio::Explicit(fd.raw()), None))
443                 }
444             }
445
446             Stdio::MakePipe => {
447                 let (reader, writer) = pipe::anon_pipe()?;
448                 let (ours, theirs) = if readable {
449                     (writer, reader)
450                 } else {
451                     (reader, writer)
452                 };
453                 Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours)))
454             }
455
456             Stdio::Null => {
457                 let mut opts = OpenOptions::new();
458                 opts.read(readable);
459                 opts.write(!readable);
460                 let fd = SysFile::open(Path::new("null:"), &opts)?;
461                 Ok((ChildStdio::Owned(fd.into_fd()), None))
462             }
463         }
464     }
465 }
466
467 impl From<AnonPipe> for Stdio {
468     fn from(pipe: AnonPipe) -> Stdio {
469         Stdio::Fd(pipe.into_fd())
470     }
471 }
472
473 impl From<SysFile> for Stdio {
474     fn from(file: SysFile) -> Stdio {
475         Stdio::Fd(file.into_fd())
476     }
477 }
478
479 impl ChildStdio {
480     fn fd(&self) -> Option<usize> {
481         match *self {
482             ChildStdio::Inherit => None,
483             ChildStdio::Explicit(fd) => Some(fd),
484             ChildStdio::Owned(ref fd) => Some(fd.raw()),
485         }
486     }
487 }
488
489 impl fmt::Debug for Command {
490     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
491         write!(f, "{:?}", self.program)?;
492         for arg in &self.args {
493             write!(f, " {:?}", arg)?;
494         }
495         Ok(())
496     }
497 }
498
499 ////////////////////////////////////////////////////////////////////////////////
500 // Processes
501 ////////////////////////////////////////////////////////////////////////////////
502
503 /// Unix exit statuses
504 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
505 pub struct ExitStatus(i32);
506
507 impl ExitStatus {
508     fn exited(&self) -> bool {
509         self.0 & 0x7F == 0
510     }
511
512     pub fn success(&self) -> bool {
513         self.code() == Some(0)
514     }
515
516     pub fn code(&self) -> Option<i32> {
517         if self.exited() {
518             Some((self.0 >> 8) & 0xFF)
519         } else {
520             None
521         }
522     }
523
524     pub fn signal(&self) -> Option<i32> {
525         if !self.exited() {
526             Some(self.0 & 0x7F)
527         } else {
528             None
529         }
530     }
531 }
532
533 impl From<i32> for ExitStatus {
534     fn from(a: i32) -> ExitStatus {
535         ExitStatus(a)
536     }
537 }
538
539 impl fmt::Display for ExitStatus {
540     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
541         if let Some(code) = self.code() {
542             write!(f, "exit code: {}", code)
543         } else {
544             let signal = self.signal().unwrap();
545             write!(f, "signal: {}", signal)
546         }
547     }
548 }
549
550 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
551 pub struct ExitCode(u8);
552
553 impl ExitCode {
554     pub const SUCCESS: ExitCode = ExitCode(EXIT_SUCCESS as _);
555     pub const FAILURE: ExitCode = ExitCode(EXIT_FAILURE as _);
556
557     pub fn as_i32(&self) -> i32 {
558         self.0 as i32
559     }
560 }
561
562 /// The unique ID of the process (this should never be negative).
563 pub struct Process {
564     pid: usize,
565     status: Option<ExitStatus>,
566 }
567
568 impl Process {
569     pub fn id(&self) -> u32 {
570         self.pid as u32
571     }
572
573     pub fn kill(&mut self) -> io::Result<()> {
574         // If we've already waited on this process then the pid can be recycled
575         // and used for another process, and we probably shouldn't be killing
576         // random processes, so just return an error.
577         if self.status.is_some() {
578             Err(Error::new(ErrorKind::InvalidInput,
579                            "invalid argument: can't kill an exited process"))
580         } else {
581             cvt(syscall::kill(self.pid, syscall::SIGKILL))?;
582             Ok(())
583         }
584     }
585
586     pub fn wait(&mut self) -> io::Result<ExitStatus> {
587         if let Some(status) = self.status {
588             return Ok(status)
589         }
590         let mut status = 0;
591         cvt(syscall::waitpid(self.pid, &mut status, 0))?;
592         self.status = Some(ExitStatus(status as i32));
593         Ok(ExitStatus(status as i32))
594     }
595
596     pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
597         if let Some(status) = self.status {
598             return Ok(Some(status))
599         }
600         let mut status = 0;
601         let pid = cvt(syscall::waitpid(self.pid, &mut status, syscall::WNOHANG))?;
602         if pid == 0 {
603             Ok(None)
604         } else {
605             self.status = Some(ExitStatus(status as i32));
606             Ok(Some(ExitStatus(status as i32)))
607         }
608     }
609 }