Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setdomainname function #1244

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions src/backend/libc/system/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a quick glance, the libc crate does have declarations for setdomainname; could you comment on why tthis uses syscall! instead of just libc::setdomainname?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out libc::setdomainname isn't there for android (failed CI)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate to make non-Linux users use a weak symbol though. Could you use libc::setdomainname on all platforms except Android using cfgs?

I also filed rust-lang/libc#4212 to add the declarations to libc for Android.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


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
23 changes: 23 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Loading