diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index 0cf01a27e077961a6b4afd03f853b039319f0233..6dd4e99253bfcda1ea7475b114ddeef1eb08e073 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -1,4 +1,4 @@ -use core::ptr; +use core::{mem, ptr}; use types::*; use *; @@ -8,6 +8,19 @@ const AT_EMPTY_PATH: c_int = 0x1000; const AT_REMOVEDIR: c_int = 0x200; const AT_SYMLINK_NOFOLLOW: c_int = 0x100; +// Also in sys_utsname. Has to be both because cbindgen +const UTSLENGTH: usize = 65; + +#[repr(C)] +pub struct utsname { + pub sysname: [c_char; UTSLENGTH], + pub nodename: [c_char; UTSLENGTH], + pub release: [c_char; UTSLENGTH], + pub version: [c_char; UTSLENGTH], + pub machine: [c_char; UTSLENGTH], + pub domainname: [c_char; UTSLENGTH], +} + pub fn e(sys: usize) -> usize { if (sys as isize) < 0 && (sys as isize) >= -256 { unsafe { @@ -123,6 +136,34 @@ pub fn getgid() -> gid_t { e(unsafe { syscall!(GETGID) }) } +pub unsafe fn gethostname(mut name: *mut c_char, len: size_t) -> c_int { + // len only needs to be mutable on linux + let mut len = len; + + let mut uts = mem::uninitialized(); + let err = uname(&mut uts); + if err < 0 { + mem::forget(uts); + return err; + } + for c in uts.nodename.iter() { + if len == 0 { + break; + } + len -= 1; + + *name = *c; + + if *name == 0 { + // We do want to copy the zero also, so we check this after the copying. + break; + } + + name = name.offset(1); + } + 0 +} + pub unsafe fn getpeername( socket: c_int, address: *mut sockaddr, @@ -309,7 +350,7 @@ pub fn socketpair(domain: c_int, kind: c_int, protocol: c_int, socket_vector: *m e(unsafe { syscall!(SOCKETPAIR, domain, kind, protocol, socket_vector) }) as c_int } -pub fn uname(utsname: usize) -> c_int { +pub fn uname(utsname: *mut utsname) -> c_int { e(unsafe { syscall!(UNAME, utsname, 0) }) as c_int } diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 60326c4cf195b162a11c8afe3575449b6d0e89b7..18ac015c5603621ea99a658cf11bff46aa80da99 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -284,6 +284,22 @@ pub fn getgid() -> gid_t { e(syscall::getgid()) as gid_t } +pub unsafe fn gethostname(mut name: *mut c_char, len: size_t) -> c_int { + let fd = e(syscall::open("/etc/hostname", O_RDONLY)) as i32; + if fd < 0 { + return fd; + } + let mut reader = FileReader(fd); + for _ in 0..len { + if !reader.read_u8(&mut *(name as *mut u8)) { + *name = 0; + break; + } + name = name.offset(1); + } + 0 +} + unsafe fn inner_get_name( local: bool, socket: c_int, diff --git a/src/sys_utsname/src/lib.rs b/src/sys_utsname/src/lib.rs index 07f753eb642859c596fc8e9b4cc78ce14a28e495..06363566c3979d49215a5c6842f3d80fc34285e7 100644 --- a/src/sys_utsname/src/lib.rs +++ b/src/sys_utsname/src/lib.rs @@ -23,7 +23,7 @@ mod inner { #[no_mangle] pub unsafe extern "C" fn uname(uts: *mut utsname) -> c_int { - platform::uname(uts as usize) + platform::uname(uts as *mut platform::utsname) } } #[cfg(target_os = "linux")] diff --git a/src/unistd/src/lib.rs b/src/unistd/src/lib.rs index d04d44c93b8153d9c8f139af8289ac8bbe96f7a2..1a1c081a1e9a0e10f43058e3f50fa2a596a7b047 100644 --- a/src/unistd/src/lib.rs +++ b/src/unistd/src/lib.rs @@ -205,55 +205,8 @@ pub extern "C" fn gethostid() -> c_long { } #[no_mangle] -pub unsafe extern "C" fn gethostname(mut name: *mut c_char, len: size_t) -> c_int { - #[cfg(target_os = "linux")] - { - use core::mem; - - // len only needs to be mutable on linux - let mut len = len; - - let mut uts: sys_utsname::utsname = mem::uninitialized(); - let err = sys_utsname::uname(&mut uts); - if err < 0 { - mem::forget(uts); - return err; - } - for c in uts.nodename.iter() { - if len == 0 { - break; - } - len -= 1; - - *name = *c; - - if *name == 0 { - // We do want to copy the zero also, so we check this after the copying. - break; - } - - name = name.offset(1); - } - } - #[cfg(target_os = "redox")] - { - use platform::syscall::flag::*; - use platform::{e, FileReader, Read}; - - let fd = e(platform::syscall::open("/etc/hostname", O_RDONLY)) as i32; - if fd < 0 { - return fd; - } - let mut reader = FileReader(fd); - for _ in 0..len { - if !reader.read_u8(&mut *(name as *mut u8)) { - *name = 0; - break; - } - name = name.offset(1); - } - } - 0 +pub unsafe extern "C" fn gethostname(name: *mut c_char, len: size_t) -> c_int { + platform::gethostname(name, len) } // #[no_mangle]