1 use crate::off64_t;
2 use crate::prelude::*;
3 
4 pub type shmatt_t = c_ulong;
5 pub type msgqnum_t = c_ulong;
6 pub type msglen_t = c_ulong;
7 pub type regoff_t = c_int;
8 pub type rlim_t = c_ulong;
9 pub type __rlimit_resource_t = c_ulong;
10 pub type __priority_which_t = c_uint;
11 
12 cfg_if! {
13     if #[cfg(doc)] {
14         // Used in `linux::arch` to define ioctl constants.
15         pub(crate) type Ioctl = c_ulong;
16     } else {
17         #[doc(hidden)]
18         pub type Ioctl = c_ulong;
19     }
20 }
21 
22 s! {
23     pub struct statvfs {
24         // Different than GNU!
25         pub f_bsize: c_ulong,
26         pub f_frsize: c_ulong,
27         pub f_blocks: crate::fsblkcnt_t,
28         pub f_bfree: crate::fsblkcnt_t,
29         pub f_bavail: crate::fsblkcnt_t,
30         pub f_files: crate::fsfilcnt_t,
31         pub f_ffree: crate::fsfilcnt_t,
32         pub f_favail: crate::fsfilcnt_t,
33         #[cfg(target_endian = "little")]
34         pub f_fsid: c_ulong,
35         #[cfg(target_pointer_width = "32")]
36         __f_unused: c_int,
37         #[cfg(target_endian = "big")]
38         pub f_fsid: c_ulong,
39         pub f_flag: c_ulong,
40         pub f_namemax: c_ulong,
41         __f_spare: [c_int; 6],
42     }
43 
44     pub struct regex_t {
45         __buffer: *mut c_void,
46         __allocated: size_t,
47         __used: size_t,
48         __syntax: c_ulong,
49         __fastmap: *mut c_char,
50         __translate: *mut c_char,
51         __re_nsub: size_t,
52         __bitfield: u8,
53     }
54 
55     pub struct rtentry {
56         pub rt_pad1: c_ulong,
57         pub rt_dst: crate::sockaddr,
58         pub rt_gateway: crate::sockaddr,
59         pub rt_genmask: crate::sockaddr,
60         pub rt_flags: c_ushort,
61         pub rt_pad2: c_short,
62         pub rt_pad3: c_ulong,
63         pub rt_tos: c_uchar,
64         pub rt_class: c_uchar,
65         #[cfg(target_pointer_width = "64")]
66         pub rt_pad4: [c_short; 3usize],
67         #[cfg(not(target_pointer_width = "64"))]
68         pub rt_pad4: c_short,
69         pub rt_metric: c_short,
70         pub rt_dev: *mut c_char,
71         pub rt_mtu: c_ulong,
72         pub rt_window: c_ulong,
73         pub rt_irtt: c_ushort,
74     }
75 
76     pub struct __exit_status {
77         pub e_termination: c_short,
78         pub e_exit: c_short,
79     }
80 
81     pub struct ptrace_peeksiginfo_args {
82         pub off: crate::__u64,
83         pub flags: crate::__u32,
84         pub nr: crate::__s32,
85     }
86 
87     #[cfg_attr(
88         any(
89             target_pointer_width = "32",
90             target_arch = "x86_64",
91             target_arch = "powerpc64",
92             target_arch = "mips64",
93             target_arch = "s390x",
94             target_arch = "sparc64"
95         ),
96         repr(align(4))
97     )]
98     #[cfg_attr(
99         not(any(
100             target_pointer_width = "32",
101             target_arch = "x86_64",
102             target_arch = "powerpc64",
103             target_arch = "mips64",
104             target_arch = "s390x",
105             target_arch = "sparc64"
106         )),
107         repr(align(8))
108     )]
109     pub struct pthread_mutexattr_t {
110         size: [u8; crate::__SIZEOF_PTHREAD_MUTEXATTR_T],
111     }
112 
113     #[repr(align(4))]
114     pub struct pthread_condattr_t {
115         size: [u8; crate::__SIZEOF_PTHREAD_CONDATTR_T],
116     }
117 }
118 
119 impl siginfo_t {
120     pub unsafe fn si_addr(&self) -> *mut c_void {
121         #[repr(C)]
122         struct siginfo_sigfault {
123             _si_signo: c_int,
124             _si_errno: c_int,
125             _si_code: c_int,
126             si_addr: *mut c_void,
127         }
128         (*(self as *const siginfo_t as *const siginfo_sigfault)).si_addr
129     }
130 
131     pub unsafe fn si_value(&self) -> crate::sigval {
132         #[repr(C)]
133         struct siginfo_si_value {
134             _si_signo: c_int,
135             _si_errno: c_int,
136             _si_code: c_int,
137             _si_timerid: c_int,
138             _si_overrun: c_int,
139             si_value: crate::sigval,
140         }
141         (*(self as *const siginfo_t as *const siginfo_si_value)).si_value
142     }
143 }
144 
145 // Internal, for casts to access union fields
146 #[repr(C)]
147 struct sifields_sigchld {
148     si_pid: crate::pid_t,
149     si_uid: crate::uid_t,
150     si_status: c_int,
151     si_utime: c_long,
152     si_stime: c_long,
153 }
154 impl Copy for sifields_sigchld {}
155 impl Clone for sifields_sigchld {
156     fn clone(&self) -> sifields_sigchld {
157         *self
158     }
159 }
160 
161 // Internal, for casts to access union fields
162 #[repr(C)]
163 union sifields {
164     _align_pointer: *mut c_void,
165     sigchld: sifields_sigchld,
166 }
167 
168 // Internal, for casts to access union fields. Note that some variants
169 // of sifields start with a pointer, which makes the alignment of
170 // sifields vary on 32-bit and 64-bit architectures.
171 #[repr(C)]
172 struct siginfo_f {
173     _siginfo_base: [c_int; 3],
174     sifields: sifields,
175 }
176 
177 impl siginfo_t {
178     unsafe fn sifields(&self) -> &sifields {
179         &(*(self as *const siginfo_t as *const siginfo_f)).sifields
180     }
181 
182     pub unsafe fn si_pid(&self) -> crate::pid_t {
183         self.sifields().sigchld.si_pid
184     }
185 
186     pub unsafe fn si_uid(&self) -> crate::uid_t {
187         self.sifields().sigchld.si_uid
188     }
189 
190     pub unsafe fn si_status(&self) -> c_int {
191         self.sifields().sigchld.si_status
192     }
193 
194     pub unsafe fn si_utime(&self) -> c_long {
195         self.sifields().sigchld.si_utime
196     }
197 
198     pub unsafe fn si_stime(&self) -> c_long {
199         self.sifields().sigchld.si_stime
200     }
201 }
202 
203 pub const MCL_CURRENT: c_int = 0x0001;
204 pub const MCL_FUTURE: c_int = 0x0002;
205 pub const MCL_ONFAULT: c_int = 0x0004;
206 
207 pub const SIGEV_THREAD_ID: c_int = 4;
208 
209 pub const AF_VSOCK: c_int = 40;
210 
211 // Most `*_SUPER_MAGIC` constants are defined at the `linux_like` level; the
212 // following are only available on newer Linux versions than the versions
213 // currently used in CI in some configurations, so we define them here.
214 pub const BINDERFS_SUPER_MAGIC: c_long = 0x6c6f6f70;
215 pub const XFS_SUPER_MAGIC: c_long = 0x58465342;
216 
217 pub const PTRACE_TRACEME: c_int = 0;
218 pub const PTRACE_PEEKTEXT: c_int = 1;
219 pub const PTRACE_PEEKDATA: c_int = 2;
220 pub const PTRACE_PEEKUSER: c_int = 3;
221 pub const PTRACE_POKETEXT: c_int = 4;
222 pub const PTRACE_POKEDATA: c_int = 5;
223 pub const PTRACE_POKEUSER: c_int = 6;
224 pub const PTRACE_CONT: c_int = 7;
225 pub const PTRACE_KILL: c_int = 8;
226 pub const PTRACE_SINGLESTEP: c_int = 9;
227 pub const PTRACE_GETREGS: c_int = 12;
228 pub const PTRACE_SETREGS: c_int = 13;
229 pub const PTRACE_GETFPREGS: c_int = 14;
230 pub const PTRACE_SETFPREGS: c_int = 15;
231 pub const PTRACE_ATTACH: c_int = 16;
232 pub const PTRACE_DETACH: c_int = 17;
233 pub const PTRACE_GETFPXREGS: c_int = 18;
234 pub const PTRACE_SETFPXREGS: c_int = 19;
235 pub const PTRACE_SYSCALL: c_int = 24;
236 pub const PTRACE_SETOPTIONS: c_int = 0x4200;
237 pub const PTRACE_GETEVENTMSG: c_int = 0x4201;
238 pub const PTRACE_GETSIGINFO: c_int = 0x4202;
239 pub const PTRACE_SETSIGINFO: c_int = 0x4203;
240 pub const PTRACE_GETREGSET: c_int = 0x4204;
241 pub const PTRACE_SETREGSET: c_int = 0x4205;
242 pub const PTRACE_SEIZE: c_int = 0x4206;
243 pub const PTRACE_INTERRUPT: c_int = 0x4207;
244 pub const PTRACE_LISTEN: c_int = 0x4208;
245 
246 pub const POSIX_FADV_DONTNEED: c_int = 4;
247 pub const POSIX_FADV_NOREUSE: c_int = 5;
248 
249 // These are different than GNU!
250 pub const LC_CTYPE: c_int = 0;
251 pub const LC_NUMERIC: c_int = 1;
252 pub const LC_TIME: c_int = 3;
253 pub const LC_COLLATE: c_int = 4;
254 pub const LC_MONETARY: c_int = 2;
255 pub const LC_MESSAGES: c_int = 5;
256 pub const LC_ALL: c_int = 6;
257 // end different section
258 
259 // MS_ flags for mount(2)
260 pub const MS_RMT_MASK: c_ulong =
261     crate::MS_RDONLY | crate::MS_SYNCHRONOUS | crate::MS_MANDLOCK | crate::MS_I_VERSION;
262 
263 pub const ENOTSUP: c_int = EOPNOTSUPP;
264 
265 pub const IPV6_JOIN_GROUP: c_int = 20;
266 pub const IPV6_LEAVE_GROUP: c_int = 21;
267 
268 // These are different from GNU
269 pub const ABDAY_1: crate::nl_item = 0x300;
270 pub const ABDAY_2: crate::nl_item = 0x301;
271 pub const ABDAY_3: crate::nl_item = 0x302;
272 pub const ABDAY_4: crate::nl_item = 0x303;
273 pub const ABDAY_5: crate::nl_item = 0x304;
274 pub const ABDAY_6: crate::nl_item = 0x305;
275 pub const ABDAY_7: crate::nl_item = 0x306;
276 pub const DAY_1: crate::nl_item = 0x307;
277 pub const DAY_2: crate::nl_item = 0x308;
278 pub const DAY_3: crate::nl_item = 0x309;
279 pub const DAY_4: crate::nl_item = 0x30A;
280 pub const DAY_5: crate::nl_item = 0x30B;
281 pub const DAY_6: crate::nl_item = 0x30C;
282 pub const DAY_7: crate::nl_item = 0x30D;
283 pub const ABMON_1: crate::nl_item = 0x30E;
284 pub const ABMON_2: crate::nl_item = 0x30F;
285 pub const ABMON_3: crate::nl_item = 0x310;
286 pub const ABMON_4: crate::nl_item = 0x311;
287 pub const ABMON_5: crate::nl_item = 0x312;
288 pub const ABMON_6: crate::nl_item = 0x313;
289 pub const ABMON_7: crate::nl_item = 0x314;
290 pub const ABMON_8: crate::nl_item = 0x315;
291 pub const ABMON_9: crate::nl_item = 0x316;
292 pub const ABMON_10: crate::nl_item = 0x317;
293 pub const ABMON_11: crate::nl_item = 0x318;
294 pub const ABMON_12: crate::nl_item = 0x319;
295 pub const MON_1: crate::nl_item = 0x31A;
296 pub const MON_2: crate::nl_item = 0x31B;
297 pub const MON_3: crate::nl_item = 0x31C;
298 pub const MON_4: crate::nl_item = 0x31D;
299 pub const MON_5: crate::nl_item = 0x31E;
300 pub const MON_6: crate::nl_item = 0x31F;
301 pub const MON_7: crate::nl_item = 0x320;
302 pub const MON_8: crate::nl_item = 0x321;
303 pub const MON_9: crate::nl_item = 0x322;
304 pub const MON_10: crate::nl_item = 0x323;
305 pub const MON_11: crate::nl_item = 0x324;
306 pub const MON_12: crate::nl_item = 0x325;
307 pub const AM_STR: crate::nl_item = 0x326;
308 pub const PM_STR: crate::nl_item = 0x327;
309 pub const D_T_FMT: crate::nl_item = 0x328;
310 pub const D_FMT: crate::nl_item = 0x329;
311 pub const T_FMT: crate::nl_item = 0x32A;
312 pub const T_FMT_AMPM: crate::nl_item = 0x32B;
313 pub const ERA: crate::nl_item = 0x32C;
314 pub const ERA_D_FMT: crate::nl_item = 0x32E;
315 pub const ALT_DIGITS: crate::nl_item = 0x32F;
316 pub const ERA_D_T_FMT: crate::nl_item = 0x330;
317 pub const ERA_T_FMT: crate::nl_item = 0x331;
318 pub const CODESET: crate::nl_item = 10;
319 pub const CRNCYSTR: crate::nl_item = 0x215;
320 pub const RADIXCHAR: crate::nl_item = 0x100;
321 pub const THOUSEP: crate::nl_item = 0x101;
322 pub const NOEXPR: crate::nl_item = 0x501;
323 pub const YESSTR: crate::nl_item = 0x502;
324 pub const NOSTR: crate::nl_item = 0x503;
325 
326 // Different than Gnu.
327 pub const FILENAME_MAX: c_uint = 4095;
328 
329 pub const PRIO_PROCESS: c_int = 0;
330 pub const PRIO_PGRP: c_int = 1;
331 pub const PRIO_USER: c_int = 2;
332 
333 pub const SOMAXCONN: c_int = 128;
334 
335 pub const ST_RELATIME: c_ulong = 4096;
336 
337 pub const AF_NFC: c_int = PF_NFC;
338 pub const BUFSIZ: c_int = 4096;
339 pub const EDEADLOCK: c_int = EDEADLK;
340 pub const EXTA: c_uint = B19200;
341 pub const EXTB: c_uint = B38400;
342 pub const EXTPROC: crate::tcflag_t = 0o200000;
343 pub const FOPEN_MAX: c_int = 16;
344 pub const F_GETOWN: c_int = 9;
345 pub const F_OFD_GETLK: c_int = 36;
346 pub const F_OFD_SETLK: c_int = 37;
347 pub const F_OFD_SETLKW: c_int = 38;
348 pub const F_RDLCK: c_int = 0;
349 pub const F_SETOWN: c_int = 8;
350 pub const F_UNLCK: c_int = 2;
351 pub const F_WRLCK: c_int = 1;
352 pub const IPV6_MULTICAST_ALL: c_int = 29;
353 pub const IPV6_ROUTER_ALERT_ISOLATE: c_int = 30;
354 pub const MAP_HUGE_SHIFT: c_int = 26;
355 pub const MAP_HUGE_MASK: c_int = 0x3f;
356 pub const MAP_HUGE_64KB: c_int = 16 << MAP_HUGE_SHIFT;
357 pub const MAP_HUGE_512KB: c_int = 19 << MAP_HUGE_SHIFT;
358 pub const MAP_HUGE_1MB: c_int = 20 << MAP_HUGE_SHIFT;
359 pub const MAP_HUGE_2MB: c_int = 21 << MAP_HUGE_SHIFT;
360 pub const MAP_HUGE_8MB: c_int = 23 << MAP_HUGE_SHIFT;
361 pub const MAP_HUGE_16MB: c_int = 24 << MAP_HUGE_SHIFT;
362 pub const MAP_HUGE_32MB: c_int = 25 << MAP_HUGE_SHIFT;
363 pub const MAP_HUGE_256MB: c_int = 28 << MAP_HUGE_SHIFT;
364 pub const MAP_HUGE_512MB: c_int = 29 << MAP_HUGE_SHIFT;
365 pub const MAP_HUGE_1GB: c_int = 30 << MAP_HUGE_SHIFT;
366 pub const MAP_HUGE_2GB: c_int = 31 << MAP_HUGE_SHIFT;
367 pub const MAP_HUGE_16GB: c_int = 34 << MAP_HUGE_SHIFT;
368 pub const MINSIGSTKSZ: c_int = 2048;
369 pub const MSG_COPY: c_int = 0o40000;
370 pub const NI_MAXHOST: crate::socklen_t = 1025;
371 pub const O_TMPFILE: c_int = 0o20000000 | O_DIRECTORY;
372 pub const PACKET_MR_UNICAST: c_int = 3;
373 pub const PF_NFC: c_int = 39;
374 pub const PF_VSOCK: c_int = 40;
375 pub const POSIX_MADV_DONTNEED: c_int = 4;
376 pub const PTRACE_EVENT_STOP: c_int = 128;
377 pub const PTRACE_GETSIGMASK: c_uint = 0x420a;
378 pub const PTRACE_PEEKSIGINFO: c_int = 0x4209;
379 pub const PTRACE_SETSIGMASK: c_uint = 0x420b;
380 pub const RTLD_NOLOAD: c_int = 0x00004;
381 pub const RUSAGE_THREAD: c_int = 1;
382 pub const SHM_EXEC: c_int = 0o100000;
383 pub const SIGPOLL: c_int = SIGIO;
384 pub const SOCK_DCCP: c_int = 6;
385 #[deprecated(since = "0.2.70", note = "AF_PACKET must be used instead")]
386 pub const SOCK_PACKET: c_int = 10;
387 pub const TCP_COOKIE_TRANSACTIONS: c_int = 15;
388 pub const UDP_GRO: c_int = 104;
389 pub const UDP_SEGMENT: c_int = 103;
390 pub const YESEXPR: c_int = ((5) << 8) | (0);
391 
392 extern "C" {
393     pub fn gettimeofday(tp: *mut crate::timeval, tz: *mut crate::timezone) -> c_int;
394 
395     pub fn pthread_rwlockattr_getkind_np(
396         attr: *const crate::pthread_rwlockattr_t,
397         val: *mut c_int,
398     ) -> c_int;
399     pub fn pthread_rwlockattr_setkind_np(
400         attr: *mut crate::pthread_rwlockattr_t,
401         val: c_int,
402     ) -> c_int;
403 
404     pub fn ptrace(request: c_uint, ...) -> c_long;
405 
406     pub fn sendmmsg(
407         sockfd: c_int,
408         msgvec: *mut crate::mmsghdr,
409         vlen: c_uint,
410         flags: c_int,
411     ) -> c_int;
412     pub fn recvmmsg(
413         sockfd: c_int,
414         msgvec: *mut crate::mmsghdr,
415         vlen: c_uint,
416         flags: c_int,
417         timeout: *mut crate::timespec,
418     ) -> c_int;
419 
420     pub fn openpty(
421         amaster: *mut c_int,
422         aslave: *mut c_int,
423         name: *mut c_char,
424         termp: *mut termios,
425         winp: *mut crate::winsize,
426     ) -> c_int;
427     pub fn forkpty(
428         amaster: *mut c_int,
429         name: *mut c_char,
430         termp: *mut termios,
431         winp: *mut crate::winsize,
432     ) -> crate::pid_t;
433 
434     pub fn getnameinfo(
435         sa: *const crate::sockaddr,
436         salen: crate::socklen_t,
437         host: *mut c_char,
438         hostlen: crate::socklen_t,
439         serv: *mut c_char,
440         servlen: crate::socklen_t,
441         flags: c_int,
442     ) -> c_int;
443 
444     pub fn pwritev(fd: c_int, iov: *const crate::iovec, iovcnt: c_int, offset: off64_t) -> ssize_t;
445     pub fn preadv(fd: c_int, iov: *const crate::iovec, iovcnt: c_int, offset: off64_t) -> ssize_t;
446 
447     pub fn sethostid(hostid: c_long) -> c_int;
448     pub fn fanotify_mark(
449         fd: c_int,
450         flags: c_uint,
451         mask: u64,
452         dirfd: c_int,
453         path: *const c_char,
454     ) -> c_int;
455     pub fn getrlimit64(resource: crate::__rlimit_resource_t, rlim: *mut crate::rlimit64) -> c_int;
456     pub fn setrlimit64(resource: crate::__rlimit_resource_t, rlim: *const crate::rlimit64)
457         -> c_int;
458     pub fn getrlimit(resource: crate::__rlimit_resource_t, rlim: *mut crate::rlimit) -> c_int;
459     pub fn setrlimit(resource: crate::__rlimit_resource_t, rlim: *const crate::rlimit) -> c_int;
460     pub fn getpriority(which: crate::__priority_which_t, who: crate::id_t) -> c_int;
461     pub fn setpriority(which: crate::__priority_which_t, who: crate::id_t, prio: c_int) -> c_int;
462     pub fn getauxval(type_: c_ulong) -> c_ulong;
463 }
464 
465 cfg_if! {
466     if #[cfg(any(target_arch = "mips", target_arch = "mips64"))] {
467         mod mips;
468         pub use self::mips::*;
469     } else if #[cfg(target_arch = "x86_64")] {
470         mod x86_64;
471         pub use self::x86_64::*;
472     } else if #[cfg(target_arch = "arm")] {
473         mod arm;
474         pub use self::arm::*;
475     } else {
476         pub use unsupported_target;
477     }
478 }
479