Debian Codegraph @

.cargo/vendor/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 // TODO: Remove once https://github.com/rust-lang/libc/pull/3377 is merged and released.
90 #[cfg(target_os = "netbsd")]
91 #[cfg(feature = "net")]
92 pub(crate) const SO_NOSIGPIPE: c_int = 0x0800;
93
94 // It is defined as 0 in libc under 64-bit platforms, but is automatically set by kernel.
95 // https://github.com/torvalds/linux/blob/v6.7/fs/open.c#L1458-L1459
96 #[cfg(linux_kernel)]
97 pub(crate) const O_LARGEFILE: c_int = linux_raw_sys::general::O_LARGEFILE as _;
98
99 // Gated under `_LARGEFILE_SOURCE` but automatically set by the kernel.
100 // https://github.com/illumos/illumos-gate/blob/fb2cb638e5604b214d8ea8d4f01ad2e77b437c17/usr/src/ucbhead/sys/fcntl.h#L64
101 #[cfg(target_os = "illumos")]
102 pub(crate) const O_LARGEFILE: c_int = 0x2000;
103
104 // On PowerPC, the regular `termios` has the `termios2` fields and there is no
105 // `termios2`. linux-raw-sys has aliases `termios2` to `termios` to cover this
106 // difference, but we still need to manually import it since `libc` doesn't
107 // have this.
108 #[cfg(all(
109 linux_kernel,
110 feature = "termios",
111 any(target_arch = "powerpc", target_arch = "powerpc64")
112 ))]
113 pub(crate) use {
114 linux_raw_sys::general::{termios2, CIBAUD},
115 linux_raw_sys::ioctl::{TCGETS2, TCSETS2, TCSETSF2, TCSETSW2},
116 };
117
118 // Automatically enable “large file” support (LFS) features.
119
120 #[cfg(target_os = "vxworks")]
121 pub(super) use libc::_Vx_ticks64_t as _Vx_ticks_t;
122 #[cfg(linux_kernel)]
123 pub(super) use libc::fallocate64 as fallocate;
124 #[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
125 #[cfg(any(linux_like, target_os = "aix"))]
126 pub(super) use libc::open64 as open;
127 #[cfg(any(
128 linux_kernel,
129 target_os = "aix",
130 target_os = "hurd",
131 target_os = "l4re"
132 ))]
133 pub(super) use libc::posix_fallocate64 as posix_fallocate;
134 #[cfg(any(all(linux_like, not(target_os = "android")), target_os = "aix"))]
135 pub(super) use libc::{blkcnt64_t as blkcnt_t, rlim64_t as rlim_t};
136 // TODO: AIX has `stat64x`, `fstat64x`, `lstat64x`, and `stat64xat`; add them
137 // to the upstream libc crate and implement rustix's `statat` etc. with them.
138 #[cfg(target_os = "aix")]
139 pub(super) use libc::{
140 blksize64_t as blksize_t, fstat64 as fstat, fstatfs64 as fstatfs, fstatvfs64 as fstatvfs,
141 ftruncate64 as ftruncate, getrlimit64 as getrlimit, ino_t, lseek64 as lseek, mmap,
142 off64_t as off_t, openat, posix_fadvise64 as posix_fadvise, preadv, pwritev,
143 rlimit64 as rlimit, setrlimit64 as setrlimit, stat64at as fstatat, statfs64 as statfs,
144 statvfs64 as statvfs, RLIM_INFINITY,
145 };
146 #[cfg(any(linux_like, target_os = "hurd"))]
147 pub(super) use libc::{
148 fstat64 as fstat, fstatat64 as fstatat, fstatfs64 as fstatfs, fstatvfs64 as fstatvfs,
149 ftruncate64 as ftruncate, getrlimit64 as getrlimit, ino64_t as ino_t, lseek64 as lseek,
150 mmap64 as mmap, off64_t as off_t, openat64 as openat, posix_fadvise64 as posix_fadvise,
151 rlimit64 as rlimit, setrlimit64 as setrlimit, statfs64 as statfs, statvfs64 as statvfs,
152 RLIM64_INFINITY as RLIM_INFINITY,
153 };
154 #[cfg(apple)]
155 pub(super) use libc::{
156 host_info64_t as host_info_t, host_statistics64 as host_statistics,
157 vm_statistics64_t as vm_statistics_t,
158 };
159 #[cfg(not(all(
160 linux_kernel,
161 any(
162 target_pointer_width = "32",
163 target_arch = "mips64",
164 target_arch = "mips64r6"
165 )
166 )))]
167 #[cfg(any(linux_like, target_os = "aix", target_os = "hurd"))]
168 pub(super) use libc::{lstat64 as lstat, stat64 as stat};
169 #[cfg(any(
170 linux_kernel,
171 target_os = "aix",
172 target_os = "hurd",
173 target_os = "emscripten"
174 ))]
175 pub(super) use libc::{pread64 as pread, pwrite64 as pwrite};
176 #[cfg(any(target_os = "linux", target_os = "hurd", target_os = "emscripten"))]
177 pub(super) use libc::{preadv64 as preadv, pwritev64 as pwritev};
178
179 #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]
180 pub(super) unsafe fn prlimit(
181 pid: libc::pid_t,
182 resource: libc::__rlimit_resource_t,
183 new_limit: *const libc::rlimit64,
184 old_limit: *mut libc::rlimit64,
185 ) -> libc::c_int {
186 // `prlimit64` wasn't supported in glibc until 2.13.
187 weak_or_syscall! {
188 fn prlimit64(
189 pid: libc::pid_t,
190 resource: libc::__rlimit_resource_t,
191 new_limit: *const libc::rlimit64,
192 old_limit: *mut libc::rlimit64
193 ) via SYS_prlimit64 -> libc::c_int
194 }
195
196 prlimit64(pid, resource, new_limit, old_limit)
197 }
198
199 #[cfg(all(target_os = "linux", target_env = "musl"))]
200 pub(super) unsafe fn prlimit(
201 pid: libc::pid_t,
202 resource: libc::c_int,
203 new_limit: *const libc::rlimit64,
204 old_limit: *mut libc::rlimit64,
205 ) -> libc::c_int {
206 weak_or_syscall! {
207 fn prlimit64(
208 pid: libc::pid_t,
209 resource: libc::c_int,
210 new_limit: *const libc::rlimit64,
211 old_limit: *mut libc::rlimit64
212 ) via SYS_prlimit64 -> libc::c_int
213 }
214
215 prlimit64(pid, resource, new_limit, old_limit)
216 }
217
218 #[cfg(target_os = "android")]
219 pub(super) unsafe fn prlimit(
220 pid: libc::pid_t,
221 resource: libc::c_int,
222 new_limit: *const libc::rlimit64,
223 old_limit: *mut libc::rlimit64,
224 ) -> libc::c_int {
225 weak_or_syscall! {
226 fn prlimit64(
227 pid: libc::pid_t,
228 resource: libc::c_int,
229 new_limit: *const libc::rlimit64,
230 old_limit: *mut libc::rlimit64
231 ) via SYS_prlimit64 -> libc::c_int
232 }
233
234 prlimit64(pid, resource, new_limit, old_limit)
235 }
236
237 #[cfg(target_os = "android")]
238 mod readwrite_pv64 {
239 use super::*;
240
241 pub(in super::super) unsafe fn preadv64(
242 fd: libc::c_int,
243 iov: *const libc::iovec,
244 iovcnt: libc::c_int,
245 offset: libc::off64_t,
246 ) -> libc::ssize_t {
247 // Older Android libc lacks `preadv64`, so use the `weak!` mechanism to
248 // test for it, and call back to `libc::syscall`. We don't use
249 // `weak_or_syscall` here because we need to pass the 64-bit offset
250 // specially.
251 weak! {
252 fn preadv64(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t) -> libc::ssize_t
253 }
254 if let Some(fun) = preadv64.get() {
255 fun(fd, iov, iovcnt, offset)
256 } else {
257 // Unlike the plain "p" functions, the "pv" functions pass their
258 // offset in an endian-independent way, and always in two registers.
259 syscall! {
260 fn preadv(
261 fd: libc::c_int,
262 iov: *const libc::iovec,
263 iovcnt: libc::c_int,
264 offset_lo: usize,
265 offset_hi: usize
266 ) via SYS_preadv -> libc::ssize_t
267 }
268 preadv(fd, iov, iovcnt, offset as usize, (offset >> 32) as usize)
269 }
270 }
271 pub(in super::super) unsafe fn pwritev64(
272 fd: libc::c_int,
273 iov: *const libc::iovec,
274 iovcnt: libc::c_int,
275 offset: libc::off64_t,
276 ) -> libc::ssize_t {
277 // See the comments in `preadv64`.
278 weak! {
279 fn pwritev64(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t) -> libc::ssize_t
280 }
281 if let Some(fun) = pwritev64.get() {
282 fun(fd, iov, iovcnt, offset)
283 } else {
284 // Unlike the plain "p" functions, the "pv" functions pass their
285 // offset in an endian-independent way, and always in two registers.
286 syscall! {
287 fn pwritev(
288 fd: libc::c_int,
289 iov: *const libc::iovec,
290 iovcnt: libc::c_int,
291 offset_lo: usize,
292 offset_hi: usize
293 ) via SYS_pwritev -> libc::ssize_t
294 }
295 pwritev(fd, iov, iovcnt, offset as usize, (offset >> 32) as usize)
296 }
297 }
298 }
299 #[cfg(target_os = "android")]
300 pub(super) use readwrite_pv64::{preadv64 as preadv, pwritev64 as pwritev};
301
302 // macOS added `preadv` and `pwritev` in version 11.0.
303 #[cfg(apple)]
304 mod readwrite_pv {
305 weakcall! {
306 pub(in super::super) fn preadv(
307 fd: libc::c_int,
308 iov: *const libc::iovec,
309 iovcnt: libc::c_int,
310 offset: libc::off_t
311 ) -> libc::ssize_t
312 }
313 weakcall! {
314 pub(in super::super) fn pwritev(
315 fd: libc::c_int,
316 iov: *const libc::iovec,
317 iovcnt: libc::c_int, offset: libc::off_t
318 ) -> libc::ssize_t
319 }
320 }
321 #[cfg(apple)]
322 pub(super) use readwrite_pv::{preadv, pwritev};
323
324 // glibc added `preadv64v2` and `pwritev64v2` in version 2.26.
325 #[cfg(all(target_os = "linux", target_env = "gnu"))]
326 mod readwrite_pv64v2 {
327 use super::*;
328
329 pub(in super::super) unsafe fn preadv64v2(
330 fd: libc::c_int,
331 iov: *const libc::iovec,
332 iovcnt: libc::c_int,
333 offset: libc::off64_t,
334 flags: libc::c_int,
335 ) -> libc::ssize_t {
336 // Older glibc lacks `preadv64v2`, so use the `weak!` mechanism to
337 // test for it, and call back to `libc::syscall`. We don't use
338 // `weak_or_syscall` here because we need to pass the 64-bit offset
339 // specially.
340 weak! {
341 fn preadv64v2(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t, libc::c_int) -> libc::ssize_t
342 }
343 if let Some(fun) = preadv64v2.get() {
344 fun(fd, iov, iovcnt, offset, flags)
345 } else {
346 // Unlike the plain "p" functions, the "pv" functions pass their
347 // offset in an endian-independent way, and always in two registers.
348 syscall! {
349 fn preadv2(
350 fd: libc::c_int,
351 iov: *const libc::iovec,
352 iovcnt: libc::c_int,
353 offset_lo: usize,
354 offset_hi: usize,
355 flags: libc::c_int
356 ) via SYS_preadv2 -> libc::ssize_t
357 }
358 preadv2(
359 fd,
360 iov,
361 iovcnt,
362 offset as usize,
363 (offset >> 32) as usize,
364 flags,
365 )
366 }
367 }
368 pub(in super::super) unsafe fn pwritev64v2(
369 fd: libc::c_int,
370 iov: *const libc::iovec,
371 iovcnt: libc::c_int,
372 offset: libc::off64_t,
373 flags: libc::c_int,
374 ) -> libc::ssize_t {
375 // See the comments in `preadv64v2`.
376 weak! {
377 fn pwritev64v2(libc::c_int, *const libc::iovec, libc::c_int, libc::off64_t, libc::c_int) -> libc::ssize_t
378 }
379 if let Some(fun) = pwritev64v2.get() {
380 fun(fd, iov, iovcnt, offset, flags)
381 } else {
382 // Unlike the plain "p" functions, the "pv" functions pass their
383 // offset in an endian-independent way, and always in two registers.
384 syscall! {
385 fn pwritev2(
386 fd: libc::c_int,
387 iov: *const libc::iovec,
388 iovec: libc::c_int,
389 offset_lo: usize,
390 offset_hi: usize,
391 flags: libc::c_int
392 ) via SYS_pwritev2 -> libc::ssize_t
393 }
394 pwritev2(
395 fd,
396 iov,
397 iovcnt,
398 offset as usize,
399 (offset >> 32) as usize,
400 flags,
401 )
402 }
403 }
404 }
405 #[cfg(all(target_os = "linux", target_env = "gnu"))]
406 pub(super) use readwrite_pv64v2::{preadv64v2 as preadv2, pwritev64v2 as pwritev2};
407
408 // On non-glibc, assume we don't have `pwritev2`/`preadv2` in libc and use
409 // `c::syscall` instead.
410 #[cfg(any(
411 target_os = "android",
412 all(target_os = "linux", not(target_env = "gnu")),
413 ))]
414 mod readwrite_pv64v2 {
415 use super::*;
416
417 pub(in super::super) unsafe fn preadv64v2(
418 fd: libc::c_int,
419 iov: *const libc::iovec,
420 iovcnt: libc::c_int,
421 offset: libc::off64_t,
422 flags: libc::c_int,
423 ) -> libc::ssize_t {
424 // Unlike the plain "p" functions, the "pv" functions pass their offset
425 // in an endian-independent way, and always in two registers.
426 syscall! {
427 fn preadv2(
428 fd: libc::c_int,
429 iov: *const libc::iovec,
430 iovcnt: libc::c_int,
431 offset_lo: usize,
432 offset_hi: usize,
433 flags: libc::c_int
434 ) via SYS_preadv2 -> libc::ssize_t
435 }
436 preadv2(
437 fd,
438 iov,
439 iovcnt,
440 offset as usize,
441 (offset >> 32) as usize,
442 flags,
443 )
444 }
445 pub(in super::super) unsafe fn pwritev64v2(
446 fd: libc::c_int,
447 iov: *const libc::iovec,
448 iovcnt: libc::c_int,
449 offset: libc::off64_t,
450 flags: libc::c_int,
451 ) -> libc::ssize_t {
452 // Unlike the plain "p" functions, the "pv" functions pass their offset
453 // in an endian-independent way, and always in two registers.
454 syscall! {
455 fn pwritev2(
456 fd: libc::c_int,
457 iov: *const libc::iovec,
458 iovcnt: libc::c_int,
459 offset_lo: usize,
460 offset_hi: usize,
461 flags: libc::c_int
462 ) via SYS_pwritev2 -> libc::ssize_t
463 }
464 pwritev2(
465 fd,
466 iov,
467 iovcnt,
468 offset as usize,
469 (offset >> 32) as usize,
470 flags,
471 )
472 }
473 }
474 #[cfg(any(
475 target_os = "android",
476 all(target_os = "linux", not(target_env = "gnu")),
477 ))]
478 pub(super) use readwrite_pv64v2::{preadv64v2 as preadv2, pwritev64v2 as pwritev2};