Debian Codegraph @

third_party/rust/rustix/src/backend/libc/c.rs

raw
1 //! Libc and supplemental types and constants.
2
3 #![allow(unused_imports)]
4
5 // Import everything from libc, but we'll add some stuff and override some
6 // things below.
7 pub(crate) use libc::*;
8
9 /// `PROC_SUPER_MAGIC`—The magic number for the procfs filesystem.
10 #[cfg(all(linux_kernel, target_env = "musl"))]
11 pub(crate) const PROC_SUPER_MAGIC: u32 = 0x0000_9fa0;
12
13 /// `NFS_SUPER_MAGIC`—The magic number for the NFS filesystem.
14 #[cfg(all(linux_kernel, target_env = "musl"))]
15 pub(crate) const NFS_SUPER_MAGIC: u32 = 0x0000_6969;
16
17 #[cfg(feature = "process")]
18 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
19 pub(crate) const EXIT_SIGNALED_SIGABRT: c_int = 128 + SIGABRT as c_int;
20
21 // TODO: Upstream these.
22 #[cfg(all(linux_kernel, feature = "net"))]
23 pub(crate) const ETH_P_TSN: c_int = linux_raw_sys::if_ether::ETH_P_TSN as _;
24 #[cfg(all(linux_kernel, feature = "net"))]
25 pub(crate) const ETH_P_ERSPAN2: c_int = linux_raw_sys::if_ether::ETH_P_ERSPAN2 as _;
26 #[cfg(all(linux_kernel, feature = "net"))]
27 pub(crate) const ETH_P_ERSPAN: c_int = linux_raw_sys::if_ether::ETH_P_ERSPAN as _;
28 #[cfg(all(linux_kernel, feature = "net"))]
29 pub(crate) const ETH_P_PROFINET: c_int = linux_raw_sys::if_ether::ETH_P_PROFINET as _;
30 #[cfg(all(linux_kernel, feature = "net"))]
31 pub(crate) const ETH_P_REALTEK: c_int = linux_raw_sys::if_ether::ETH_P_REALTEK as _;
32 #[cfg(all(linux_kernel, feature = "net"))]
33 pub(crate) const ETH_P_ETHERCAT: c_int = linux_raw_sys::if_ether::ETH_P_ETHERCAT as _;
34 #[cfg(all(linux_kernel, feature = "net"))]
35 pub(crate) const ETH_P_PREAUTH: c_int = linux_raw_sys::if_ether::ETH_P_PREAUTH as _;
36 #[cfg(all(linux_kernel, feature = "net"))]
37 pub(crate) const ETH_P_LLDP: c_int = linux_raw_sys::if_ether::ETH_P_LLDP as _;
38 #[cfg(all(linux_kernel, feature = "net"))]
39 pub(crate) const ETH_P_MRP: c_int = linux_raw_sys::if_ether::ETH_P_MRP as _;
40 #[cfg(all(linux_kernel, feature = "net"))]
41 pub(crate) const ETH_P_NCSI: c_int = linux_raw_sys::if_ether::ETH_P_NCSI as _;
42 #[cfg(all(linux_kernel, feature = "net"))]
43 pub(crate) const ETH_P_CFM: c_int = linux_raw_sys::if_ether::ETH_P_CFM as _;
44 #[cfg(all(linux_kernel, feature = "net"))]
45 pub(crate) const ETH_P_IBOE: c_int = linux_raw_sys::if_ether::ETH_P_IBOE as _;
46 #[cfg(all(linux_kernel, feature = "net"))]
47 pub(crate) const ETH_P_HSR: c_int = linux_raw_sys::if_ether::ETH_P_HSR as _;
48 #[cfg(all(linux_kernel, feature = "net"))]
49 pub(crate) const ETH_P_NSH: c_int = linux_raw_sys::if_ether::ETH_P_NSH as _;
50 #[cfg(all(linux_kernel, feature = "net"))]
51 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"))]
53 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"))]
55 pub(crate) const ETH_P_IFE: c_int = linux_raw_sys::if_ether::ETH_P_IFE as _;
56 #[cfg(all(linux_kernel, feature = "net"))]
57 pub(crate) const ETH_P_CAN: c_int = linux_raw_sys::if_ether::ETH_P_CAN as _;
58 #[cfg(all(linux_kernel, feature = "net"))]
59 pub(crate) const ETH_P_CANXL: c_int = linux_raw_sys::if_ether::ETH_P_CANXL as _;
60 #[cfg(all(linux_kernel, feature = "net"))]
61 pub(crate) const ETH_P_XDSA: c_int = linux_raw_sys::if_ether::ETH_P_XDSA as _;
62 #[cfg(all(linux_kernel, feature = "net"))]
63 pub(crate) const ETH_P_MAP: c_int = linux_raw_sys::if_ether::ETH_P_MAP as _;
64 #[cfg(all(linux_kernel, feature = "net"))]
65 pub(crate) const ETH_P_MCTP: c_int = linux_raw_sys::if_ether::ETH_P_MCTP as _;
66
67 #[cfg(all(
68 linux_kernel,
69 any(
70 target_arch = "mips",
71 target_arch = "mips32r6",
72 target_arch = "mips64",
73 target_arch = "mips64r6",
74 target_arch = "sparc",
75 target_arch = "sparc64"
76 )
77 ))]
78 pub(crate) const SIGEMT: c_int = linux_raw_sys::general::SIGEMT as _;
79
80 // TODO: Upstream these.
81 #[cfg(all(linux_kernel, feature = "termios"))]
82 pub(crate) const IUCLC: tcflag_t = linux_raw_sys::general::IUCLC as _;
83 #[cfg(all(linux_kernel, feature = "termios"))]
84 pub(crate) const XCASE: tcflag_t = linux_raw_sys::general::XCASE as _;
85
86 #[cfg(target_os = "aix")]
87 pub(crate) const MSG_DONTWAIT: c_int = libc::MSG_NONBLOCK;
88
89 // `O_LARGEFILE` can be automatically set by the kernel on Linux:
90 // <https://github.com/torvalds/linux/blob/v6.7/fs/open.c#L1458-L1459>
91 // so libc implementations may leave it undefined or defined to zero.
92 #[cfg(linux_kernel)]
93 pub(crate) const O_LARGEFILE: c_int = linux_raw_sys::general::O_LARGEFILE as _;
94
95 // Gated under `_LARGEFILE_SOURCE` but automatically set by the kernel.
96 // <https://github.com/illumos/illumos-gate/blob/fb2cb638e5604b214d8ea8d4f01ad2e77b437c17/usr/src/ucbhead/sys/fcntl.h#L64>
97 #[cfg(target_os = "illumos")]
98 pub(crate) const O_LARGEFILE: c_int = 0x2000;
99
100 // TODO: This is new in Linux 6.11; remove when linux-raw-sys is updated.
101 #[cfg(linux_kernel)]
102 pub(crate) const MAP_DROPPABLE: u32 = 0x8;
103
104 // On PowerPC, the regular `termios` has the `termios2` fields and there is no
105 // `termios2`, so we define aliases.
106 #[cfg(all(
107 linux_kernel,
108 feature = "termios",
109 any(target_arch = "powerpc", target_arch = "powerpc64")
110 ))]
111 pub(crate) use libc::{
112 termios as termios2, TCGETS as TCGETS2, TCSETS as TCSETS2, TCSETSF as TCSETSF2,
113 TCSETSW as TCSETSW2,
114 };
115
116 // And PowerPC doesn't define `CIBAUD`, but it does define `IBSHIFT`, so we can
117 // compute `CIBAUD` ourselves.
118 #[cfg(all(
119 linux_kernel,
120 feature = "termios",
121 any(target_arch = "powerpc", target_arch = "powerpc64")
122 ))]
123 pub(crate) const CIBAUD: u32 = libc::CBAUD << libc::IBSHIFT;
124
125 // Automatically enable “large file” support (LFS) features.
126
127 #[cfg(target_os = "vxworks")]
128 pub(super) use libc::_Vx_ticks64_t as _Vx_ticks_t;
129 #[cfg(linux_kernel)]
130 pub(super) use libc::fallocate64 as fallocate;
131 #[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
132 #[cfg(any(linux_like, target_os = "aix"))]
133 pub(super) use libc::open64 as open;
134 #[cfg(any(
135 linux_kernel,
136 target_os = "aix",
137 target_os = "hurd",
138 target_os = "l4re"
139 ))]
140 pub(super) use libc::posix_fallocate64 as posix_fallocate;
141 #[cfg(any(all(linux_like, not(target_os = "android")), target_os = "aix"))]
142 pub(super) use libc::{blkcnt64_t as blkcnt_t, rlim64_t as rlim_t};
143 // TODO: AIX has `stat64x`, `fstat64x`, `lstat64x`, and `stat64xat`; add them
144 // to the upstream libc crate and implement rustix's `statat` etc. with them.
145 #[cfg(target_os = "aix")]
146 pub(super) use libc::{
147 blksize64_t as blksize_t, fstat64 as fstat, fstatfs64 as fstatfs, fstatvfs64 as fstatvfs,
148 ftruncate64 as ftruncate, getrlimit64 as getrlimit, ino_t, lseek64 as lseek, mmap,
149 off64_t as off_t, openat, posix_fadvise64 as posix_fadvise, preadv, pwritev,
150 rlimit64 as rlimit, setrlimit64 as setrlimit, stat64at as fstatat, statfs64 as statfs,
151 statvfs64 as statvfs, RLIM_INFINITY,
152 };
153 #[cfg(any(linux_like, target_os = "hurd"))]
154 pub(super) use libc::{
155 fstat64 as fstat, fstatat64 as fstatat, fstatfs64 as fstatfs, fstatvfs64 as fstatvfs,
156 ftruncate64 as ftruncate, getrlimit64 as getrlimit, ino64_t as ino_t, lseek64 as lseek,
157 mmap64 as mmap, off64_t as off_t, openat64 as openat, posix_fadvise64 as posix_fadvise,
158 rlimit64 as rlimit, setrlimit64 as setrlimit, statfs64 as statfs, statvfs64 as statvfs,
159 RLIM64_INFINITY as RLIM_INFINITY,
160 };
161 #[cfg(apple)]
162 pub(super) use libc::{
163 host_info64_t as host_info_t, host_statistics64 as host_statistics,
164 vm_statistics64_t as vm_statistics_t,
165 };
166 #[cfg(not(all(
167 linux_kernel,
168 any(
169 target_pointer_width = "32",
170 target_arch = "mips64",
171 target_arch = "mips64r6"
172 )
173 )))]
174 #[cfg(any(linux_like, target_os = "aix", target_os = "hurd"))]
175 pub(super) use libc::{lstat64 as lstat, stat64 as stat};
176 #[cfg(any(
177 linux_kernel,
178 target_os = "aix",
179 target_os = "hurd",
180 target_os = "emscripten"
181 ))]
182 pub(super) use libc::{pread64 as pread, pwrite64 as pwrite};
183 #[cfg(any(target_os = "linux", target_os = "hurd", target_os = "emscripten"))]
184 pub(super) use libc::{preadv64 as preadv, pwritev64 as pwritev};
185
186 #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]
187 pub(super) unsafe fn prlimit(
188 pid: libc::pid_t,
189 resource: libc::__rlimit_resource_t,
190 new_limit: *const libc::rlimit64,
191 old_limit: *mut libc::rlimit64,
192 ) -> libc::c_int {
193 // `prlimit64` wasn't supported in glibc until 2.13.
194 weak_or_syscall! {
195 fn prlimit64(
196 pid: libc::pid_t,
197 resource: libc::__rlimit_resource_t,
198 new_limit: *const libc::rlimit64,
199 old_limit: *mut libc::rlimit64
200 ) via SYS_prlimit64 -> libc::c_int
201 }
202
203 prlimit64(pid, resource, new_limit, old_limit)
204 }
205
206 #[cfg(all(target_os = "linux", target_env = "musl"))]
207 pub(super) unsafe fn prlimit(
208 pid: libc::pid_t,
209 resource: libc::c_int,
210 new_limit: *const libc::rlimit64,
211 old_limit: *mut libc::rlimit64,
212 ) -> libc::c_int {
213 weak_or_syscall! {
214 fn prlimit64(
215 pid: libc::pid_t,
216 resource: libc::c_int,
217 new_limit: *const libc::rlimit64,
218 old_limit: *mut libc::rlimit64
219 ) via SYS_prlimit64 -> libc::c_int
220 }
221
222 prlimit64(pid, resource, new_limit, old_limit)
223 }
224
225 #[cfg(target_os = "android")]
226 pub(super) unsafe fn prlimit(
227 pid: libc::pid_t,
228 resource: libc::c_int,
229 new_limit: *const libc::rlimit64,
230 old_limit: *mut libc::rlimit64,
231 ) -> libc::c_int {
232 weak_or_syscall! {
233 fn prlimit64(
234 pid: libc::pid_t,
235 resource: libc::c_int,
236 new_limit: *const libc::rlimit64,
237 old_limit: *mut libc::rlimit64
238 ) via SYS_prlimit64 -> libc::c_int
239 }
240
241 prlimit64(pid, resource, new_limit, old_limit)
242 }
243
244 #[cfg(target_os = "android")]
245 mod readwrite_pv64 {
246 use super::*;
247
248 pub(in super::super) unsafe fn preadv64(
249 fd: libc::c_int,
250 iov: *const libc::iovec,
251 iovcnt: libc::c_int,
252 offset: libc::off64_t,
253 ) -> libc::ssize_t {
254 // Older Android libc lacks `preadv64`, so use the `weak!` mechanism to
255 // test for it, and call back to `libc::syscall`. We don't use
256 // `weak_or_syscall` here because we need to pass the 64-bit offset
257 // specially.
258 weak! {
259 fn preadv64(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t) -> libc::ssize_t
260 }
261 if let Some(fun) = preadv64.get() {
262 fun(fd, iov, iovcnt, offset)
263 } else {
264 // Unlike the plain "p" functions, the "pv" functions pass their
265 // offset in an endian-independent way, and always in two registers.
266 syscall! {
267 fn preadv(
268 fd: libc::c_int,
269 iov: *const libc::iovec,
270 iovcnt: libc::c_int,
271 offset_lo: usize,
272 offset_hi: usize
273 ) via SYS_preadv -> libc::ssize_t
274 }
275 preadv(fd, iov, iovcnt, offset as usize, (offset >> 32) as usize)
276 }
277 }
278 pub(in super::super) unsafe fn pwritev64(
279 fd: libc::c_int,
280 iov: *const libc::iovec,
281 iovcnt: libc::c_int,
282 offset: libc::off64_t,
283 ) -> libc::ssize_t {
284 // See the comments in `preadv64`.
285 weak! {
286 fn pwritev64(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t) -> libc::ssize_t
287 }
288 if let Some(fun) = pwritev64.get() {
289 fun(fd, iov, iovcnt, offset)
290 } else {
291 // Unlike the plain "p" functions, the "pv" functions pass their
292 // offset in an endian-independent way, and always in two registers.
293 syscall! {
294 fn pwritev(
295 fd: libc::c_int,
296 iov: *const libc::iovec,
297 iovcnt: libc::c_int,
298 offset_lo: usize,
299 offset_hi: usize
300 ) via SYS_pwritev -> libc::ssize_t
301 }
302 pwritev(fd, iov, iovcnt, offset as usize, (offset >> 32) as usize)
303 }
304 }
305 }
306 #[cfg(target_os = "android")]
307 pub(super) use readwrite_pv64::{preadv64 as preadv, pwritev64 as pwritev};
308
309 // macOS added `preadv` and `pwritev` in version 11.0.
310 #[cfg(apple)]
311 mod readwrite_pv {
312 weakcall! {
313 pub(in super::super) fn preadv(
314 fd: libc::c_int,
315 iov: *const libc::iovec,
316 iovcnt: libc::c_int,
317 offset: libc::off_t
318 ) -> libc::ssize_t
319 }
320 weakcall! {
321 pub(in super::super) fn pwritev(
322 fd: libc::c_int,
323 iov: *const libc::iovec,
324 iovcnt: libc::c_int, offset: libc::off_t
325 ) -> libc::ssize_t
326 }
327 }
328 #[cfg(apple)]
329 pub(super) use readwrite_pv::{preadv, pwritev};
330
331 // glibc added `preadv64v2` and `pwritev64v2` in version 2.26.
332 #[cfg(all(target_os = "linux", target_env = "gnu"))]
333 mod readwrite_pv64v2 {
334 use super::*;
335
336 pub(in super::super) unsafe fn preadv64v2(
337 fd: libc::c_int,
338 iov: *const libc::iovec,
339 iovcnt: libc::c_int,
340 offset: libc::off64_t,
341 flags: libc::c_int,
342 ) -> libc::ssize_t {
343 // Older glibc lacks `preadv64v2`, so use the `weak!` mechanism to
344 // test for it, and call back to `libc::syscall`. We don't use
345 // `weak_or_syscall` here because we need to pass the 64-bit offset
346 // specially.
347 weak! {
348 fn preadv64v2(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t, libc::c_int) -> libc::ssize_t
349 }
350 if let Some(fun) = preadv64v2.get() {
351 fun(fd, iov, iovcnt, offset, flags)
352 } else {
353 // Unlike the plain "p" functions, the "pv" functions pass their
354 // offset in an endian-independent way, and always in two registers.
355 syscall! {
356 fn preadv2(
357 fd: libc::c_int,
358 iov: *const libc::iovec,
359 iovcnt: libc::c_int,
360 offset_lo: usize,
361 offset_hi: usize,
362 flags: libc::c_int
363 ) via SYS_preadv2 -> libc::ssize_t
364 }
365 preadv2(
366 fd,
367 iov,
368 iovcnt,
369 offset as usize,
370 (offset >> 32) as usize,
371 flags,
372 )
373 }
374 }
375 pub(in super::super) unsafe fn pwritev64v2(
376 fd: libc::c_int,
377 iov: *const libc::iovec,
378 iovcnt: libc::c_int,
379 offset: libc::off64_t,
380 flags: libc::c_int,
381 ) -> libc::ssize_t {
382 // See the comments in `preadv64v2`.
383 weak! {
384 fn pwritev64v2(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t, libc::c_int) -> libc::ssize_t
385 }
386 if let Some(fun) = pwritev64v2.get() {
387 fun(fd, iov, iovcnt, offset, flags)
388 } else {
389 // Unlike the plain "p" functions, the "pv" functions pass their
390 // offset in an endian-independent way, and always in two registers.
391 syscall! {
392 fn pwritev2(
393 fd: libc::c_int,
394 iov: *const libc::iovec,
395 iovec: libc::c_int,
396 offset_lo: usize,
397 offset_hi: usize,
398 flags: libc::c_int
399 ) via SYS_pwritev2 -> libc::ssize_t
400 }
401 pwritev2(
402 fd,
403 iov,
404 iovcnt,
405 offset as usize,
406 (offset >> 32) as usize,
407 flags,
408 )
409 }
410 }
411 }
412 #[cfg(all(target_os = "linux", target_env = "gnu"))]
413 pub(super) use readwrite_pv64v2::{preadv64v2 as preadv2, pwritev64v2 as pwritev2};
414
415 // On non-glibc, assume we don't have `pwritev2`/`preadv2` in libc and use
416 // `c::syscall` instead.
417 #[cfg(any(
418 target_os = "android",
419 all(target_os = "linux", not(target_env = "gnu")),
420 ))]
421 mod readwrite_pv64v2 {
422 use super::*;
423
424 pub(in super::super) unsafe fn preadv64v2(
425 fd: libc::c_int,
426 iov: *const libc::iovec,
427 iovcnt: libc::c_int,
428 offset: libc::off64_t,
429 flags: libc::c_int,
430 ) -> libc::ssize_t {
431 // Unlike the plain "p" functions, the "pv" functions pass their offset
432 // in an endian-independent way, and always in two registers.
433 syscall! {
434 fn preadv2(
435 fd: libc::c_int,
436 iov: *const libc::iovec,
437 iovcnt: libc::c_int,
438 offset_lo: usize,
439 offset_hi: usize,
440 flags: libc::c_int
441 ) via SYS_preadv2 -> libc::ssize_t
442 }
443 preadv2(
444 fd,
445 iov,
446 iovcnt,
447 offset as usize,
448 (offset >> 32) as usize,
449 flags,
450 )
451 }
452 pub(in super::super) unsafe fn pwritev64v2(
453 fd: libc::c_int,
454 iov: *const libc::iovec,
455 iovcnt: libc::c_int,
456 offset: libc::off64_t,
457 flags: libc::c_int,
458 ) -> libc::ssize_t {
459 // Unlike the plain "p" functions, the "pv" functions pass their offset
460 // in an endian-independent way, and always in two registers.
461 syscall! {
462 fn pwritev2(
463 fd: libc::c_int,
464 iov: *const libc::iovec,
465 iovcnt: libc::c_int,
466 offset_lo: usize,
467 offset_hi: usize,
468 flags: libc::c_int
469 ) via SYS_pwritev2 -> libc::ssize_t
470 }
471 pwritev2(
472 fd,
473 iov,
474 iovcnt,
475 offset as usize,
476 (offset >> 32) as usize,
477 flags,
478 )
479 }
480 }
481 #[cfg(any(
482 target_os = "android",
483 all(target_os = "linux", not(target_env = "gnu")),
484 ))]
485 pub(super) use readwrite_pv64v2::{preadv64v2 as preadv2, pwritev64v2 as pwritev2};