Skip to content

New syscalls #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use self::linux::*;

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

#[allow(improper_ctypes)]
extern {
Expand All @@ -28,7 +28,7 @@ mod ffi {

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

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

#[inline]
pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<uid_t>, group: Option<gid_t>) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
// We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3).
unsafe { ffi::chown(cstr.as_ptr(), owner.unwrap_or(0 - 1), group.unwrap_or(0 - 1)) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a tiny comment explaining the 0-1 thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}));

Errno::result(res).map(drop)
}

fn to_exec_array(args: &[CString]) -> Vec<*const c_char> {
use std::ptr;
use libc::c_char;
Expand Down Expand Up @@ -369,6 +379,20 @@ pub fn getegid() -> gid_t {
unsafe { ffi::getegid() }
}

#[inline]
pub fn setuid(uid: uid_t) -> Result<()> {
let res = unsafe { ffi::setuid(uid) };

Errno::result(res).map(drop)
}

#[inline]
pub fn setgid(gid: gid_t) -> Result<()> {
let res = unsafe { ffi::setgid(gid) };

Errno::result(res).map(drop)
}

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux {
use sys::syscall::{syscall, SYSPIVOTROOT};
Expand Down