Skip to main content

async_process/
unix.rs

1//! Unix-specific extensions.
2
3use std::ffi::OsStr;
4use std::io;
5use std::os::unix::process::CommandExt as _;
6
7use cfg_if::cfg_if;
8
9use crate::Command;
10
11cfg_if! {
12    if #[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita"))] {
13        type UserId = u16;
14        type GroupId = u16;
15    } else if #[cfg(any(target_os = "nto", target_os = "qnx"))] {
16        // Both IDs are signed, see `sys/target_nto.h` of the QNX Neutrino SDP.
17        // Only positive values should be used, see e.g.
18        // https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/s/setuid.html
19        type UserId = i32;
20        type GroupId = i32;
21    } else {
22        type UserId = u32;
23        type GroupId = u32;
24    }
25}
26
27/// Unix-specific extensions to the [`Command`] builder.
28///
29/// This trait is sealed: it cannot be implemented outside `async-process`.
30/// This is so that future additional methods are not breaking changes.
31pub trait CommandExt: crate::sealed::Sealed {
32    /// Sets the child process's user ID. This translates to a
33    /// `setuid` call in the child process. Failure in the `setuid`
34    /// call will cause the spawn to fail.
35    fn uid(&mut self, id: UserId) -> &mut Command;
36
37    /// Similar to `uid`, but sets the group ID of the child process. This has
38    /// the same semantics as the `uid` field.
39    fn gid(&mut self, id: GroupId) -> &mut Command;
40
41    /// Performs all the required setup by this `Command`, followed by calling
42    /// the `execvp` syscall.
43    ///
44    /// On success this function will not return, and otherwise it will return
45    /// an error indicating why the exec (or another part of the setup of the
46    /// `Command`) failed.
47    ///
48    /// `exec` not returning has the same implications as calling
49    /// [`std::process::exit`] – no destructors on the current stack or any other
50    /// thread’s stack will be run. Therefore, it is recommended to only call
51    /// `exec` at a point where it is fine to not run any destructors. Note,
52    /// that the `execvp` syscall independently guarantees that all memory is
53    /// freed and all file descriptors with the `CLOEXEC` option (set by default
54    /// on all file descriptors opened by the standard library) are closed.
55    ///
56    /// This function, unlike `spawn`, will **not** `fork` the process to create
57    /// a new child. Like spawn, however, the default behavior for the stdio
58    /// descriptors will be to inherited from the current process.
59    ///
60    /// # Notes
61    ///
62    /// The process may be in a "broken state" if this function returns in
63    /// error. For example the working directory, environment variables, signal
64    /// handling settings, various user/group information, or aspects of stdio
65    /// file descriptors may have changed. If a "transactional spawn" is
66    /// required to gracefully handle errors it is recommended to use the
67    /// cross-platform `spawn` instead.
68    fn exec(&mut self) -> io::Error;
69
70    /// Set executable argument
71    ///
72    /// Set the first process argument, `argv[0]`, to something other than the
73    /// default executable path.
74    fn arg0<S>(&mut self, arg: S) -> &mut Command
75    where
76        S: AsRef<OsStr>;
77}
78
79impl crate::sealed::Sealed for Command {}
80impl CommandExt for Command {
81    fn uid(&mut self, id: UserId) -> &mut Command {
82        self.inner.uid(id);
83        self
84    }
85
86    fn gid(&mut self, id: GroupId) -> &mut Command {
87        self.inner.gid(id);
88        self
89    }
90
91    fn exec(&mut self) -> io::Error {
92        self.inner.exec()
93    }
94
95    fn arg0<S>(&mut self, arg: S) -> &mut Command
96    where
97        S: AsRef<OsStr>,
98    {
99        self.inner.arg0(arg);
100        self
101    }
102}