# HG changeset patch # User AnaTofuZ # Date 1603356016 -32400 # Node ID 5bdb02e05c86d5410530af189246bc9806db2181 # Parent 1632a34a3f6c9c8801479c8b9a31802d4d2d4318 show virsh list --all diff -r 1632a34a3f6c -r 5bdb02e05c86 Cargo.lock --- a/Cargo.lock Thu Oct 22 13:09:13 2020 +0900 +++ b/Cargo.lock Thu Oct 22 17:40:16 2020 +0900 @@ -22,6 +22,7 @@ name = "ie-virsh" version = "0.1.0" dependencies = [ + "libc", "nix", ] diff -r 1632a34a3f6c -r 5bdb02e05c86 Cargo.toml --- a/Cargo.toml Thu Oct 22 13:09:13 2020 +0900 +++ b/Cargo.toml Thu Oct 22 17:40:16 2020 +0900 @@ -8,3 +8,9 @@ [dependencies] nix = "0.19.0" +libc = "0.2.77" + +[profile.release] +codegen-units = 1 +lto = true +opt-level = "z" diff -r 1632a34a3f6c -r 5bdb02e05c86 src/main.rs --- a/src/main.rs Thu Oct 22 13:09:13 2020 +0900 +++ b/src/main.rs Thu Oct 22 17:40:16 2020 +0900 @@ -1,12 +1,18 @@ use nix; +use std::process::Command; +//use libc; fn main() { - let uid = getuid(); - let gid = getgid(); - let login_user = getlogin(uid); - println!("uid: {} gid: {} name: {}", uid, gid, login_user); + let uid = getuid(); + let gid = getgid(); + let user_name = getlogin(uid); + println!("uid: {} gid: {} name: {}", uid, gid, user_name); + set_root_id(); + + list_command(); } + fn getlogin(uid: u32) -> &'static str { use std::ffi::CStr; let user_passwd = unsafe { nix::libc::getpwuid(uid)}; @@ -16,10 +22,37 @@ fn getuid() -> u32 { let uid_struct = nix::unistd::getuid(); - return uid_struct.as_raw(); + return uid_struct.into(); } fn getgid() -> u32 { let gid_struct = nix::unistd::getgid(); - return gid_struct.as_raw(); + return gid_struct.into(); +} + +fn list_command() { + Command::new("virsh").arg("list").arg("--all").status().expect("failed to virsh"); } + + +fn set_root_id(){ + let id = 0; + match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) { + Err(err) => panic!("failed seteuid {}", err), + Ok(_) => {} + } + match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) { + Err(err) => panic!("failed setedid{}", err), + Ok(_) => {} + } + + match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) { + Err(err) => panic!("failed setuid {}", err), + Ok(_) => {} + } + + match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) { + Err(err) => panic!("failed setdid{}", err), + Ok(_) => {} + } +} \ No newline at end of file