6
|
1 use clap::{App, Arg, Clap};
|
1
|
2 use nix;
|
3
|
3 use std::process::Command;
|
1
|
4
|
5
|
5 #[derive(Clap)]
|
|
6 struct Opts {
|
|
7 #[clap(subcommand)]
|
|
8 subcmd: SubCommand,
|
|
9 }
|
|
10
|
|
11 #[derive(Clap)]
|
|
12 enum SubCommand {
|
|
13 define(Define),
|
|
14 shutdown(Shutdown),
|
|
15 }
|
|
16
|
|
17 #[derive(Clap)]
|
|
18 struct Define {
|
|
19 name: String,
|
|
20 }
|
|
21
|
|
22 #[derive(Clap)]
|
|
23 struct Shutdown {
|
|
24 name: String,
|
|
25 }
|
|
26
|
|
27 struct VM {
|
|
28 id: u32,
|
|
29 name: String,
|
|
30 is_vm_running: bool,
|
|
31 }
|
|
32
|
0
|
33 fn main() {
|
4
|
34 let uid = getuid();
|
|
35 let gid = getgid();
|
3
|
36 let user_name = getlogin(uid);
|
|
37 println!("uid: {} gid: {} name: {}", uid, gid, user_name);
|
5
|
38
|
|
39 let matches = App::new("ie-virsh")
|
6
|
40 .version("1.0")
|
|
41 .author("AnatofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")
|
|
42 .about("virsh wrapper for ie members")
|
|
43 .get_matches();
|
5
|
44
|
6
|
45 /*
|
5
|
46 let opts: Opts = Opts::parse();
|
|
47
|
|
48 match opts.subcmd {
|
|
49 SubCommand::define(name) => {
|
|
50 },
|
|
51 SubCommand::shutdown(name) => {
|
|
52
|
|
53 }
|
|
54 }
|
|
55 */
|
|
56
|
|
57 //set_root_id();
|
|
58 //list_command(user_name);
|
0
|
59 }
|
1
|
60
|
2
|
61 fn getlogin(uid: u32) -> &'static str {
|
|
62 use std::ffi::CStr;
|
4
|
63 let user_passwd = unsafe { nix::libc::getpwuid(uid) };
|
|
64 let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) };
|
1
|
65 return c_str.to_str().unwrap();
|
|
66 }
|
|
67
|
|
68 fn getuid() -> u32 {
|
|
69 let uid_struct = nix::unistd::getuid();
|
3
|
70 return uid_struct.into();
|
1
|
71 }
|
|
72
|
|
73 fn getgid() -> u32 {
|
|
74 let gid_struct = nix::unistd::getgid();
|
3
|
75 return gid_struct.into();
|
|
76 }
|
|
77
|
5
|
78 fn list_command(user_name: &'static str) {
|
4
|
79 let output = Command::new("virsh")
|
|
80 .arg("list")
|
|
81 .arg("--all")
|
|
82 .output()
|
|
83 .expect("failed to virsh");
|
|
84 let virsh_list = String::from_utf8_lossy(&output.stdout);
|
|
85 let mut virsh_list = virsh_list.split("\n");
|
|
86 let info_msg = virsh_list.next().unwrap();
|
|
87 let border_line = virsh_list.next().unwrap();
|
|
88
|
5
|
89 let virsh_emit_vm_list = virsh_list.filter(|&x| x.contains(user_name));
|
|
90
|
6
|
91 let mut vm_list: Vec<VM> = Vec::new();
|
4
|
92
|
|
93 println!("{}\n{}", info_msg, border_line);
|
5
|
94 for emit_vm in virsh_emit_vm_list {
|
|
95 let mut vm_infos = emit_vm.split(" ");
|
|
96 let vm_status = vm_infos.next().unwrap();
|
|
97 let is_vm_running = if vm_status != "-" { true } else { false };
|
|
98 let name = vm_infos.next().unwrap();
|
6
|
99 let vm = VM {
|
|
100 id: 0,
|
|
101 is_vm_running: is_vm_running,
|
|
102 name: String::from(name),
|
|
103 };
|
5
|
104 vm_list.push(vm);
|
4
|
105 }
|
1
|
106 }
|
3
|
107
|
4
|
108 fn set_root_id() {
|
3
|
109 let id = 0;
|
|
110 match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) {
|
|
111 Err(err) => panic!("failed seteuid {}", err),
|
|
112 Ok(_) => {}
|
|
113 }
|
|
114 match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) {
|
|
115 Err(err) => panic!("failed setedid{}", err),
|
|
116 Ok(_) => {}
|
|
117 }
|
|
118
|
|
119 match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) {
|
|
120 Err(err) => panic!("failed setuid {}", err),
|
|
121 Ok(_) => {}
|
|
122 }
|
|
123
|
|
124 match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) {
|
|
125 Err(err) => panic!("failed setdid{}", err),
|
|
126 Ok(_) => {}
|
|
127 }
|
4
|
128 }
|
5
|
129
|
6
|
130 fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel: bool) {}
|