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     pub struct tcp_info {
119         pub tcpi_state: u8,
120         pub tcpi_ca_state: u8,
121         pub tcpi_retransmits: u8,
122         pub tcpi_probes: u8,
123         pub tcpi_backoff: u8,
124         pub tcpi_options: u8,
125         /// This contains the bitfields `tcpi_snd_wscale` and `tcpi_rcv_wscale`.
126         /// Each is 4 bits.
127         pub tcpi_snd_rcv_wscale: u8,
128         pub tcpi_rto: u32,
129         pub tcpi_ato: u32,
130         pub tcpi_snd_mss: u32,
131         pub tcpi_rcv_mss: u32,
132         pub tcpi_unacked: u32,
133         pub tcpi_sacked: u32,
134         pub tcpi_lost: u32,
135         pub tcpi_retrans: u32,
136         pub tcpi_fackets: u32,
137         pub tcpi_last_data_sent: u32,
138         pub tcpi_last_ack_sent: u32,
139         pub tcpi_last_data_recv: u32,
140         pub tcpi_last_ack_recv: u32,
141         pub tcpi_pmtu: u32,
142         pub tcpi_rcv_ssthresh: u32,
143         pub tcpi_rtt: u32,
144         pub tcpi_rttvar: u32,
145         pub tcpi_snd_ssthresh: u32,
146         pub tcpi_snd_cwnd: u32,
147         pub tcpi_advmss: u32,
148         pub tcpi_reordering: u32,
149         pub tcpi_rcv_rtt: u32,
150         pub tcpi_rcv_space: u32,
151         pub tcpi_total_retrans: u32,
152     }
153 }
154 
155 impl siginfo_t {
156     pub unsafe fn si_addr(&self) -> *mut c_void {
157         #[repr(C)]
158         struct siginfo_sigfault {
159             _si_signo: c_int,
160             _si_errno: c_int,
161             _si_code: c_int,
162             si_addr: *mut c_void,
163         }
164         (*(self as *const siginfo_t as *const siginfo_sigfault)).si_addr
165     }
166 
167     pub unsafe fn si_value(&self) -> crate::sigval {
168         #[repr(C)]
169         struct siginfo_si_value {
170             _si_signo: c_int,
171             _si_errno: c_int,
172             _si_code: c_int,
173             _si_timerid: c_int,
174             _si_overrun: c_int,
175             si_value: crate::sigval,
176         }
177         (*(self as *const siginfo_t as *const siginfo_si_value)).si_value
178     }
179 }
180 
181 // Internal, for casts to access union fields
182 #[repr(C)]
183 struct sifields_sigchld {
184     si_pid: crate::pid_t,
185     si_uid: crate::uid_t,
186     si_status: c_int,
187     si_utime: c_long,
188     si_stime: c_long,
189 }
190 impl Copy for sifields_sigchld {}
191 impl Clone for sifields_sigchld {
192     fn clone(&self) -> sifields_sigchld {
193         *self
194     }
195 }
196 
197 // Internal, for casts to access union fields
198 #[repr(C)]
199 union sifields {
200     _align_pointer: *mut c_void,
201     sigchld: sifields_sigchld,
202 }
203 
204 // Internal, for casts to access union fields. Note that some variants
205 // of sifields start with a pointer, which makes the alignment of
206 // sifields vary on 32-bit and 64-bit architectures.
207 #[repr(C)]
208 struct siginfo_f {
209     _siginfo_base: [c_int; 3],
210     sifields: sifields,
211 }
212 
213 impl siginfo_t {
214     unsafe fn sifields(&self) -> &sifields {
215         &(*(self as *const siginfo_t as *const siginfo_f)).sifields
216     }
217 
218     pub unsafe fn si_pid(&self) -> crate::pid_t {
219         self.sifields().sigchld.si_pid
220     }
221 
222     pub unsafe fn si_uid(&self) -> crate::uid_t {
223         self.sifields().sigchld.si_uid
224     }
225 
226     pub unsafe fn si_status(&self) -> c_int {
227         self.sifields().sigchld.si_status
228     }
229 
230     pub unsafe fn si_utime(&self) -> c_long {
231         self.sifields().sigchld.si_utime
232     }
233 
234     pub unsafe fn si_stime(&self) -> c_long {
235         self.sifields().sigchld.si_stime
236     }
237 }
238 
239 pub const MCL_CURRENT: c_int = 0x0001;
240 pub const MCL_FUTURE: c_int = 0x0002;
241 pub const MCL_ONFAULT: c_int = 0x0004;
242 
243 pub const SIGEV_THREAD_ID: c_int = 4;
244 
245 pub const AF_VSOCK: c_int = 40;
246 
247 // Most `*_SUPER_MAGIC` constants are defined at the `linux_like` level; the
248 // following are only available on newer Linux versions than the versions
249 // currently used in CI in some configurations, so we define them here.
250 pub const BINDERFS_SUPER_MAGIC: c_long = 0x6c6f6f70;
251 pub const XFS_SUPER_MAGIC: c_long = 0x58465342;
252 
253 pub const PTRACE_TRACEME: c_int = 0;
254 pub const PTRACE_PEEKTEXT: c_int = 1;
255 pub const PTRACE_PEEKDATA: c_int = 2;
256 pub const PTRACE_PEEKUSER: c_int = 3;
257 pub const PTRACE_POKETEXT: c_int = 4;
258 pub const PTRACE_POKEDATA: c_int = 5;
259 pub const PTRACE_POKEUSER: c_int = 6;
260 pub const PTRACE_CONT: c_int = 7;
261 pub const PTRACE_KILL: c_int = 8;
262 pub const PTRACE_SINGLESTEP: c_int = 9;
263 pub const PTRACE_GETREGS: c_int = 12;
264 pub const PTRACE_SETREGS: c_int = 13;
265 pub const PTRACE_GETFPREGS: c_int = 14;
266 pub const PTRACE_SETFPREGS: c_int = 15;
267 pub const PTRACE_ATTACH: c_int = 16;
268 pub const PTRACE_DETACH: c_int = 17;
269 pub const PTRACE_GETFPXREGS: c_int = 18;
270 pub const PTRACE_SETFPXREGS: c_int = 19;
271 pub const PTRACE_SYSCALL: c_int = 24;
272 pub const PTRACE_SETOPTIONS: c_int = 0x4200;
273 pub const PTRACE_GETEVENTMSG: c_int = 0x4201;
274 pub const PTRACE_GETSIGINFO: c_int = 0x4202;
275 pub const PTRACE_SETSIGINFO: c_int = 0x4203;
276 pub const PTRACE_GETREGSET: c_int = 0x4204;
277 pub const PTRACE_SETREGSET: c_int = 0x4205;
278 pub const PTRACE_SEIZE: c_int = 0x4206;
279 pub const PTRACE_INTERRUPT: c_int = 0x4207;
280 pub const PTRACE_LISTEN: c_int = 0x4208;
281 
282 pub const POSIX_FADV_DONTNEED: c_int = 4;
283 pub const POSIX_FADV_NOREUSE: c_int = 5;
284 
285 // These are different than GNU!
286 pub const LC_CTYPE: c_int = 0;
287 pub const LC_NUMERIC: c_int = 1;
288 pub const LC_TIME: c_int = 3;
289 pub const LC_COLLATE: c_int = 4;
290 pub const LC_MONETARY: c_int = 2;
291 pub const LC_MESSAGES: c_int = 5;
292 pub const LC_ALL: c_int = 6;
293 // end different section
294 
295 // MS_ flags for mount(2)
296 pub const MS_RMT_MASK: c_ulong =
297     crate::MS_RDONLY | crate::MS_SYNCHRONOUS | crate::MS_MANDLOCK | crate::MS_I_VERSION;
298 
299 pub const ENOTSUP: c_int = EOPNOTSUPP;
300 
301 pub const IPV6_JOIN_GROUP: c_int = 20;
302 pub const IPV6_LEAVE_GROUP: c_int = 21;
303 
304 // These are different from GNU
305 pub const ABDAY_1: crate::nl_item = 0x300;
306 pub const ABDAY_2: crate::nl_item = 0x301;
307 pub const ABDAY_3: crate::nl_item = 0x302;
308 pub const ABDAY_4: crate::nl_item = 0x303;
309 pub const ABDAY_5: crate::nl_item = 0x304;
310 pub const ABDAY_6: crate::nl_item = 0x305;
311 pub const ABDAY_7: crate::nl_item = 0x306;
312 pub const DAY_1: crate::nl_item = 0x307;
313 pub const DAY_2: crate::nl_item = 0x308;
314 pub const DAY_3: crate::nl_item = 0x309;
315 pub const DAY_4: crate::nl_item = 0x30A;
316 pub const DAY_5: crate::nl_item = 0x30B;
317 pub const DAY_6: crate::nl_item = 0x30C;
318 pub const DAY_7: crate::nl_item = 0x30D;
319 pub const ABMON_1: crate::nl_item = 0x30E;
320 pub const ABMON_2: crate::nl_item = 0x30F;
321 pub const ABMON_3: crate::nl_item = 0x310;
322 pub const ABMON_4: crate::nl_item = 0x311;
323 pub const ABMON_5: crate::nl_item = 0x312;
324 pub const ABMON_6: crate::nl_item = 0x313;
325 pub const ABMON_7: crate::nl_item = 0x314;
326 pub const ABMON_8: crate::nl_item = 0x315;
327 pub const ABMON_9: crate::nl_item = 0x316;
328 pub const ABMON_10: crate::nl_item = 0x317;
329 pub const ABMON_11: crate::nl_item = 0x318;
330 pub const ABMON_12: crate::nl_item = 0x319;
331 pub const MON_1: crate::nl_item = 0x31A;
332 pub const MON_2: crate::nl_item = 0x31B;
333 pub const MON_3: crate::nl_item = 0x31C;
334 pub const MON_4: crate::nl_item = 0x31D;
335 pub const MON_5: crate::nl_item = 0x31E;
336 pub const MON_6: crate::nl_item = 0x31F;
337 pub const MON_7: crate::nl_item = 0x320;
338 pub const MON_8: crate::nl_item = 0x321;
339 pub const MON_9: crate::nl_item = 0x322;
340 pub const MON_10: crate::nl_item = 0x323;
341 pub const MON_11: crate::nl_item = 0x324;
342 pub const MON_12: crate::nl_item = 0x325;
343 pub const AM_STR: crate::nl_item = 0x326;
344 pub const PM_STR: crate::nl_item = 0x327;
345 pub const D_T_FMT: crate::nl_item = 0x328;
346 pub const D_FMT: crate::nl_item = 0x329;
347 pub const T_FMT: crate::nl_item = 0x32A;
348 pub const T_FMT_AMPM: crate::nl_item = 0x32B;
349 pub const ERA: crate::nl_item = 0x32C;
350 pub const ERA_D_FMT: crate::nl_item = 0x32E;
351 pub const ALT_DIGITS: crate::nl_item = 0x32F;
352 pub const ERA_D_T_FMT: crate::nl_item = 0x330;
353 pub const ERA_T_FMT: crate::nl_item = 0x331;
354 pub const CODESET: crate::nl_item = 10;
355 pub const CRNCYSTR: crate::nl_item = 0x215;
356 pub const RADIXCHAR: crate::nl_item = 0x100;
357 pub const THOUSEP: crate::nl_item = 0x101;
358 pub const NOEXPR: crate::nl_item = 0x501;
359 pub const YESSTR: crate::nl_item = 0x502;
360 pub const NOSTR: crate::nl_item = 0x503;
361 
362 // Different than Gnu.
363 pub const FILENAME_MAX: c_uint = 4095;
364 
365 pub const PRIO_PROCESS: c_int = 0;
366 pub const PRIO_PGRP: c_int = 1;
367 pub const PRIO_USER: c_int = 2;
368 
369 pub const SOMAXCONN: c_int = 128;
370 
371 pub const ST_RELATIME: c_ulong = 4096;
372 
373 pub const AF_NFC: c_int = PF_NFC;
374 pub const BUFSIZ: c_int = 4096;
375 pub const EDEADLOCK: c_int = EDEADLK;
376 pub const EXTA: c_uint = B19200;
377 pub const EXTB: c_uint = B38400;
378 pub const EXTPROC: crate::tcflag_t = 0o200000;
379 pub const FOPEN_MAX: c_int = 16;
380 pub const F_GETOWN: c_int = 9;
381 pub const F_OFD_GETLK: c_int = 36;
382 pub const F_OFD_SETLK: c_int = 37;
383 pub const F_OFD_SETLKW: c_int = 38;
384 pub const F_RDLCK: c_int = 0;
385 pub const F_SETOWN: c_int = 8;
386 pub const F_UNLCK: c_int = 2;
387 pub const F_WRLCK: c_int = 1;
388 pub const IPV6_MULTICAST_ALL: c_int = 29;
389 pub const IPV6_ROUTER_ALERT_ISOLATE: c_int = 30;
390 pub const MAP_HUGE_SHIFT: c_int = 26;
391 pub const MAP_HUGE_MASK: c_int = 0x3f;
392 pub const MAP_HUGE_64KB: c_int = 16 << MAP_HUGE_SHIFT;
393 pub const MAP_HUGE_512KB: c_int = 19 << MAP_HUGE_SHIFT;
394 pub const MAP_HUGE_1MB: c_int = 20 << MAP_HUGE_SHIFT;
395 pub const MAP_HUGE_2MB: c_int = 21 << MAP_HUGE_SHIFT;
396 pub const MAP_HUGE_8MB: c_int = 23 << MAP_HUGE_SHIFT;
397 pub const MAP_HUGE_16MB: c_int = 24 << MAP_HUGE_SHIFT;
398 pub const MAP_HUGE_32MB: c_int = 25 << MAP_HUGE_SHIFT;
399 pub const MAP_HUGE_256MB: c_int = 28 << MAP_HUGE_SHIFT;
400 pub const MAP_HUGE_512MB: c_int = 29 << MAP_HUGE_SHIFT;
401 pub const MAP_HUGE_1GB: c_int = 30 << MAP_HUGE_SHIFT;
402 pub const MAP_HUGE_2GB: c_int = 31 << MAP_HUGE_SHIFT;
403 pub const MAP_HUGE_16GB: c_int = 34 << MAP_HUGE_SHIFT;
404 pub const MINSIGSTKSZ: c_int = 2048;
405 pub const MSG_COPY: c_int = 0o40000;
406 pub const NI_MAXHOST: crate::socklen_t = 1025;
407 pub const O_TMPFILE: c_int = 0o20000000 | O_DIRECTORY;
408 pub const PACKET_MR_UNICAST: c_int = 3;
409 pub const PF_NFC: c_int = 39;
410 pub const PF_VSOCK: c_int = 40;
411 pub const POSIX_MADV_DONTNEED: c_int = 4;
412 pub const PTRACE_EVENT_STOP: c_int = 128;
413 pub const PTRACE_GETSIGMASK: c_uint = 0x420a;
414 pub const PTRACE_PEEKSIGINFO: c_int = 0x4209;
415 pub const PTRACE_SETSIGMASK: c_uint = 0x420b;
416 pub const RTLD_NOLOAD: c_int = 0x00004;
417 pub const RUSAGE_THREAD: c_int = 1;
418 pub const SHM_EXEC: c_int = 0o100000;
419 pub const SIGPOLL: c_int = SIGIO;
420 pub const SOCK_DCCP: c_int = 6;
421 #[deprecated(since = "0.2.70", note = "AF_PACKET must be used instead")]
422 pub const SOCK_PACKET: c_int = 10;
423 pub const TCP_COOKIE_TRANSACTIONS: c_int = 15;
424 pub const UDP_GRO: c_int = 104;
425 pub const UDP_SEGMENT: c_int = 103;
426 pub const YESEXPR: c_int = ((5) << 8) | (0);
427 
428 extern "C" {
429     pub fn gettimeofday(tp: *mut crate::timeval, tz: *mut crate::timezone) -> c_int;
430 
431     pub fn pthread_rwlockattr_getkind_np(
432         attr: *const crate::pthread_rwlockattr_t,
433         val: *mut c_int,
434     ) -> c_int;
435     pub fn pthread_rwlockattr_setkind_np(
436         attr: *mut crate::pthread_rwlockattr_t,
437         val: c_int,
438     ) -> c_int;
439 
440     pub fn ptrace(request: c_uint, ...) -> c_long;
441 
442     pub fn sendmmsg(
443         sockfd: c_int,
444         msgvec: *mut crate::mmsghdr,
445         vlen: c_uint,
446         flags: c_int,
447     ) -> c_int;
448     pub fn recvmmsg(
449         sockfd: c_int,
450         msgvec: *mut crate::mmsghdr,
451         vlen: c_uint,
452         flags: c_int,
453         timeout: *mut crate::timespec,
454     ) -> c_int;
455 
456     pub fn openpty(
457         amaster: *mut c_int,
458         aslave: *mut c_int,
459         name: *mut c_char,
460         termp: *mut termios,
461         winp: *mut crate::winsize,
462     ) -> c_int;
463     pub fn forkpty(
464         amaster: *mut c_int,
465         name: *mut c_char,
466         termp: *mut termios,
467         winp: *mut crate::winsize,
468     ) -> crate::pid_t;
469 
470     pub fn getnameinfo(
471         sa: *const crate::sockaddr,
472         salen: crate::socklen_t,
473         host: *mut c_char,
474         hostlen: crate::socklen_t,
475         serv: *mut c_char,
476         servlen: crate::socklen_t,
477         flags: c_int,
478     ) -> c_int;
479 
480     pub fn pwritev(fd: c_int, iov: *const crate::iovec, iovcnt: c_int, offset: off64_t) -> ssize_t;
481     pub fn preadv(fd: c_int, iov: *const crate::iovec, iovcnt: c_int, offset: off64_t) -> ssize_t;
482 
483     pub fn sethostid(hostid: c_long) -> c_int;
484     pub fn fanotify_mark(
485         fd: c_int,
486         flags: c_uint,
487         mask: u64,
488         dirfd: c_int,
489         path: *const c_char,
490     ) -> c_int;
491     pub fn getrlimit64(resource: crate::__rlimit_resource_t, rlim: *mut crate::rlimit64) -> c_int;
492     pub fn setrlimit64(resource: crate::__rlimit_resource_t, rlim: *const crate::rlimit64)
493         -> c_int;
494     pub fn getrlimit(resource: crate::__rlimit_resource_t, rlim: *mut crate::rlimit) -> c_int;
495     pub fn setrlimit(resource: crate::__rlimit_resource_t, rlim: *const crate::rlimit) -> c_int;
496     pub fn getpriority(which: crate::__priority_which_t, who: crate::id_t) -> c_int;
497     pub fn setpriority(which: crate::__priority_which_t, who: crate::id_t, prio: c_int) -> c_int;
498     pub fn getauxval(type_: c_ulong) -> c_ulong;
499 }
500 
501 cfg_if! {
502     if #[cfg(any(target_arch = "mips", target_arch = "mips64"))] {
503         mod mips;
504         pub use self::mips::*;
505     } else if #[cfg(target_arch = "x86_64")] {
506         mod x86_64;
507         pub use self::x86_64::*;
508     } else if #[cfg(target_arch = "arm")] {
509         mod arm;
510         pub use self::arm::*;
511     } else {
512         pub use unsupported_target;
513     }
514 }
515