Mercurial > hg > Members > anatofuz > ie-virsh
diff src/main.rs @ 12:0df4d067badb
move each crates
author | AnaTofuZ <k198584@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 29 Oct 2020 16:15:52 +0900 |
parents | 9fac2c578228 |
children | 56e9763abeef |
line wrap: on
line diff
--- a/src/main.rs Thu Oct 29 09:44:38 2020 +0900 +++ b/src/main.rs Thu Oct 29 16:15:52 2020 +0900 @@ -1,6 +1,5 @@ use clap::Clap; -use nix; -use std::process::Command; +use ie_virsh::{command, user}; #[derive(Clap)] #[clap(version = "1.0", author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")] @@ -19,8 +18,7 @@ Console(Console), Start(Start), Ttyconsole(TTyConsole), - VNCDisplay(VNCDisplay) - //todo define-gdb dumpxml + VNCDisplay(VNCDisplay), //todo define-gdb dumpxml } /// define (but don't start) a domain from an template XML file @@ -75,11 +73,6 @@ name_or_id: String, } -struct ListDumpMsg { - info_msg: String, - border_line: String, -} - struct VM { id: u32, name: String, @@ -89,19 +82,15 @@ fn main() { let opts: Opts = Opts::parse(); - let uid = getuid(); - let gid = getgid(); - let user_name = getlogin(uid); + let uid = user::getuid(); + let gid = user::getgid(); + let user_name = user::getlogin(uid); println!("uid: {} gid: {} name: {}", uid, gid, user_name); match opts.subcmd { SubCommand::List(_) => { - set_root_id(); - let (ldump_msg, vm_list_strs) = list_command(user_name); - println!("{}\n{}", ldump_msg.info_msg, ldump_msg.border_line); - for vm_info in vm_list_strs { - println!("{}", vm_info); - } + user::set_root_id(); + command::list_command(user_name); } SubCommand::Define(name) => {} SubCommand::Shutdown(name) => {} @@ -115,68 +104,4 @@ //list_command(user_name); } -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) }; - return c_str.to_str().unwrap(); -} - -fn getuid() -> u32 { - let uid_struct = nix::unistd::getuid(); - return uid_struct.into(); -} - -fn getgid() -> u32 { - let gid_struct = nix::unistd::getgid(); - return gid_struct.into(); -} - -fn list_command(user_name: &'static str) -> (ListDumpMsg, Vec<String>) { - let output = Command::new("virsh") - .arg("list") - .arg("--all") - .output() - .expect("failed to virsh"); - let virsh_list = String::from_utf8_lossy(&output.stdout); - let mut virsh_list = virsh_list.split("\n"); - - let info_msg = virsh_list.next().unwrap(); - let border_line = virsh_list.next().unwrap(); - let ldump_msg = ListDumpMsg { - info_msg: String::from(info_msg), - border_line: String::from(border_line), - }; - - return ( - ldump_msg, - virsh_list - .filter(|&x| x.contains(user_name)) - .map(|x| x.to_string()) - .collect(), - ); -} - -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(_) => {} - } -} - fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel: bool) {}