Skip to content

Commit

Permalink
Merge branch 'master' into issue1814-expose-domainname-of-utsname
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC committed Sep 12, 2022
2 parents e0918dc + 1038ee5 commit 50b41a8
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -10,9 +10,11 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#1804](https://github.com/nix-rust/nix/pull/1804))
- Added `line_discipline` field to `Termios` on Linux, Android and Haiku
([#1805](https://github.com/nix-rust/nix/pull/1805))
- Expose the memfd module on FreeBSD (memfd was added in FreeBSD 13)
([#1808](https://github.com/nix-rust/nix/pull/1808))
- Added `domainname` field of `UtsName` on Android and Linux
([#1817](https://github.com/nix-rust/nix/pull/1817))

### Changed

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

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

### Removed

Expand Down
4 changes: 1 addition & 3 deletions src/fcntl.rs
Expand Up @@ -809,9 +809,7 @@ pub fn fspacectl_all(fd: RawFd, offset: libc::off_t, len: libc::off_t)
0, // No flags are currently supported
&mut rqsr
)};
if let Err(e) = Errno::result(res) {
return Err(e);
}
Errno::result(res)?;
}
Ok(())
}
Expand Down
19 changes: 18 additions & 1 deletion src/sys/memfd.rs
@@ -1,6 +1,8 @@
//! Interfaces for managing memory-backed files.

use std::os::unix::io::RawFd;
use cfg_if::cfg_if;

use crate::Result;
use crate::errno::Errno;
use std::ffi::CStr;
Expand Down Expand Up @@ -40,7 +42,22 @@ libc_bitflags!(
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
let res = unsafe {
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
cfg_if! {
if #[cfg(all(
// Android does not have a memfd_create symbol
not(target_os = "android"),
any(
target_os = "freebsd",
// If the OS is Linux, gnu and musl expose a memfd_create symbol but not uclibc
target_env = "gnu",
target_env = "musl",
)))]
{
libc::memfd_create(name.as_ptr(), flags.bits())
} else {
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
}
}
};

Errno::result(res).map(|r| r as RawFd)
Expand Down
2 changes: 1 addition & 1 deletion src/sys/mod.rs
Expand Up @@ -50,7 +50,7 @@ feature! {
#[macro_use]
pub mod ioctl;

#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
feature! {
#![feature = "fs"]
pub mod memfd;
Expand Down
2 changes: 1 addition & 1 deletion src/sys/socket/mod.rs
Expand Up @@ -1459,7 +1459,7 @@ pub fn sendmsg<S>(fd: RawFd, iov: &[IoSlice<'_>], cmsgs: &[ControlMessage],
// because subsequent code will not clear the padding bytes.
let mut cmsg_buffer = vec![0u8; capacity];

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/sys/socket/sockopt.rs
Expand Up @@ -816,7 +816,7 @@ struct SetBool {

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

fn ffi_ptr(&self) -> *const c_void {
Expand Down
10 changes: 8 additions & 2 deletions src/unistd.rs
Expand Up @@ -3143,7 +3143,10 @@ impl User {
/// assert_eq!(res.name, "root");
/// ```
pub fn from_name(name: &str) -> Result<Option<Self>> {
let name = CString::new(name).unwrap();
let name = match CString::new(name) {
Ok(c_str) => c_str,
Err(_nul_error) => return Ok(None),
};
User::from_anything(|pwd, cbuf, cap, res| {
unsafe { libc::getpwnam_r(name.as_ptr(), pwd, cbuf, cap, res) }
})
Expand Down Expand Up @@ -3268,7 +3271,10 @@ impl Group {
/// assert!(res.name == "root");
/// ```
pub fn from_name(name: &str) -> Result<Option<Self>> {
let name = CString::new(name).unwrap();
let name = match CString::new(name) {
Ok(c_str) => c_str,
Err(_nul_error) => return Ok(None),
};
Group::from_anything(|grp, cbuf, cap, res| {
unsafe { libc::getgrnam_r(name.as_ptr(), grp, cbuf, cap, res) }
})
Expand Down
2 changes: 1 addition & 1 deletion test/test_fcntl.rs
Expand Up @@ -216,7 +216,7 @@ fn test_readlink() {
let src = tempdir.path().join("a");
let dst = tempdir.path().join("b");
println!("a: {:?}, b: {:?}", &src, &dst);
fs::symlink(&src.as_path(), &dst.as_path()).unwrap();
fs::symlink(src.as_path(), dst.as_path()).unwrap();
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap();
let expected_dir = src.to_str().unwrap();

Expand Down

0 comments on commit 50b41a8

Please sign in to comment.