Mercurial > hg > Members > anatofuz > ie-virsh
comparison 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 |
comparison
equal
deleted
inserted
replaced
11:9fac2c578228 | 12:0df4d067badb |
---|---|
1 use clap::Clap; | 1 use clap::Clap; |
2 use nix; | 2 use ie_virsh::{command, user}; |
3 use std::process::Command; | |
4 | 3 |
5 #[derive(Clap)] | 4 #[derive(Clap)] |
6 #[clap(version = "1.0", author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")] | 5 #[clap(version = "1.0", author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")] |
7 struct Opts { | 6 struct Opts { |
8 #[clap(subcommand)] | 7 #[clap(subcommand)] |
17 Shutdown(Shutdown), | 16 Shutdown(Shutdown), |
18 Destroy(Destroy), | 17 Destroy(Destroy), |
19 Console(Console), | 18 Console(Console), |
20 Start(Start), | 19 Start(Start), |
21 Ttyconsole(TTyConsole), | 20 Ttyconsole(TTyConsole), |
22 VNCDisplay(VNCDisplay) | 21 VNCDisplay(VNCDisplay), //todo define-gdb dumpxml |
23 //todo define-gdb dumpxml | |
24 } | 22 } |
25 | 23 |
26 /// define (but don't start) a domain from an template XML file | 24 /// define (but don't start) a domain from an template XML file |
27 #[derive(Clap)] | 25 #[derive(Clap)] |
28 struct Define { | 26 struct Define { |
73 #[derive(Clap)] | 71 #[derive(Clap)] |
74 struct Console { | 72 struct Console { |
75 name_or_id: String, | 73 name_or_id: String, |
76 } | 74 } |
77 | 75 |
78 struct ListDumpMsg { | |
79 info_msg: String, | |
80 border_line: String, | |
81 } | |
82 | |
83 struct VM { | 76 struct VM { |
84 id: u32, | 77 id: u32, |
85 name: String, | 78 name: String, |
86 is_vm_running: bool, | 79 is_vm_running: bool, |
87 } | 80 } |
88 | 81 |
89 fn main() { | 82 fn main() { |
90 let opts: Opts = Opts::parse(); | 83 let opts: Opts = Opts::parse(); |
91 | 84 |
92 let uid = getuid(); | 85 let uid = user::getuid(); |
93 let gid = getgid(); | 86 let gid = user::getgid(); |
94 let user_name = getlogin(uid); | 87 let user_name = user::getlogin(uid); |
95 println!("uid: {} gid: {} name: {}", uid, gid, user_name); | 88 println!("uid: {} gid: {} name: {}", uid, gid, user_name); |
96 | 89 |
97 match opts.subcmd { | 90 match opts.subcmd { |
98 SubCommand::List(_) => { | 91 SubCommand::List(_) => { |
99 set_root_id(); | 92 user::set_root_id(); |
100 let (ldump_msg, vm_list_strs) = list_command(user_name); | 93 command::list_command(user_name); |
101 println!("{}\n{}", ldump_msg.info_msg, ldump_msg.border_line); | |
102 for vm_info in vm_list_strs { | |
103 println!("{}", vm_info); | |
104 } | |
105 } | 94 } |
106 SubCommand::Define(name) => {} | 95 SubCommand::Define(name) => {} |
107 SubCommand::Shutdown(name) => {} | 96 SubCommand::Shutdown(name) => {} |
108 SubCommand::Console(name) => {} | 97 SubCommand::Console(name) => {} |
109 | 98 |
113 | 102 |
114 //set_root_id(); | 103 //set_root_id(); |
115 //list_command(user_name); | 104 //list_command(user_name); |
116 } | 105 } |
117 | 106 |
118 fn getlogin(uid: u32) -> &'static str { | |
119 use std::ffi::CStr; | |
120 let user_passwd = unsafe { nix::libc::getpwuid(uid) }; | |
121 let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) }; | |
122 return c_str.to_str().unwrap(); | |
123 } | |
124 | |
125 fn getuid() -> u32 { | |
126 let uid_struct = nix::unistd::getuid(); | |
127 return uid_struct.into(); | |
128 } | |
129 | |
130 fn getgid() -> u32 { | |
131 let gid_struct = nix::unistd::getgid(); | |
132 return gid_struct.into(); | |
133 } | |
134 | |
135 fn list_command(user_name: &'static str) -> (ListDumpMsg, Vec<String>) { | |
136 let output = Command::new("virsh") | |
137 .arg("list") | |
138 .arg("--all") | |
139 .output() | |
140 .expect("failed to virsh"); | |
141 let virsh_list = String::from_utf8_lossy(&output.stdout); | |
142 let mut virsh_list = virsh_list.split("\n"); | |
143 | |
144 let info_msg = virsh_list.next().unwrap(); | |
145 let border_line = virsh_list.next().unwrap(); | |
146 let ldump_msg = ListDumpMsg { | |
147 info_msg: String::from(info_msg), | |
148 border_line: String::from(border_line), | |
149 }; | |
150 | |
151 return ( | |
152 ldump_msg, | |
153 virsh_list | |
154 .filter(|&x| x.contains(user_name)) | |
155 .map(|x| x.to_string()) | |
156 .collect(), | |
157 ); | |
158 } | |
159 | |
160 fn set_root_id() { | |
161 let id = 0; | |
162 match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) { | |
163 Err(err) => panic!("failed seteuid {}", err), | |
164 Ok(_) => {} | |
165 } | |
166 match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) { | |
167 Err(err) => panic!("failed setedid{}", err), | |
168 Ok(_) => {} | |
169 } | |
170 | |
171 match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) { | |
172 Err(err) => panic!("failed setuid {}", err), | |
173 Ok(_) => {} | |
174 } | |
175 | |
176 match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) { | |
177 Err(err) => panic!("failed setdid{}", err), | |
178 Ok(_) => {} | |
179 } | |
180 } | |
181 | |
182 fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel: bool) {} | 107 fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel: bool) {} |