|
| 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