diff --git a/src/backend/libc/system/syscalls.rs b/src/backend/libc/system/syscalls.rs index 55978c7f6..38b6fa8d2 100644 --- a/src/backend/libc/system/syscalls.rs +++ b/src/backend/libc/system/syscalls.rs @@ -63,6 +63,44 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> { } } +#[cfg(not(any( + 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" +)))] +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( + 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..ee3e7a087 100644 --- a/src/system.rs +++ b/src/system.rs @@ -168,6 +168,29 @@ 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 = "illumos", + target_os = "redox", + target_os = "solaris", + 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)]