Skip to content

Commit 7c00440

Browse files
committed
Auto merge of #274 - abbradar:new-syscalls, r=kamalmarhubi
New syscalls Part of #270
2 parents 6cad03a + 6a2522c commit 7c00440

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/unistd.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::linux::*;
1313

1414
mod ffi {
1515
use libc::{c_char, c_int, size_t};
16-
pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid};
16+
pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid, setuid, setgid, chown};
1717

1818
#[allow(improper_ctypes)]
1919
extern {
@@ -28,7 +28,7 @@ mod ffi {
2828

2929
// Execute PATH with arguments ARGV and environment from `environ'.
3030
// doc: http://man7.org/linux/man-pages/man3/execv.3.html
31-
pub fn execv (path: *const c_char, argv: *const *const c_char) -> c_int;
31+
pub fn execv(path: *const c_char, argv: *const *const c_char) -> c_int;
3232

3333
// execute program
3434
// doc: http://man7.org/linux/man-pages/man2/execve.2.html
@@ -157,6 +157,16 @@ pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
157157
Errno::result(res).map(drop)
158158
}
159159

160+
#[inline]
161+
pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<uid_t>, group: Option<gid_t>) -> Result<()> {
162+
let res = try!(path.with_nix_path(|cstr| {
163+
// We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3).
164+
unsafe { ffi::chown(cstr.as_ptr(), owner.unwrap_or(0 - 1), group.unwrap_or(0 - 1)) }
165+
}));
166+
167+
Errno::result(res).map(drop)
168+
}
169+
160170
fn to_exec_array(args: &[CString]) -> Vec<*const c_char> {
161171
use std::ptr;
162172
use libc::c_char;
@@ -369,6 +379,20 @@ pub fn getegid() -> gid_t {
369379
unsafe { ffi::getegid() }
370380
}
371381

382+
#[inline]
383+
pub fn setuid(uid: uid_t) -> Result<()> {
384+
let res = unsafe { ffi::setuid(uid) };
385+
386+
Errno::result(res).map(drop)
387+
}
388+
389+
#[inline]
390+
pub fn setgid(gid: gid_t) -> Result<()> {
391+
let res = unsafe { ffi::setgid(gid) };
392+
393+
Errno::result(res).map(drop)
394+
}
395+
372396
#[cfg(any(target_os = "linux", target_os = "android"))]
373397
mod linux {
374398
use sys::syscall::{syscall, SYSPIVOTROOT};

0 commit comments

Comments
 (0)