From baeed516a7612dad8c80ca7fbf48522d28598fb8 Mon Sep 17 00:00:00 2001 From: joonho Date: Sun, 5 Jan 2020 18:26:33 +0900 Subject: [PATCH] Add os.chroot --- vm/src/stdlib/os.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 46aa9d8f6a..98d189e9d6 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -888,6 +888,11 @@ fn os_chdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { env::set_current_dir(path.as_str()).map_err(|err| convert_io_error(vm, err)) } +#[cfg(unix)] +fn os_chroot(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { + nix::unistd::chroot(path.as_str()).map_err(|err| convert_nix_error(vm, err)) +} + #[cfg(unix)] fn os_get_inheritable(fd: RawFd, vm: &VirtualMachine) -> PyResult { use nix::fcntl::fcntl; @@ -1280,7 +1285,6 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { SupportFunc::new(vm, "chdir", os_chdir, Some(false), None, None), // chflags Some, None Some // chown Some Some Some - // chroot Some None None SupportFunc::new(vm, "listdir", os_listdir, Some(false), None, None), SupportFunc::new(vm, "mkdir", os_mkdir, Some(false), Some(false), None), // mkfifo Some Some None @@ -1300,14 +1304,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { // utime Some Some Some ]; #[cfg(unix)] - support_funcs.extend(vec![SupportFunc::new( - vm, - "chmod", - os_chmod, - Some(false), - Some(false), - Some(false), - )]); + support_funcs.extend(vec![ + SupportFunc::new(vm, "chmod", os_chmod, Some(false), Some(false), Some(false)), + SupportFunc::new(vm, "chroot", os_chroot, Some(false), None, None), + ]); let supports_fd = PySet::default().into_ref(vm); let supports_dir_fd = PySet::default().into_ref(vm); let supports_follow_symlinks = PySet::default().into_ref(vm); @@ -1391,6 +1391,7 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) -> extend_module!(vm, module, { "access" => ctx.new_rustfunc(os_access), "chmod" => ctx.new_rustfunc(os_chmod), + "chroot" => ctx.new_rustfunc(os_chroot), "get_inheritable" => ctx.new_rustfunc(os_get_inheritable), // TODO: windows "get_blocking" => ctx.new_rustfunc(os_get_blocking), "getppid" => ctx.new_rustfunc(os_getppid),