From 2b96f8f0c1ce2db8ac5f97e1b476bab118e1aea8 Mon Sep 17 00:00:00 2001 From: Knight Date: Tue, 2 Aug 2016 14:25:19 +0800 Subject: [PATCH] chroot: use uucore::entries --- src/chroot/Cargo.toml | 7 +++++-- src/chroot/chroot.rs | 34 ++++++++++------------------------ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/chroot/Cargo.toml b/src/chroot/Cargo.toml index ab19f96bf..6761366a8 100644 --- a/src/chroot/Cargo.toml +++ b/src/chroot/Cargo.toml @@ -9,8 +9,11 @@ path = "chroot.rs" [dependencies] getopts = "*" -libc = "*" -uucore = { path="../uucore" } + +[dependencies.uucore] +path = "../uucore" +default-features = false +features = ["entries"] [[bin]] name = "chroot" diff --git a/src/chroot/chroot.rs b/src/chroot/chroot.rs index fa7f8bcff..6f5c63eeb 100644 --- a/src/chroot/chroot.rs +++ b/src/chroot/chroot.rs @@ -4,39 +4,25 @@ * This file is part of the uutils coreutils package. * * (c) Vsevolod Velichko + * (c) Jian Zeng * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ extern crate getopts; -extern crate libc; #[macro_use] extern crate uucore; +use uucore::libc::{self, setgid, setuid, chroot, setgroups}; +use uucore::entries; use getopts::Options; -use libc::{setgid, setuid}; use std::ffi::CString; use std::io::{Error, Write}; use std::iter::FromIterator; use std::path::Path; use std::process::Command; -use uucore::c_types::{get_pw_from_args, get_group}; - -extern { - fn chroot(path: *const libc::c_char) -> libc::c_int; -} - -#[cfg(any(target_os = "macos", target_os = "freebsd"))] -extern { - fn setgroups(size: libc::c_int, list: *const libc::gid_t) -> libc::c_int; -} - -#[cfg(target_os = "linux")] -extern { - fn setgroups(size: libc::size_t, list: *const libc::gid_t) -> libc::c_int; -} static NAME: &'static str = "chroot"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -146,9 +132,9 @@ fn enter_chroot(root: &Path) { fn set_main_group(group: &str) { if !group.is_empty() { - let group_id = match get_group(group) { - None => crash!(1, "no such group: {}", group), - Some(g) => g.gr_gid + let group_id = match entries::grp2gid(group) { + Ok(g) => g, + _ => crash!(1, "no such group: {}", group), }; let err = unsafe { setgid(group_id) }; if err != 0 { @@ -177,9 +163,9 @@ fn set_groups_from_str(groups: &str) { if !groups.is_empty() { let groups_vec: Vec = FromIterator::from_iter( groups.split(',').map( - |x| match get_group(x) { - None => crash!(1, "no such group: {}", x), - Some(g) => g.gr_gid + |x| match entries::grp2gid(x) { + Ok(g) => g, + _ => crash!(1, "no such group: {}", x), }) ); let err = set_groups(groups_vec); @@ -191,7 +177,7 @@ fn set_groups_from_str(groups: &str) { fn set_user(user: &str) { if !user.is_empty() { - let user_id = get_pw_from_args(&vec!(user.to_owned())).unwrap().pw_uid; + let user_id = entries::usr2uid(user).unwrap(); let err = unsafe { setuid(user_id as libc::uid_t) }; if err != 0 { crash!(1, "cannot set user to {}: {}", user, Error::last_os_error())