Jelajahi Sumber

getid functions

Paul Sajna 7 tahun lalu
induk
melakukan
3a07bc27c2
6 mengubah file dengan 93 tambahan dan 7 penghapusan
  1. 42 0
      platform/src/linux/mod.rs
  2. 28 0
      platform/src/redox/mod.rs
  3. 7 7
      src/unistd/src/lib.rs
  4. 1 0
      tests/.gitignore
  5. 1 0
      tests/Makefile
  6. 14 0
      tests/getid.c

+ 42 - 0
platform/src/linux/mod.rs

@@ -80,6 +80,48 @@ pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
     }
 }
 
+pub fn getegid() -> gid_t {
+    unsafe {
+        syscall!(GETEGID)
+    }
+}
+
+pub fn geteuid() -> uid_t {
+    unsafe {
+        syscall!(GETEUID)
+    }
+}
+
+pub fn getgid() -> gid_t {
+   unsafe {
+       syscall!(GETGID)
+    }
+}
+
+pub fn getpgid(pid: pid_t) -> pid_t {
+    unsafe {
+        syscall!(GETPGID, pid)
+    }
+}
+
+pub fn getpid() -> pid_t {
+    unsafe {
+        syscall!(GETPID)
+    }
+}
+
+pub fn getppid() -> pid_t {
+    unsafe {
+        syscall!(GETPPID)
+    }
+}
+
+pub fn getuid() -> uid_t {
+    unsafe {
+        syscall!(GETUID)
+    }
+}
+
 #[cfg(target_arch = "x86_64")]
 pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
     unsafe {

+ 28 - 0
platform/src/redox/mod.rs

@@ -64,6 +64,34 @@ pub fn getcwd(buf: *mut c_char, size: size_t) -> {
     }
 }
 
+pub fn getegid() -> gid_t {
+    syscall::getegid()? as gid_t
+}
+
+pub fn geteuid() -> uid_t {
+    syscall::geteuid()? as uid_t
+}
+
+pub fn getgid() -> gid_t {
+    syscall::getgid()? as gid_t
+}
+
+pub fn getpgid(pid: pid_t) -> pid_t {
+    syscall::getpgid(pid as usize)? as pid_t
+}
+
+pub fn getpid() -> pid_t {
+    syscall::getpid()? as pid_t
+}
+
+pub fn getppid() -> pid_t {
+    syscall::getppid()? as pid_t
+}
+
+pub fn getuid() -> uid_t {
+    syscall::getuid()? as pid_t
+}
+
 pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
     let path = unsafe { c_str(path) };
     syscall::open(path, (oflag as usize) | (mode as usize)).unwrap() as c_int

+ 7 - 7
src/unistd/src/lib.rs

@@ -166,17 +166,17 @@ pub extern "C" fn getdtablesize() -> c_int {
 
 #[no_mangle]
 pub extern "C" fn getegid() -> gid_t {
-    unimplemented!();
+    platform::getegid()
 }
 
 #[no_mangle]
 pub extern "C" fn geteuid() -> uid_t {
-    unimplemented!();
+    platform::geteuid()
 }
 
 #[no_mangle]
 pub extern "C" fn getgid() -> gid_t {
-    unimplemented!();
+    platform::getgid()
 }
 
 #[no_mangle]
@@ -216,7 +216,7 @@ pub extern "C" fn getpass(prompt: *const c_char) -> *mut c_char {
 
 #[no_mangle]
 pub extern "C" fn getpgid(pid: pid_t) -> pid_t {
-    unimplemented!();
+    platform::getpgid(pid)
 }
 
 #[no_mangle]
@@ -226,12 +226,12 @@ pub extern "C" fn getpgrp() -> pid_t {
 
 #[no_mangle]
 pub extern "C" fn getpid() -> pid_t {
-    unimplemented!();
+    platform::getpid()
 }
 
 #[no_mangle]
 pub extern "C" fn getppid() -> pid_t {
-    unimplemented!();
+    platform::getppid()
 }
 
 #[no_mangle]
@@ -241,7 +241,7 @@ pub extern "C" fn getsid(pid: pid_t) -> pid_t {
 
 #[no_mangle]
 pub extern "C" fn getuid() -> uid_t {
-    unimplemented!();
+    platform::getuid()
 }
 
 #[no_mangle]

+ 1 - 0
tests/.gitignore

@@ -10,6 +10,7 @@
 /fsync
 /ftruncate
 /ftruncate.out
+/getid
 /math
 /printf
 /write

+ 1 - 0
tests/Makefile

@@ -8,6 +8,7 @@ BINS=\
 	fchdir \
 	fsync \
 	ftruncate \
+	getid \
 	math \
 	printf \
 	write

+ 14 - 0
tests/getid.c

@@ -0,0 +1,14 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char** argv) {
+    gid_t egid = getegid();
+    uid_t euid = geteuid();
+    gid_t gid = getgid();
+    pid_t pgid = getpgid(0);
+    pid_t pid = getpid();
+    pid_t ppid = getppid();
+    uid_t uid = getuid();
+    printf("egid: %d, euid: %d, gid: %d, pgid: %d, pid: %d, ppid %d, uid %d\n",
+            egid, euid, gid, pgid, pid, ppid, uid);
+}