libc/unix/bsd/
mod.rs

1use crate::prelude::*;
2
3pub type off_t = i64;
4pub type useconds_t = u32;
5pub type blkcnt_t = i64;
6pub type socklen_t = u32;
7pub type sa_family_t = u8;
8pub type pthread_t = crate::uintptr_t;
9pub type nfds_t = c_uint;
10pub type regoff_t = off_t;
11
12s! {
13    pub struct sockaddr {
14        pub sa_len: u8,
15        pub sa_family: sa_family_t,
16        pub sa_data: [c_char; 14],
17    }
18
19    pub struct sockaddr_in6 {
20        pub sin6_len: u8,
21        pub sin6_family: sa_family_t,
22        pub sin6_port: crate::in_port_t,
23        pub sin6_flowinfo: u32,
24        pub sin6_addr: crate::in6_addr,
25        pub sin6_scope_id: u32,
26    }
27
28    pub struct passwd {
29        pub pw_name: *mut c_char,
30        pub pw_passwd: *mut c_char,
31        pub pw_uid: crate::uid_t,
32        pub pw_gid: crate::gid_t,
33        pub pw_change: crate::time_t,
34        pub pw_class: *mut c_char,
35        pub pw_gecos: *mut c_char,
36        pub pw_dir: *mut c_char,
37        pub pw_shell: *mut c_char,
38        pub pw_expire: crate::time_t,
39
40        #[cfg(not(any(
41            target_os = "macos",
42            target_os = "ios",
43            target_os = "tvos",
44            target_os = "watchos",
45            target_os = "visionos",
46            target_os = "netbsd",
47            target_os = "openbsd"
48        )))]
49        pub pw_fields: c_int,
50    }
51
52    pub struct ifaddrs {
53        pub ifa_next: *mut ifaddrs,
54        pub ifa_name: *mut c_char,
55        pub ifa_flags: c_uint,
56        pub ifa_addr: *mut crate::sockaddr,
57        pub ifa_netmask: *mut crate::sockaddr,
58        pub ifa_dstaddr: *mut crate::sockaddr,
59        pub ifa_data: *mut c_void,
60        #[cfg(target_os = "netbsd")]
61        pub ifa_addrflags: c_uint,
62    }
63
64    pub struct fd_set {
65        #[cfg(all(
66            target_pointer_width = "64",
67            any(target_os = "freebsd", target_os = "dragonfly")
68        ))]
69        fds_bits: [i64; FD_SETSIZE as usize / 64],
70        #[cfg(not(all(
71            target_pointer_width = "64",
72            any(target_os = "freebsd", target_os = "dragonfly")
73        )))]
74        fds_bits: [i32; FD_SETSIZE as usize / 32],
75    }
76
77    pub struct tm {
78        pub tm_sec: c_int,
79        pub tm_min: c_int,
80        pub tm_hour: c_int,
81        pub tm_mday: c_int,
82        pub tm_mon: c_int,
83        pub tm_year: c_int,
84        pub tm_wday: c_int,
85        pub tm_yday: c_int,
86        pub tm_isdst: c_int,
87        pub tm_gmtoff: c_long,
88        pub tm_zone: *mut c_char,
89    }
90
91    pub struct msghdr {
92        pub msg_name: *mut c_void,
93        pub msg_namelen: crate::socklen_t,
94        pub msg_iov: *mut crate::iovec,
95        pub msg_iovlen: c_int,
96        pub msg_control: *mut c_void,
97        pub msg_controllen: crate::socklen_t,
98        pub msg_flags: c_int,
99    }
100
101    pub struct cmsghdr {
102        pub cmsg_len: crate::socklen_t,
103        pub cmsg_level: c_int,
104        pub cmsg_type: c_int,
105    }
106
107    pub struct fsid_t {
108        __fsid_val: [i32; 2],
109    }
110
111    pub struct if_nameindex {
112        pub if_index: c_uint,
113        pub if_name: *mut c_char,
114    }
115
116    pub struct regex_t {
117        __re_magic: c_int,
118        __re_nsub: size_t,
119        __re_endp: *const c_char,
120        __re_g: *mut c_void,
121    }
122
123    pub struct regmatch_t {
124        pub rm_so: regoff_t,
125        pub rm_eo: regoff_t,
126    }
127
128    pub struct option {
129        pub name: *const c_char,
130        pub has_arg: c_int,
131        pub flag: *mut c_int,
132        pub val: c_int,
133    }
134}
135
136s_no_extra_traits! {
137    pub struct sockaddr_un {
138        pub sun_len: u8,
139        pub sun_family: sa_family_t,
140        pub sun_path: [c_char; 104],
141    }
142
143    pub struct utsname {
144        #[cfg(not(target_os = "dragonfly"))]
145        pub sysname: [c_char; 256],
146        #[cfg(target_os = "dragonfly")]
147        pub sysname: [c_char; 32],
148        #[cfg(not(target_os = "dragonfly"))]
149        pub nodename: [c_char; 256],
150        #[cfg(target_os = "dragonfly")]
151        pub nodename: [c_char; 32],
152        #[cfg(not(target_os = "dragonfly"))]
153        pub release: [c_char; 256],
154        #[cfg(target_os = "dragonfly")]
155        pub release: [c_char; 32],
156        #[cfg(not(target_os = "dragonfly"))]
157        pub version: [c_char; 256],
158        #[cfg(target_os = "dragonfly")]
159        pub version: [c_char; 32],
160        #[cfg(not(target_os = "dragonfly"))]
161        pub machine: [c_char; 256],
162        #[cfg(target_os = "dragonfly")]
163        pub machine: [c_char; 32],
164    }
165}
166
167cfg_if! {
168    if #[cfg(feature = "extra_traits")] {
169        impl PartialEq for sockaddr_un {
170            fn eq(&self, other: &sockaddr_un) -> bool {
171                self.sun_len == other.sun_len
172                    && self.sun_family == other.sun_family
173                    && self
174                        .sun_path
175                        .iter()
176                        .zip(other.sun_path.iter())
177                        .all(|(a, b)| a == b)
178            }
179        }
180
181        impl Eq for sockaddr_un {}
182
183        impl fmt::Debug for sockaddr_un {
184            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185                f.debug_struct("sockaddr_un")
186                    .field("sun_len", &self.sun_len)
187                    .field("sun_family", &self.sun_family)
188                    // FIXME: .field("sun_path", &self.sun_path)
189                    .finish()
190            }
191        }
192
193        impl hash::Hash for sockaddr_un {
194            fn hash<H: hash::Hasher>(&self, state: &mut H) {
195                self.sun_len.hash(state);
196                self.sun_family.hash(state);
197                self.sun_path.hash(state);
198            }
199        }
200
201        impl PartialEq for utsname {
202            fn eq(&self, other: &utsname) -> bool {
203                self.sysname
204                    .iter()
205                    .zip(other.sysname.iter())
206                    .all(|(a, b)| a == b)
207                    && self
208                        .nodename
209                        .iter()
210                        .zip(other.nodename.iter())
211                        .all(|(a, b)| a == b)
212                    && self
213                        .release
214                        .iter()
215                        .zip(other.release.iter())
216                        .all(|(a, b)| a == b)
217                    && self
218                        .version
219                        .iter()
220                        .zip(other.version.iter())
221                        .all(|(a, b)| a == b)
222                    && self
223                        .machine
224                        .iter()
225                        .zip(other.machine.iter())
226                        .all(|(a, b)| a == b)
227            }
228        }
229
230        impl Eq for utsname {}
231
232        impl fmt::Debug for utsname {
233            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234                f.debug_struct("utsname")
235                    // FIXME: .field("sysname", &self.sysname)
236                    // FIXME: .field("nodename", &self.nodename)
237                    // FIXME: .field("release", &self.release)
238                    // FIXME: .field("version", &self.version)
239                    // FIXME: .field("machine", &self.machine)
240                    .finish()
241            }
242        }
243
244        impl hash::Hash for utsname {
245            fn hash<H: hash::Hasher>(&self, state: &mut H) {
246                self.sysname.hash(state);
247                self.nodename.hash(state);
248                self.release.hash(state);
249                self.version.hash(state);
250                self.machine.hash(state);
251            }
252        }
253    }
254}
255
256pub const LC_ALL: c_int = 0;
257pub const LC_COLLATE: c_int = 1;
258pub const LC_CTYPE: c_int = 2;
259pub const LC_MONETARY: c_int = 3;
260pub const LC_NUMERIC: c_int = 4;
261pub const LC_TIME: c_int = 5;
262pub const LC_MESSAGES: c_int = 6;
263
264pub const FIOCLEX: c_ulong = 0x20006601;
265pub const FIONCLEX: c_ulong = 0x20006602;
266pub const FIONREAD: c_ulong = 0x4004667f;
267pub const FIONBIO: c_ulong = 0x8004667e;
268pub const FIOASYNC: c_ulong = 0x8004667d;
269pub const FIOSETOWN: c_ulong = 0x8004667c;
270pub const FIOGETOWN: c_ulong = 0x4004667b;
271
272pub const PATH_MAX: c_int = 1024;
273pub const MAXPATHLEN: c_int = PATH_MAX;
274
275pub const IOV_MAX: c_int = 1024;
276
277pub const SA_ONSTACK: c_int = 0x0001;
278pub const SA_SIGINFO: c_int = 0x0040;
279pub const SA_RESTART: c_int = 0x0002;
280pub const SA_RESETHAND: c_int = 0x0004;
281pub const SA_NOCLDSTOP: c_int = 0x0008;
282pub const SA_NODEFER: c_int = 0x0010;
283pub const SA_NOCLDWAIT: c_int = 0x0020;
284
285pub const SS_ONSTACK: c_int = 1;
286pub const SS_DISABLE: c_int = 4;
287
288pub const SIGCHLD: c_int = 20;
289pub const SIGBUS: c_int = 10;
290pub const SIGUSR1: c_int = 30;
291pub const SIGUSR2: c_int = 31;
292pub const SIGCONT: c_int = 19;
293pub const SIGSTOP: c_int = 17;
294pub const SIGTSTP: c_int = 18;
295pub const SIGURG: c_int = 16;
296pub const SIGIO: c_int = 23;
297pub const SIGSYS: c_int = 12;
298pub const SIGTTIN: c_int = 21;
299pub const SIGTTOU: c_int = 22;
300pub const SIGXCPU: c_int = 24;
301pub const SIGXFSZ: c_int = 25;
302pub const SIGVTALRM: c_int = 26;
303pub const SIGPROF: c_int = 27;
304pub const SIGWINCH: c_int = 28;
305pub const SIGINFO: c_int = 29;
306
307pub const SIG_SETMASK: c_int = 3;
308pub const SIG_BLOCK: c_int = 0x1;
309pub const SIG_UNBLOCK: c_int = 0x2;
310
311pub const IP_TOS: c_int = 3;
312pub const IP_MULTICAST_IF: c_int = 9;
313pub const IP_MULTICAST_TTL: c_int = 10;
314pub const IP_MULTICAST_LOOP: c_int = 11;
315
316pub const IPV6_UNICAST_HOPS: c_int = 4;
317pub const IPV6_MULTICAST_IF: c_int = 9;
318pub const IPV6_MULTICAST_HOPS: c_int = 10;
319pub const IPV6_MULTICAST_LOOP: c_int = 11;
320pub const IPV6_V6ONLY: c_int = 27;
321pub const IPV6_DONTFRAG: c_int = 62;
322
323pub const IPTOS_ECN_NOTECT: u8 = 0x00;
324pub const IPTOS_ECN_MASK: u8 = 0x03;
325pub const IPTOS_ECN_ECT1: u8 = 0x01;
326pub const IPTOS_ECN_ECT0: u8 = 0x02;
327pub const IPTOS_ECN_CE: u8 = 0x03;
328
329pub const ST_RDONLY: c_ulong = 1;
330
331pub const SCM_RIGHTS: c_int = 0x01;
332
333pub const NCCS: usize = 20;
334
335pub const O_ACCMODE: c_int = 0x3;
336pub const O_RDONLY: c_int = 0;
337pub const O_WRONLY: c_int = 1;
338pub const O_RDWR: c_int = 2;
339pub const O_APPEND: c_int = 8;
340pub const O_CREAT: c_int = 512;
341pub const O_TRUNC: c_int = 1024;
342pub const O_EXCL: c_int = 2048;
343pub const O_ASYNC: c_int = 0x40;
344pub const O_SYNC: c_int = 0x80;
345pub const O_NONBLOCK: c_int = 0x4;
346pub const O_NOFOLLOW: c_int = 0x100;
347pub const O_SHLOCK: c_int = 0x10;
348pub const O_EXLOCK: c_int = 0x20;
349pub const O_FSYNC: c_int = O_SYNC;
350pub const O_NDELAY: c_int = O_NONBLOCK;
351
352pub const F_GETOWN: c_int = 5;
353pub const F_SETOWN: c_int = 6;
354
355pub const F_RDLCK: c_short = 1;
356pub const F_UNLCK: c_short = 2;
357pub const F_WRLCK: c_short = 3;
358
359pub const MNT_RDONLY: c_int = 0x00000001;
360pub const MNT_SYNCHRONOUS: c_int = 0x00000002;
361pub const MNT_NOEXEC: c_int = 0x00000004;
362pub const MNT_NOSUID: c_int = 0x00000008;
363pub const MNT_ASYNC: c_int = 0x00000040;
364pub const MNT_EXPORTED: c_int = 0x00000100;
365pub const MNT_UPDATE: c_int = 0x00010000;
366pub const MNT_RELOAD: c_int = 0x00040000;
367pub const MNT_FORCE: c_int = 0x00080000;
368
369pub const Q_SYNC: c_int = 0x600;
370pub const Q_QUOTAON: c_int = 0x100;
371pub const Q_QUOTAOFF: c_int = 0x200;
372
373pub const TCIOFF: c_int = 3;
374pub const TCION: c_int = 4;
375pub const TCOOFF: c_int = 1;
376pub const TCOON: c_int = 2;
377pub const TCIFLUSH: c_int = 1;
378pub const TCOFLUSH: c_int = 2;
379pub const TCIOFLUSH: c_int = 3;
380pub const TCSANOW: c_int = 0;
381pub const TCSADRAIN: c_int = 1;
382pub const TCSAFLUSH: c_int = 2;
383pub const VEOF: usize = 0;
384pub const VEOL: usize = 1;
385pub const VEOL2: usize = 2;
386pub const VERASE: usize = 3;
387pub const VWERASE: usize = 4;
388pub const VKILL: usize = 5;
389pub const VREPRINT: usize = 6;
390pub const VINTR: usize = 8;
391pub const VQUIT: usize = 9;
392pub const VSUSP: usize = 10;
393pub const VDSUSP: usize = 11;
394pub const VSTART: usize = 12;
395pub const VSTOP: usize = 13;
396pub const VLNEXT: usize = 14;
397pub const VDISCARD: usize = 15;
398pub const VMIN: usize = 16;
399pub const VTIME: usize = 17;
400pub const VSTATUS: usize = 18;
401pub const _POSIX_VDISABLE: crate::cc_t = 0xff;
402pub const IGNBRK: crate::tcflag_t = 0x00000001;
403pub const BRKINT: crate::tcflag_t = 0x00000002;
404pub const IGNPAR: crate::tcflag_t = 0x00000004;
405pub const PARMRK: crate::tcflag_t = 0x00000008;
406pub const INPCK: crate::tcflag_t = 0x00000010;
407pub const ISTRIP: crate::tcflag_t = 0x00000020;
408pub const INLCR: crate::tcflag_t = 0x00000040;
409pub const IGNCR: crate::tcflag_t = 0x00000080;
410pub const ICRNL: crate::tcflag_t = 0x00000100;
411pub const IXON: crate::tcflag_t = 0x00000200;
412pub const IXOFF: crate::tcflag_t = 0x00000400;
413pub const IXANY: crate::tcflag_t = 0x00000800;
414pub const IMAXBEL: crate::tcflag_t = 0x00002000;
415pub const OPOST: crate::tcflag_t = 0x1;
416pub const ONLCR: crate::tcflag_t = 0x2;
417pub const OXTABS: crate::tcflag_t = 0x4;
418pub const ONOEOT: crate::tcflag_t = 0x8;
419pub const CIGNORE: crate::tcflag_t = 0x00000001;
420pub const CSIZE: crate::tcflag_t = 0x00000300;
421pub const CS5: crate::tcflag_t = 0x00000000;
422pub const CS6: crate::tcflag_t = 0x00000100;
423pub const CS7: crate::tcflag_t = 0x00000200;
424pub const CS8: crate::tcflag_t = 0x00000300;
425pub const CSTOPB: crate::tcflag_t = 0x00000400;
426pub const CREAD: crate::tcflag_t = 0x00000800;
427pub const PARENB: crate::tcflag_t = 0x00001000;
428pub const PARODD: crate::tcflag_t = 0x00002000;
429pub const HUPCL: crate::tcflag_t = 0x00004000;
430pub const CLOCAL: crate::tcflag_t = 0x00008000;
431pub const ECHOKE: crate::tcflag_t = 0x00000001;
432pub const ECHOE: crate::tcflag_t = 0x00000002;
433pub const ECHOK: crate::tcflag_t = 0x00000004;
434pub const ECHO: crate::tcflag_t = 0x00000008;
435pub const ECHONL: crate::tcflag_t = 0x00000010;
436pub const ECHOPRT: crate::tcflag_t = 0x00000020;
437pub const ECHOCTL: crate::tcflag_t = 0x00000040;
438pub const ISIG: crate::tcflag_t = 0x00000080;
439pub const ICANON: crate::tcflag_t = 0x00000100;
440pub const ALTWERASE: crate::tcflag_t = 0x00000200;
441pub const IEXTEN: crate::tcflag_t = 0x00000400;
442pub const EXTPROC: crate::tcflag_t = 0x00000800;
443pub const TOSTOP: crate::tcflag_t = 0x00400000;
444pub const FLUSHO: crate::tcflag_t = 0x00800000;
445pub const NOKERNINFO: crate::tcflag_t = 0x02000000;
446pub const PENDIN: crate::tcflag_t = 0x20000000;
447pub const NOFLSH: crate::tcflag_t = 0x80000000;
448pub const MDMBUF: crate::tcflag_t = 0x00100000;
449
450pub const WNOHANG: c_int = 0x00000001;
451pub const WUNTRACED: c_int = 0x00000002;
452
453pub const RTLD_LAZY: c_int = 0x1;
454pub const RTLD_NOW: c_int = 0x2;
455pub const RTLD_NEXT: *mut c_void = -1isize as *mut c_void;
456pub const RTLD_DEFAULT: *mut c_void = -2isize as *mut c_void;
457pub const RTLD_SELF: *mut c_void = -3isize as *mut c_void;
458
459pub const LOG_CRON: c_int = 9 << 3;
460pub const LOG_AUTHPRIV: c_int = 10 << 3;
461pub const LOG_FTP: c_int = 11 << 3;
462pub const LOG_PERROR: c_int = 0x20;
463
464pub const TCP_NODELAY: c_int = 1;
465pub const TCP_MAXSEG: c_int = 2;
466
467pub const PIPE_BUF: usize = 512;
468
469// si_code values for SIGBUS signal
470pub const BUS_ADRALN: c_int = 1;
471pub const BUS_ADRERR: c_int = 2;
472pub const BUS_OBJERR: c_int = 3;
473
474// si_code values for SIGCHLD signal
475pub const CLD_EXITED: c_int = 1;
476pub const CLD_KILLED: c_int = 2;
477pub const CLD_DUMPED: c_int = 3;
478pub const CLD_TRAPPED: c_int = 4;
479pub const CLD_STOPPED: c_int = 5;
480pub const CLD_CONTINUED: c_int = 6;
481
482pub const POLLIN: c_short = 0x1;
483pub const POLLPRI: c_short = 0x2;
484pub const POLLOUT: c_short = 0x4;
485pub const POLLERR: c_short = 0x8;
486pub const POLLHUP: c_short = 0x10;
487pub const POLLNVAL: c_short = 0x20;
488pub const POLLRDNORM: c_short = 0x040;
489pub const POLLWRNORM: c_short = 0x004;
490pub const POLLRDBAND: c_short = 0x080;
491pub const POLLWRBAND: c_short = 0x100;
492
493pub const BIOCGBLEN: c_ulong = 0x40044266;
494pub const BIOCSBLEN: c_ulong = 0xc0044266;
495pub const BIOCFLUSH: c_uint = 0x20004268;
496pub const BIOCPROMISC: c_uint = 0x20004269;
497pub const BIOCGDLT: c_ulong = 0x4004426a;
498pub const BIOCGETIF: c_ulong = 0x4020426b;
499pub const BIOCSETIF: c_ulong = 0x8020426c;
500pub const BIOCGSTATS: c_ulong = 0x4008426f;
501pub const BIOCIMMEDIATE: c_ulong = 0x80044270;
502pub const BIOCVERSION: c_ulong = 0x40044271;
503pub const BIOCGHDRCMPLT: c_ulong = 0x40044274;
504pub const BIOCSHDRCMPLT: c_ulong = 0x80044275;
505pub const SIOCGIFADDR: c_ulong = 0xc0206921;
506
507pub const REG_BASIC: c_int = 0o0000;
508pub const REG_EXTENDED: c_int = 0o0001;
509pub const REG_ICASE: c_int = 0o0002;
510pub const REG_NOSUB: c_int = 0o0004;
511pub const REG_NEWLINE: c_int = 0o0010;
512pub const REG_NOSPEC: c_int = 0o0020;
513pub const REG_PEND: c_int = 0o0040;
514pub const REG_DUMP: c_int = 0o0200;
515
516pub const REG_NOMATCH: c_int = 1;
517pub const REG_BADPAT: c_int = 2;
518pub const REG_ECOLLATE: c_int = 3;
519pub const REG_ECTYPE: c_int = 4;
520pub const REG_EESCAPE: c_int = 5;
521pub const REG_ESUBREG: c_int = 6;
522pub const REG_EBRACK: c_int = 7;
523pub const REG_EPAREN: c_int = 8;
524pub const REG_EBRACE: c_int = 9;
525pub const REG_BADBR: c_int = 10;
526pub const REG_ERANGE: c_int = 11;
527pub const REG_ESPACE: c_int = 12;
528pub const REG_BADRPT: c_int = 13;
529pub const REG_EMPTY: c_int = 14;
530pub const REG_ASSERT: c_int = 15;
531pub const REG_INVARG: c_int = 16;
532pub const REG_ATOI: c_int = 255;
533pub const REG_ITOA: c_int = 0o0400;
534
535pub const REG_NOTBOL: c_int = 0o00001;
536pub const REG_NOTEOL: c_int = 0o00002;
537pub const REG_STARTEND: c_int = 0o00004;
538pub const REG_TRACE: c_int = 0o00400;
539pub const REG_LARGE: c_int = 0o01000;
540pub const REG_BACKR: c_int = 0o02000;
541
542pub const TIOCCBRK: c_uint = 0x2000747a;
543pub const TIOCSBRK: c_uint = 0x2000747b;
544
545pub const PRIO_PROCESS: c_int = 0;
546pub const PRIO_PGRP: c_int = 1;
547pub const PRIO_USER: c_int = 2;
548
549pub const ITIMER_REAL: c_int = 0;
550pub const ITIMER_VIRTUAL: c_int = 1;
551pub const ITIMER_PROF: c_int = 2;
552
553// net/route.h
554
555pub const RTF_UP: c_int = 0x1;
556pub const RTF_GATEWAY: c_int = 0x2;
557pub const RTF_HOST: c_int = 0x4;
558pub const RTF_REJECT: c_int = 0x8;
559pub const RTF_DYNAMIC: c_int = 0x10;
560pub const RTF_MODIFIED: c_int = 0x20;
561pub const RTF_DONE: c_int = 0x40;
562pub const RTF_STATIC: c_int = 0x800;
563pub const RTF_BLACKHOLE: c_int = 0x1000;
564pub const RTF_PROTO2: c_int = 0x4000;
565pub const RTF_PROTO1: c_int = 0x8000;
566
567// Message types
568pub const RTM_ADD: c_int = 0x1;
569pub const RTM_DELETE: c_int = 0x2;
570pub const RTM_CHANGE: c_int = 0x3;
571pub const RTM_GET: c_int = 0x4;
572pub const RTM_LOSING: c_int = 0x5;
573pub const RTM_REDIRECT: c_int = 0x6;
574pub const RTM_MISS: c_int = 0x7;
575
576// Bitmask values for rtm_addrs.
577pub const RTA_DST: c_int = 0x1;
578pub const RTA_GATEWAY: c_int = 0x2;
579pub const RTA_NETMASK: c_int = 0x4;
580pub const RTA_GENMASK: c_int = 0x8;
581pub const RTA_IFP: c_int = 0x10;
582pub const RTA_IFA: c_int = 0x20;
583pub const RTA_AUTHOR: c_int = 0x40;
584pub const RTA_BRD: c_int = 0x80;
585
586// Index offsets for sockaddr array for alternate internal encoding.
587pub const RTAX_DST: c_int = 0;
588pub const RTAX_GATEWAY: c_int = 1;
589pub const RTAX_NETMASK: c_int = 2;
590pub const RTAX_GENMASK: c_int = 3;
591pub const RTAX_IFP: c_int = 4;
592pub const RTAX_IFA: c_int = 5;
593pub const RTAX_AUTHOR: c_int = 6;
594pub const RTAX_BRD: c_int = 7;
595
596f! {
597    pub fn CMSG_FIRSTHDR(mhdr: *const crate::msghdr) -> *mut cmsghdr {
598        if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
599            (*mhdr).msg_control as *mut cmsghdr
600        } else {
601            core::ptr::null_mut()
602        }
603    }
604
605    pub fn FD_CLR(fd: c_int, set: *mut fd_set) -> () {
606        let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
607        let fd = fd as usize;
608        (*set).fds_bits[fd / bits] &= !(1 << (fd % bits));
609        return;
610    }
611
612    pub fn FD_ISSET(fd: c_int, set: *const fd_set) -> bool {
613        let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
614        let fd = fd as usize;
615        return ((*set).fds_bits[fd / bits] & (1 << (fd % bits))) != 0;
616    }
617
618    pub fn FD_SET(fd: c_int, set: *mut fd_set) -> () {
619        let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
620        let fd = fd as usize;
621        (*set).fds_bits[fd / bits] |= 1 << (fd % bits);
622        return;
623    }
624
625    pub fn FD_ZERO(set: *mut fd_set) -> () {
626        for slot in (*set).fds_bits.iter_mut() {
627            *slot = 0;
628        }
629    }
630}
631
632safe_f! {
633    pub {const} fn WTERMSIG(status: c_int) -> c_int {
634        status & 0o177
635    }
636
637    pub {const} fn WIFEXITED(status: c_int) -> bool {
638        (status & 0o177) == 0
639    }
640
641    pub {const} fn WEXITSTATUS(status: c_int) -> c_int {
642        status >> 8
643    }
644
645    pub {const} fn WCOREDUMP(status: c_int) -> bool {
646        (status & 0o200) != 0
647    }
648
649    pub {const} fn QCMD(cmd: c_int, type_: c_int) -> c_int {
650        (cmd << 8) | (type_ & 0x00ff)
651    }
652}
653
654extern "C" {
655    #[cfg_attr(
656        all(target_os = "macos", target_arch = "x86"),
657        link_name = "getrlimit$UNIX2003"
658    )]
659    pub fn getrlimit(resource: c_int, rlim: *mut crate::rlimit) -> c_int;
660    #[cfg_attr(
661        all(target_os = "macos", target_arch = "x86"),
662        link_name = "setrlimit$UNIX2003"
663    )]
664    pub fn setrlimit(resource: c_int, rlim: *const crate::rlimit) -> c_int;
665
666    pub fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int;
667    pub fn abs(i: c_int) -> c_int;
668    pub fn labs(i: c_long) -> c_long;
669    #[cfg_attr(
670        all(target_os = "freebsd", any(freebsd12, freebsd11, freebsd10)),
671        link_name = "rand@FBSD_1.0"
672    )]
673    pub fn rand() -> c_int;
674    #[cfg_attr(
675        all(target_os = "freebsd", any(freebsd12, freebsd11, freebsd10)),
676        link_name = "srand@FBSD_1.0"
677    )]
678    pub fn srand(seed: c_uint);
679
680    pub fn getifaddrs(ifap: *mut *mut crate::ifaddrs) -> c_int;
681    pub fn freeifaddrs(ifa: *mut crate::ifaddrs);
682    pub fn setgroups(ngroups: c_int, ptr: *const crate::gid_t) -> c_int;
683    pub fn setlogin(name: *const c_char) -> c_int;
684    pub fn ioctl(fd: c_int, request: c_ulong, ...) -> c_int;
685    pub fn kqueue() -> c_int;
686    pub fn unmount(target: *const c_char, arg: c_int) -> c_int;
687    pub fn syscall(num: c_int, ...) -> c_int;
688    #[cfg_attr(target_os = "netbsd", link_name = "__getpwent50")]
689    pub fn getpwent() -> *mut passwd;
690    pub fn setpwent();
691    pub fn endpwent();
692    pub fn endgrent();
693    pub fn getgrent() -> *mut crate::group;
694
695    pub fn getprogname() -> *const c_char;
696    pub fn setprogname(name: *const c_char);
697    pub fn getloadavg(loadavg: *mut c_double, nelem: c_int) -> c_int;
698    pub fn if_nameindex() -> *mut if_nameindex;
699    pub fn if_freenameindex(ptr: *mut if_nameindex);
700
701    pub fn getpeereid(socket: c_int, euid: *mut crate::uid_t, egid: *mut crate::gid_t) -> c_int;
702
703    #[cfg_attr(
704        all(target_os = "macos", not(target_arch = "aarch64")),
705        link_name = "glob$INODE64"
706    )]
707    #[cfg_attr(target_os = "netbsd", link_name = "__glob30")]
708    #[cfg_attr(
709        all(target_os = "freebsd", any(freebsd11, freebsd10)),
710        link_name = "glob@FBSD_1.0"
711    )]
712    pub fn glob(
713        pattern: *const c_char,
714        flags: c_int,
715        errfunc: Option<extern "C" fn(epath: *const c_char, errno: c_int) -> c_int>,
716        pglob: *mut crate::glob_t,
717    ) -> c_int;
718    #[cfg_attr(target_os = "netbsd", link_name = "__globfree30")]
719    #[cfg_attr(
720        all(target_os = "freebsd", any(freebsd11, freebsd10)),
721        link_name = "globfree@FBSD_1.0"
722    )]
723    pub fn globfree(pglob: *mut crate::glob_t);
724
725    pub fn posix_madvise(addr: *mut c_void, len: size_t, advice: c_int) -> c_int;
726
727    pub fn shm_unlink(name: *const c_char) -> c_int;
728
729    #[cfg_attr(
730        all(target_os = "macos", target_arch = "x86_64"),
731        link_name = "seekdir$INODE64"
732    )]
733    #[cfg_attr(
734        all(target_os = "macos", target_arch = "x86"),
735        link_name = "seekdir$INODE64$UNIX2003"
736    )]
737    pub fn seekdir(dirp: *mut crate::DIR, loc: c_long);
738
739    #[cfg_attr(
740        all(target_os = "macos", target_arch = "x86_64"),
741        link_name = "telldir$INODE64"
742    )]
743    #[cfg_attr(
744        all(target_os = "macos", target_arch = "x86"),
745        link_name = "telldir$INODE64$UNIX2003"
746    )]
747    pub fn telldir(dirp: *mut crate::DIR) -> c_long;
748    pub fn madvise(addr: *mut c_void, len: size_t, advice: c_int) -> c_int;
749
750    #[cfg_attr(
751        all(target_os = "macos", target_arch = "x86"),
752        link_name = "msync$UNIX2003"
753    )]
754    #[cfg_attr(target_os = "netbsd", link_name = "__msync13")]
755    pub fn msync(addr: *mut c_void, len: size_t, flags: c_int) -> c_int;
756
757    #[cfg_attr(
758        all(target_os = "macos", target_arch = "x86"),
759        link_name = "recvfrom$UNIX2003"
760    )]
761    pub fn recvfrom(
762        socket: c_int,
763        buf: *mut c_void,
764        len: size_t,
765        flags: c_int,
766        addr: *mut crate::sockaddr,
767        addrlen: *mut crate::socklen_t,
768    ) -> ssize_t;
769    pub fn mkstemps(template: *mut c_char, suffixlen: c_int) -> c_int;
770    #[cfg_attr(target_os = "netbsd", link_name = "__futimes50")]
771    pub fn futimes(fd: c_int, times: *const crate::timeval) -> c_int;
772    pub fn nl_langinfo(item: crate::nl_item) -> *mut c_char;
773
774    #[cfg_attr(
775        all(target_os = "macos", target_arch = "x86"),
776        link_name = "bind$UNIX2003"
777    )]
778    pub fn bind(
779        socket: c_int,
780        address: *const crate::sockaddr,
781        address_len: crate::socklen_t,
782    ) -> c_int;
783
784    #[cfg_attr(
785        all(target_os = "macos", target_arch = "x86"),
786        link_name = "writev$UNIX2003"
787    )]
788    pub fn writev(fd: c_int, iov: *const crate::iovec, iovcnt: c_int) -> ssize_t;
789    #[cfg_attr(
790        all(target_os = "macos", target_arch = "x86"),
791        link_name = "readv$UNIX2003"
792    )]
793    pub fn readv(fd: c_int, iov: *const crate::iovec, iovcnt: c_int) -> ssize_t;
794
795    #[cfg_attr(
796        all(target_os = "macos", target_arch = "x86"),
797        link_name = "sendmsg$UNIX2003"
798    )]
799    pub fn sendmsg(fd: c_int, msg: *const crate::msghdr, flags: c_int) -> ssize_t;
800    #[cfg_attr(
801        all(target_os = "macos", target_arch = "x86"),
802        link_name = "recvmsg$UNIX2003"
803    )]
804    pub fn recvmsg(fd: c_int, msg: *mut crate::msghdr, flags: c_int) -> ssize_t;
805
806    pub fn sync();
807    pub fn getgrgid_r(
808        gid: crate::gid_t,
809        grp: *mut crate::group,
810        buf: *mut c_char,
811        buflen: size_t,
812        result: *mut *mut crate::group,
813    ) -> c_int;
814    #[cfg_attr(
815        all(target_os = "macos", target_arch = "x86"),
816        link_name = "sigaltstack$UNIX2003"
817    )]
818    #[cfg_attr(target_os = "netbsd", link_name = "__sigaltstack14")]
819    pub fn sigaltstack(ss: *const stack_t, oss: *mut stack_t) -> c_int;
820    pub fn sigsuspend(mask: *const crate::sigset_t) -> c_int;
821    pub fn sem_close(sem: *mut sem_t) -> c_int;
822    pub fn getdtablesize() -> c_int;
823    pub fn getgrnam_r(
824        name: *const c_char,
825        grp: *mut crate::group,
826        buf: *mut c_char,
827        buflen: size_t,
828        result: *mut *mut crate::group,
829    ) -> c_int;
830    #[cfg_attr(
831        all(target_os = "macos", target_arch = "x86"),
832        link_name = "pthread_sigmask$UNIX2003"
833    )]
834    pub fn pthread_sigmask(how: c_int, set: *const sigset_t, oldset: *mut sigset_t) -> c_int;
835    pub fn sem_open(name: *const c_char, oflag: c_int, ...) -> *mut sem_t;
836    pub fn getgrnam(name: *const c_char) -> *mut crate::group;
837    #[cfg_attr(
838        all(target_os = "macos", target_arch = "x86"),
839        link_name = "pthread_cancel$UNIX2003"
840    )]
841    pub fn pthread_cancel(thread: crate::pthread_t) -> c_int;
842    pub fn pthread_kill(thread: crate::pthread_t, sig: c_int) -> c_int;
843    pub fn sched_get_priority_min(policy: c_int) -> c_int;
844    pub fn sched_get_priority_max(policy: c_int) -> c_int;
845    pub fn sem_unlink(name: *const c_char) -> c_int;
846    #[cfg_attr(target_os = "netbsd", link_name = "__getpwnam_r50")]
847    pub fn getpwnam_r(
848        name: *const c_char,
849        pwd: *mut passwd,
850        buf: *mut c_char,
851        buflen: size_t,
852        result: *mut *mut passwd,
853    ) -> c_int;
854    #[cfg_attr(target_os = "netbsd", link_name = "__getpwuid_r50")]
855    pub fn getpwuid_r(
856        uid: crate::uid_t,
857        pwd: *mut passwd,
858        buf: *mut c_char,
859        buflen: size_t,
860        result: *mut *mut passwd,
861    ) -> c_int;
862    #[cfg_attr(
863        all(target_os = "macos", target_arch = "x86"),
864        link_name = "sigwait$UNIX2003"
865    )]
866    pub fn sigwait(set: *const sigset_t, sig: *mut c_int) -> c_int;
867    pub fn pthread_atfork(
868        prepare: Option<unsafe extern "C" fn()>,
869        parent: Option<unsafe extern "C" fn()>,
870        child: Option<unsafe extern "C" fn()>,
871    ) -> c_int;
872    pub fn getgrgid(gid: crate::gid_t) -> *mut crate::group;
873    #[cfg_attr(
874        all(target_os = "macos", target_arch = "x86"),
875        link_name = "popen$UNIX2003"
876    )]
877    pub fn popen(command: *const c_char, mode: *const c_char) -> *mut crate::FILE;
878    pub fn faccessat(dirfd: c_int, pathname: *const c_char, mode: c_int, flags: c_int) -> c_int;
879    pub fn pthread_create(
880        native: *mut crate::pthread_t,
881        attr: *const crate::pthread_attr_t,
882        f: extern "C" fn(*mut c_void) -> *mut c_void,
883        value: *mut c_void,
884    ) -> c_int;
885    pub fn acct(filename: *const c_char) -> c_int;
886    #[cfg_attr(
887        all(target_os = "macos", target_arch = "x86"),
888        link_name = "wait4$UNIX2003"
889    )]
890    #[cfg_attr(
891        all(target_os = "freebsd", any(freebsd12, freebsd11, freebsd10)),
892        link_name = "wait4@FBSD_1.0"
893    )]
894    pub fn wait4(
895        pid: crate::pid_t,
896        status: *mut c_int,
897        options: c_int,
898        rusage: *mut crate::rusage,
899    ) -> crate::pid_t;
900    #[cfg_attr(
901        all(target_os = "macos", target_arch = "x86"),
902        link_name = "getitimer$UNIX2003"
903    )]
904    pub fn getitimer(which: c_int, curr_value: *mut crate::itimerval) -> c_int;
905    #[cfg_attr(
906        all(target_os = "macos", target_arch = "x86"),
907        link_name = "setitimer$UNIX2003"
908    )]
909    pub fn setitimer(
910        which: c_int,
911        new_value: *const crate::itimerval,
912        old_value: *mut crate::itimerval,
913    ) -> c_int;
914
915    pub fn regcomp(preg: *mut regex_t, pattern: *const c_char, cflags: c_int) -> c_int;
916
917    pub fn regexec(
918        preg: *const regex_t,
919        input: *const c_char,
920        nmatch: size_t,
921        pmatch: *mut regmatch_t,
922        eflags: c_int,
923    ) -> c_int;
924
925    pub fn regerror(
926        errcode: c_int,
927        preg: *const regex_t,
928        errbuf: *mut c_char,
929        errbuf_size: size_t,
930    ) -> size_t;
931
932    pub fn regfree(preg: *mut regex_t);
933
934    pub fn arc4random() -> u32;
935    pub fn arc4random_buf(buf: *mut c_void, size: size_t);
936    pub fn arc4random_uniform(l: u32) -> u32;
937
938    pub fn drand48() -> c_double;
939    pub fn erand48(xseed: *mut c_ushort) -> c_double;
940    pub fn lrand48() -> c_long;
941    pub fn nrand48(xseed: *mut c_ushort) -> c_long;
942    pub fn mrand48() -> c_long;
943    pub fn jrand48(xseed: *mut c_ushort) -> c_long;
944    pub fn srand48(seed: c_long);
945    pub fn seed48(xseed: *mut c_ushort) -> *mut c_ushort;
946    pub fn lcong48(p: *mut c_ushort);
947    pub fn getopt_long(
948        argc: c_int,
949        argv: *const *mut c_char,
950        optstring: *const c_char,
951        longopts: *const option,
952        longindex: *mut c_int,
953    ) -> c_int;
954
955    pub fn strftime(
956        buf: *mut c_char,
957        maxsize: size_t,
958        format: *const c_char,
959        timeptr: *const crate::tm,
960    ) -> size_t;
961    pub fn strftime_l(
962        buf: *mut c_char,
963        maxsize: size_t,
964        format: *const c_char,
965        timeptr: *const crate::tm,
966        locale: crate::locale_t,
967    ) -> size_t;
968}
969
970cfg_if! {
971    if #[cfg(any(
972        target_os = "macos",
973        target_os = "ios",
974        target_os = "tvos",
975        target_os = "watchos",
976        target_os = "visionos"
977    ))] {
978        mod apple;
979        pub use self::apple::*;
980    } else if #[cfg(any(target_os = "openbsd", target_os = "netbsd"))] {
981        mod netbsdlike;
982        pub use self::netbsdlike::*;
983    } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] {
984        mod freebsdlike;
985        pub use self::freebsdlike::*;
986    } else {
987        // Unknown target_os
988    }
989}