From 3d49429f554e8aa43261a736ae7dcaea3bdf3572 Mon Sep 17 00:00:00 2001 From: Yuki Sireneva Date: Tue, 17 Dec 2024 20:47:18 +0300 Subject: [PATCH 1/3] 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)] From 01ad8ac2bc4c74c5432134ab06632ab89903d0dd Mon Sep 17 00:00:00 2001 From: Yuki Sireneva Date: Wed, 18 Dec 2024 21:06:03 +0300 Subject: [PATCH 2/3] Use `libc::setdomainname` where possible Use weak `setdomainname` only for android; remove this special case once [libc#4212] is merged. [libc#4212]: https://github.com/rust-lang/libc/pull/4212 --- src/backend/libc/system/syscalls.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/backend/libc/system/syscalls.rs b/src/backend/libc/system/syscalls.rs index 8807de983..0c5b0f7d5 100644 --- a/src/backend/libc/system/syscalls.rs +++ b/src/backend/libc/system/syscalls.rs @@ -64,6 +64,7 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> { } #[cfg(not(any( + target_os = "android", target_os = "emscripten", target_os = "espidf", target_os = "haiku", @@ -71,6 +72,17 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> { target_os = "vita", target_os = "wasi" )))] +pub(crate) fn setdomainname(name: &[u8]) -> io::Result<()> { + unsafe { + ret(c::setdomainname( + name.as_ptr().cast(), + name.len().try_into().map_err(|_| io::Errno::INVAL)?, + )) + } +} + +// https://github.com/rust-lang/libc/pull/4212 +#[cfg(target_os = "android")] pub(crate) fn setdomainname(name: &[u8]) -> io::Result<()> { syscall! { fn setdomainname( From ebeb70e78ae59f6fb28e4fed994c2bc31bf0c62d Mon Sep 17 00:00:00 2001 From: Yuki Sireneva Date: Wed, 18 Dec 2024 21:19:14 +0300 Subject: [PATCH 3/3] Disable setdomainname on illumos and solaris --- src/backend/libc/system/syscalls.rs | 2 ++ src/system.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/backend/libc/system/syscalls.rs b/src/backend/libc/system/syscalls.rs index 0c5b0f7d5..38b6fa8d2 100644 --- a/src/backend/libc/system/syscalls.rs +++ b/src/backend/libc/system/syscalls.rs @@ -67,8 +67,10 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> { target_os = "android", target_os = "emscripten", target_os = "espidf", + target_os = "illumos", target_os = "haiku", target_os = "redox", + target_os = "solaris", target_os = "vita", target_os = "wasi" )))] diff --git a/src/system.rs b/src/system.rs index 6d97158c0..ee3e7a087 100644 --- a/src/system.rs +++ b/src/system.rs @@ -180,7 +180,9 @@ pub fn sethostname(name: &[u8]) -> io::Result<()> { target_os = "emscripten", target_os = "espidf", target_os = "haiku", + target_os = "illumos", target_os = "redox", + target_os = "solaris", target_os = "vita", target_os = "wasi" )))]