Add uname

This commit is contained in:
Tate, Hongliang Tian 2019-04-08 23:27:40 +08:00 committed by Tate Tian
parent abe553ba1c
commit 8846c62b5e
8 changed files with 87 additions and 1 deletions

@ -41,6 +41,7 @@ mod syscall;
mod time; mod time;
mod util; mod util;
mod vm; mod vm;
mod misc;
use prelude::*; use prelude::*;

@ -0,0 +1,5 @@
use super::*;
mod uname;
pub use self::uname::{utsname_t, do_uname};

@ -0,0 +1,51 @@
use super::*;
use std::ffi::{CStr, CString};
/// A sample of `struct utsname`
/// ```
/// sysname = Linux
/// nodename = tian-nuc
/// release = 4.15.0-42-generic
/// version = #45~16.04.1-Ubuntu SMP Mon Nov 19 13:02:27 UTC 2018
/// machine = x86_64
/// domainname = (none)
/// ```
///
/// By the way, UTS stands for UNIX Timesharing System.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct utsname_t {
sysname: [u8; 65],
nodename: [u8; 65],
release: [u8; 65],
version: [u8; 65],
machine: [u8; 65],
domainname: [u8; 65],
}
pub fn do_uname(name: &mut utsname_t) -> Result<(), Error> {
copy_from_cstr_to_u8_array(&SYSNAME, &mut name.sysname);
copy_from_cstr_to_u8_array(&NODENAME, &mut name.nodename);
copy_from_cstr_to_u8_array(&RELEASE, &mut name.release);
copy_from_cstr_to_u8_array(&VERSION, &mut name.version);
copy_from_cstr_to_u8_array(&MACHINE, &mut name.machine);
copy_from_cstr_to_u8_array(&DOMAINNAME, &mut name.domainname);
Ok(())
}
lazy_static! {
static ref SYSNAME : CString = CString::new("Occlum").unwrap();
static ref NODENAME: CString = CString::new("occlum-node").unwrap();
static ref RELEASE: CString = CString::new("0.1").unwrap();
static ref VERSION: CString = CString::new("0.1").unwrap();
static ref MACHINE: CString = CString::new("x86-64").unwrap();
static ref DOMAINNAME: CString = CString::new("").unwrap();
}
fn copy_from_cstr_to_u8_array(src: &CStr, dst: &mut [u8]) {
let src : &[u8] = src.to_bytes_with_nul();
let len = min(dst.len() - 1, src.len());
dst[..len].copy_from_slice(&src[..len]);
dst[len] = 0;
}

@ -21,6 +21,7 @@ pub use std::iter::Iterator;
pub use std::rc::Rc; pub use std::rc::Rc;
pub use std::string::String; pub use std::string::String;
pub use std::vec::Vec; pub use std::vec::Vec;
pub use std::cmp::{min, max};
pub use errno::Errno; pub use errno::Errno;
pub use errno::Errno::*; pub use errno::Errno::*;

@ -8,6 +8,7 @@ use std::ptr;
use time::timeval_t; use time::timeval_t;
use util::mem_util::from_user::*; use util::mem_util::from_user::*;
use vm::{VMAreaFlags, VMResizeOptions}; use vm::{VMAreaFlags, VMResizeOptions};
use misc::{utsname_t};
use super::*; use super::*;
@ -131,6 +132,8 @@ pub extern "C" fn dispatch_syscall(
SYS_GETTIMEOFDAY => do_gettimeofday(arg0 as *mut timeval_t), SYS_GETTIMEOFDAY => do_gettimeofday(arg0 as *mut timeval_t),
SYS_UNAME => do_uname(arg0 as *mut utsname_t),
_ => do_unknown(num, arg0, arg1, arg2, arg3, arg4, arg5), _ => do_unknown(num, arg0, arg1, arg2, arg3, arg4, arg5),
}; };
debug!("syscall return: {:?}", ret); debug!("syscall return: {:?}", ret);
@ -696,3 +699,9 @@ fn do_set_tid_address(tidptr: *mut pid_t) -> Result<isize, Error> {
check_mut_ptr(tidptr)?; check_mut_ptr(tidptr)?;
process::do_set_tid_address(tidptr).map(|tid| tid as isize) process::do_set_tid_address(tidptr).map(|tid| tid as isize)
} }
fn do_uname(name: *mut utsname_t) -> Result<isize, Error> {
check_mut_ptr(name)?;
let name = unsafe { &mut *name };
misc::do_uname(name).map(|_| 0)
}

@ -4,7 +4,7 @@ PROJECT_DIR := $(realpath $(CUR_DIR)/../)
# Dependencies: need to be compiled but not to run by any Makefile target # Dependencies: need to be compiled but not to run by any Makefile target
TEST_DEPS := dev_null TEST_DEPS := dev_null
# Tests: need to be compiled and run by test-% target # Tests: need to be compiled and run by test-% target
TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir link tls pthread TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir link tls pthread uname
# Benchmarks: need to be compiled and run by bench-% target # Benchmarks: need to be compiled and run by bench-% target
BENCHES := spawn_and_exit_latency pipe_throughput BENCHES := spawn_and_exit_latency pipe_throughput

5
test/uname/Makefile Normal file

@ -0,0 +1,5 @@
include ../test_common.mk
EXTRA_C_FLAGS :=
EXTRA_LINK_FLAGS :=
BIN_ARGS :=

14
test/uname/main.c Normal file

@ -0,0 +1,14 @@
#include <sys/utsname.h>
#include <stdio.h>
int main(void) {
struct utsname name;
uname(&name);
printf("sysname = %s\n", (const char*)&name.sysname);
printf("nodename = %s\n", (const char*)&name.nodename);
printf("release = %s\n", (const char*)&name.release);
printf("version = %s\n", (const char*)&name.version);
printf("machine = %s\n", (const char*)&name.machine);
printf("domainname = %s\n", (const char*)&name.__domainname);
return 0;
}