Skip to content

Commit d873d94

Browse files
committed
Merge pull request #2 from carllerche/hostname
Add gethostname and sethostname
2 parents 1fac0ee + 98e73f1 commit d873d94

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/unistd.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use errno::{SysResult, SysError, from_ffi};
88
pub use self::linux::*;
99

1010
mod ffi {
11-
use libc::{c_char, c_int};
11+
use libc::{c_char, c_int, size_t};
1212
pub use libc::{close, read, write};
1313

1414
extern {
@@ -29,6 +29,14 @@ mod ffi {
2929
// run the current process in the background
3030
// doc: http://man7.org/linux/man-pages/man3/daemon.3.html
3131
pub fn daemon(nochdir: c_int, noclose: c_int) -> c_int;
32+
33+
// sets the hostname to the value given
34+
// doc: http://man7.org/linux/man-pages/man2/gethostname.2.html
35+
pub fn gethostname(name: *mut c_char, len: size_t) -> c_int;
36+
37+
// gets the hostname
38+
// doc: http://man7.org/linux/man-pages/man2/gethostname.2.html
39+
pub fn sethostname(name: *const c_char, len: size_t) -> c_int;
3240
}
3341
}
3442

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

113+
pub fn sethostname(name: &[u8]) -> SysResult<()> {
114+
let ptr = name.as_ptr() as *const c_char;
115+
let len = name.len() as u64;
116+
117+
let res = unsafe { ffi::sethostname(ptr, len) };
118+
from_ffi(res)
119+
}
120+
121+
pub fn gethostname(name: &mut [u8]) -> SysResult<()> {
122+
let ptr = name.as_mut_ptr() as *mut c_char;
123+
let len = name.len() as u64;
124+
125+
let res = unsafe { ffi::gethostname(ptr, len) };
126+
from_ffi(res)
127+
}
128+
105129
pub fn close(fd: Fd) -> SysResult<()> {
106130
let res = unsafe { ffi::close(fd) };
107131
from_ffi(res)

0 commit comments

Comments
 (0)