Skip to content

Commit bc65825

Browse files
committed
[RFC][wip] rustix: on Linux, support a build without linux-raw-sys
Rustix support was added to Mesa3D recently, but it only works with the linux-raw-sys backend. On Linux, even the libc backend, there is a dependency on linux-raw-sys for certain ABI definitions. When updating rustix in Android to support this, I realized only the libc backend is supported in Android. This is because Android as a platform wants to discourage raw syscalls, and to be able to globally update libc. Features like fdtrack would not work with the linux-raw-sys backend: https://android.googlesource.com/platform/bionic/+/HEAD/docs/fdtrack.md Given Android wants to incentize use of libc only, this MR introduces the concept of "linux-raw-dep". This means that linux-raw-sys is present as a dependency for the user. This will be on by default even for the libc backend, but will be turned off for Android. This will also help others who don't need all the features rustix offers on Linux, so they have one less dependency to maintain. Tested via: cargo build --features=use-libc,net,std,alloc,event,fs,mm,param,pipe These are the same set of features needed by Mesa3D.
1 parent 0a6cb07 commit bc65825

12 files changed

Lines changed: 77 additions & 72 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ check-cfg = [
259259
'cfg(linux_kernel)',
260260
'cfg(linux_like)',
261261
'cfg(linux_raw)',
262+
'cfg(linux_raw_dep)',
262263
'cfg(lower_upper_exp_for_non_zero)',
263264
'cfg(netbsdlike)',
264265
'cfg(rustc_attrs)',

src/backend/libc/c.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,49 @@ pub(crate) const NFS_SUPER_MAGIC: u32 = 0x0000_6969;
1919
pub(crate) const EXIT_SIGNALED_SIGABRT: c_int = 128 + SIGABRT as c_int;
2020

2121
// TODO: Upstream these.
22-
#[cfg(all(linux_kernel, feature = "net"))]
22+
#[cfg(all(linux_raw_dep, feature = "net"))]
2323
pub(crate) const ETH_P_TSN: c_int = linux_raw_sys::if_ether::ETH_P_TSN as _;
24-
#[cfg(all(linux_kernel, feature = "net"))]
24+
#[cfg(all(linux_raw_dep, feature = "net"))]
2525
pub(crate) const ETH_P_ERSPAN2: c_int = linux_raw_sys::if_ether::ETH_P_ERSPAN2 as _;
26-
#[cfg(all(linux_kernel, feature = "net"))]
26+
#[cfg(all(linux_raw_dep, feature = "net"))]
2727
pub(crate) const ETH_P_ERSPAN: c_int = linux_raw_sys::if_ether::ETH_P_ERSPAN as _;
28-
#[cfg(all(linux_kernel, feature = "net"))]
28+
#[cfg(all(linux_raw_dep, feature = "net"))]
2929
pub(crate) const ETH_P_PROFINET: c_int = linux_raw_sys::if_ether::ETH_P_PROFINET as _;
30-
#[cfg(all(linux_kernel, feature = "net"))]
30+
#[cfg(all(linux_raw_dep, feature = "net"))]
3131
pub(crate) const ETH_P_REALTEK: c_int = linux_raw_sys::if_ether::ETH_P_REALTEK as _;
32-
#[cfg(all(linux_kernel, feature = "net"))]
32+
#[cfg(all(linux_raw_dep, feature = "net"))]
3333
pub(crate) const ETH_P_ETHERCAT: c_int = linux_raw_sys::if_ether::ETH_P_ETHERCAT as _;
34-
#[cfg(all(linux_kernel, feature = "net"))]
34+
#[cfg(all(linux_raw_dep, feature = "net"))]
3535
pub(crate) const ETH_P_PREAUTH: c_int = linux_raw_sys::if_ether::ETH_P_PREAUTH as _;
36-
#[cfg(all(linux_kernel, feature = "net"))]
36+
#[cfg(all(linux_raw_dep, feature = "net"))]
3737
pub(crate) const ETH_P_LLDP: c_int = linux_raw_sys::if_ether::ETH_P_LLDP as _;
38-
#[cfg(all(linux_kernel, feature = "net"))]
38+
#[cfg(all(linux_raw_dep, feature = "net"))]
3939
pub(crate) const ETH_P_MRP: c_int = linux_raw_sys::if_ether::ETH_P_MRP as _;
40-
#[cfg(all(linux_kernel, feature = "net"))]
40+
#[cfg(all(linux_raw_dep, feature = "net"))]
4141
pub(crate) const ETH_P_NCSI: c_int = linux_raw_sys::if_ether::ETH_P_NCSI as _;
42-
#[cfg(all(linux_kernel, feature = "net"))]
42+
#[cfg(all(linux_raw_dep, feature = "net"))]
4343
pub(crate) const ETH_P_CFM: c_int = linux_raw_sys::if_ether::ETH_P_CFM as _;
44-
#[cfg(all(linux_kernel, feature = "net"))]
44+
#[cfg(all(linux_raw_dep, feature = "net"))]
4545
pub(crate) const ETH_P_IBOE: c_int = linux_raw_sys::if_ether::ETH_P_IBOE as _;
46-
#[cfg(all(linux_kernel, feature = "net"))]
46+
#[cfg(all(linux_raw_dep, feature = "net"))]
4747
pub(crate) const ETH_P_HSR: c_int = linux_raw_sys::if_ether::ETH_P_HSR as _;
48-
#[cfg(all(linux_kernel, feature = "net"))]
48+
#[cfg(all(linux_raw_dep, feature = "net"))]
4949
pub(crate) const ETH_P_NSH: c_int = linux_raw_sys::if_ether::ETH_P_NSH as _;
50-
#[cfg(all(linux_kernel, feature = "net"))]
50+
#[cfg(all(linux_raw_dep, feature = "net"))]
5151
pub(crate) const ETH_P_DSA_8021Q: c_int = linux_raw_sys::if_ether::ETH_P_DSA_8021Q as _;
52-
#[cfg(all(linux_kernel, feature = "net"))]
52+
#[cfg(all(linux_raw_dep, feature = "net"))]
5353
pub(crate) const ETH_P_DSA_A5PSW: c_int = linux_raw_sys::if_ether::ETH_P_DSA_A5PSW as _;
54-
#[cfg(all(linux_kernel, feature = "net"))]
54+
#[cfg(all(linux_raw_dep, feature = "net"))]
5555
pub(crate) const ETH_P_IFE: c_int = linux_raw_sys::if_ether::ETH_P_IFE as _;
56-
#[cfg(all(linux_kernel, feature = "net"))]
56+
#[cfg(all(linux_raw_dep, feature = "net"))]
5757
pub(crate) const ETH_P_CAN: c_int = linux_raw_sys::if_ether::ETH_P_CAN as _;
58-
#[cfg(all(linux_kernel, feature = "net"))]
58+
#[cfg(all(linux_raw_dep, feature = "net"))]
5959
pub(crate) const ETH_P_CANXL: c_int = linux_raw_sys::if_ether::ETH_P_CANXL as _;
60-
#[cfg(all(linux_kernel, feature = "net"))]
60+
#[cfg(all(linux_raw_dep, feature = "net"))]
6161
pub(crate) const ETH_P_XDSA: c_int = linux_raw_sys::if_ether::ETH_P_XDSA as _;
62-
#[cfg(all(linux_kernel, feature = "net"))]
62+
#[cfg(all(linux_raw_dep, feature = "net"))]
6363
pub(crate) const ETH_P_MAP: c_int = linux_raw_sys::if_ether::ETH_P_MAP as _;
64-
#[cfg(all(linux_kernel, feature = "net"))]
64+
#[cfg(all(linux_raw_dep, feature = "net"))]
6565
pub(crate) const ETH_P_MCTP: c_int = linux_raw_sys::if_ether::ETH_P_MCTP as _;
6666

6767
#[cfg(all(
@@ -90,7 +90,7 @@ pub(crate) const MSG_DONTWAIT: c_int = MSG_NONBLOCK;
9090
// <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/open.c?h=v6.13#n1423>
9191
// so libc implementations may leave it undefined or defined to zero.
9292
#[cfg(linux_kernel)]
93-
pub(crate) const O_LARGEFILE: c_int = linux_raw_sys::general::O_LARGEFILE as _;
93+
pub(crate) const O_LARGEFILE: c_int = libc::O_LARGEFILE as _;
9494

9595
// Gated under `_LARGEFILE_SOURCE` but automatically set by the kernel.
9696
// <https://github.com/illumos/illumos-gate/blob/fb2cb638e5604b214d8ea8d4f01ad2e77b437c17/usr/src/ucbhead/sys/fcntl.h#L64>

src/backend/libc/fs/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ pub(crate) mod syscalls;
1616
pub(crate) mod types;
1717

1818
// TODO: Fix linux-raw-sys to define ioctl codes for sparc.
19-
#[cfg(all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")))]
19+
#[cfg(all(linux_raw_dep, any(target_arch = "sparc", target_arch = "sparc64")))]
2020
pub(crate) const EXT4_IOC_RESIZE_FS: crate::ioctl::Opcode = 0x8008_6610;
2121

22-
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
22+
#[cfg(all(linux_raw_dep, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
2323
pub(crate) const EXT4_IOC_RESIZE_FS: crate::ioctl::Opcode =
2424
linux_raw_sys::ioctl::EXT4_IOC_RESIZE_FS as crate::ioctl::Opcode;

src/backend/libc/fs/syscalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ pub(crate) fn memfd_create(name: &CStr, flags: MemfdFlags) -> io::Result<OwnedFd
18401840
unsafe { ret_owned_fd(memfd_create(c_str(name), bitflags_bits!(flags))) }
18411841
}
18421842

1843-
#[cfg(linux_kernel)]
1843+
#[cfg(linux_raw_dep)]
18441844
pub(crate) fn openat2(
18451845
dirfd: BorrowedFd<'_>,
18461846
path: &CStr,

src/backend/libc/io/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ bitflags! {
2727
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
2828
pub struct ReadWriteFlags: u32 {
2929
/// `RWF_DSYNC` (since Linux 4.7)
30-
const DSYNC = linux_raw_sys::general::RWF_DSYNC;
30+
const DSYNC = libc::RWF_DSYNC as u32;
3131
/// `RWF_HIPRI` (since Linux 4.6)
32-
const HIPRI = linux_raw_sys::general::RWF_HIPRI;
32+
const HIPRI = libc::RWF_HIPRI as u32;
3333
/// `RWF_SYNC` (since Linux 4.7)
34-
const SYNC = linux_raw_sys::general::RWF_SYNC;
34+
const SYNC = libc::RWF_SYNC as u32;
3535
/// `RWF_NOWAIT` (since Linux 4.14)
36-
const NOWAIT = linux_raw_sys::general::RWF_NOWAIT;
36+
const NOWAIT = libc::RWF_NOWAIT as u32;
3737
/// `RWF_APPEND` (since Linux 4.16)
38-
const APPEND = linux_raw_sys::general::RWF_APPEND;
38+
const APPEND = libc::RWF_APPEND as u32;
3939

4040
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
4141
const _ = !0;

src/backend/libc/mm/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ bitflags! {
4444
#[cfg(linux_kernel)]
4545
const GROWSDOWN = bitcast!(c::PROT_GROWSDOWN);
4646
/// `PROT_SEM`
47-
#[cfg(linux_kernel)]
47+
#[cfg(linux_raw_dep)]
4848
const SEM = linux_raw_sys::general::PROT_SEM;
4949
/// `PROT_BTI`
50-
#[cfg(all(linux_kernel, target_arch = "aarch64"))]
50+
#[cfg(all(linux_raw_dep, target_arch = "aarch64"))]
5151
const BTI = linux_raw_sys::general::PROT_BTI;
5252
/// `PROT_MTE`
53-
#[cfg(all(linux_kernel, target_arch = "aarch64"))]
53+
#[cfg(all(linux_raw_dep, target_arch = "aarch64"))]
5454
const MTE = linux_raw_sys::general::PROT_MTE;
5555
/// `PROT_SAO`
56-
#[cfg(all(linux_kernel, any(target_arch = "powerpc", target_arch = "powerpc64")))]
56+
#[cfg(all(linux_raw_dep, any(target_arch = "powerpc", target_arch = "powerpc64")))]
5757
const SAO = linux_raw_sys::general::PROT_SAO;
5858
/// `PROT_ADI`
59-
#[cfg(all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")))]
59+
#[cfg(all(linux_raw_dep, any(target_arch = "sparc", target_arch = "sparc64")))]
6060
const ADI = linux_raw_sys::general::PROT_ADI;
6161

6262
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>

src/backend/libc/net/sockopt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use c::TCP_KEEPALIVE as TCP_KEEPIDLE;
7676
use c::TCP_KEEPIDLE;
7777
use core::mem::{size_of, MaybeUninit};
7878
use core::time::Duration;
79-
#[cfg(target_os = "linux")]
79+
#[cfg(linux_raw_dep)]
8080
use linux_raw_sys::xdp::{xdp_mmap_offsets, xdp_statistics, xdp_statistics_v1};
8181
#[cfg(windows)]
8282
use windows_sys::Win32::Foundation::BOOL;
@@ -1090,7 +1090,7 @@ pub(crate) fn set_xdp_rx_ring_size(fd: BorrowedFd<'_>, value: u32) -> io::Result
10901090
setsockopt(fd, c::SOL_XDP, c::XDP_RX_RING, value)
10911091
}
10921092

1093-
#[cfg(target_os = "linux")]
1093+
#[cfg(linux_raw_dep)]
10941094
#[inline]
10951095
pub(crate) fn xdp_mmap_offsets(fd: BorrowedFd<'_>) -> io::Result<XdpMmapOffsets> {
10961096
// The kernel will write `xdp_mmap_offsets` or `xdp_mmap_offsets_v1` to the
@@ -1177,7 +1177,7 @@ pub(crate) fn xdp_mmap_offsets(fd: BorrowedFd<'_>) -> io::Result<XdpMmapOffsets>
11771177
}
11781178
}
11791179

1180-
#[cfg(target_os = "linux")]
1180+
#[cfg(linux_raw_dep)]
11811181
#[inline]
11821182
pub(crate) fn xdp_statistics(fd: BorrowedFd<'_>) -> io::Result<XdpStatistics> {
11831183
let mut optlen = size_of::<xdp_statistics>().try_into().unwrap();

src/fs/ioctl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn ioctl_ficlone<Fd: AsFd, SrcFd: AsFd>(fd: Fd, src_fd: SrcFd) -> io::Result
5858
}
5959

6060
/// `ioctl(fd, EXT4_IOC_RESIZE_FS, blocks)`—Resize ext4 filesystem on fd.
61-
#[cfg(linux_kernel)]
61+
#[cfg(linux_raw_dep)]
6262
#[inline]
6363
#[doc(alias = "EXT4_IOC_RESIZE_FS")]
6464
pub fn ext4_ioc_resize_fs<Fd: AsFd>(fd: Fd, blocks: u64) -> io::Result<()> {
@@ -94,7 +94,7 @@ unsafe impl ioctl::Ioctl for Ficlone<'_> {
9494
}
9595
}
9696

97-
#[cfg(linux_kernel)]
97+
#[cfg(linux_raw_dep)]
9898
bitflags! {
9999
/// `FS_*` constants for use with [`ioctl_getflags`].
100100
///
@@ -136,7 +136,7 @@ bitflags! {
136136
/// `ioctl(fd, FS_IOC_GETFLAGS)`—Returns the [inode flags] attributes
137137
///
138138
/// [inode flags]: https://man7.org/linux/man-pages/man2/ioctl_iflags.2.html
139-
#[cfg(linux_kernel)]
139+
#[cfg(linux_raw_dep)]
140140
#[inline]
141141
#[doc(alias = "FS_IOC_GETFLAGS")]
142142
pub fn ioctl_getflags<Fd: AsFd>(fd: Fd) -> io::Result<IFlags> {
@@ -153,7 +153,7 @@ pub fn ioctl_getflags<Fd: AsFd>(fd: Fd) -> io::Result<IFlags> {
153153
/// `ioctl(fd, FS_IOC_SETFLAGS)`—Modify the [inode flags] attributes
154154
///
155155
/// [inode flags]: https://man7.org/linux/man-pages/man2/ioctl_iflags.2.html
156-
#[cfg(linux_kernel)]
156+
#[cfg(linux_raw_dep)]
157157
#[inline]
158158
#[doc(alias = "FS_IOC_SETFLAGS")]
159159
pub fn ioctl_setflags<Fd: AsFd>(fd: Fd, flags: IFlags) -> io::Result<()> {

src/fs/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) mod fd;
3030
mod getpath;
3131
#[cfg(not(target_os = "wasi"))] // WASI doesn't have get[gpu]id.
3232
mod id;
33-
#[cfg(linux_kernel)]
33+
#[cfg(linux_raw_dep)]
3434
pub mod inotify;
3535
#[cfg(linux_kernel)]
3636
mod ioctl;
@@ -45,7 +45,7 @@ mod ioctl;
4545
mod makedev;
4646
#[cfg(any(linux_kernel, target_os = "freebsd"))]
4747
mod memfd_create;
48-
#[cfg(linux_kernel)]
48+
#[cfg(linux_raw_dep)]
4949
mod openat2;
5050
#[cfg(linux_kernel)]
5151
mod raw_dir;
@@ -110,7 +110,7 @@ pub use ioctl::*;
110110
pub use makedev::*;
111111
#[cfg(any(linux_kernel, target_os = "freebsd"))]
112112
pub use memfd_create::memfd_create;
113-
#[cfg(linux_kernel)]
113+
#[cfg(linux_raw_dep)]
114114
pub use openat2::openat2;
115115
#[cfg(linux_kernel)]
116116
pub use raw_dir::{RawDir, RawDirEntry};

src/fs/raw_dir.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use crate::fs::FileType;
77
use crate::io;
88
use core::fmt;
99
use core::mem::{align_of, MaybeUninit};
10+
11+
#[cfg(not(linux_raw_dep))]
12+
use libc::dirent64 as linux_dirent64;
13+
#[cfg(linux_raw_dep)]
1014
use linux_raw_sys::general::linux_dirent64;
1115

1216
/// A directory iterator implemented with getdents.

0 commit comments

Comments
 (0)