From 58b2b641830664fff0be752ce0e452db8b1a64d2 Mon Sep 17 00:00:00 2001 From: Marat Safin Date: Thu, 22 Mar 2018 14:11:45 +0300 Subject: [PATCH 1/2] add clock_gettime and time --- src/platform/src/linux/mod.rs | 4 ++++ src/platform/src/redox/mod.rs | 14 +++++++++++++ src/platform/src/types.rs | 1 + src/time/cbindgen.toml | 4 ++++ src/time/src/lib.rs | 38 +++++++++++++++++++++++++++++++++-- tests/.gitignore | 2 +- tests/Makefile | 3 ++- tests/time.c | 11 ++++++++++ 8 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 tests/time.c diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index 44d7ed2cfb..d055989d17 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -170,3 +170,7 @@ pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t { e(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t } + +pub fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int { + e(unsafe { syscall!(CLOCK_GETTIME, clk_id, tp) }) as c_int +} diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 3e03bbde90..ea7954ec20 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -220,3 +220,17 @@ pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { pub fn write(fd: c_int, buf: &[u8]) -> ssize_t { e(syscall::write(fd as usize, buf)) as ssize_t } + +pub fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int { + let mut redox_tp = unsafe { redox_timespec::from(&*tp) }; + match e(syscall::clock_gettime(clk_id as usize, &mut redox_tp)) as c_int { + -1 => -1, + _ => { + unsafe { + (*tp).tv_sec = redox_tp.tv_sec; + (*tp).tv_nsec = redox_tp.tv_nsec as i64; + }; + 0 + } + } +} diff --git a/src/platform/src/types.rs b/src/platform/src/types.rs index d6e19dbd58..722fe706ce 100644 --- a/src/platform/src/types.rs +++ b/src/platform/src/types.rs @@ -68,6 +68,7 @@ pub type clockid_t = i32; pub type timer_t = c_void; #[repr(C)] +#[derive(Default)] pub struct timespec { pub tv_sec: time_t, pub tv_nsec: c_long, diff --git a/src/time/cbindgen.toml b/src/time/cbindgen.toml index ef3a5240e6..40448b8f89 100644 --- a/src/time/cbindgen.toml +++ b/src/time/cbindgen.toml @@ -4,3 +4,7 @@ language = "C" [enum] prefix_with_name = true + +[defines] +"target_os = linux" = "__linux__" +"target_os = redox" = "__redox__" diff --git a/src/time/src/lib.rs b/src/time/src/lib.rs index 5b90e86f74..5835d7014b 100644 --- a/src/time/src/lib.rs +++ b/src/time/src/lib.rs @@ -6,6 +6,33 @@ extern crate platform; use platform::types::*; +#[cfg(target_os = "redox")] +pub const CLOCK_REALTIME: c_int = 1; +#[cfg(target_os = "redox")] +pub const CLOCK_MONOTONIC: c_int = 4; +#[cfg(target_os = "linux")] +pub const CLOCK_REALTIME: c_int = 0; +#[cfg(target_os = "linux")] +pub const CLOCK_MONOTONIC: c_int = 1; +#[cfg(target_os = "linux")] +pub const CLOCK_PROCESS_CPUTIME_ID: c_int = 2; +#[cfg(target_os = "linux")] +pub const CLOCK_THREAD_CPUTIME_ID: c_int = 3; +#[cfg(target_os = "linux")] +pub const CLOCK_MONOTONIC_RAW: c_int = 4; +#[cfg(target_os = "linux")] +pub const CLOCK_REALTIME_COARSE: c_int = 5; +#[cfg(target_os = "linux")] +pub const CLOCK_MONOTONIC_COARSE: c_int = 6; +#[cfg(target_os = "linux")] +pub const CLOCK_BOOTTIME: c_int = 7; +#[cfg(target_os = "linux")] +pub const CLOCK_REALTIME_ALARM: c_int = 8; +#[cfg(target_os = "linux")] +pub const CLOCK_BOOTTIME_ALARM: c_int = 9; +#[cfg(target_os = "linux")] +pub const CLOCK_TAI: c_int = 11; + /* *#[repr(C)] *pub struct timespec { @@ -59,7 +86,7 @@ pub extern "C" fn clock_getres(clock_id: clockid_t, res: *mut timespec) -> c_int #[no_mangle] pub extern "C" fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int { - unimplemented!(); + platform::clock_gettime(clock_id, tp) } #[no_mangle] @@ -134,7 +161,14 @@ pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut t #[no_mangle] pub extern "C" fn time(tloc: *mut time_t) -> time_t { - unimplemented!(); + let mut ts: timespec = Default::default(); + platform::clock_gettime(CLOCK_REALTIME, &mut ts); + unsafe { + if !tloc.is_null() { + *tloc = ts.tv_sec + }; + } + ts.tv_sec } #[no_mangle] diff --git a/tests/.gitignore b/tests/.gitignore index 433d1e3fd6..e946dbbed8 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -47,4 +47,4 @@ /unlink /waitpid /write - +/time diff --git a/tests/Makefile b/tests/Makefile index e788930ed0..cd6117e6e0 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -41,7 +41,8 @@ EXPECT_BINS=\ unistd/getopt \ unlink \ waitpid \ - write + write \ + time # Binaries that may generate varied output BINS=\ diff --git a/tests/time.c b/tests/time.c new file mode 100644 index 0000000000..e6ce164c25 --- /dev/null +++ b/tests/time.c @@ -0,0 +1,11 @@ +#include +#include + +int main(int argc, char** argv) { + timespec tm = {0, 0}; + clock_gettime(CLOCK_REALTIME, &tm); + perror("clock_gettime"); + time(NULL); + perror("time"); + return 0; +} From 43e95a9b9216e83c9c753a3ff3a8b16b9803413a Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Sun, 25 Mar 2018 14:22:12 -0400 Subject: [PATCH 2/2] Implement kill() and killpg() --- src/platform/src/linux/mod.rs | 8 ++++++++ src/platform/src/redox/mod.rs | 8 ++++++++ src/signal/src/lib.rs | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index d055989d17..b764d1f742 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -115,6 +115,14 @@ pub fn getuid() -> uid_t { e(unsafe { syscall!(GETUID) }) } +pub fn kill(pid: pid_t, sig: c_int) -> c_int { + e(unsafe { syscall!(KILL, pid, sig) }) as c_int +} + +pub fn killpg(pgrp: pid_t, sig: c_int) -> c_int { + e(unsafe { syscall!(KILL, -(pgrp as isize) as pid_t, sig) }) as c_int +} + pub fn link(path1: *const c_char, path2: *const c_char) -> c_int { e(unsafe { syscall!(LINKAT, AT_FDCWD, path1, AT_FDCWD, path2, 0) }) as c_int } diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index ea7954ec20..4dfb2b28d4 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -119,6 +119,14 @@ pub fn getuid() -> uid_t { e(syscall::getuid()) as pid_t } +pub fn kill(pid: pid_t, sig: c_int) -> c_int { + e(syscall::kill(pid, sig as usize)) as c_int +} + +pub fn killpg(pgrp: pid_t, sig: c_int) -> c_int { + e(syscall::kill(-(pgrp as isize) as pid_t, sig as usize)) as c_int +} + pub fn link(path1: *const c_char, path2: *const c_char) -> c_int { let path1 = unsafe { c_str(path1) }; let path2 = unsafe { c_str(path2) }; diff --git a/src/signal/src/lib.rs b/src/signal/src/lib.rs index 052cb13770..031b8e4836 100644 --- a/src/signal/src/lib.rs +++ b/src/signal/src/lib.rs @@ -27,12 +27,12 @@ pub type sigset_t = sys_sigset_t; #[no_mangle] pub extern "C" fn kill(pid: pid_t, sig: c_int) -> c_int { - unimplemented!(); + platform::kill(pid, sig) } #[no_mangle] pub extern "C" fn killpg(pgrp: pid_t, sig: c_int) -> c_int { - unimplemented!(); + platform::killpg(pgrp, sig) } #[no_mangle]