@@ -32,18 +32,19 @@ and the fallback fork/exec is used.
32
32
Note that some reworking of rust's handling of program/argv[0] was
33
33
needed to support the SHIM functionality
34
34
35
+ Co-authored-by: Ruben De Smet <ruben.de.smet@rubdos.be>
35
36
Signed-off-by: David Greaves <david.greaves@jolla.com>
36
37
Signed-off-by: Ruben De Smet <ruben.de.smet@rubdos.be>
37
38
---
38
- .../src/sys/unix/process/process_common.rs | 50 ++++++-
39
- .../std/src/sys/unix/process/process_unix.rs | 138 ++++++++++++++++--
40
- 2 files changed, 170 insertions(+), 18 deletions(-)
39
+ .../src/sys/unix/process/process_common.rs | 55 ++++++-
40
+ .../std/src/sys/unix/process/process_unix.rs | 137 ++++++++++++++++--
41
+ 2 files changed, 172 insertions(+), 20 deletions(-)
41
42
42
43
diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs
43
- index 27bee714f5b4317132db508fe916c7e194d05bc5..7746177e0423b7689833cc84b6132240a8bb2bf0 100644
44
+ index bac32d9e60e..3cd9767da88 100644
44
45
--- a/library/std/src/sys/unix/process/process_common.rs
45
46
+++ b/library/std/src/sys/unix/process/process_common.rs
46
- @@ -61 ,7 +61 ,7 @@ cfg_if::cfg_if! {
47
+ @@ -85 ,7 +85 ,7 @@ pub unsafe fn sigaddset(set: *mut libc::sigset_t, signum: libc::c_int) -> libc::
47
48
////////////////////////////////////////////////////////////////////////////////
48
49
49
50
pub struct Command {
@@ -52,7 +53,7 @@ index 27bee714f5b4317132db508fe916c7e194d05bc5..7746177e0423b7689833cc84b6132240
52
53
args: Vec<CString>,
53
54
/// Exactly what will be passed to `execvp`.
54
55
///
55
- @@ -70 ,6 +70 ,13 @@ pub struct Command {
56
+ @@ -94 ,6 +94 ,13 @@ pub struct Command {
56
57
/// `args` to properly update this as well.
57
58
argv: Argv,
58
59
env: CommandEnv,
@@ -64,9 +65,9 @@ index 27bee714f5b4317132db508fe916c7e194d05bc5..7746177e0423b7689833cc84b6132240
64
65
+ pub(crate) setgid: Option<SetgidFn>,
65
66
+ pub(crate) setgroups: Option<SetgroupsFn>,
66
67
68
+ program_kind: ProgramKind,
67
69
cwd: Option<CString>,
68
- uid: Option<uid_t>,
69
- @@ -85,6 +92,14 @@ pub struct Command {
70
+ @@ -110,6 +117,14 @@ pub struct Command {
70
71
pgroup: Option<pid_t>,
71
72
}
72
73
@@ -81,18 +82,18 @@ index 27bee714f5b4317132db508fe916c7e194d05bc5..7746177e0423b7689833cc84b6132240
81
82
// Create a new type for argv, so that we can make it `Send` and `Sync`
82
83
struct Argv(Vec<*const c_char>);
83
84
84
- @@ -132,15 +147,23 @@ impl Command {
85
- pub fn new(program: &OsStr) -> Command {
85
+ @@ -183,16 +198,24 @@ pub fn new(program: &OsStr) -> Command {
86
86
let mut saw_nul = false;
87
+ let program_kind = ProgramKind::new(program.as_ref());
87
88
let program = os2c(program, &mut saw_nul);
88
89
+ let arg0 = program.clone();
89
90
Command {
90
91
- argv: Argv(vec![program.as_ptr(), ptr::null()]),
91
92
- args: vec![program.clone()],
92
- - program,
93
93
+ argv: Argv(vec![arg0.as_ptr(), ptr::null()]),
94
94
+ args: vec![arg0],
95
- + program: program,
95
+ program,
96
+ program_kind,
96
97
env: Default::default(),
97
98
+ execvp: None,
98
99
+ dup2: None,
@@ -109,18 +110,18 @@ index 27bee714f5b4317132db508fe916c7e194d05bc5..7746177e0423b7689833cc84b6132240
109
110
closures: Vec::new(),
110
111
groups: None,
111
112
stdin: None,
112
- @@ -154,15 +177,23 @@ impl Command {
113
- pub fn new(program: &OsStr) -> Command {
113
+ @@ -207,16 +230,24 @@ pub fn new(program: &OsStr) -> Command {
114
114
let mut saw_nul = false;
115
+ let program_kind = ProgramKind::new(program.as_ref());
115
116
let program = os2c(program, &mut saw_nul);
116
117
+ let arg0 = program.clone();
117
118
Command {
118
119
- argv: Argv(vec![program.as_ptr(), ptr::null()]),
119
120
- args: vec![program.clone()],
120
- - program,
121
121
+ argv: Argv(vec![arg0.as_ptr(), ptr::null()]),
122
122
+ args: vec![arg0],
123
- + program: program,
123
+ program,
124
+ program_kind,
124
125
env: Default::default(),
125
126
+ execvp: None,
126
127
+ dup2: None,
@@ -137,7 +138,7 @@ index 27bee714f5b4317132db508fe916c7e194d05bc5..7746177e0423b7689833cc84b6132240
137
138
closures: Vec::new(),
138
139
groups: None,
139
140
stdin: None,
140
- @@ -173 ,6 +204 ,16 @@ impl Command {
141
+ @@ -227 ,6 +258 ,16 @@ pub fn new(program: &OsStr) -> Command {
141
142
}
142
143
}
143
144
@@ -155,18 +156,21 @@ index 27bee714f5b4317132db508fe916c7e194d05bc5..7746177e0423b7689833cc84b6132240
155
156
// Set a new arg0
156
157
let arg = os2c(arg, &mut self.saw_nul);
157
158
diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs
158
- index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b24293617376 100644
159
+ index 72aca4e6659..495368b58ec 100644
159
160
--- a/library/std/src/sys/unix/process/process_unix.rs
160
161
+++ b/library/std/src/sys/unix/process/process_unix.rs
161
- @@ -27,11 +27,15 @@
162
+ @@ -25,7 +25,7 @@
162
163
use libc::RTP_ID as pid_t;
163
164
164
165
#[cfg(not(target_os = "vxworks"))]
165
166
- use libc::{c_int, pid_t};
166
167
+ use libc::{c_char, c_int, dlsym, pid_t};
167
168
168
- #[cfg(not(any(target_os = "vxworks", target_os = "l4re")))]
169
- use libc::{gid_t, uid_t};
169
+ #[cfg(not(any(
170
+ target_os = "vxworks",
171
+ @@ -62,6 +62,10 @@ fn get_clock_resolution() -> Duration {
172
+ }
173
+ }
170
174
171
175
+ use crate::ffi::OsString;
172
176
+ use crate::intrinsics::transmute;
@@ -175,8 +179,8 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
175
179
////////////////////////////////////////////////////////////////////////////////
176
180
// Command
177
181
////////////////////////////////////////////////////////////////////////////////
178
- @@ -61 ,6 +65 ,65 @@ pub fn spawn(
179
-
182
+ @@ -95 ,6 +99 ,65 @@ pub fn spawn(
183
+ #[cfg(not(target_os = "linux"))]
180
184
let (input, output) = sys::pipe::anon_pipe()?;
181
185
182
186
+ // If there is a RUST_EXEC_SHIM (could be "/usr/bin/env --")
@@ -241,22 +245,21 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
241
245
// Whatever happens after the fork is almost for sure going to touch or
242
246
// look at the environment in one way or another (PATH in `execvp` or
243
247
// accessing the `environ` pointer ourselves). Make sure no other thread
244
- @@ -76 ,7 +139 ,7 @@ pub fn spawn(
248
+ @@ -111 ,7 +174 ,7 @@ pub fn spawn(
245
249
if pid == 0 {
246
250
crate::panic::always_abort();
247
- mem::forget(env_lock);
251
+ mem::forget(env_lock); // avoid non-async-signal-safe unlocking
248
252
- drop(input);
249
253
+ self.unwrap_drop(input);
250
- let Err(err) = unsafe { self.do_exec(theirs, envp.as_ref()) };
251
- let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
252
- let errno = errno.to_be_bytes( );
253
- @@ -243 ,7 +306,48 @@ pub fn exec(&mut self, default: Stdio) -> io::Error {
254
+ #[cfg(target_os = "linux")]
255
+ if self.get_create_pidfd() {
256
+ self.send_pidfd(&output );
257
+ @@ -268 ,7 +331,47 @@ pub fn exec(&mut self, default: Stdio) -> io::Error {
254
258
Err(e) => e,
255
259
}
256
260
}
257
261
-
258
- + fn unwrap_drop(&mut self, fh: sys::unix::pipe::AnonPipe) {
259
- + use crate::os::unix::io::AsRawFd;
262
+ + fn unwrap_drop(&mut self, fh: impl crate::os::unix::io::AsRawFd) {
260
263
+ // drop() simply calls libc::close(fh.fd)
261
264
+ match self.close {
262
265
+ Some(real_close) => {
@@ -300,7 +303,7 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
300
303
// And at this point we've reached a special time in the life of the
301
304
// child. The child must now be considered hamstrung and unable to
302
305
// do anything other than syscalls really. Consider the following
303
- @@ -282 ,13 +386 ,13 @@ unsafe fn do_exec(
306
+ @@ -308 ,13 +411 ,13 @@ unsafe fn do_exec(
304
307
use crate::sys::{self, cvt_r};
305
308
306
309
if let Some(fd) = stdio.stdin.fd() {
@@ -317,7 +320,7 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
317
320
}
318
321
319
322
#[cfg(not(target_os = "l4re"))]
320
- @@ -296 ,10 +400 ,10 @@ unsafe fn do_exec(
323
+ @@ -322 ,10 +425 ,10 @@ unsafe fn do_exec(
321
324
if let Some(_g) = self.get_groups() {
322
325
//FIXME: Redox kernel does not support setgroups yet
323
326
#[cfg(not(target_os = "redox"))]
@@ -330,12 +333,12 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
330
333
}
331
334
if let Some(u) = self.get_uid() {
332
335
// When dropping privileges from root, the `setgroups` call
333
- @@ -311 ,13 +415 ,13 @@ unsafe fn do_exec(
336
+ @@ -337 ,13 +440 ,13 @@ unsafe fn do_exec(
334
337
//FIXME: Redox kernel does not support setgroups yet
335
338
#[cfg(not(target_os = "redox"))]
336
339
if libc::getuid() == 0 && self.get_groups().is_none() {
337
- - cvt(libc::setgroups(0, ptr::null()))?;
338
- + cvt(self.unwrap_setgroups(0, ptr::null()))?;
340
+ - cvt(libc::setgroups(0, crate:: ptr::null()))?;
341
+ + cvt(self.unwrap_setgroups(0, crate:: ptr::null()))?;
339
342
}
340
343
- cvt(libc::setuid(u as uid_t))?;
341
344
+ cvt(self.unwrap_setuid(u as uid_t))?;
@@ -347,7 +350,7 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
347
350
}
348
351
349
352
if let Some(pgroup) = self.get_pgroup() {
350
- @@ -378 ,8 +482 ,12 @@ fn drop(&mut self) {
353
+ @@ -408 ,8 +511 ,12 @@ fn drop(&mut self) {
351
354
_reset = Some(Reset(*sys::os::environ()));
352
355
*sys::os::environ() = envp.as_ptr();
353
356
}
@@ -362,17 +365,17 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
362
365
Err(io::Error::last_os_error())
363
366
}
364
367
365
- @@ -394 ,6 +502 ,7 @@ fn posix_spawn(
368
+ @@ -436 ,6 +543 ,7 @@ fn posix_spawn(
366
369
_: &ChildPipes,
367
370
_: Option<&CStringArray>,
368
371
) -> io::Result<Option<Process>> {
369
372
+ eprintln!("process_unix:270: in null posix_spawn");
370
373
Ok(None)
371
374
}
372
375
373
- @@ -413 ,12 +522 ,15 @@ fn posix_spawn(
376
+ @@ -459 ,12 +567 ,15 @@ fn posix_spawn(
374
377
use crate::mem::MaybeUninit;
375
- use crate::sys::{self, cvt_nz};
378
+ use crate::sys::{self, cvt_nz, unix_sigpipe_attr_specified };
376
379
377
380
+ let skip_spawnvp: bool = getenv(&OsString::from("SB2_RUST_NO_SPAWNVP")).is_some();
378
381
+
@@ -386,3 +389,6 @@ index 3d305cd7310fd4b8419d220d1adeadebb9fda914..5d1c667b943cb2af0e0e918c17a7b242
386
389
{
387
390
return Ok(None);
388
391
}
392
+ - -
393
+ 2.43.0
394
+
0 commit comments