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