Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现uname系统调用 #614

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions kernel/src/process/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,43 @@ use crate::{
},
};

//参考资料:https://code.dragonos.org.cn/xref/linux-6.1.9/include/uapi/linux/utsname.h#17
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct PosixOldUtsName {
pub sysname: [u8; 65],
pub nodename: [u8; 65],
pub release: [u8; 65],
pub version: [u8; 65],
pub machine: [u8; 65],
}

impl PosixOldUtsName {
pub fn new() -> Self {
const SYS_NAME: &[u8] = b"Dragon.OS";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改成DragonOS比较好

const NODENAME: &[u8] = b"RinGoTek";
const RELEASE: &[u8] = b"0.1.9";
const VERSION: &[u8] = b"2022.01.01";
const MACHINE: &[u8] = b"x86_64";

let mut r = Self {
sysname: [0; 65],
nodename: [0; 65],
release: [0; 65],
version: [0; 65],
machine: [0; 65],
};

r.sysname[0..SYS_NAME.len()].copy_from_slice(SYS_NAME);
r.nodename[0..NODENAME.len()].copy_from_slice(NODENAME);
r.release[0..RELEASE.len()].copy_from_slice(RELEASE);
r.version[0..VERSION.len()].copy_from_slice(VERSION);
r.machine[0..MACHINE.len()].copy_from_slice(MACHINE);

return r;
}
}

impl Syscall {
pub fn fork(frame: &TrapFrame) -> Result<usize, SystemError> {
ProcessManager::fork(frame, CloneFlags::empty()).map(|pid| pid.into())
Expand Down Expand Up @@ -341,4 +378,16 @@ impl Syscall {
}
}
}

pub fn uname(name: *mut PosixOldUtsName) -> Result<usize, SystemError> {
if name.is_null() {
return Err(SystemError::EFAULT);
}

let mut writer =
UserBufferWriter::new(name, core::mem::size_of::<PosixOldUtsName>(), true)?;
writer.copy_one_to_user(&PosixOldUtsName::new(), 0)?;

return Ok(0);
}
}
8 changes: 6 additions & 2 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ use crate::{
libs::align::page_align_up,
mm::{verify_area, MemoryManagementArch, VirtAddr},
net::syscall::SockAddr,
process::{fork::CloneFlags, Pid},
process::{fork::CloneFlags, Pid,
syscall::PosixOldUtsName},
time::{
syscall::{PosixTimeZone, PosixTimeval},
TimeSpec,
Expand Down Expand Up @@ -1031,7 +1032,10 @@ impl Syscall {
}

SYS_SCHED_YIELD => Self::sched_yield(),

SYS_UNAME => {
let name = args[0] as *mut PosixOldUtsName;
Self::uname(name)
}
_ => panic!("Unsupported syscall ID: {}", syscall_num),
};

Expand Down
1 change: 1 addition & 0 deletions kernel/src/syscall/syscall_num.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#define SYS_EXIT 60
#define SYS_WAIT4 61
#define SYS_KILL 62
#define SYS_UNAME 63

#define SYS_FCNTL 72

Expand Down
20 changes: 20 additions & 0 deletions user/apps/test_uname/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ifeq ($(ARCH), x86_64)
CROSS_COMPILE=x86_64-linux-musl-
else ifeq ($(ARCH), riscv64)
CROSS_COMPILE=riscv64-linux-musl-
endif

CC=$(CROSS_COMPILE)gcc

.PHONY: all
all: main.c
$(CC) -static -o test_uname main.c

.PHONY: install clean
install: all
mv test_uname $(DADK_CURRENT_BUILD_DIR)/test_uname

clean:
rm test_uname *.o

fmt:
19 changes: 19 additions & 0 deletions user/apps/test_uname/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <sys/utsname.h>
#include <stdlib.h>

int main() {
struct utsname system_info;

// 调用 uname 函数获取系统信息
uname(&system_info);

// 打印系统信息
printf("System name: %s\n", system_info.sysname);
printf("Node name: %s\n", system_info.nodename);
printf("Release: %s\n", system_info.release);
printf("Version: %s\n", system_info.version);
printf("Machine: %s\n", system_info.machine);

return 0;
}
22 changes: 22 additions & 0 deletions user/dadk/config/test_uname_0_1_0.dadk
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "test_uname",
"version": "0.1.0",
"description": "一个用来测试uname的程序",
"task_type": {
"BuildFromSource": {
"Local": {
"path": "apps/test_uname"
}
}
},
"depends": [],
"build": {
"build_command": "make install"
},
"install": {
"in_dragonos_path": "/bin"
},
"clean": {
"clean_command": "make clean"
}
}
Loading