Skip to content

Commit 3937ba4

Browse files
committed
Fix the build with the next version of libc
The next version of libc includes some backwards-incompatible changes. Fixes #2342
1 parent 01cd697 commit 3937ba4

File tree

9 files changed

+57
-31
lines changed

9 files changed

+57
-31
lines changed

.cirrus.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env:
99
RUSTFLAGS: -D warnings
1010
RUSTDOCFLAGS: -D warnings
1111
TOOL: cargo
12-
MSRV: 1.69.0
12+
MSRV: 1.71.0
1313
ZFLAGS:
1414

1515
# Tests that don't require executing the build binaries
@@ -70,7 +70,7 @@ task:
7070
matrix:
7171
- name: Linux aarch64
7272
arm_container:
73-
image: rust:1.69.0
73+
image: rust:1.71.0
7474
cpu: 1
7575
env:
7676
TARGET: aarch64-unknown-linux-gnu

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ permissions:
1313
contents: read
1414

1515
env:
16-
MSRV: 1.69.0
16+
MSRV: 1.71.0
1717

1818
jobs:
1919

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ targets = [
2828
]
2929

3030
[dependencies]
31-
libc = { version = "0.2.153", features = ["extra_traits"] }
31+
libc = { git = "https://github.com/rust-lang/libc", rev = "a0f5b4b21391252fe38b2df9310dc65e37b07d9f", features = ["extra_traits", "const-extern-fn"] }
3232
bitflags = "2.3.1"
3333
cfg-if = "1.0"
3434
pin-utils = { version = "0.1.0", optional = true }

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ The following targets are supported by `nix`:
106106

107107
## Minimum Supported Rust Version (MSRV)
108108

109-
nix is supported on Rust 1.69 and higher. Its MSRV will not be
109+
nix is supported on Rust 1.71 and higher. Its MSRV will not be
110110
changed in the future without bumping the major or minor version.
111111

112112
## Contributing

src/sys/epoll.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ use std::mem;
66
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd, RawFd};
77

88
libc_bitflags!(
9-
pub struct EpollFlags: c_int {
10-
EPOLLIN;
11-
EPOLLPRI;
12-
EPOLLOUT;
13-
EPOLLRDNORM;
14-
EPOLLRDBAND;
15-
EPOLLWRNORM;
16-
EPOLLWRBAND;
17-
EPOLLMSG;
18-
EPOLLERR;
19-
EPOLLHUP;
20-
EPOLLRDHUP;
21-
EPOLLEXCLUSIVE;
9+
pub struct EpollFlags: u32 {
10+
EPOLLIN as u32;
11+
EPOLLPRI as u32;
12+
EPOLLOUT as u32;
13+
EPOLLRDNORM as u32;
14+
EPOLLRDBAND as u32;
15+
EPOLLWRNORM as u32;
16+
EPOLLWRBAND as u32;
17+
EPOLLMSG as u32;
18+
EPOLLERR as u32;
19+
EPOLLHUP as u32;
20+
EPOLLRDHUP as u32;
21+
EPOLLEXCLUSIVE as u32;
2222
#[cfg(not(target_arch = "mips"))]
23-
EPOLLWAKEUP;
24-
EPOLLONESHOT;
25-
EPOLLET;
23+
EPOLLWAKEUP as u32;
24+
EPOLLONESHOT as u32;
25+
EPOLLET as u32;
2626
}
2727
);
2828

@@ -51,7 +51,7 @@ impl EpollEvent {
5151
pub fn new(events: EpollFlags, data: u64) -> Self {
5252
EpollEvent {
5353
event: libc::epoll_event {
54-
events: events.bits() as u32,
54+
events: events.bits(),
5555
u64: data,
5656
},
5757
}
@@ -62,7 +62,7 @@ impl EpollEvent {
6262
}
6363

6464
pub fn events(&self) -> EpollFlags {
65-
EpollFlags::from_bits(self.event.events as c_int).unwrap()
65+
EpollFlags::from_bits(self.event.events).unwrap()
6666
}
6767

6868
pub fn data(&self) -> u64 {

src/sys/select.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct FdSet<'fd> {
2222

2323
fn assert_fd_valid(fd: RawFd) {
2424
assert!(
25-
usize::try_from(fd).map_or(false, |fd| fd < FD_SETSIZE),
25+
fd < RawFd::try_from(FD_SETSIZE).unwrap_or(RawFd::min_value()),
2626
"fd must be in the range 0..FD_SETSIZE",
2727
);
2828
}
@@ -107,10 +107,11 @@ impl<'fd> FdSet<'fd> {
107107
/// assert_eq!(fds, vec![4, 9]);
108108
/// ```
109109
#[inline]
110+
#[allow(clippy::unnecessary_cast)] // Not unnecessary with libc 0.2.154+
110111
pub fn fds(&self, highest: Option<RawFd>) -> Fds {
111112
Fds {
112113
set: self,
113-
range: 0..highest.map(|h| h as usize + 1).unwrap_or(FD_SETSIZE),
114+
range: 0..highest.map(|h| h as usize + 1).unwrap_or(FD_SETSIZE as usize),
114115
}
115116
}
116117
}

src/unistd.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,12 @@ fn to_exec_array<S: AsRef<CStr>>(args: &[S]) -> Vec<*const c_char> {
823823
pub fn execv<S: AsRef<CStr>>(path: &CStr, argv: &[S]) -> Result<Infallible> {
824824
let args_p = to_exec_array(argv);
825825

826-
unsafe { libc::execv(path.as_ptr(), args_p.as_ptr()) };
826+
// SAFETY:
827+
// The const cast looks unsafe. But it's actually fine. The problem is that POSIX requires
828+
// "execv" and friends to take mutable pointers in their signatures, even while it prohibits
829+
// them from actually modifying those arguments. See discussion at
830+
// https://github.com/rust-lang/libc/issues/1272 .
831+
unsafe { libc::execv(path.as_ptr(), args_p.as_ptr() as *const _) };
827832

828833
Err(Errno::last())
829834
}
@@ -849,7 +854,12 @@ pub fn execve<SA: AsRef<CStr>, SE: AsRef<CStr>>(
849854
let args_p = to_exec_array(args);
850855
let env_p = to_exec_array(env);
851856

852-
unsafe { libc::execve(path.as_ptr(), args_p.as_ptr(), env_p.as_ptr()) };
857+
// SAFETY:
858+
// The const cast looks unsafe. But it's actually fine. The problem is that POSIX requires
859+
// "execv" and friends to take mutable pointers in their signatures, even while it prohibits
860+
// them from actually modifying those arguments. See discussion at
861+
// https://github.com/rust-lang/libc/issues/1272 .
862+
unsafe { libc::execve(path.as_ptr(), args_p.as_ptr() as *const _, env_p.as_ptr() as *const _) };
853863

854864
Err(Errno::last())
855865
}
@@ -870,7 +880,12 @@ pub fn execvp<S: AsRef<CStr>>(
870880
) -> Result<Infallible> {
871881
let args_p = to_exec_array(args);
872882

873-
unsafe { libc::execvp(filename.as_ptr(), args_p.as_ptr()) };
883+
// SAFETY:
884+
// The const cast looks unsafe. But it's actually fine. The problem is that POSIX requires
885+
// "execv" and friends to take mutable pointers in their signatures, even while it prohibits
886+
// them from actually modifying those arguments. See discussion at
887+
// https://github.com/rust-lang/libc/issues/1272 .
888+
unsafe { libc::execvp(filename.as_ptr(), args_p.as_ptr() as *const _) };
874889

875890
Err(Errno::last())
876891
}
@@ -891,8 +906,13 @@ pub fn execvpe<SA: AsRef<CStr>, SE: AsRef<CStr>>(
891906
let args_p = to_exec_array(args);
892907
let env_p = to_exec_array(env);
893908

909+
// SAFETY:
910+
// The const cast looks unsafe. But it's actually fine. The problem is that POSIX requires
911+
// "execv" and friends to take mutable pointers in their signatures, even while it prohibits
912+
// them from actually modifying those arguments. See discussion at
913+
// https://github.com/rust-lang/libc/issues/1272 .
894914
unsafe {
895-
libc::execvpe(filename.as_ptr(), args_p.as_ptr(), env_p.as_ptr())
915+
libc::execvpe(filename.as_ptr(), args_p.as_ptr() as *const _, env_p.as_ptr() as *const _)
896916
};
897917

898918
Err(Errno::last())
@@ -918,7 +938,12 @@ pub fn fexecve<SA: AsRef<CStr>, SE: AsRef<CStr>>(
918938
let args_p = to_exec_array(args);
919939
let env_p = to_exec_array(env);
920940

921-
unsafe { libc::fexecve(fd, args_p.as_ptr(), env_p.as_ptr()) };
941+
// SAFETY:
942+
// The const cast looks unsafe. But it's actually fine. The problem is that POSIX requires
943+
// "execv" and friends to take mutable pointers in their signatures, even while it prohibits
944+
// them from actually modifying those arguments. See discussion at
945+
// https://github.com/rust-lang/libc/issues/1272 .
946+
unsafe { libc::fexecve(fd, args_p.as_ptr() as *const _, env_p.as_ptr() as *const _) };
922947

923948
Err(Errno::last())
924949
}

test/sys/test_select.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ macro_rules! generate_fdset_bad_fd_tests {
6666
}
6767
}
6868

69+
#[allow(clippy::useless_conversion)] // Not unnecessary with libc 0.2.153
6970
mod test_fdset_too_large_fd {
7071
use super::*;
7172
generate_fdset_bad_fd_tests!(

test/sys/test_timer.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use nix::sys::signal::{
44
};
55
use nix::sys::timer::{Expiration, Timer, TimerSetTimeFlags};
66
use nix::time::ClockId;
7-
use std::convert::TryFrom;
87
use std::sync::atomic::{AtomicBool, Ordering};
98
use std::thread;
109
use std::time::{Duration, Instant};

0 commit comments

Comments
 (0)