Add time syscall and default localtime support

This commit is contained in:
LI Qing 2021-03-26 12:06:58 +08:00 committed by Zongmin.Gu
parent ef7b663c4d
commit 64a980f529
3 changed files with 41 additions and 2 deletions

@ -285,7 +285,7 @@ macro_rules! process_syscall_table_with_callback {
(Lremovexattr = 198) => handle_unsupported(), (Lremovexattr = 198) => handle_unsupported(),
(Fremovexattr = 199) => handle_unsupported(), (Fremovexattr = 199) => handle_unsupported(),
(Tkill = 200) => do_tkill(tid: pid_t, sig: c_int), (Tkill = 200) => do_tkill(tid: pid_t, sig: c_int),
(Time = 201) => handle_unsupported(), (Time = 201) => do_time(tloc_u: *mut time_t),
(Futex = 202) => do_futex(futex_addr: *const i32, futex_op: u32, futex_val: i32, timeout: u64, futex_new_addr: *const i32, bitset: u32), (Futex = 202) => do_futex(futex_addr: *const i32, futex_op: u32, futex_val: i32, timeout: u64, futex_new_addr: *const i32, bitset: u32),
(SchedSetaffinity = 203) => do_sched_setaffinity(pid: pid_t, cpusize: size_t, buf: *const c_uchar), (SchedSetaffinity = 203) => do_sched_setaffinity(pid: pid_t, cpusize: size_t, buf: *const c_uchar),
(SchedGetaffinity = 204) => do_sched_getaffinity(pid: pid_t, cpusize: size_t, buf: *mut c_uchar), (SchedGetaffinity = 204) => do_sched_getaffinity(pid: pid_t, cpusize: size_t, buf: *mut c_uchar),
@ -833,6 +833,17 @@ fn do_clock_gettime(clockid: clockid_t, ts_u: *mut timespec_t) -> Result<isize>
Ok(0) Ok(0)
} }
fn do_time(tloc_u: *mut time_t) -> Result<isize> {
let ts = time::do_clock_gettime(time::ClockID::CLOCK_REALTIME)?;
if !tloc_u.is_null() {
check_mut_ptr(tloc_u)?;
unsafe {
*tloc_u = ts.sec();
}
}
Ok(ts.sec() as isize)
}
fn do_clock_getres(clockid: clockid_t, res_u: *mut timespec_t) -> Result<isize> { fn do_clock_getres(clockid: clockid_t, res_u: *mut timespec_t) -> Result<isize> {
if res_u.is_null() { if res_u.is_null() {
return Ok(0); return Ok(0);

@ -50,6 +50,24 @@ int test_clock_getres() {
return 0; return 0;
} }
// ============================================================================
// Test cases for localtime
// ============================================================================
int test_get_localtime() {
time_t t = time(NULL);
if (t == (time_t) -1) {
THROW_ERROR("failed to get time");
}
struct tm *local_time = localtime(&t);
if (local_time == NULL) {
THROW_ERROR("failed to convert a time value to a local time");
}
printf("Offset to GMT is %lds.\n", local_time->tm_gmtoff);
printf("The time zone is '%s'.\n", local_time->tm_zone);
return 0;
}
// ============================================================================ // ============================================================================
// Test suite // Test suite
// ============================================================================ // ============================================================================
@ -58,6 +76,7 @@ static test_case_t test_cases[] = {
TEST_CASE(test_gettimeofday), TEST_CASE(test_gettimeofday),
TEST_CASE(test_clock_gettime), TEST_CASE(test_clock_gettime),
TEST_CASE(test_clock_getres), TEST_CASE(test_clock_getres),
TEST_CASE(test_get_localtime),
}; };
int main() { int main() {

@ -157,15 +157,19 @@ cmd_init() {
mkdir -p image/tmp mkdir -p image/tmp
mkdir -p image/dev mkdir -p image/dev
mkdir -p image/proc mkdir -p image/proc
mkdir -p image/etc
local occlum_glibc_lib=/opt/occlum/glibc/lib local occlum_glibc_lib=/opt/occlum/glibc/lib
local occlum_glibc_etc=/opt/occlum/glibc/etc
local cpu_lib=/sys/devices/system/cpu local cpu_lib=/sys/devices/system/cpu
if [ -d "$occlum_glibc_lib" ]; then if [ -d "$occlum_glibc_lib" ]; then
mkdir -p "image/$occlum_glibc_lib" mkdir -p "image/$occlum_glibc_lib"
mkdir -p "image/$cpu_lib" mkdir -p "image/$cpu_lib"
mkdir -p "image/$occlum_glibc_etc"
fi fi
# add default /etc/hosts # add default /etc/hosts
mkdir -p image/etc
echo "127.0.0.1 localhost" > image/etc/hosts echo "127.0.0.1 localhost" > image/etc/hosts
# add default timezone file
cp /etc/localtime image/etc/
# add musl # add musl
local occlum_musl_lib=/usr/local/occlum/x86_64-linux-musl/lib local occlum_musl_lib=/usr/local/occlum/x86_64-linux-musl/lib
@ -195,6 +199,8 @@ cmd_init() {
"/usr/lib64/libstdc++.so.6" \ "/usr/lib64/libstdc++.so.6" \
"/usr/lib64/libgcc_s.so.1" "/usr/lib64/libgcc_s.so.1"
fi fi
cp -t "image/$occlum_glibc_etc" \
/etc/localtime
cp -t "image/$cpu_lib" \ cp -t "image/$cpu_lib" \
"$cpu_lib/online" "$cpu_lib/online"
fi fi
@ -207,6 +213,9 @@ cmd_init() {
mkdir -p initfs/etc mkdir -p initfs/etc
# add default /etc/hosts # add default /etc/hosts
echo "127.0.0.1 localhost" > initfs/etc/hosts echo "127.0.0.1 localhost" > initfs/etc/hosts
# add default timezone file
cp /etc/localtime initfs/etc/
# add musl # add musl
local occlum_musl_lib=/usr/local/occlum/x86_64-linux-musl/lib local occlum_musl_lib=/usr/local/occlum/x86_64-linux-musl/lib
cp -t initfs/lib \ cp -t initfs/lib \