rustix/backend/linux_raw/
c.rs

1//! Adapt the Linux API to resemble a POSIX-style libc API.
2//!
3//! The linux_raw backend doesn't use actual libc; this just defines certain
4//! types that are convenient to have defined.
5
6#![allow(unused_imports)]
7#![allow(non_camel_case_types)]
8
9pub(crate) type size_t = usize;
10pub(crate) use linux_raw_sys::ctypes::*;
11pub(crate) use linux_raw_sys::errno::EINVAL;
12pub(crate) use linux_raw_sys::ioctl::{FIONBIO, FIONREAD};
13// Import the kernel's `uid_t` and `gid_t` if they're 32-bit.
14#[cfg(not(any(target_arch = "arm", target_arch = "sparc", target_arch = "x86")))]
15pub(crate) use linux_raw_sys::general::{__kernel_gid_t as gid_t, __kernel_uid_t as uid_t};
16pub(crate) use linux_raw_sys::general::{
17    __kernel_pid_t as pid_t, __kernel_time64_t as time_t, __kernel_timespec as timespec, iovec,
18    O_CLOEXEC, O_NOCTTY, O_NONBLOCK, O_RDWR,
19};
20
21#[cfg(feature = "event")]
22#[cfg(test)]
23pub(crate) use linux_raw_sys::general::epoll_event;
24
25#[cfg(any(
26    feature = "fs",
27    all(
28        not(feature = "use-libc-auxv"),
29        not(feature = "use-explicitly-provided-auxv"),
30        any(
31            feature = "param",
32            feature = "process",
33            feature = "runtime",
34            feature = "time",
35            target_arch = "x86",
36        )
37    )
38))]
39pub(crate) use linux_raw_sys::general::{
40    AT_FDCWD, NFS_SUPER_MAGIC, O_LARGEFILE, PROC_SUPER_MAGIC, UTIME_NOW, UTIME_OMIT, XATTR_CREATE,
41    XATTR_REPLACE,
42};
43
44pub(crate) use linux_raw_sys::ioctl::{BLKPBSZGET, BLKSSZGET, FICLONE};
45#[cfg(target_pointer_width = "32")]
46pub(crate) use linux_raw_sys::ioctl::{FS_IOC32_GETFLAGS, FS_IOC32_SETFLAGS};
47#[cfg(target_pointer_width = "64")]
48pub(crate) use linux_raw_sys::ioctl::{FS_IOC_GETFLAGS, FS_IOC_SETFLAGS};
49
50#[cfg(feature = "io_uring")]
51pub(crate) use linux_raw_sys::{general::open_how, io_uring::*};
52
53#[cfg(feature = "net")]
54pub(crate) use linux_raw_sys::{
55    cmsg_macros::*,
56    general::{O_CLOEXEC as SOCK_CLOEXEC, O_NONBLOCK as SOCK_NONBLOCK},
57    if_ether::*,
58    net::{
59        linger, msghdr, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_un, socklen_t, AF_DECnet,
60        __kernel_sa_family_t as sa_family_t, __kernel_sockaddr_storage as sockaddr_storage,
61        cmsghdr, in6_addr, in_addr, ip_mreq, ip_mreq_source, ip_mreqn, ipv6_mreq, AF_APPLETALK,
62        AF_ASH, AF_ATMPVC, AF_ATMSVC, AF_AX25, AF_BLUETOOTH, AF_BRIDGE, AF_CAN, AF_ECONET,
63        AF_IEEE802154, AF_INET, AF_INET6, AF_IPX, AF_IRDA, AF_ISDN, AF_IUCV, AF_KEY, AF_LLC,
64        AF_NETBEUI, AF_NETLINK, AF_NETROM, AF_PACKET, AF_PHONET, AF_PPPOX, AF_RDS, AF_ROSE,
65        AF_RXRPC, AF_SECURITY, AF_SNA, AF_TIPC, AF_UNIX, AF_UNSPEC, AF_WANPIPE, AF_X25, AF_XDP,
66        IP6T_SO_ORIGINAL_DST, IPPROTO_FRAGMENT, IPPROTO_ICMPV6, IPPROTO_MH, IPPROTO_ROUTING,
67        IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_FREEBIND, IPV6_MULTICAST_HOPS,
68        IPV6_MULTICAST_LOOP, IPV6_RECVTCLASS, IPV6_TCLASS, IPV6_UNICAST_HOPS, IPV6_V6ONLY,
69        IP_ADD_MEMBERSHIP, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
70        IP_FREEBIND, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_RECVTOS, IP_TOS, IP_TTL,
71        MSG_CMSG_CLOEXEC, MSG_CONFIRM, MSG_DONTROUTE, MSG_DONTWAIT, MSG_EOR, MSG_ERRQUEUE,
72        MSG_MORE, MSG_NOSIGNAL, MSG_OOB, MSG_PEEK, MSG_TRUNC, MSG_WAITALL, SCM_CREDENTIALS,
73        SCM_RIGHTS, SHUT_RD, SHUT_RDWR, SHUT_WR, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET,
74        SOCK_STREAM, SOL_SOCKET, SOL_XDP, SO_ACCEPTCONN, SO_BROADCAST, SO_COOKIE, SO_DOMAIN,
75        SO_ERROR, SO_INCOMING_CPU, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_ORIGINAL_DST,
76        SO_PASSCRED, SO_PROTOCOL, SO_RCVBUF, SO_RCVTIMEO_NEW, SO_RCVTIMEO_NEW as SO_RCVTIMEO,
77        SO_RCVTIMEO_OLD, SO_REUSEADDR, SO_REUSEPORT, SO_SNDBUF, SO_SNDTIMEO_NEW,
78        SO_SNDTIMEO_NEW as SO_SNDTIMEO, SO_SNDTIMEO_OLD, SO_TYPE, TCP_CONGESTION, TCP_CORK,
79        TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_NODELAY, TCP_QUICKACK,
80        TCP_THIN_LINEAR_TIMEOUTS, TCP_USER_TIMEOUT,
81    },
82    netlink::*,
83    xdp::{
84        sockaddr_xdp, xdp_desc, xdp_mmap_offsets, xdp_mmap_offsets_v1, xdp_options,
85        xdp_ring_offset, xdp_ring_offset_v1, xdp_statistics, xdp_statistics_v1, xdp_umem_reg,
86        xdp_umem_reg_v1, XDP_COPY, XDP_MMAP_OFFSETS, XDP_OPTIONS, XDP_OPTIONS_ZEROCOPY,
87        XDP_PGOFF_RX_RING, XDP_PGOFF_TX_RING, XDP_PKT_CONTD, XDP_RING_NEED_WAKEUP, XDP_RX_RING,
88        XDP_SHARED_UMEM, XDP_STATISTICS, XDP_TX_RING, XDP_UMEM_COMPLETION_RING, XDP_UMEM_FILL_RING,
89        XDP_UMEM_PGOFF_COMPLETION_RING, XDP_UMEM_PGOFF_FILL_RING, XDP_UMEM_REG,
90        XDP_UMEM_UNALIGNED_CHUNK_FLAG, XDP_USE_NEED_WAKEUP, XDP_USE_SG, XDP_ZEROCOPY,
91        XSK_UNALIGNED_BUF_ADDR_MASK, XSK_UNALIGNED_BUF_OFFSET_SHIFT,
92    },
93};
94
95// Cast away bindgen's `enum` type to make these consistent with the other
96// `setsockopt`/`getsockopt` level values.
97#[cfg(feature = "net")]
98pub(crate) const IPPROTO_IP: u32 = linux_raw_sys::net::IPPROTO_IP as _;
99#[cfg(feature = "net")]
100pub(crate) const IPPROTO_ICMP: u32 = linux_raw_sys::net::IPPROTO_ICMP as _;
101#[cfg(feature = "net")]
102pub(crate) const IPPROTO_IGMP: u32 = linux_raw_sys::net::IPPROTO_IGMP as _;
103#[cfg(feature = "net")]
104pub(crate) const IPPROTO_IPIP: u32 = linux_raw_sys::net::IPPROTO_IPIP as _;
105#[cfg(feature = "net")]
106pub(crate) const IPPROTO_TCP: u32 = linux_raw_sys::net::IPPROTO_TCP as _;
107#[cfg(feature = "net")]
108pub(crate) const IPPROTO_EGP: u32 = linux_raw_sys::net::IPPROTO_EGP as _;
109#[cfg(feature = "net")]
110pub(crate) const IPPROTO_PUP: u32 = linux_raw_sys::net::IPPROTO_PUP as _;
111#[cfg(feature = "net")]
112pub(crate) const IPPROTO_UDP: u32 = linux_raw_sys::net::IPPROTO_UDP as _;
113#[cfg(feature = "net")]
114pub(crate) const IPPROTO_IDP: u32 = linux_raw_sys::net::IPPROTO_IDP as _;
115#[cfg(feature = "net")]
116pub(crate) const IPPROTO_TP: u32 = linux_raw_sys::net::IPPROTO_TP as _;
117#[cfg(feature = "net")]
118pub(crate) const IPPROTO_DCCP: u32 = linux_raw_sys::net::IPPROTO_DCCP as _;
119#[cfg(feature = "net")]
120pub(crate) const IPPROTO_IPV6: u32 = linux_raw_sys::net::IPPROTO_IPV6 as _;
121#[cfg(feature = "net")]
122pub(crate) const IPPROTO_RSVP: u32 = linux_raw_sys::net::IPPROTO_RSVP as _;
123#[cfg(feature = "net")]
124pub(crate) const IPPROTO_GRE: u32 = linux_raw_sys::net::IPPROTO_GRE as _;
125#[cfg(feature = "net")]
126pub(crate) const IPPROTO_ESP: u32 = linux_raw_sys::net::IPPROTO_ESP as _;
127#[cfg(feature = "net")]
128pub(crate) const IPPROTO_AH: u32 = linux_raw_sys::net::IPPROTO_AH as _;
129#[cfg(feature = "net")]
130pub(crate) const IPPROTO_MTP: u32 = linux_raw_sys::net::IPPROTO_MTP as _;
131#[cfg(feature = "net")]
132pub(crate) const IPPROTO_BEETPH: u32 = linux_raw_sys::net::IPPROTO_BEETPH as _;
133#[cfg(feature = "net")]
134pub(crate) const IPPROTO_ENCAP: u32 = linux_raw_sys::net::IPPROTO_ENCAP as _;
135#[cfg(feature = "net")]
136pub(crate) const IPPROTO_PIM: u32 = linux_raw_sys::net::IPPROTO_PIM as _;
137#[cfg(feature = "net")]
138pub(crate) const IPPROTO_COMP: u32 = linux_raw_sys::net::IPPROTO_COMP as _;
139#[cfg(feature = "net")]
140pub(crate) const IPPROTO_SCTP: u32 = linux_raw_sys::net::IPPROTO_SCTP as _;
141#[cfg(feature = "net")]
142pub(crate) const IPPROTO_UDPLITE: u32 = linux_raw_sys::net::IPPROTO_UDPLITE as _;
143#[cfg(feature = "net")]
144pub(crate) const IPPROTO_MPLS: u32 = linux_raw_sys::net::IPPROTO_MPLS as _;
145#[cfg(feature = "net")]
146pub(crate) const IPPROTO_ETHERNET: u32 = linux_raw_sys::net::IPPROTO_ETHERNET as _;
147#[cfg(feature = "net")]
148pub(crate) const IPPROTO_RAW: u32 = linux_raw_sys::net::IPPROTO_RAW as _;
149#[cfg(feature = "net")]
150pub(crate) const IPPROTO_MPTCP: u32 = linux_raw_sys::net::IPPROTO_MPTCP as _;
151
152#[cfg(any(feature = "process", feature = "runtime"))]
153pub(crate) use linux_raw_sys::general::siginfo_t;
154
155#[cfg(any(feature = "process", feature = "runtime"))]
156pub(crate) const EXIT_SUCCESS: c_int = 0;
157#[cfg(any(feature = "process", feature = "runtime"))]
158pub(crate) const EXIT_FAILURE: c_int = 1;
159#[cfg(feature = "process")]
160pub(crate) const EXIT_SIGNALED_SIGABRT: c_int = 128 + linux_raw_sys::general::SIGABRT as c_int;
161#[cfg(feature = "runtime")]
162pub(crate) const CLONE_CHILD_SETTID: c_int = linux_raw_sys::general::CLONE_CHILD_SETTID as c_int;
163
164#[cfg(feature = "process")]
165pub(crate) use linux_raw_sys::{
166    general::{
167        CLD_CONTINUED, CLD_DUMPED, CLD_EXITED, CLD_KILLED, CLD_STOPPED, CLD_TRAPPED,
168        O_NONBLOCK as PIDFD_NONBLOCK, P_ALL, P_PGID, P_PID, P_PIDFD,
169    },
170    ioctl::TIOCSCTTY,
171};
172
173#[cfg(feature = "pty")]
174pub(crate) use linux_raw_sys::ioctl::TIOCGPTPEER;
175
176#[cfg(feature = "termios")]
177pub(crate) use linux_raw_sys::{
178    general::{
179        cc_t, speed_t, tcflag_t, termios, termios2, winsize, B0, B1000000, B110, B115200, B1152000,
180        B1200, B134, B150, B1500000, B1800, B19200, B200, B2000000, B230400, B2400, B2500000, B300,
181        B3000000, B3500000, B38400, B4000000, B460800, B4800, B50, B500000, B57600, B576000, B600,
182        B75, B921600, B9600, BOTHER, BRKINT, BS0, BS1, BSDLY, CBAUD, CBAUDEX, CIBAUD, CLOCAL,
183        CMSPAR, CR0, CR1, CR2, CR3, CRDLY, CREAD, CRTSCTS, CS5, CS6, CS7, CS8, CSIZE, CSTOPB, ECHO,
184        ECHOCTL, ECHOE, ECHOK, ECHOKE, ECHONL, ECHOPRT, EXTA, EXTB, EXTPROC, FF0, FF1, FFDLY,
185        FLUSHO, HUPCL, IBSHIFT, ICANON, ICRNL, IEXTEN, IGNBRK, IGNCR, IGNPAR, IMAXBEL, INLCR,
186        INPCK, ISIG, ISTRIP, IUCLC, IUTF8, IXANY, IXOFF, IXON, NCCS, NL0, NL1, NLDLY, NOFLSH,
187        OCRNL, OFDEL, OFILL, OLCUC, ONLCR, ONLRET, ONOCR, OPOST, PARENB, PARMRK, PARODD, PENDIN,
188        TAB0, TAB1, TAB2, TAB3, TABDLY, TCIFLUSH, TCIOFF, TCIOFLUSH, TCION, TCOFLUSH, TCOOFF,
189        TCOON, TCSADRAIN, TCSAFLUSH, TCSANOW, TOSTOP, VDISCARD, VEOF, VEOL, VEOL2, VERASE, VINTR,
190        VKILL, VLNEXT, VMIN, VQUIT, VREPRINT, VSTART, VSTOP, VSUSP, VSWTC, VT0, VT1, VTDLY, VTIME,
191        VWERASE, XCASE, XTABS,
192    },
193    ioctl::{
194        TCFLSH, TCGETS, TCGETS2, TCSBRK, TCSETS, TCSETS2, TCSETSF2, TCSETSW2, TCXONC, TIOCEXCL,
195        TIOCGPGRP, TIOCGSID, TIOCGWINSZ, TIOCNXCL, TIOCSPGRP, TIOCSWINSZ,
196    },
197};
198
199// Define our own `uid_t` and `gid_t` if the kernel's versions are not 32-bit.
200#[cfg(any(target_arch = "arm", target_arch = "sparc", target_arch = "x86"))]
201pub(crate) type uid_t = u32;
202#[cfg(any(target_arch = "arm", target_arch = "sparc", target_arch = "x86"))]
203pub(crate) type gid_t = u32;
204
205// Bindgen infers `u32` for many of these macro types which meant to be
206// used with `c_int` in the C APIs, so cast them to `c_int`.
207
208// Convert the signal constants from `u32` to `c_int`.
209pub(crate) const SIGHUP: c_int = linux_raw_sys::general::SIGHUP as _;
210pub(crate) const SIGINT: c_int = linux_raw_sys::general::SIGINT as _;
211pub(crate) const SIGQUIT: c_int = linux_raw_sys::general::SIGQUIT as _;
212pub(crate) const SIGILL: c_int = linux_raw_sys::general::SIGILL as _;
213pub(crate) const SIGTRAP: c_int = linux_raw_sys::general::SIGTRAP as _;
214pub(crate) const SIGABRT: c_int = linux_raw_sys::general::SIGABRT as _;
215pub(crate) const SIGBUS: c_int = linux_raw_sys::general::SIGBUS as _;
216pub(crate) const SIGFPE: c_int = linux_raw_sys::general::SIGFPE as _;
217pub(crate) const SIGKILL: c_int = linux_raw_sys::general::SIGKILL as _;
218pub(crate) const SIGUSR1: c_int = linux_raw_sys::general::SIGUSR1 as _;
219pub(crate) const SIGSEGV: c_int = linux_raw_sys::general::SIGSEGV as _;
220pub(crate) const SIGUSR2: c_int = linux_raw_sys::general::SIGUSR2 as _;
221pub(crate) const SIGPIPE: c_int = linux_raw_sys::general::SIGPIPE as _;
222pub(crate) const SIGALRM: c_int = linux_raw_sys::general::SIGALRM as _;
223pub(crate) const SIGTERM: c_int = linux_raw_sys::general::SIGTERM as _;
224#[cfg(not(any(
225    target_arch = "mips",
226    target_arch = "mips32r6",
227    target_arch = "mips64",
228    target_arch = "mips64r6",
229    target_arch = "sparc",
230    target_arch = "sparc64"
231)))]
232pub(crate) const SIGSTKFLT: c_int = linux_raw_sys::general::SIGSTKFLT as _;
233pub(crate) const SIGCHLD: c_int = linux_raw_sys::general::SIGCHLD as _;
234pub(crate) const SIGCONT: c_int = linux_raw_sys::general::SIGCONT as _;
235pub(crate) const SIGSTOP: c_int = linux_raw_sys::general::SIGSTOP as _;
236pub(crate) const SIGTSTP: c_int = linux_raw_sys::general::SIGTSTP as _;
237pub(crate) const SIGTTIN: c_int = linux_raw_sys::general::SIGTTIN as _;
238pub(crate) const SIGTTOU: c_int = linux_raw_sys::general::SIGTTOU as _;
239pub(crate) const SIGURG: c_int = linux_raw_sys::general::SIGURG as _;
240pub(crate) const SIGXCPU: c_int = linux_raw_sys::general::SIGXCPU as _;
241pub(crate) const SIGXFSZ: c_int = linux_raw_sys::general::SIGXFSZ as _;
242pub(crate) const SIGVTALRM: c_int = linux_raw_sys::general::SIGVTALRM as _;
243pub(crate) const SIGPROF: c_int = linux_raw_sys::general::SIGPROF as _;
244pub(crate) const SIGWINCH: c_int = linux_raw_sys::general::SIGWINCH as _;
245pub(crate) const SIGIO: c_int = linux_raw_sys::general::SIGIO as _;
246pub(crate) const SIGPWR: c_int = linux_raw_sys::general::SIGPWR as _;
247pub(crate) const SIGSYS: c_int = linux_raw_sys::general::SIGSYS as _;
248#[cfg(any(
249    target_arch = "mips",
250    target_arch = "mips32r6",
251    target_arch = "mips64",
252    target_arch = "mips64r6",
253    target_arch = "sparc",
254    target_arch = "sparc64"
255))]
256pub(crate) const SIGEMT: c_int = linux_raw_sys::general::SIGEMT as _;
257
258#[cfg(feature = "stdio")]
259pub(crate) const STDIN_FILENO: c_int = linux_raw_sys::general::STDIN_FILENO as _;
260#[cfg(feature = "stdio")]
261pub(crate) const STDOUT_FILENO: c_int = linux_raw_sys::general::STDOUT_FILENO as _;
262#[cfg(feature = "stdio")]
263pub(crate) const STDERR_FILENO: c_int = linux_raw_sys::general::STDERR_FILENO as _;
264
265pub(crate) const PIPE_BUF: usize = linux_raw_sys::general::PIPE_BUF as _;
266
267pub(crate) const CLOCK_MONOTONIC: c_int = linux_raw_sys::general::CLOCK_MONOTONIC as _;
268pub(crate) const CLOCK_REALTIME: c_int = linux_raw_sys::general::CLOCK_REALTIME as _;
269pub(crate) const CLOCK_MONOTONIC_RAW: c_int = linux_raw_sys::general::CLOCK_MONOTONIC_RAW as _;
270pub(crate) const CLOCK_MONOTONIC_COARSE: c_int =
271    linux_raw_sys::general::CLOCK_MONOTONIC_COARSE as _;
272pub(crate) const CLOCK_REALTIME_COARSE: c_int = linux_raw_sys::general::CLOCK_REALTIME_COARSE as _;
273pub(crate) const CLOCK_THREAD_CPUTIME_ID: c_int =
274    linux_raw_sys::general::CLOCK_THREAD_CPUTIME_ID as _;
275pub(crate) const CLOCK_PROCESS_CPUTIME_ID: c_int =
276    linux_raw_sys::general::CLOCK_PROCESS_CPUTIME_ID as _;
277#[cfg(any(feature = "thread", feature = "time", target_arch = "x86"))]
278pub(crate) const CLOCK_BOOTTIME: c_int = linux_raw_sys::general::CLOCK_BOOTTIME as _;
279#[cfg(any(feature = "thread", feature = "time", target_arch = "x86"))]
280pub(crate) const CLOCK_BOOTTIME_ALARM: c_int = linux_raw_sys::general::CLOCK_BOOTTIME_ALARM as _;
281#[cfg(any(feature = "thread", feature = "time", target_arch = "x86"))]
282pub(crate) const CLOCK_TAI: c_int = linux_raw_sys::general::CLOCK_TAI as _;
283#[cfg(any(feature = "thread", feature = "time", target_arch = "x86"))]
284pub(crate) const CLOCK_REALTIME_ALARM: c_int = linux_raw_sys::general::CLOCK_REALTIME_ALARM as _;
285
286#[cfg(feature = "system")]
287mod reboot_symbols {
288    use super::c_int;
289
290    pub(crate) const LINUX_REBOOT_MAGIC1: c_int = linux_raw_sys::general::LINUX_REBOOT_MAGIC1 as _;
291    pub(crate) const LINUX_REBOOT_MAGIC2: c_int = linux_raw_sys::general::LINUX_REBOOT_MAGIC2 as _;
292
293    pub(crate) const LINUX_REBOOT_CMD_RESTART: c_int =
294        linux_raw_sys::general::LINUX_REBOOT_CMD_RESTART as _;
295    pub(crate) const LINUX_REBOOT_CMD_HALT: c_int =
296        linux_raw_sys::general::LINUX_REBOOT_CMD_HALT as _;
297    pub(crate) const LINUX_REBOOT_CMD_CAD_ON: c_int =
298        linux_raw_sys::general::LINUX_REBOOT_CMD_CAD_ON as _;
299    pub(crate) const LINUX_REBOOT_CMD_CAD_OFF: c_int =
300        linux_raw_sys::general::LINUX_REBOOT_CMD_CAD_OFF as _;
301    pub(crate) const LINUX_REBOOT_CMD_POWER_OFF: c_int =
302        linux_raw_sys::general::LINUX_REBOOT_CMD_POWER_OFF as _;
303    pub(crate) const LINUX_REBOOT_CMD_SW_SUSPEND: c_int =
304        linux_raw_sys::general::LINUX_REBOOT_CMD_SW_SUSPEND as _;
305    pub(crate) const LINUX_REBOOT_CMD_KEXEC: c_int =
306        linux_raw_sys::general::LINUX_REBOOT_CMD_KEXEC as _;
307}
308#[cfg(feature = "system")]
309pub(crate) use reboot_symbols::*;
310
311// TODO: This is new in Linux 6.11; remove when linux-raw-sys is updated.
312pub(crate) const MAP_DROPPABLE: u32 = 0x8;
313
314// TODO: This is new in Linux 6.5; remove when linux-raw-sys is updated.
315pub(crate) const MOVE_MOUNT_BENEATH: u32 = 0x200;