From 3d49429f554e8aa43261a736ae7dcaea3bdf3572 Mon Sep 17 00:00:00 2001 From: Yuki Sireneva Date: Tue, 17 Dec 2024 20:47:18 +0300 Subject: [PATCH] Add setdomainname function --- src/backend/libc/system/syscalls.rs | 24 ++++++++++++++++++++++++ src/backend/linux_raw/system/syscalls.rs | 6 ++++++ src/system.rs | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/backend/libc/system/syscalls.rs b/src/backend/libc/system/syscalls.rs index 55978c7f6..8807de983 100644 --- a/src/backend/libc/system/syscalls.rs +++ b/src/backend/libc/system/syscalls.rs @@ -63,6 +63,30 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> { } } +#[cfg(not(any( + target_os = "emscripten", + target_os = "espidf", + target_os = "haiku", + target_os = "redox", + target_os = "vita", + target_os = "wasi" +)))] +pub(crate) fn setdomainname(name: &[u8]) -> io::Result<()> { + syscall! { + fn setdomainname( + name: *const c::c_char, + len: c::size_t + ) via SYS_setdomainname -> c::c_int + } + + unsafe { + ret(setdomainname( + name.as_ptr().cast(), + name.len().try_into().map_err(|_| io::Errno::INVAL)?, + )) + } +} + #[cfg(target_os = "linux")] pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> { unsafe { ret(c::reboot(cmd as i32)) } diff --git a/src/backend/linux_raw/system/syscalls.rs b/src/backend/linux_raw/system/syscalls.rs index 211ee20da..687658a22 100644 --- a/src/backend/linux_raw/system/syscalls.rs +++ b/src/backend/linux_raw/system/syscalls.rs @@ -39,6 +39,12 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> { unsafe { ret(syscall_readonly!(__NR_sethostname, ptr, len)) } } +#[inline] +pub(crate) fn setdomainname(name: &[u8]) -> io::Result<()> { + let (ptr, len) = slice(name); + unsafe { ret(syscall_readonly!(__NR_setdomainname, ptr, len)) } +} + #[inline] pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> { unsafe { diff --git a/src/system.rs b/src/system.rs index 22472182b..6d97158c0 100644 --- a/src/system.rs +++ b/src/system.rs @@ -168,6 +168,27 @@ pub fn sethostname(name: &[u8]) -> io::Result<()> { backend::system::syscalls::sethostname(name) } +/// `setdomain(name)`—Sets the system NIS domain name. +/// +/// # References +/// - [Linux] +/// - [FreeBSD] +/// +/// [Linux]: https://man7.org/linux/man-pages/man2/setdomainname.2.html +/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=setdomainname&sektion=3 +#[cfg(not(any( + target_os = "emscripten", + target_os = "espidf", + target_os = "haiku", + target_os = "redox", + target_os = "vita", + target_os = "wasi" +)))] +#[inline] +pub fn setdomainname(name: &[u8]) -> io::Result<()> { + backend::system::syscalls::setdomainname(name) +} + /// Reboot command for use with [`reboot`]. #[cfg(target_os = "linux")] #[derive(Copy, Clone, Debug, Eq, PartialEq)]