Skip to content

Commit a3bc486

Browse files
yuki0iqsunfishcode
authored andcommitted
Add setdomainname function (#1244)
* Add setdomainname function * Use `libc::setdomainname` where possible Use weak `setdomainname` only for android; remove this special case once [libc#4212] is merged. [libc#4212]: rust-lang/libc#4212 * Disable setdomainname on illumos and solaris
1 parent a7ffbaa commit a3bc486

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/backend/libc/system/syscalls.rs

+38
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> {
6363
}
6464
}
6565

66+
#[cfg(not(any(
67+
target_os = "android",
68+
target_os = "emscripten",
69+
target_os = "espidf",
70+
target_os = "illumos",
71+
target_os = "haiku",
72+
target_os = "redox",
73+
target_os = "solaris",
74+
target_os = "vita",
75+
target_os = "wasi"
76+
)))]
77+
pub(crate) fn setdomainname(name: &[u8]) -> io::Result<()> {
78+
unsafe {
79+
ret(c::setdomainname(
80+
name.as_ptr().cast(),
81+
name.len().try_into().map_err(|_| io::Errno::INVAL)?,
82+
))
83+
}
84+
}
85+
86+
// https://github.com/rust-lang/libc/pull/4212
87+
#[cfg(target_os = "android")]
88+
pub(crate) fn setdomainname(name: &[u8]) -> io::Result<()> {
89+
syscall! {
90+
fn setdomainname(
91+
name: *const c::c_char,
92+
len: c::size_t
93+
) via SYS_setdomainname -> c::c_int
94+
}
95+
96+
unsafe {
97+
ret(setdomainname(
98+
name.as_ptr().cast(),
99+
name.len().try_into().map_err(|_| io::Errno::INVAL)?,
100+
))
101+
}
102+
}
103+
66104
#[cfg(target_os = "linux")]
67105
pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
68106
unsafe { ret(c::reboot(cmd as i32)) }

src/backend/linux_raw/system/syscalls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> {
3939
unsafe { ret(syscall_readonly!(__NR_sethostname, ptr, len)) }
4040
}
4141

42+
#[inline]
43+
pub(crate) fn setdomainname(name: &[u8]) -> io::Result<()> {
44+
let (ptr, len) = slice(name);
45+
unsafe { ret(syscall_readonly!(__NR_setdomainname, ptr, len)) }
46+
}
47+
4248
#[inline]
4349
pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
4450
unsafe {

src/system.rs

+23
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ pub fn sethostname(name: &[u8]) -> io::Result<()> {
168168
backend::system::syscalls::sethostname(name)
169169
}
170170

171+
/// `setdomain(name)`—Sets the system NIS domain name.
172+
///
173+
/// # References
174+
/// - [Linux]
175+
/// - [FreeBSD]
176+
///
177+
/// [Linux]: https://man7.org/linux/man-pages/man2/setdomainname.2.html
178+
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=setdomainname&sektion=3
179+
#[cfg(not(any(
180+
target_os = "emscripten",
181+
target_os = "espidf",
182+
target_os = "haiku",
183+
target_os = "illumos",
184+
target_os = "redox",
185+
target_os = "solaris",
186+
target_os = "vita",
187+
target_os = "wasi"
188+
)))]
189+
#[inline]
190+
pub fn setdomainname(name: &[u8]) -> io::Result<()> {
191+
backend::system::syscalls::setdomainname(name)
192+
}
193+
171194
/// Reboot command for use with [`reboot`].
172195
#[cfg(target_os = "linux")]
173196
#[derive(Copy, Clone, Debug, Eq, PartialEq)]

0 commit comments

Comments
 (0)