瀏覽代碼

Merge pull request #47 from sajattack/unistd

implement setid functions and unlink
Jeremy Soller 7 年之前
父節點
當前提交
77c0a5b430
共有 8 個文件被更改,包括 80 次插入9 次删除
  1. 17 1
      src/platform/src/linux/mod.rs
  2. 17 0
      src/platform/src/redox/mod.rs
  3. 5 5
      src/unistd/src/lib.rs
  4. 2 0
      tests/.gitignore
  5. 4 2
      tests/Makefile
  6. 2 1
      tests/link.c
  7. 26 0
      tests/setid.c
  8. 7 0
      tests/unlink.c

+ 17 - 1
src/platform/src/linux/mod.rs

@@ -116,7 +116,7 @@ pub fn getuid() -> uid_t {
 }
 
 pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
-    e(unsafe { syscall!(LINKAT, AT_FDCWD, path1, path2) }) as c_int
+    e(unsafe { syscall!(LINKAT, AT_FDCWD, path1, AT_FDCWD, path2, 0) }) as c_int
 }
 
 pub fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
@@ -139,6 +139,22 @@ pub fn rmdir(path: *const c_char) -> c_int {
     e(unsafe { syscall!(UNLINKAT, AT_FDCWD, path, AT_REMOVEDIR) }) as c_int
 }
 
+pub fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
+    e(unsafe { syscall!(SETPGID, pid, pgid) }) as c_int
+}
+
+pub fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
+    e(unsafe { syscall!(SETREGID, rgid, egid) }) as c_int
+}
+
+pub fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
+    e(unsafe { syscall!(SETREUID, ruid, euid) }) as c_int
+}
+
+pub fn unlink(path: *const c_char) -> c_int {
+    e(unsafe { syscall!(UNLINKAT, AT_FDCWD, path, 0) }) as c_int
+}
+
 pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
     e(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
 }

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

@@ -157,6 +157,23 @@ pub fn rmdir(path: *const c_char) -> c_int {
     e(syscall::rmdir(path)) as c_int
 }
 
+pub fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
+    e(syscall::setpgid(pid as usize, pgid as usize)) as c_int
+}
+
+pub fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
+    e(syscall::setregid(rgid as usize, egid as usize)) as c_int
+}
+
+pub fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
+    e(syscall::setreuid(ruid as usize, euid as usize)) as c_int
+}
+
+pub fn unlink(path: *const c_char) -> c_int {
+    let path = unsafe { c_str(path) };
+    e(syscall::unlink(path)) as c_int
+}
+
 pub fn write(fd: c_int, buf: &[u8]) -> ssize_t {
     e(syscall::write(fd as usize, buf)) as ssize_t
 }

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

@@ -225,7 +225,7 @@ pub extern "C" fn getpgid(pid: pid_t) -> pid_t {
 
 #[no_mangle]
 pub extern "C" fn getpgrp() -> pid_t {
-    unimplemented!();
+    platform::getpgid(platform::getpid())
 }
 
 #[no_mangle]
@@ -351,7 +351,7 @@ pub extern "C" fn setgid(gid: gid_t) -> c_int {
 
 #[no_mangle]
 pub extern "C" fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
-    unimplemented!();
+    platform::setpgid(pid, pgid)
 }
 
 #[no_mangle]
@@ -361,12 +361,12 @@ pub extern "C" fn setpgrp() -> pid_t {
 
 #[no_mangle]
 pub extern "C" fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
-    unimplemented!();
+    platform::setregid(rgid, egid)
 }
 
 #[no_mangle]
 pub extern "C" fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
-    unimplemented!();
+    platform::setreuid(ruid, euid)
 }
 
 #[no_mangle]
@@ -436,7 +436,7 @@ pub extern "C" fn ualarm(useconds: useconds_t, interval: useconds_t) -> useconds
 
 #[no_mangle]
 pub extern "C" fn unlink(path: *const c_char) -> c_int {
-    unimplemented!();
+    platform::unlink(path)
 }
 
 #[no_mangle]

+ 2 - 0
tests/.gitignore

@@ -23,5 +23,7 @@
 /pipe
 /printf
 /rmdir
+/setid
+/unlink
 /stdlib/strtol
 /write

+ 4 - 2
tests/Makefile

@@ -16,9 +16,11 @@ BINS=\
 	getid \
 	link \
 	math \
-	rmdir \
 	pipe \
-	printf \
+	printf \ 
+	rmdir \
+	setid \
+	unlink \
 	stdlib/strtol \
 	write
 

+ 2 - 1
tests/link.c

@@ -1,5 +1,6 @@
 #include <unistd.h>
 
 int main(int argc, char** argv) {
-    int status = link("link.c", "link.out");
+    link("./link.c", "./link.out");
+    perror("link");
 }

+ 26 - 0
tests/setid.c

@@ -0,0 +1,26 @@
+/*
+ * The process joins process group 0.
+ */
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+int main( void )
+  {
+    if( setpgid( getpid(), 0 ) == -1 ) {
+        perror( "setpgid" );
+    }
+    printf( "%d belongs to process group %d\n",
+         getpid(), getpgrp() );
+
+    if( setregid(-1, -1) == -1 ) {
+        perror( "setregid" );
+    }
+    printf("%d has egid %d and gid %d\n", getpid(), getegid(), getgid());
+
+    if( setreuid(-1, -1) == -1 ) {
+        perror( "setreuid" );
+    }
+    printf("%d has euid %d and uid %d\n", getpid(), geteuid(), getuid());
+  }

+ 7 - 0
tests/unlink.c

@@ -0,0 +1,7 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char** argv) {
+    link("./unlink.c", "./unlink.out");
+    perror("unlink");
+}