Skip to content

Commit 8c6f218

Browse files
sspphhfslongjin
andauthored
实现uname系统调用 (#614)
* 实现uname系统调用 Co-authored-by: longjin <[email protected]>
1 parent 82df0a1 commit 8c6f218

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

kernel/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dragonos_kernel"
3-
version = "0.1.0"
3+
version = "0.1.9"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

kernel/src/process/syscall.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,51 @@ use crate::{
2929
},
3030
};
3131

32+
//参考资料:https://code.dragonos.org.cn/xref/linux-6.1.9/include/uapi/linux/utsname.h#17
33+
#[repr(C)]
34+
#[derive(Debug, Clone, Copy)]
35+
pub struct PosixOldUtsName {
36+
pub sysname: [u8; 65],
37+
pub nodename: [u8; 65],
38+
pub release: [u8; 65],
39+
pub version: [u8; 65],
40+
pub machine: [u8; 65],
41+
}
42+
43+
impl PosixOldUtsName {
44+
pub fn new() -> Self {
45+
const SYS_NAME: &[u8] = b"DragonOS";
46+
const NODENAME: &[u8] = b"DragonOS";
47+
const RELEASE: &[u8] = env!("CARGO_PKG_VERSION").as_bytes();
48+
const VERSION: &[u8] = env!("CARGO_PKG_VERSION").as_bytes();
49+
50+
#[cfg(target_arch = "x86_64")]
51+
const MACHINE: &[u8] = b"x86_64";
52+
53+
#[cfg(target_arch = "aarch64")]
54+
const MACHINE: &[u8] = b"aarch64";
55+
56+
#[cfg(target_arch = "riscv64")]
57+
const MACHINE: &[u8] = b"riscv64";
58+
59+
let mut r = Self {
60+
sysname: [0; 65],
61+
nodename: [0; 65],
62+
release: [0; 65],
63+
version: [0; 65],
64+
machine: [0; 65],
65+
};
66+
67+
r.sysname[0..SYS_NAME.len()].copy_from_slice(SYS_NAME);
68+
r.nodename[0..NODENAME.len()].copy_from_slice(NODENAME);
69+
r.release[0..RELEASE.len()].copy_from_slice(RELEASE);
70+
r.version[0..VERSION.len()].copy_from_slice(VERSION);
71+
r.machine[0..MACHINE.len()].copy_from_slice(MACHINE);
72+
73+
return r;
74+
}
75+
}
76+
3277
impl Syscall {
3378
pub fn fork(frame: &TrapFrame) -> Result<usize, SystemError> {
3479
ProcessManager::fork(frame, CloneFlags::empty()).map(|pid| pid.into())
@@ -341,4 +386,12 @@ impl Syscall {
341386
}
342387
}
343388
}
389+
390+
pub fn uname(name: *mut PosixOldUtsName) -> Result<usize, SystemError> {
391+
let mut writer =
392+
UserBufferWriter::new(name, core::mem::size_of::<PosixOldUtsName>(), true)?;
393+
writer.copy_one_to_user(&PosixOldUtsName::new(), 0)?;
394+
395+
return Ok(0);
396+
}
344397
}

kernel/src/syscall/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::{
3333
libs::align::page_align_up,
3434
mm::{verify_area, MemoryManagementArch, VirtAddr},
3535
net::syscall::SockAddr,
36-
process::{fork::CloneFlags, Pid},
36+
process::{fork::CloneFlags, syscall::PosixOldUtsName, Pid},
3737
time::{
3838
syscall::{PosixTimeZone, PosixTimeval},
3939
TimeSpec,
@@ -949,7 +949,10 @@ impl Syscall {
949949
}
950950

951951
SYS_SCHED_YIELD => Self::sched_yield(),
952-
952+
SYS_UNAME => {
953+
let name = args[0] as *mut PosixOldUtsName;
954+
Self::uname(name)
955+
}
953956
_ => panic!("Unsupported syscall ID: {}", syscall_num),
954957
};
955958

kernel/src/syscall/syscall_num.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#define SYS_EXIT 60
5454
#define SYS_WAIT4 61
5555
#define SYS_KILL 62
56+
#define SYS_UNAME 63
5657

5758
#define SYS_FCNTL 72
5859

0 commit comments

Comments
 (0)