annotate src/main.rs @ 6:08247e54d78d

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