From b390ecaae993b8f445d1603cf9580a90d077f210 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Fri, 6 Aug 2021 09:35:22 +0800 Subject: [PATCH] Add creat syscall --- src/libos/src/fs/syscalls.rs | 6 ++++++ src/libos/src/syscall/mod.rs | 16 ++++++++-------- test/open/main.c | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/libos/src/fs/syscalls.rs b/src/libos/src/fs/syscalls.rs index 4962f886..8328908c 100644 --- a/src/libos/src/fs/syscalls.rs +++ b/src/libos/src/fs/syscalls.rs @@ -97,6 +97,12 @@ pub fn do_timerfd_gettime(fd: FileDesc, curr_value_ptr: *mut itimerspec_t) -> Re Ok(0) } +pub fn do_creat(path: *const i8, mode: u32) -> Result { + let flags = + AccessMode::O_WRONLY as u32 | (CreationFlags::O_CREAT | CreationFlags::O_TRUNC).bits(); + self::do_open(path, flags, mode) +} + pub fn do_open(path: *const i8, flags: u32, mode: u32) -> Result { self::do_openat(AT_FDCWD, path, flags, mode) } diff --git a/src/libos/src/syscall/mod.rs b/src/libos/src/syscall/mod.rs index 7b7a7d85..b1c9f6dd 100644 --- a/src/libos/src/syscall/mod.rs +++ b/src/libos/src/syscall/mod.rs @@ -22,13 +22,13 @@ use util::mem_util::from_user::*; use crate::exception::do_handle_exception; use crate::fs::{ - do_access, do_chdir, do_chmod, do_chown, do_close, do_dup, do_dup2, do_dup3, do_eventfd, - do_eventfd2, do_faccessat, do_fallocate, do_fchdir, do_fchmod, do_fchmodat, do_fchown, - do_fchownat, do_fcntl, do_fdatasync, do_fstat, do_fstatat, do_fstatfs, do_fsync, do_ftruncate, - do_getcwd, do_getdents, do_getdents64, do_ioctl, do_lchown, do_link, do_linkat, do_lseek, - do_lstat, do_mkdir, do_mkdirat, do_mount_rootfs, do_open, do_openat, do_pipe, do_pipe2, - do_pread, do_pwrite, do_read, do_readlink, do_readlinkat, do_readv, do_rename, do_renameat, - do_rmdir, do_sendfile, do_stat, do_statfs, do_symlink, do_symlinkat, do_sync, + do_access, do_chdir, do_chmod, do_chown, do_close, do_creat, do_dup, do_dup2, do_dup3, + do_eventfd, do_eventfd2, do_faccessat, do_fallocate, do_fchdir, do_fchmod, do_fchmodat, + do_fchown, do_fchownat, do_fcntl, do_fdatasync, do_fstat, do_fstatat, do_fstatfs, do_fsync, + do_ftruncate, do_getcwd, do_getdents, do_getdents64, do_ioctl, do_lchown, do_link, do_linkat, + do_lseek, do_lstat, do_mkdir, do_mkdirat, do_mount_rootfs, do_open, do_openat, do_pipe, + do_pipe2, do_pread, do_pwrite, do_read, do_readlink, do_readlinkat, do_readv, do_rename, + do_renameat, do_rmdir, do_sendfile, do_stat, do_statfs, do_symlink, do_symlinkat, do_sync, do_timerfd_create, do_timerfd_gettime, do_timerfd_settime, do_truncate, do_unlink, do_unlinkat, do_write, do_writev, iovec_t, AsTimer, File, FileDesc, FileRef, HostStdioFds, Stat, Statfs, }; @@ -172,7 +172,7 @@ macro_rules! process_syscall_table_with_callback { (Rename = 82) => do_rename(oldpath: *const i8, newpath: *const i8), (Mkdir = 83) => do_mkdir(path: *const i8, mode: usize), (Rmdir = 84) => do_rmdir(path: *const i8), - (Creat = 85) => handle_unsupported(), + (Creat = 85) => do_creat(path: *const i8, mode: u32), (Link = 86) => do_link(oldpath: *const i8, newpath: *const i8), (Unlink = 87) => do_unlink(path: *const i8), (Symlink = 88) => do_symlink(target: *const i8, link_path: *const i8), diff --git a/test/open/main.c b/test/open/main.c index 518df6bf..13484d05 100644 --- a/test/open/main.c +++ b/test/open/main.c @@ -1,3 +1,4 @@ +#include #include #include #include "test_fs.h" @@ -102,6 +103,15 @@ static int __test_openat_with_dirfd(const char *file_path, int flags, int mode) return 0; } +static int __test_creat(const char *file_path, int flags, int mode) { + int fd = creat(file_path, mode); + if (fd < 0) { + THROW_ERROR("failed to creat a file"); + } + close(fd); + return 0; +} + typedef int(*test_open_func_t)(const char *, int, int); static int test_open_framework(test_open_func_t fn) { @@ -138,6 +148,10 @@ static int test_openat_with_dirfd() { return test_open_framework(__test_openat_with_dirfd); } +static int test_creat() { + return test_open_framework(__test_creat); +} + // ============================================================================ // Test suite main // ============================================================================ @@ -148,6 +162,7 @@ static test_case_t test_cases[] = { TEST_CASE(test_open_dir_with_write_flags), TEST_CASE(test_openat_with_abs_path), TEST_CASE(test_openat_with_dirfd), + TEST_CASE(test_creat), }; int main(int argc, const char *argv[]) {