@@ -35,18 +35,19 @@ needed to support the SHIM functionality
3535Co-authored-by: Ruben De Smet <ruben.de.smet@rubdos.be>
3636Signed-off-by: David Greaves <david.greaves@jolla.com>
3737Signed-off-by: Ruben De Smet <ruben.de.smet@rubdos.be>
38+ Signed-off-by: Matti Viljanen <matti.viljanen@kapsi.fi>
3839---
39- .../src/ sys/unix/process/process_common.rs | 55 ++++++-
40- .../std/ src/sys/unix/process/process_unix.rs | 137 ++++++++++++++++--
40+ .../sys/pal /unix/process/process_common.rs | 55 ++++++-
41+ .../src/sys/pal /unix/process/process_unix.rs | 137 ++++++++++++++++--
4142 2 files changed, 172 insertions(+), 20 deletions(-)
4243
43- diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs
44- index bac32d9e60e..3cd9767da88 100644
45- --- a/library/std/src/sys/unix/process/process_common.rs
46- +++ b/library/std/src/sys/unix/process/process_common.rs
47- @@ -85,7 +85,7 @@ pub unsafe fn sigaddset(set: *mut libc::sigset_t, signum: libc::c_int) -> libc::
44+ diff --git a/library/std/src/sys/pal/ unix/process/process_common.rs b/library/std/src/sys/pal /unix/process/process_common.rs
45+ index f615e8086..7e90aa40f 100644
46+ --- a/library/std/src/sys/pal/ unix/process/process_common.rs
47+ +++ b/library/std/src/sys/pal/ unix/process/process_common.rs
48+ @@ -85,7 +85,7 @@ cfg_if::cfg_if! {
4849 ////////////////////////////////////////////////////////////////////////////////
49-
50+
5051 pub struct Command {
5152- program: CString,
5253+ pub(crate) program: CString,
@@ -64,13 +65,13 @@ index bac32d9e60e..3cd9767da88 100644
6465+ pub(crate) setuid: Option<SetuidFn>,
6566+ pub(crate) setgid: Option<SetgidFn>,
6667+ pub(crate) setgroups: Option<SetgroupsFn>,
67-
68+
6869 program_kind: ProgramKind,
6970 cwd: Option<CString>,
7071@@ -110,6 +117,14 @@ pub struct Command {
7172 pgroup: Option<pid_t>,
7273 }
73-
74+
7475+ pub(crate) type ExecvpFn = fn(*const c_char, *const *const c_char) -> c_int;
7576+ pub(crate) type Dup2Fn = fn(c_int, c_int) -> c_int;
7677+ pub(crate) type CloseFn = fn(c_int) -> c_int;
@@ -81,8 +82,8 @@ index bac32d9e60e..3cd9767da88 100644
8182+
8283 // Create a new type for argv, so that we can make it `Send` and `Sync`
8384 struct Argv(Vec<*const c_char>);
84-
85- @@ -183,16 +198,24 @@ pub fn new(program: &OsStr) -> Command {
85+
86+ @@ -183,16 +198,24 @@ impl Command {
8687 let mut saw_nul = false;
8788 let program_kind = ProgramKind::new(program.as_ref());
8889 let program = os2c(program, &mut saw_nul);
@@ -110,7 +111,7 @@ index bac32d9e60e..3cd9767da88 100644
110111 closures: Vec::new(),
111112 groups: None,
112113 stdin: None,
113- @@ -207,16 +230,24 @@ pub fn new(program: &OsStr) -> Command {
114+ @@ -207,16 +230,24 @@ impl Command {
114115 let mut saw_nul = false;
115116 let program_kind = ProgramKind::new(program.as_ref());
116117 let program = os2c(program, &mut saw_nul);
@@ -138,10 +139,10 @@ index bac32d9e60e..3cd9767da88 100644
138139 closures: Vec::new(),
139140 groups: None,
140141 stdin: None,
141- @@ -227,6 +258,16 @@ pub fn new(program: &OsStr) -> Command {
142+ @@ -227,6 +258,16 @@ impl Command {
142143 }
143144 }
144-
145+
145146+ // This allows process_unix::{spawn, exec} to push program to the
146147+ // start of /usr/bin/env's arg list
147148+ pub fn insert_program(&mut self, arg: String) {
@@ -155,34 +156,34 @@ index bac32d9e60e..3cd9767da88 100644
155156 pub fn set_arg_0(&mut self, arg: &OsStr) {
156157 // Set a new arg0
157158 let arg = os2c(arg, &mut self.saw_nul);
158- diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs
159- index 72aca4e6659..495368b58ec 100644
160- --- a/library/std/src/sys/unix/process/process_unix.rs
161- +++ b/library/std/src/sys/unix/process/process_unix.rs
162- @@ -25 ,7 +25 ,7 @@
159+ diff --git a/library/std/src/sys/pal/ unix/process/process_unix.rs b/library/std/src/sys/pal /unix/process/process_unix.rs
160+ index d65657790..9b2f68b8c 100644
161+ --- a/library/std/src/sys/pal/ unix/process/process_unix.rs
162+ +++ b/library/std/src/sys/pal/ unix/process/process_unix.rs
163+ @@ -15 ,7 +15 ,7 @@ use crate::os::unix::io::AsRawFd;
163164 use libc::RTP_ID as pid_t;
164-
165+
165166 #[cfg(not(target_os = "vxworks"))]
166167- use libc::{c_int, pid_t};
167168+ use libc::{c_char, c_int, dlsym, pid_t};
168-
169+
169170 #[cfg(not(any(
170171 target_os = "vxworks",
171- @@ -62 ,6 +62 ,10 @@ fn get_clock_resolution() -> Duration {
172+ @@ -52 ,6 +52 ,10 @@ cfg_if::cfg_if! {
172173 }
173174 }
174-
175+
175176+ use crate::ffi::OsString;
176177+ use crate::intrinsics::transmute;
177178+ use sys::os::getenv;
178179+
179180 ////////////////////////////////////////////////////////////////////////////////
180181 // Command
181182 ////////////////////////////////////////////////////////////////////////////////
182- @@ -95 ,6 +99 ,65 @@ pub fn spawn(
183+ @@ -85 ,6 +89 ,65 @@ impl Command {
183184 #[cfg(not(target_os = "linux"))]
184185 let (input, output) = sys::pipe::anon_pipe()?;
185-
186+
186187+ // If there is a RUST_EXEC_SHIM (could be "/usr/bin/env --")
187188+ // then we're probably going to directly execvp it via dlsym
188189+ // to avoid issues with threads and malloc post-fork and
@@ -245,7 +246,7 @@ index 72aca4e6659..495368b58ec 100644
245246 // Whatever happens after the fork is almost for sure going to touch or
246247 // look at the environment in one way or another (PATH in `execvp` or
247248 // accessing the `environ` pointer ourselves). Make sure no other thread
248- @@ -111 ,7 +174 ,7 @@ pub fn spawn(
249+ @@ -101 ,7 +164 ,7 @@ impl Command {
249250 if pid == 0 {
250251 crate::panic::always_abort();
251252 mem::forget(env_lock); // avoid non-async-signal-safe unlocking
@@ -254,7 +255,7 @@ index 72aca4e6659..495368b58ec 100644
254255 #[cfg(target_os = "linux")]
255256 if self.get_create_pidfd() {
256257 self.send_pidfd(&output);
257- @@ -268 ,7 +331 ,47 @@ pub fn exec(&mut self, default: Stdio) -> io::Error {
258+ @@ -257 ,7 +320 ,47 @@ impl Command {
258259 Err(e) => e,
259260 }
260261 }
@@ -303,9 +304,9 @@ index 72aca4e6659..495368b58ec 100644
303304 // And at this point we've reached a special time in the life of the
304305 // child. The child must now be considered hamstrung and unable to
305306 // do anything other than syscalls really. Consider the following
306- @@ -308 ,13 +411 ,13 @@ unsafe fn do_exec(
307+ @@ -297 ,13 +400 ,13 @@ impl Command {
307308 use crate::sys::{self, cvt_r};
308-
309+
309310 if let Some(fd) = stdio.stdin.fd() {
310311- cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
311312+ cvt_r(|| self.unwrap_dup2(fd, libc::STDIN_FILENO))?;
@@ -318,9 +319,9 @@ index 72aca4e6659..495368b58ec 100644
318319- cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
319320+ cvt_r(|| self.unwrap_dup2(fd, libc::STDERR_FILENO))?;
320321 }
321-
322+
322323 #[cfg(not(target_os = "l4re"))]
323- @@ -322 ,10 +425 ,10 @@ unsafe fn do_exec(
324+ @@ -311 ,10 +414 ,10 @@ impl Command {
324325 if let Some(_g) = self.get_groups() {
325326 //FIXME: Redox kernel does not support setgroups yet
326327 #[cfg(not(target_os = "redox"))]
@@ -333,12 +334,18 @@ index 72aca4e6659..495368b58ec 100644
333334 }
334335 if let Some(u) = self.get_uid() {
335336 // When dropping privileges from root, the `setgroups` call
336- @@ -337,13 +440,13 @@ unsafe fn do_exec(
337+ @@ -326,7 +429,7 @@ impl Command {
337338 //FIXME: Redox kernel does not support setgroups yet
338339 #[cfg(not(target_os = "redox"))]
339- if libc::getuid() == 0 && self.get_groups().is_none() {
340- - cvt(libc::setgroups(0, crate::ptr::null()))?;
341- + cvt(self.unwrap_setgroups(0, crate::ptr::null()))?;
340+ if self.get_groups().is_none() {
341+ - let res = cvt(libc::setgroups(0, crate::ptr::null()));
342+ + let res = cvt(self.unwrap_setgroups(0, crate::ptr::null()));
343+ if let Err(e) = res {
344+ // Here we ignore the case of not having CAP_SETGID.
345+ // An alternative would be to require CAP_SETGID (in
346+ @@ -336,11 +439,11 @@ impl Command {
347+ }
348+ }
342349 }
343350- cvt(libc::setuid(u as uid_t))?;
344351+ cvt(self.unwrap_setuid(u as uid_t))?;
@@ -348,9 +355,9 @@ index 72aca4e6659..495368b58ec 100644
348355- cvt(libc::chdir(cwd.as_ptr()))?;
349356+ cvt(self.unwrap_chdir(cwd.as_ptr()))?;
350357 }
351-
358+
352359 if let Some(pgroup) = self.get_pgroup() {
353- @@ -408 ,8 +511 ,12 @@ fn drop(&mut self) {
360+ @@ -405 ,8 +508 ,12 @@ impl Command {
354361 _reset = Some(Reset(*sys::os::environ()));
355362 *sys::os::environ() = envp.as_ptr();
356363 }
@@ -364,19 +371,19 @@ index 72aca4e6659..495368b58ec 100644
364371+ };
365372 Err(io::Error::last_os_error())
366373 }
367-
368- @@ -436 ,6 +543 ,7 @@ fn posix_spawn(
374+
375+ @@ -433 ,6 +540 ,7 @@ impl Command {
369376 _: &ChildPipes,
370377 _: Option<&CStringArray>,
371378 ) -> io::Result<Option<Process>> {
372379+ eprintln!("process_unix:270: in null posix_spawn");
373380 Ok(None)
374381 }
375-
376- @@ -459 ,12 +567 ,15 @@ fn posix_spawn(
377- use crate::mem::MaybeUninit ;
382+
383+ @@ -457 ,12 +565 ,15 @@ impl Command {
384+ use crate::sys::weak::weak ;
378385 use crate::sys::{self, cvt_nz, unix_sigpipe_attr_specified};
379-
386+
380387+ let skip_spawnvp: bool = getenv(&OsString::from("SB2_RUST_NO_SPAWNVP")).is_some();
381388+
382389 if self.get_gid().is_some()
@@ -389,6 +396,6 @@ index 72aca4e6659..495368b58ec 100644
389396 {
390397 return Ok(None);
391398 }
392- - -
393- 2.43.0
399+ - -
400+ 2.45.2
394401
0 commit comments