changeset 1:dba3414e8f7e

get uid, gid, login_user
author AnaTofuZ <anatofuz@gmail.com>
date Thu, 22 Oct 2020 12:57:16 +0900
parents 3ef828bc5261
children 1632a34a3f6c
files Cargo.lock Cargo.toml src/main.rs
diffstat 3 files changed, 70 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Cargo.lock	Thu Oct 22 12:57:16 2020 +0900
@@ -0,0 +1,44 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "cc"
+version = "1.0.61"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed67cbde08356238e75fc4656be4749481eeffb09e19f320a25237d5221c985d"
+
+[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+
+[[package]]
+name = "ie-virsh"
+version = "0.1.0"
+dependencies = [
+ "nix",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.79"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
+
+[[package]]
+name = "nix"
+version = "0.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85db2feff6bf70ebc3a4793191517d5f0331100a2f10f9bf93b5e5214f32b7b7"
+dependencies = [
+ "bitflags",
+ "cc",
+ "cfg-if",
+ "libc",
+]
--- a/Cargo.toml	Thu Oct 22 11:58:06 2020 +0900
+++ b/Cargo.toml	Thu Oct 22 12:57:16 2020 +0900
@@ -7,3 +7,4 @@
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+nix = "0.19.0"
--- a/src/main.rs	Thu Oct 22 11:58:06 2020 +0900
+++ b/src/main.rs	Thu Oct 22 12:57:16 2020 +0900
@@ -1,3 +1,27 @@
+use nix;
+
+use std::ffi::CStr;
+
 fn main() {
-    println!("Hello, world!");
+    let uid = getuid();
+    let gid = getgid();
+    let login_user = get_login_user(uid);
+    println!("{} { } {} !", uid, gid, login_user);
 }
+
+
+fn get_login_user(uid: u32) -> &'static str {
+    let user_passwd = unsafe { nix::libc::getpwuid(uid)};
+    let c_str  = unsafe { CStr::from_ptr((*user_passwd).pw_name)} ;
+    return c_str.to_str().unwrap();
+}
+
+fn getuid() -> u32 {
+    let uid_struct = nix::unistd::getuid();
+    return uid_struct.as_raw();
+}
+
+fn getgid() -> u32 {
+    let gid_struct = nix::unistd::getgid();
+    return gid_struct.as_raw();
+}