Skip to content

Add gethostname and sethostname #2

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 1 commit into from
Sep 11, 2014
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
26 changes: 25 additions & 1 deletion src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use errno::{SysResult, SysError, from_ffi};
pub use self::linux::*;

mod ffi {
use libc::{c_char, c_int};
use libc::{c_char, c_int, size_t};
pub use libc::{close, read, write};

extern {
Expand All @@ -29,6 +29,14 @@ mod ffi {
// run the current process in the background
// doc: http://man7.org/linux/man-pages/man3/daemon.3.html
pub fn daemon(nochdir: c_int, noclose: c_int) -> c_int;

// sets the hostname to the value given
// doc: http://man7.org/linux/man-pages/man2/gethostname.2.html
pub fn gethostname(name: *mut c_char, len: size_t) -> c_int;

// gets the hostname
// doc: http://man7.org/linux/man-pages/man2/gethostname.2.html
pub fn sethostname(name: *const c_char, len: size_t) -> c_int;
}
}

Expand Down Expand Up @@ -102,6 +110,22 @@ pub fn daemon(nochdir: bool, noclose: bool) -> SysResult<()> {
from_ffi(res)
}

pub fn sethostname(name: &[u8]) -> SysResult<()> {
let ptr = name.as_ptr() as *const c_char;
let len = name.len() as u64;

let res = unsafe { ffi::sethostname(ptr, len) };
from_ffi(res)
}

pub fn gethostname(name: &mut [u8]) -> SysResult<()> {
let ptr = name.as_mut_ptr() as *mut c_char;
let len = name.len() as u64;

let res = unsafe { ffi::gethostname(ptr, len) };
from_ffi(res)
}

pub fn close(fd: Fd) -> SysResult<()> {
let res = unsafe { ffi::close(fd) };
from_ffi(res)
Expand Down