Skip to content

Commit 50b41a8

Browse files
authored
Merge branch 'master' into issue1814-expose-domainname-of-utsname
2 parents e0918dc + 1038ee5 commit 50b41a8

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1010
([#1804](https://github.com/nix-rust/nix/pull/1804))
1111
- Added `line_discipline` field to `Termios` on Linux, Android and Haiku
1212
([#1805](https://github.com/nix-rust/nix/pull/1805))
13+
- Expose the memfd module on FreeBSD (memfd was added in FreeBSD 13)
14+
([#1808](https://github.com/nix-rust/nix/pull/1808))
1315
- Added `domainname` field of `UtsName` on Android and Linux
1416
([#1817](https://github.com/nix-rust/nix/pull/1817))
15-
17+
1618
### Changed
1719

1820
- The MSRV is now 1.56.1
@@ -22,6 +24,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2224

2325
- Fix microsecond calculation for `TimeSpec`.
2426
([#1801](https://github.com/nix-rust/nix/pull/1801))
27+
- Fix `User::from_name` and `Group::from_name` panicking
28+
when given a name containing a nul.
29+
([#1815](https://github.com/nix-rust/nix/pull/1815))
2530

2631
### Removed
2732

src/fcntl.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,9 +809,7 @@ pub fn fspacectl_all(fd: RawFd, offset: libc::off_t, len: libc::off_t)
809809
0, // No flags are currently supported
810810
&mut rqsr
811811
)};
812-
if let Err(e) = Errno::result(res) {
813-
return Err(e);
814-
}
812+
Errno::result(res)?;
815813
}
816814
Ok(())
817815
}

src/sys/memfd.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Interfaces for managing memory-backed files.
22
33
use std::os::unix::io::RawFd;
4+
use cfg_if::cfg_if;
5+
46
use crate::Result;
57
use crate::errno::Errno;
68
use std::ffi::CStr;
@@ -40,7 +42,22 @@ libc_bitflags!(
4042
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
4143
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
4244
let res = unsafe {
43-
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
45+
cfg_if! {
46+
if #[cfg(all(
47+
// Android does not have a memfd_create symbol
48+
not(target_os = "android"),
49+
any(
50+
target_os = "freebsd",
51+
// If the OS is Linux, gnu and musl expose a memfd_create symbol but not uclibc
52+
target_env = "gnu",
53+
target_env = "musl",
54+
)))]
55+
{
56+
libc::memfd_create(name.as_ptr(), flags.bits())
57+
} else {
58+
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
59+
}
60+
}
4461
};
4562

4663
Errno::result(res).map(|r| r as RawFd)

src/sys/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ feature! {
5050
#[macro_use]
5151
pub mod ioctl;
5252

53-
#[cfg(any(target_os = "android", target_os = "linux"))]
53+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
5454
feature! {
5555
#![feature = "fs"]
5656
pub mod memfd;

src/sys/socket/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ pub fn sendmsg<S>(fd: RawFd, iov: &[IoSlice<'_>], cmsgs: &[ControlMessage],
14591459
// because subsequent code will not clear the padding bytes.
14601460
let mut cmsg_buffer = vec![0u8; capacity];
14611461

1462-
let mhdr = pack_mhdr_to_send(&mut cmsg_buffer[..], &iov, &cmsgs, addr);
1462+
let mhdr = pack_mhdr_to_send(&mut cmsg_buffer[..], iov, cmsgs, addr);
14631463

14641464
let ret = unsafe { libc::sendmsg(fd, &mhdr, flags.bits()) };
14651465

src/sys/socket/sockopt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ struct SetBool {
816816

817817
impl<'a> Set<'a, bool> for SetBool {
818818
fn new(val: &'a bool) -> SetBool {
819-
SetBool { val: if *val { 1 } else { 0 } }
819+
SetBool { val: i32::from(*val) }
820820
}
821821

822822
fn ffi_ptr(&self) -> *const c_void {

src/unistd.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,7 +3143,10 @@ impl User {
31433143
/// assert_eq!(res.name, "root");
31443144
/// ```
31453145
pub fn from_name(name: &str) -> Result<Option<Self>> {
3146-
let name = CString::new(name).unwrap();
3146+
let name = match CString::new(name) {
3147+
Ok(c_str) => c_str,
3148+
Err(_nul_error) => return Ok(None),
3149+
};
31473150
User::from_anything(|pwd, cbuf, cap, res| {
31483151
unsafe { libc::getpwnam_r(name.as_ptr(), pwd, cbuf, cap, res) }
31493152
})
@@ -3268,7 +3271,10 @@ impl Group {
32683271
/// assert!(res.name == "root");
32693272
/// ```
32703273
pub fn from_name(name: &str) -> Result<Option<Self>> {
3271-
let name = CString::new(name).unwrap();
3274+
let name = match CString::new(name) {
3275+
Ok(c_str) => c_str,
3276+
Err(_nul_error) => return Ok(None),
3277+
};
32723278
Group::from_anything(|grp, cbuf, cap, res| {
32733279
unsafe { libc::getgrnam_r(name.as_ptr(), grp, cbuf, cap, res) }
32743280
})

test/test_fcntl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn test_readlink() {
216216
let src = tempdir.path().join("a");
217217
let dst = tempdir.path().join("b");
218218
println!("a: {:?}, b: {:?}", &src, &dst);
219-
fs::symlink(&src.as_path(), &dst.as_path()).unwrap();
219+
fs::symlink(src.as_path(), dst.as_path()).unwrap();
220220
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap();
221221
let expected_dir = src.to_str().unwrap();
222222

0 commit comments

Comments
 (0)