Skip to content

Commit

Permalink
Add setdomainname function
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki0iq committed Dec 17, 2024
1 parent 74bcc35 commit 3d49429
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/backend/libc/system/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down
6 changes: 6 additions & 0 deletions src/backend/linux_raw/system/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 3d49429

Please sign in to comment.