Skip to content

Commit 325706d

Browse files
committed
Add _posixsubprocess
1 parent f40c3d4 commit 325706d

File tree

4 files changed

+258
-16
lines changed

4 files changed

+258
-16
lines changed

vm/src/pyobject.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,23 @@ where
13831383

13841384
pub type PyComparisonValue = PyArithmaticValue<bool>;
13851385

1386+
#[derive(Clone)]
1387+
pub struct PySequence<T = PyObjectRef>(Vec<T>);
1388+
1389+
impl<T> PySequence<T> {
1390+
pub fn into_vec(self) -> Vec<T> {
1391+
self.0
1392+
}
1393+
pub fn as_slice(&self) -> &[T] {
1394+
&self.0
1395+
}
1396+
}
1397+
impl<T: TryFromObject> TryFromObject for PySequence<T> {
1398+
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
1399+
vm.extract_elements(&obj).map(Self)
1400+
}
1401+
}
1402+
13861403
#[cfg(test)]
13871404
mod tests {
13881405
use super::*;

vm/src/stdlib/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ mod faulthandler;
4949
mod msvcrt;
5050
#[cfg(not(target_arch = "wasm32"))]
5151
mod multiprocessing;
52+
#[cfg(unix)]
53+
mod posixsubprocess;
5254
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
5355
mod pwd;
5456
#[cfg(not(target_arch = "wasm32"))]
@@ -145,6 +147,14 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
145147
modules.insert("pwd".to_owned(), Box::new(pwd::make_module));
146148
}
147149

150+
#[cfg(unix)]
151+
{
152+
modules.insert(
153+
"_posixsubprocess".to_owned(),
154+
Box::new(posixsubprocess::make_module),
155+
);
156+
}
157+
148158
// Windows-only
149159
#[cfg(windows)]
150160
{

vm/src/stdlib/os.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ enum OutputMode {
119119
fn output_by_mode(val: String, mode: OutputMode, vm: &VirtualMachine) -> PyObjectRef {
120120
match mode {
121121
OutputMode::String => vm.ctx.new_str(val),
122-
OutputMode::Bytes => vm.ctx.new_bytes(val.as_bytes().to_vec()),
122+
OutputMode::Bytes => vm.ctx.new_bytes(val.into_bytes()),
123123
}
124124
}
125125

@@ -140,6 +140,9 @@ impl PyPathLike {
140140
use std::os::windows::ffi::OsStrExt;
141141
self.path.encode_wide().chain(std::iter::once(0)).collect()
142142
}
143+
pub fn into_os_string(self) -> ffi::OsString {
144+
self.path
145+
}
143146
}
144147

145148
impl TryFromObject for PyPathLike {
@@ -1036,28 +1039,27 @@ fn os_get_inheritable(fd: RawFd, vm: &VirtualMachine) -> PyResult<bool> {
10361039
}
10371040
}
10381041

1042+
#[cfg(unix)]
1043+
pub(crate) fn set_inheritable(fd: RawFd, inheritable: bool) -> nix::Result<()> {
1044+
use nix::fcntl;
1045+
1046+
let flags = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFD)?);
1047+
let mut new_flags = flags;
1048+
new_flags.set(fcntl::FdFlag::FD_CLOEXEC, !inheritable);
1049+
if flags != new_flags {
1050+
fcntl::fcntl(fd, fcntl::FcntlArg::F_SETFD(new_flags))?;
1051+
}
1052+
Ok(())
1053+
}
1054+
10391055
fn os_set_inheritable(fd: i64, inheritable: bool, vm: &VirtualMachine) -> PyResult<()> {
10401056
#[cfg(not(any(unix, windows)))]
10411057
{
10421058
unimplemented!()
10431059
}
10441060
#[cfg(unix)]
10451061
{
1046-
let fd = fd as RawFd;
1047-
let _set_flag = || {
1048-
use nix::fcntl::fcntl;
1049-
use nix::fcntl::FcntlArg;
1050-
use nix::fcntl::FdFlag;
1051-
1052-
let flags = FdFlag::from_bits_truncate(fcntl(fd, FcntlArg::F_GETFD)?);
1053-
let mut new_flags = flags;
1054-
new_flags.set(FdFlag::from_bits_truncate(libc::FD_CLOEXEC), !inheritable);
1055-
if flags != new_flags {
1056-
fcntl(fd, FcntlArg::F_SETFD(new_flags))?;
1057-
}
1058-
Ok(())
1059-
};
1060-
_set_flag().or_else(|err| Err(convert_nix_error(vm, err)))
1062+
set_inheritable(fd as RawFd, inheritable).map_err(|err| convert_nix_error(vm, err))
10611063
}
10621064
#[cfg(windows)]
10631065
{
@@ -1860,6 +1862,12 @@ fn os_kill(pid: i32, sig: isize, vm: &VirtualMachine) -> PyResult<()> {
18601862
}
18611863
}
18621864

1865+
fn os_strerror(e: i32) -> String {
1866+
unsafe { ffi::CStr::from_ptr(libc::strerror(e)) }
1867+
.to_string_lossy()
1868+
.into_owned()
1869+
}
1870+
18631871
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
18641872
let ctx = &vm.ctx;
18651873

@@ -1970,6 +1978,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
19701978
"set_inheritable" => ctx.new_function(os_set_inheritable),
19711979
"link" => ctx.new_function(os_link),
19721980
"kill" => ctx.new_function(os_kill),
1981+
"strerror" => ctx.new_function(os_strerror),
19731982

19741983
"O_RDONLY" => ctx.new_int(libc::O_RDONLY),
19751984
"O_WRONLY" => ctx.new_int(libc::O_WRONLY),

vm/src/stdlib/posixsubprocess.rs

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
use nix::{dir, errno::Errno, fcntl, unistd};
2+
use std::convert::Infallible as Never;
3+
use std::ffi::{CStr, CString};
4+
use std::io::{self, prelude::*};
5+
use std::os::unix::ffi::OsStringExt;
6+
7+
use crate::pyobject::{PyObjectRef, PyResult, PySequence, TryFromObject};
8+
use crate::VirtualMachine;
9+
10+
use super::os::{convert_nix_error, set_inheritable, PyPathLike};
11+
12+
macro_rules! gen_args {
13+
($($field:ident: $t:ty),*$(,)?) => {
14+
#[derive(FromArgs)]
15+
struct ForkExecArgs {
16+
$(#[pyarg(positional_only)] $field: $t,)*
17+
}
18+
};
19+
}
20+
21+
struct CStrPathLike {
22+
s: CString,
23+
}
24+
impl TryFromObject for CStrPathLike {
25+
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
26+
let s = PyPathLike::try_from_object(vm, obj)?
27+
.into_os_string()
28+
.into_vec();
29+
let s = CString::new(s)
30+
.map_err(|_| vm.new_value_error("embedded null character".to_owned()))?;
31+
Ok(CStrPathLike { s })
32+
}
33+
}
34+
35+
gen_args! {
36+
args: PySequence<CStrPathLike> /* list */, exec_list: PySequence<CStrPathLike> /* list */,
37+
close_fds: bool, fds_to_keep: PySequence<i32>,
38+
cwd: Option<CStrPathLike>, env_list: Option<PySequence<CStrPathLike>>,
39+
p2cread: i32, p2cwrite: i32, c2pread: i32, c2pwrite: i32,
40+
errread: i32, errwrite: i32, errpipe_read: i32, errpipe_write: i32,
41+
restore_signals: bool, call_setsid: bool, preexec_fn: Option<PyObjectRef>,
42+
}
43+
44+
fn _posixsubprocess_fork_exec(args: ForkExecArgs, vm: &VirtualMachine) -> PyResult<libc::pid_t> {
45+
if args.preexec_fn.is_some() {
46+
return Err(vm.new_not_implemented_error("preexec_fn not supported yet".to_owned()));
47+
}
48+
let cstrs_to_ptrs = |cstrs: &[CStrPathLike]| {
49+
cstrs
50+
.iter()
51+
.map(|s| s.s.as_ptr())
52+
.chain(std::iter::once(std::ptr::null()))
53+
.collect::<Vec<_>>()
54+
};
55+
let argv = cstrs_to_ptrs(args.args.as_slice());
56+
let argv = &argv;
57+
let envp = args.env_list.as_ref().map(|s| cstrs_to_ptrs(s.as_slice()));
58+
let envp = envp.as_deref();
59+
match nix::unistd::fork().map_err(|e| convert_nix_error(vm, e))? {
60+
nix::unistd::ForkResult::Child => exec(&args, ProcArgs { argv, envp }),
61+
nix::unistd::ForkResult::Parent { child } => Ok(child.as_raw()),
62+
}
63+
}
64+
65+
// can't reallocate inside of exec(), so we reallocate prior to fork() and pass this along
66+
struct ProcArgs<'a> {
67+
argv: &'a [*const libc::c_char],
68+
envp: Option<&'a [*const libc::c_char]>,
69+
}
70+
71+
fn exec(args: &ForkExecArgs, procargs: ProcArgs) -> ! {
72+
match exec_inner(args, procargs) {
73+
Ok(x) => match x {},
74+
Err(e) => {
75+
let e = e.as_errno().expect("got a non-errno nix error");
76+
let buf: &mut [u8] = &mut [0; 256];
77+
let mut cur = io::Cursor::new(&mut *buf);
78+
// TODO: check if reached preexec, if not then have "noexec" after
79+
let _ = write!(cur, "OSError:{}:", e as i32);
80+
let pos = cur.position();
81+
let _ = unistd::write(args.errpipe_write, &buf[..pos as usize]);
82+
std::process::exit(255)
83+
}
84+
}
85+
}
86+
87+
fn exec_inner(args: &ForkExecArgs, procargs: ProcArgs) -> nix::Result<Never> {
88+
for &fd in args.fds_to_keep.as_slice() {
89+
if fd != args.errpipe_write {
90+
set_inheritable(fd, true)?
91+
}
92+
}
93+
94+
for &fd in &[args.p2cwrite, args.c2pread, args.errread] {
95+
if fd != -1 {
96+
unistd::close(fd)?;
97+
}
98+
}
99+
unistd::close(args.errpipe_read)?;
100+
101+
let c2pwrite = if args.c2pwrite == 0 {
102+
let fd = unistd::dup(args.c2pwrite)?;
103+
set_inheritable(fd, true)?;
104+
fd
105+
} else {
106+
args.c2pwrite
107+
};
108+
109+
let mut errwrite = args.errwrite;
110+
while errwrite == 0 || errwrite == 1 {
111+
errwrite = unistd::dup(errwrite)?;
112+
set_inheritable(errwrite, true)?;
113+
}
114+
115+
let dup_into_stdio = |fd, io_fd| {
116+
if fd == io_fd {
117+
set_inheritable(fd, true)
118+
} else if fd != -1 {
119+
unistd::dup2(fd, io_fd).map(drop)
120+
} else {
121+
Ok(())
122+
}
123+
};
124+
dup_into_stdio(args.p2cread, 0)?;
125+
dup_into_stdio(c2pwrite, 1)?;
126+
dup_into_stdio(errwrite, 2)?;
127+
128+
if let Some(ref cwd) = args.cwd {
129+
unistd::chdir(cwd.s.as_c_str())?
130+
}
131+
132+
if args.restore_signals {
133+
// TODO: restore signals SIGPIPE, SIGXFZ, SIGXFSZ to SIG_DFL
134+
}
135+
136+
if args.call_setsid {
137+
unistd::setsid()?;
138+
}
139+
140+
if args.close_fds {
141+
close_fds(3, args.fds_to_keep.as_slice())?;
142+
}
143+
144+
let mut first_err = None;
145+
for exec in args.exec_list.as_slice() {
146+
if let Some(envp) = procargs.envp {
147+
unsafe { libc::execve(exec.s.as_ptr(), procargs.argv.as_ptr(), envp.as_ptr()) };
148+
} else {
149+
unsafe { libc::execv(exec.s.as_ptr(), procargs.argv.as_ptr()) };
150+
}
151+
let e = Errno::last();
152+
if e != Errno::ENOENT && e != Errno::ENOTDIR && first_err.is_none() {
153+
first_err = Some(e)
154+
}
155+
}
156+
Err(first_err.unwrap_or_else(Errno::last).into())
157+
}
158+
159+
fn close_fds(above: i32, keep: &[i32]) -> nix::Result<()> {
160+
// TODO: close fds by brute force if readdir doesn't work:
161+
// https://github.com/python/cpython/blob/3.8/Modules/_posixsubprocess.c#L220
162+
let path = unsafe { CStr::from_bytes_with_nul_unchecked(FD_DIR_NAME) };
163+
let mut dir = dir::Dir::open(
164+
path,
165+
fcntl::OFlag::O_RDONLY | fcntl::OFlag::O_DIRECTORY,
166+
nix::sys::stat::Mode::empty(),
167+
)?;
168+
for e in dir.iter() {
169+
if let Some(fd) = pos_int_from_ascii(e?.file_name()) {
170+
if fd > above && !keep.contains(&fd) {
171+
unistd::close(fd)?
172+
}
173+
}
174+
}
175+
Ok(())
176+
}
177+
178+
#[cfg(any(
179+
target_os = "dragonfly",
180+
target_os = "freebsd",
181+
target_os = "netbsd",
182+
target_os = "openbsd",
183+
target_os = "macos",
184+
))]
185+
const FD_DIR_NAME: &[u8] = b"/dev/fd\0";
186+
187+
#[cfg(target_os = "linux")]
188+
const FD_DIR_NAME: &[u8] = b"/proc/self/fd\0";
189+
190+
fn pos_int_from_ascii(name: &CStr) -> Option<i32> {
191+
let mut num = 0;
192+
for c in name.to_bytes() {
193+
if !c.is_ascii_digit() {
194+
return None;
195+
}
196+
num = num * 10 + i32::from(c - b'0')
197+
}
198+
Some(num)
199+
}
200+
201+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
202+
let ctx = &vm.ctx;
203+
py_module!(vm, "_posixsubprocess", {
204+
"fork_exec" => named_function!(ctx, _posixsubprocess, fork_exec),
205+
})
206+
}

0 commit comments

Comments
 (0)