annotate src/main.rs @ 5:ef1358b993e1

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