Skip to content

Commit 0d10f70

Browse files
authored
feat(vfs): 新增creat系统调用支持 (#1482)
* feat(vfs): 新增creat系统调用支持 - 实现creat系统调用处理器,遵循Linux语义:创建新文件或截断现有文件并打开为只写模式 - 在x86_64架构下注册creat系统调用到系统调用表 - 在gvisor测试白名单中添加creat测试项 Signed-off-by: longjin <longjin@DragonOS.org> * feat(rust-jhash): Update Cargo.toml to specify Rust edition 2021 --------- Signed-off-by: longjin <longjin@DragonOS.org>
1 parent b28f766 commit 0d10f70

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

kernel/crates/rust-jhash/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
22
name = "jhash"
33
version = "0.1.1"
4+
edition = "2021"
45
authors = ["Keunhong Lee <dlrmsghd@gmail.com>"]
56

67
homepage = "https://github.com/ANLAB-KAIST/rust-jhash"

kernel/src/filesystem/vfs/syscall/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ mod sys_epoll_create;
8080
#[cfg(target_arch = "x86_64")]
8181
mod sys_epoll_wait;
8282

83+
#[cfg(target_arch = "x86_64")]
84+
mod sys_creat;
8385
#[cfg(target_arch = "x86_64")]
8486
mod sys_futimesat;
8587
#[cfg(target_arch = "x86_64")]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! System call handler for creating files (creat).
2+
//!
3+
//! The `creat` system call is a legacy interface that is equivalent to:
4+
//! `open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)`
5+
//!
6+
//! This implementation follows Linux semantics:
7+
//! - Creates a new file if it doesn't exist
8+
//! - Truncates an existing file to zero length
9+
//! - Opens the file in write-only mode
10+
//! - Returns ENAMETOOLONG if the filename is too long
11+
12+
use system_error::SystemError;
13+
14+
use crate::arch::interrupt::TrapFrame;
15+
use crate::arch::syscall::nr::SYS_CREAT;
16+
use crate::filesystem::vfs::file::FileFlags;
17+
use crate::syscall::table::FormattedSyscallParam;
18+
use crate::syscall::table::Syscall;
19+
20+
use alloc::string::ToString;
21+
use alloc::vec::Vec;
22+
23+
/// Handler for the `creat` system call.
24+
pub struct SysCreatHandle;
25+
26+
impl Syscall for SysCreatHandle {
27+
/// Returns the number of arguments this syscall takes (2).
28+
fn num_args(&self) -> usize {
29+
2
30+
}
31+
32+
/// Handles the creat syscall by extracting arguments and calling `do_open`
33+
/// with O_WRONLY | O_CREAT | O_TRUNC flags.
34+
///
35+
/// # Arguments
36+
/// * `args[0]` - Path to the file (pointer to C string)
37+
/// * `args[1]` - File mode/permissions
38+
///
39+
/// # Returns
40+
/// File descriptor on success, or error code on failure.
41+
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
42+
let path = Self::path(args);
43+
let mode = Self::mode(args);
44+
45+
// creat(path, mode) is equivalent to open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)
46+
let flags = (FileFlags::O_WRONLY | FileFlags::O_CREAT | FileFlags::O_TRUNC).bits();
47+
48+
super::open_utils::do_open(path, flags, mode)
49+
}
50+
51+
/// Formats the syscall arguments for display/debugging purposes.
52+
fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {
53+
vec![
54+
FormattedSyscallParam::new("path", format!("{:#x}", Self::path(args) as usize)),
55+
FormattedSyscallParam::new("mode", Self::mode(args).to_string()),
56+
]
57+
}
58+
}
59+
60+
impl SysCreatHandle {
61+
/// Extracts the path argument from syscall parameters.
62+
fn path(args: &[usize]) -> *const u8 {
63+
args[0] as *const u8
64+
}
65+
66+
/// Extracts the mode argument from syscall parameters.
67+
fn mode(args: &[usize]) -> u32 {
68+
args[1] as u32
69+
}
70+
}
71+
72+
syscall_table_macros::declare_syscall!(SYS_CREAT, SysCreatHandle);

user/apps/tests/syscall/gvisor/whitelist.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ getcpu_host_test
1313

1414
# 文件系统相关测试
1515
chroot_test
16+
creat_test
1617
dup_test
1718
write_test
1819
mkdir_test
@@ -80,4 +81,4 @@ pipe_test
8081
fifo_test
8182
unshare_test
8283
pty_root_test
83-
time_test
84+
time_test

0 commit comments

Comments
 (0)