1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Syd: rock-solid application kernel
// src/uts.rs: Interface to uname(2)
//
// Copyright (c) 2025 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0
// SAFETY: This module has (almost) been liberated from unsafe code!
// UtsName::as_bytes() uses std::slice::from_raw_parts which is unsafe.
// Use deny rather than forbid so we can allow this case.
#![deny(unsafe_code)]
use serde::{ser::SerializeMap, Serialize, Serializer};
use crate::path::XPath;
// Length of the entries in `struct utsname' is 65.
pub(crate) const UTSNAME_LEN: usize = 65;
/// C-compatible layout of the `utsname` structure.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(C)]
pub struct UtsName {
// Name of the operating system implementation.
pub(crate) sysname: [u8; UTSNAME_LEN],
// Network name of this machine.
pub(crate) nodename: [u8; UTSNAME_LEN],
// Release level of the operating system.
pub(crate) release: [u8; UTSNAME_LEN],
// Version level of the operating system.
pub(crate) version: [u8; UTSNAME_LEN],
// Machine hardware platform.
pub(crate) machine: [u8; UTSNAME_LEN],
// NIS or YP domain name of this machine.
pub(crate) domainname: [u8; UTSNAME_LEN],
}
impl Default for UtsName {
fn default() -> Self {
Self {
sysname: [0u8; UTSNAME_LEN],
nodename: [0u8; UTSNAME_LEN],
release: [0u8; UTSNAME_LEN],
version: [0u8; UTSNAME_LEN],
machine: [0u8; UTSNAME_LEN],
domainname: [0u8; UTSNAME_LEN],
}
}
}
impl UtsName {
/// Return a byte-wise view of the UtsName structure.
pub fn as_bytes(&self) -> &[u8] {
// SAFETY: UtsName is repr(C) and contains only [u8; N] fields.
#[expect(unsafe_code)]
unsafe {
std::slice::from_raw_parts(self as *const Self as *const u8, size_of::<Self>())
}
}
/// Return name of the operating system implementation.
pub fn sysname(&self) -> &XPath {
XPath::from_bytes_until_nul(&self.sysname)
}
/// Return network name of this machine.
pub fn nodename(&self) -> &XPath {
XPath::from_bytes_until_nul(&self.nodename)
}
/// Return release level of the operating system.
pub fn release(&self) -> &XPath {
XPath::from_bytes_until_nul(&self.release)
}
/// Return version level of the operating system.
pub fn version(&self) -> &XPath {
XPath::from_bytes_until_nul(&self.version)
}
/// Return machine hardware platform.
pub fn machine(&self) -> &XPath {
XPath::from_bytes_until_nul(&self.machine)
}
/// Return NIS or YP domain name of this machine.
pub fn domainname(&self) -> &XPath {
XPath::from_bytes_until_nul(&self.domainname)
}
}
impl Serialize for UtsName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(6))?;
map.serialize_entry("sysname", self.sysname())?;
map.serialize_entry("nodename", self.nodename())?;
map.serialize_entry("release", self.release())?;
map.serialize_entry("version", self.version())?;
map.serialize_entry("machine", self.machine())?;
map.serialize_entry("domainname", self.domainname())?;
map.end()
}
}