Replace gethostname syscall with uname

This commit is contained in:
Jeremy Soller
2018-12-01 19:11:46 -07:00
committed by Jeremy Soller
parent 52fd4d7e83
commit 3075b8b69f
4 changed files with 61 additions and 61 deletions
+24 -3
View File
@@ -1,6 +1,6 @@
//! unistd implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html
use core::{ptr, slice};
use core::{mem, ptr, slice};
use c_str::CStr;
use header::errno;
@@ -286,8 +286,29 @@ pub extern "C" fn gethostid() -> c_long {
}
#[no_mangle]
pub unsafe extern "C" fn gethostname(name: *mut c_char, len: size_t) -> c_int {
Sys::gethostname(name, len)
pub unsafe extern "C" fn gethostname(mut name: *mut c_char, mut len: size_t) -> c_int {
let mut uts = mem::uninitialized();
let err = Sys::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
}
// #[no_mangle]
+1 -28
View File
@@ -1,4 +1,4 @@
use core::{mem, ptr};
use core::ptr;
use core_io::Write;
use super::types::*;
@@ -193,33 +193,6 @@ impl Pal for Sys {
e(unsafe { syscall!(GETGID) }) as gid_t
}
fn gethostname(mut name: *mut c_char, mut len: size_t) -> c_int {
unsafe {
let mut uts = mem::uninitialized();
let err = Sys::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
}
}
fn getpgid(pid: pid_t) -> pid_t {
e(unsafe { syscall!(GETPGID, pid) }) as pid_t
}
-2
View File
@@ -72,8 +72,6 @@ pub trait Pal {
fn getgid() -> gid_t;
fn gethostname(name: *mut c_char, len: size_t) -> c_int;
fn getpgid(pid: pid_t) -> pid_t;
fn getpid() -> pid_t;
+36 -28
View File
@@ -513,32 +513,6 @@ impl Pal for Sys {
e(syscall::getgid()) as gid_t
}
fn gethostname(name: *mut c_char, len: size_t) -> c_int {
fn inner(name: &mut [u8]) -> io::Result<()> {
let mut file = File::open(
&CString::new("/etc/hostname").unwrap(),
fcntl::O_RDONLY | fcntl::O_CLOEXEC,
)?;
let mut read = 0;
loop {
match file.read(&mut name[read..])? {
0 => break,
n => read += n,
}
}
Ok(())
}
match inner(unsafe { slice::from_raw_parts_mut(name as *mut u8, len as usize) }) {
Ok(()) => 0,
Err(_) => unsafe {
errno = EIO;
-1
},
}
}
fn getpgid(pid: pid_t) -> pid_t {
e(syscall::getpgid(pid as usize)) as pid_t
}
@@ -1070,7 +1044,39 @@ impl Pal for Sys {
}
fn uname(utsname: *mut utsname) -> c_int {
fn gethostname(name: &mut [u8]) -> io::Result<()> {
if name.is_empty() {
return Ok(())
}
let mut file = File::open(
&CString::new("/etc/hostname").unwrap(),
fcntl::O_RDONLY | fcntl::O_CLOEXEC,
)?;
let mut read = 0;
let name_len = name.len();
loop {
match file.read(&mut name[read..name_len - 1])? {
0 => break,
n => read += n,
}
}
name[read] = 0;
Ok(())
}
fn inner(utsname: *mut utsname) -> CoreResult<(), i32> {
match gethostname(unsafe {
slice::from_raw_parts_mut(
(*utsname).nodename.as_mut_ptr() as *mut u8,
(*utsname).nodename.len()
)
}) {
Ok(_) => (),
Err(_) => return Err(EIO),
}
let file_path = c_str!("sys:uname");
let mut file = match File::open(file_path, fcntl::O_RDONLY | fcntl::O_CLOEXEC) {
Ok(ok) => ok,
@@ -1099,15 +1105,17 @@ impl Pal for Sys {
unsafe {
read_line(&mut (*utsname).sysname)?;
read_line(&mut (*utsname).nodename)?;
read_line(&mut (*utsname).release)?;
read_line(&mut (*utsname).version)?;
read_line(&mut (*utsname).machine)?;
// Version is not provided
ptr::write_bytes((*utsname).version.as_mut_ptr(), 0, UTSLENGTH);
// Redox doesn't provide domainname in sys:uname
//read_line(&mut (*utsname).domainname)?;
ptr::write_bytes((*utsname).domainname.as_mut_ptr(), 0, UTSLENGTH);
}
Ok(())
}