changeset 3:5bdb02e05c86

show virsh list --all
author AnaTofuZ <anatofuz@gmail.com>
date Thu, 22 Oct 2020 17:40:16 +0900
parents 1632a34a3f6c
children 668a91cd7be8
files Cargo.lock Cargo.toml src/main.rs
diffstat 3 files changed, 46 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Cargo.lock	Thu Oct 22 13:09:13 2020 +0900
+++ b/Cargo.lock	Thu Oct 22 17:40:16 2020 +0900
@@ -22,6 +22,7 @@
 name = "ie-virsh"
 version = "0.1.0"
 dependencies = [
+ "libc",
  "nix",
 ]
 
--- a/Cargo.toml	Thu Oct 22 13:09:13 2020 +0900
+++ b/Cargo.toml	Thu Oct 22 17:40:16 2020 +0900
@@ -8,3 +8,9 @@
 
 [dependencies]
 nix = "0.19.0"
+libc = "0.2.77"
+
+[profile.release]
+codegen-units = 1
+lto = true
+opt-level = "z"
--- a/src/main.rs	Thu Oct 22 13:09:13 2020 +0900
+++ b/src/main.rs	Thu Oct 22 17:40:16 2020 +0900
@@ -1,12 +1,18 @@
 use nix;
+use std::process::Command;
+//use libc;
 
 fn main() {
-    let uid = getuid();
-    let gid = getgid();
-    let login_user = getlogin(uid);
-    println!("uid: {} gid: {} name: {}", uid, gid, login_user);
+    let uid       = getuid();
+    let gid       = getgid();
+    let user_name = getlogin(uid);
+    println!("uid: {} gid: {} name: {}", uid, gid, user_name);
+    set_root_id();
+
+    list_command();
 }
 
+
 fn getlogin(uid: u32) -> &'static str {
     use std::ffi::CStr;
     let user_passwd = unsafe { nix::libc::getpwuid(uid)};
@@ -16,10 +22,37 @@
 
 fn getuid() -> u32 {
     let uid_struct = nix::unistd::getuid();
-    return uid_struct.as_raw();
+    return uid_struct.into();
 }
 
 fn getgid() -> u32 {
     let gid_struct = nix::unistd::getgid();
-    return gid_struct.as_raw();
+    return gid_struct.into();
+}
+
+fn list_command() {
+    Command::new("virsh").arg("list").arg("--all").status().expect("failed to virsh");
 }
+
+
+fn set_root_id(){
+    let id = 0;
+    match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) {
+        Err(err) => panic!("failed seteuid {}", err),
+        Ok(_) => {}
+    }
+    match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) {
+        Err(err) => panic!("failed setedid{}", err),
+        Ok(_) => {}
+    }
+
+    match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) {
+        Err(err) => panic!("failed setuid {}", err),
+        Ok(_) => {}
+    }
+
+    match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) {
+        Err(err) => panic!("failed setdid{}", err),
+        Ok(_) => {}
+    }
+}
\ No newline at end of file