view src/user.rs @ 23:0ee235caebc5

tweak
author AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
date Tue, 03 Nov 2020 17:48:50 +0900
parents e6dc51066703
children e8ba0f63c227
line wrap: on
line source

pub fn getlogin(uid: u32) -> &'static str {
    use std::ffi::CStr;
    let user_passwd = unsafe { nix::libc::getpwuid(uid) };
    let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) };
    c_str.to_str().unwrap()
}

pub fn getuid() -> u32 {
    let uid_struct = nix::unistd::getuid();
    uid_struct.into()
}

pub fn getgid() -> u32 {
    let gid_struct = nix::unistd::getgid();
    gid_struct.into()
}

const ROOT_ID: u32 = 0;

pub fn set_root_id() {
    if let Err(err) = nix::unistd::seteuid(nix::unistd::Uid::from_raw(ROOT_ID)) {
        panic!("failed seteuid {}", err)
    }
    if let Err(err) = nix::unistd::setegid(nix::unistd::Gid::from_raw(ROOT_ID)) {
        panic!("failed setedid{}", err)
    }
    if let Err(err) = nix::unistd::setuid(nix::unistd::Uid::from_raw(ROOT_ID)) {
        panic!("failed setuid {}", err)
    }
    if let Err(err) = nix::unistd::setgid(nix::unistd::Gid::from_raw(ROOT_ID)) {
        panic!("failed setdid{}", err)
    }
}