@@ -35,18 +35,19 @@ needed to support the SHIM functionality
35
35
Co-authored-by: Ruben De Smet <
[email protected] >
36
36
Signed-off-by: David Greaves <
[email protected] >
37
37
Signed-off-by: Ruben De Smet <
[email protected] >
38
+ Signed-off-by: Matti Viljanen <
[email protected] >
38
39
---
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 ++++++++++++++++--
41
42
2 files changed, 172 insertions(+), 20 deletions(-)
42
43
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! {
48
49
////////////////////////////////////////////////////////////////////////////////
49
-
50
+
50
51
pub struct Command {
51
52
- program: CString,
52
53
+ pub(crate) program: CString,
@@ -64,13 +65,13 @@ index bac32d9e60e..3cd9767da88 100644
64
65
+ pub(crate) setuid: Option<SetuidFn>,
65
66
+ pub(crate) setgid: Option<SetgidFn>,
66
67
+ pub(crate) setgroups: Option<SetgroupsFn>,
67
-
68
+
68
69
program_kind: ProgramKind,
69
70
cwd: Option<CString>,
70
71
@@ -110,6 +117,14 @@ pub struct Command {
71
72
pgroup: Option<pid_t>,
72
73
}
73
-
74
+
74
75
+ pub(crate) type ExecvpFn = fn(*const c_char, *const *const c_char) -> c_int;
75
76
+ pub(crate) type Dup2Fn = fn(c_int, c_int) -> c_int;
76
77
+ pub(crate) type CloseFn = fn(c_int) -> c_int;
@@ -81,8 +82,8 @@ index bac32d9e60e..3cd9767da88 100644
81
82
+
82
83
// Create a new type for argv, so that we can make it `Send` and `Sync`
83
84
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 {
86
87
let mut saw_nul = false;
87
88
let program_kind = ProgramKind::new(program.as_ref());
88
89
let program = os2c(program, &mut saw_nul);
@@ -110,7 +111,7 @@ index bac32d9e60e..3cd9767da88 100644
110
111
closures: Vec::new(),
111
112
groups: None,
112
113
stdin: None,
113
- @@ -207,16 +230,24 @@ pub fn new(program: &OsStr) -> Command {
114
+ @@ -207,16 +230,24 @@ impl Command {
114
115
let mut saw_nul = false;
115
116
let program_kind = ProgramKind::new(program.as_ref());
116
117
let program = os2c(program, &mut saw_nul);
@@ -138,10 +139,10 @@ index bac32d9e60e..3cd9767da88 100644
138
139
closures: Vec::new(),
139
140
groups: None,
140
141
stdin: None,
141
- @@ -227,6 +258,16 @@ pub fn new(program: &OsStr) -> Command {
142
+ @@ -227,6 +258,16 @@ impl Command {
142
143
}
143
144
}
144
-
145
+
145
146
+ // This allows process_unix::{spawn, exec} to push program to the
146
147
+ // start of /usr/bin/env's arg list
147
148
+ pub fn insert_program(&mut self, arg: String) {
@@ -155,34 +156,34 @@ index bac32d9e60e..3cd9767da88 100644
155
156
pub fn set_arg_0(&mut self, arg: &OsStr) {
156
157
// Set a new arg0
157
158
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;
163
164
use libc::RTP_ID as pid_t;
164
-
165
+
165
166
#[cfg(not(target_os = "vxworks"))]
166
167
- use libc::{c_int, pid_t};
167
168
+ use libc::{c_char, c_int, dlsym, pid_t};
168
-
169
+
169
170
#[cfg(not(any(
170
171
target_os = "vxworks",
171
- @@ -62 ,6 +62 ,10 @@ fn get_clock_resolution() -> Duration {
172
+ @@ -52 ,6 +52 ,10 @@ cfg_if::cfg_if! {
172
173
}
173
174
}
174
-
175
+
175
176
+ use crate::ffi::OsString;
176
177
+ use crate::intrinsics::transmute;
177
178
+ use sys::os::getenv;
178
179
+
179
180
////////////////////////////////////////////////////////////////////////////////
180
181
// Command
181
182
////////////////////////////////////////////////////////////////////////////////
182
- @@ -95 ,6 +99 ,65 @@ pub fn spawn(
183
+ @@ -85 ,6 +89 ,65 @@ impl Command {
183
184
#[cfg(not(target_os = "linux"))]
184
185
let (input, output) = sys::pipe::anon_pipe()?;
185
-
186
+
186
187
+ // If there is a RUST_EXEC_SHIM (could be "/usr/bin/env --")
187
188
+ // then we're probably going to directly execvp it via dlsym
188
189
+ // to avoid issues with threads and malloc post-fork and
@@ -245,7 +246,7 @@ index 72aca4e6659..495368b58ec 100644
245
246
// Whatever happens after the fork is almost for sure going to touch or
246
247
// look at the environment in one way or another (PATH in `execvp` or
247
248
// 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 {
249
250
if pid == 0 {
250
251
crate::panic::always_abort();
251
252
mem::forget(env_lock); // avoid non-async-signal-safe unlocking
@@ -254,7 +255,7 @@ index 72aca4e6659..495368b58ec 100644
254
255
#[cfg(target_os = "linux")]
255
256
if self.get_create_pidfd() {
256
257
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 {
258
259
Err(e) => e,
259
260
}
260
261
}
@@ -303,9 +304,9 @@ index 72aca4e6659..495368b58ec 100644
303
304
// And at this point we've reached a special time in the life of the
304
305
// child. The child must now be considered hamstrung and unable to
305
306
// 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 {
307
308
use crate::sys::{self, cvt_r};
308
-
309
+
309
310
if let Some(fd) = stdio.stdin.fd() {
310
311
- cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
311
312
+ cvt_r(|| self.unwrap_dup2(fd, libc::STDIN_FILENO))?;
@@ -318,9 +319,9 @@ index 72aca4e6659..495368b58ec 100644
318
319
- cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
319
320
+ cvt_r(|| self.unwrap_dup2(fd, libc::STDERR_FILENO))?;
320
321
}
321
-
322
+
322
323
#[cfg(not(target_os = "l4re"))]
323
- @@ -322 ,10 +425 ,10 @@ unsafe fn do_exec(
324
+ @@ -311 ,10 +414 ,10 @@ impl Command {
324
325
if let Some(_g) = self.get_groups() {
325
326
//FIXME: Redox kernel does not support setgroups yet
326
327
#[cfg(not(target_os = "redox"))]
@@ -333,12 +334,18 @@ index 72aca4e6659..495368b58ec 100644
333
334
}
334
335
if let Some(u) = self.get_uid() {
335
336
// When dropping privileges from root, the `setgroups` call
336
- @@ -337,13 +440,13 @@ unsafe fn do_exec(
337
+ @@ -326,7 +429,7 @@ impl Command {
337
338
//FIXME: Redox kernel does not support setgroups yet
338
339
#[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
+ }
342
349
}
343
350
- cvt(libc::setuid(u as uid_t))?;
344
351
+ cvt(self.unwrap_setuid(u as uid_t))?;
@@ -348,9 +355,9 @@ index 72aca4e6659..495368b58ec 100644
348
355
- cvt(libc::chdir(cwd.as_ptr()))?;
349
356
+ cvt(self.unwrap_chdir(cwd.as_ptr()))?;
350
357
}
351
-
358
+
352
359
if let Some(pgroup) = self.get_pgroup() {
353
- @@ -408 ,8 +511 ,12 @@ fn drop(&mut self) {
360
+ @@ -405 ,8 +508 ,12 @@ impl Command {
354
361
_reset = Some(Reset(*sys::os::environ()));
355
362
*sys::os::environ() = envp.as_ptr();
356
363
}
@@ -364,19 +371,19 @@ index 72aca4e6659..495368b58ec 100644
364
371
+ };
365
372
Err(io::Error::last_os_error())
366
373
}
367
-
368
- @@ -436 ,6 +543 ,7 @@ fn posix_spawn(
374
+
375
+ @@ -433 ,6 +540 ,7 @@ impl Command {
369
376
_: &ChildPipes,
370
377
_: Option<&CStringArray>,
371
378
) -> io::Result<Option<Process>> {
372
379
+ eprintln!("process_unix:270: in null posix_spawn");
373
380
Ok(None)
374
381
}
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 ;
378
385
use crate::sys::{self, cvt_nz, unix_sigpipe_attr_specified};
379
-
386
+
380
387
+ let skip_spawnvp: bool = getenv(&OsString::from("SB2_RUST_NO_SPAWNVP")).is_some();
381
388
+
382
389
if self.get_gid().is_some()
@@ -389,6 +396,6 @@ index 72aca4e6659..495368b58ec 100644
389
396
{
390
397
return Ok(None);
391
398
}
392
- - -
393
- 2.43.0
399
+ - -
400
+ 2.45.2
394
401
0 commit comments