Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions procfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rust-version.workspace = true
backtrace = ["dep:backtrace", "procfs-core/backtrace"]
default = ["chrono", "flate2", "procfs-core/default"]
serde1 = ["serde", "procfs-core/serde1"]
test-modifies-system = []

[dependencies]
procfs-core = { path = "../procfs-core", version = "0.18.0", default-features = false }
Expand Down
66 changes: 66 additions & 0 deletions procfs/src/sys/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! The files in this directory can be used to tune and monitor miscellaneous
//! and general things in the operation of the Linux kernel.

use std::borrow::Cow;
use std::cmp;
use std::collections::HashSet;
use std::str::FromStr;
Expand Down Expand Up @@ -289,6 +290,48 @@ impl FromStr for BuildInfo {
}
}

/// Returns domainname
///
/// See also [set_domainname](crate::sys::kernel::set_domainname)
///
/// This is taken from `/proc/sys/kernel/set_domainname`
pub fn domainname() -> ProcResult<String> {
read_value("/proc/sys/kernel/domainname")
}

/// Set the NIS/YP domainname.
///
/// See also [domainname](crate::sys::kernel::domainname)
pub fn set_domainname(new_value: &str) -> ProcResult<()> {
let value = match new_value.ends_with('\n') {
true => Cow::Borrowed(new_value),
false => Cow::Owned(format!("{}\n", new_value)),
};

write_value("/proc/sys/kernel/domainname", value)
}

/// Returns hostname
///
/// See also [set_hostname](crate::sys::kernel::set_hostname)
///
/// This is taken from `/proc/sys/kernel/hostname`
pub fn hostname() -> ProcResult<String> {
read_value("/proc/sys/kernel/hostname")
}

/// Set the NIS/YP hostname.
///
/// See also [hostname](crate::sys::kernel::hostname)
pub fn set_hostname(new_value: &str) -> ProcResult<()> {
let value = match new_value.ends_with('\n') {
true => Cow::Borrowed(new_value),
false => Cow::Owned(format!("{}\n", new_value)),
};

write_value("/proc/sys/kernel/hostname", value)
}

/// Returns the maximum process ID number.
///
/// This is taken from `/proc/sys/kernel/pid_max`.
Expand Down Expand Up @@ -570,6 +613,29 @@ mod tests {
let _ = BuildInfo::current().unwrap();
}

#[test]
#[cfg(feature = "test-modifies-system")]
fn test_set_and_get_domainname() {
let current_domain = domainname().unwrap();
let test_domain = String::from("test-host");

set_domainname(&test_domain).unwrap();
assert_eq!(domainname().unwrap(), test_domain);
set_domainname(&current_domain).unwrap();
}

#[test]
#[cfg(feature = "test-modifies-system")]
fn test_set_and_get_hostname() {
let current_hostname = hostname().unwrap();
let test_hostname = String::from("test-host");

set_hostname(&test_hostname).unwrap();
assert_eq!(hostname().unwrap(), test_hostname);

set_hostname(&current_hostname).unwrap();
}

#[test]
fn test_pid_max() {
assert!(pid_max().is_ok());
Expand Down
4 changes: 2 additions & 2 deletions support.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ This is an approximate list of all the files under the `/proc` mount, and an ind
* [ ] `/proc/sys/kernel/core_uses_pid`
* [ ] `/proc/sys/kernel/ctrl-alt-del`
* [ ] `/proc/sys/kernel/dmesg_restrict`
* [ ] `/proc/sys/kernel/domainname`
* [ ] `/proc/sys/kernel/hostname`
* [x] `/proc/sys/kernel/domainname`
* [x] `/proc/sys/kernel/hostname`
* [ ] `/proc/sys/kernel/hotplug`
* [ ] `/proc/sys/kernel/htab-reclaim`
* [x] `/proc/sys/kernel/keys/\*`
Expand Down
Loading