xref: /rust-libc-0.2.174/src/unix/bsd/mod.rs (revision 2aabbf03)
1 use crate::prelude::*;
2 
3 pub type off_t = i64;
4 pub type useconds_t = u32;
5 pub type blkcnt_t = i64;
6 pub type socklen_t = u32;
7 pub type sa_family_t = u8;
8 pub type pthread_t = crate::uintptr_t;
9 pub type nfds_t = c_uint;
10 pub type regoff_t = off_t;
11 
12 s! {
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 
136 s_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 
167 cfg_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(debug): .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(debug): .field("sysname", &self.sysname)
236                     // FIXME(debug): .field("nodename", &self.nodename)
237                     // FIXME(debug): .field("release", &self.release)
238                     // FIXME(debug): .field("version", &self.version)
239                     // FIXME(debug): .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 
256 pub const LC_ALL: c_int = 0;
257 pub const LC_COLLATE: c_int = 1;
258 pub const LC_CTYPE: c_int = 2;
259 pub const LC_MONETARY: c_int = 3;
260 pub const LC_NUMERIC: c_int = 4;
261 pub const LC_TIME: c_int = 5;
262 pub const LC_MESSAGES: c_int = 6;
263 
264 pub const FIOCLEX: c_ulong = 0x20006601;
265 pub const FIONCLEX: c_ulong = 0x20006602;
266 pub const FIONREAD: c_ulong = 0x4004667f;
267 pub const FIONBIO: c_ulong = 0x8004667e;
268 pub const FIOASYNC: c_ulong = 0x8004667d;
269 pub const FIOSETOWN: c_ulong = 0x8004667c;
270 pub const FIOGETOWN: c_ulong = 0x4004667b;
271 
272 pub const PATH_MAX: c_int = 1024;
273 pub const MAXPATHLEN: c_int = PATH_MAX;
274 
275 pub const IOV_MAX: c_int = 1024;
276 
277 pub const SA_ONSTACK: c_int = 0x0001;
278 pub const SA_SIGINFO: c_int = 0x0040;
279 pub const SA_RESTART: c_int = 0x0002;
280 pub const SA_RESETHAND: c_int = 0x0004;
281 pub const SA_NOCLDSTOP: c_int = 0x0008;
282 pub const SA_NODEFER: c_int = 0x0010;
283 pub const SA_NOCLDWAIT: c_int = 0x0020;
284 
285 pub const SS_ONSTACK: c_int = 1;
286 pub const SS_DISABLE: c_int = 4;
287 
288 pub const SIGCHLD: c_int = 20;
289 pub const SIGBUS: c_int = 10;
290 pub const SIGUSR1: c_int = 30;
291 pub const SIGUSR2: c_int = 31;
292 pub const SIGCONT: c_int = 19;
293 pub const SIGSTOP: c_int = 17;
294 pub const SIGTSTP: c_int = 18;
295 pub const SIGURG: c_int = 16;
296 pub const SIGIO: c_int = 23;
297 pub const SIGSYS: c_int = 12;
298 pub const SIGTTIN: c_int = 21;
299 pub const SIGTTOU: c_int = 22;
300 pub const SIGXCPU: c_int = 24;
301 pub const SIGXFSZ: c_int = 25;
302 pub const SIGVTALRM: c_int = 26;
303 pub const SIGPROF: c_int = 27;
304 pub const SIGWINCH: c_int = 28;
305 pub const SIGINFO: c_int = 29;
306 
307 pub const SIG_SETMASK: c_int = 3;
308 pub const SIG_BLOCK: c_int = 0x1;
309 pub const SIG_UNBLOCK: c_int = 0x2;
310 
311 pub const IP_TOS: c_int = 3;
312 pub const IP_MULTICAST_IF: c_int = 9;
313 pub const IP_MULTICAST_TTL: c_int = 10;
314 pub const IP_MULTICAST_LOOP: c_int = 11;
315 
316 pub const IPV6_UNICAST_HOPS: c_int = 4;
317 pub const IPV6_MULTICAST_IF: c_int = 9;
318 pub const IPV6_MULTICAST_HOPS: c_int = 10;
319 pub const IPV6_MULTICAST_LOOP: c_int = 11;
320 pub const IPV6_V6ONLY: c_int = 27;
321 pub const IPV6_DONTFRAG: c_int = 62;
322 
323 pub const IPTOS_ECN_NOTECT: u8 = 0x00;
324 pub const IPTOS_ECN_MASK: u8 = 0x03;
325 pub const IPTOS_ECN_ECT1: u8 = 0x01;
326 pub const IPTOS_ECN_ECT0: u8 = 0x02;
327 pub const IPTOS_ECN_CE: u8 = 0x03;
328 
329 pub const ST_RDONLY: c_ulong = 1;
330 
331 pub const SCM_RIGHTS: c_int = 0x01;
332 
333 pub const NCCS: usize = 20;
334 
335 pub const O_ACCMODE: c_int = 0x3;
336 pub const O_RDONLY: c_int = 0;
337 pub const O_WRONLY: c_int = 1;
338 pub const O_RDWR: c_int = 2;
339 pub const O_APPEND: c_int = 8;
340 pub const O_CREAT: c_int = 512;
341 pub const O_TRUNC: c_int = 1024;
342 pub const O_EXCL: c_int = 2048;
343 pub const O_ASYNC: c_int = 0x40;
344 pub const O_SYNC: c_int = 0x80;
345 pub const O_NONBLOCK: c_int = 0x4;
346 pub const O_NOFOLLOW: c_int = 0x100;
347 pub const O_SHLOCK: c_int = 0x10;
348 pub const O_EXLOCK: c_int = 0x20;
349 pub const O_FSYNC: c_int = O_SYNC;
350 pub const O_NDELAY: c_int = O_NONBLOCK;
351 
352 pub const F_GETOWN: c_int = 5;
353 pub const F_SETOWN: c_int = 6;
354 
355 pub const F_RDLCK: c_short = 1;
356 pub const F_UNLCK: c_short = 2;
357 pub const F_WRLCK: c_short = 3;
358 
359 pub const MNT_RDONLY: c_int = 0x00000001;
360 pub const MNT_SYNCHRONOUS: c_int = 0x00000002;
361 pub const MNT_NOEXEC: c_int = 0x00000004;
362 pub const MNT_NOSUID: c_int = 0x00000008;
363 pub const MNT_ASYNC: c_int = 0x00000040;
364 pub const MNT_EXPORTED: c_int = 0x00000100;
365 pub const MNT_UPDATE: c_int = 0x00010000;
366 pub const MNT_RELOAD: c_int = 0x00040000;
367 pub const MNT_FORCE: c_int = 0x00080000;
368 
369 pub const Q_SYNC: c_int = 0x600;
370 pub const Q_QUOTAON: c_int = 0x100;
371 pub const Q_QUOTAOFF: c_int = 0x200;
372 
373 pub const TCIOFF: c_int = 3;
374 pub const TCION: c_int = 4;
375 pub const TCOOFF: c_int = 1;
376 pub const TCOON: c_int = 2;
377 pub const TCIFLUSH: c_int = 1;
378 pub const TCOFLUSH: c_int = 2;
379 pub const TCIOFLUSH: c_int = 3;
380 pub const TCSANOW: c_int = 0;
381 pub const TCSADRAIN: c_int = 1;
382 pub const TCSAFLUSH: c_int = 2;
383 pub const VEOF: usize = 0;
384 pub const VEOL: usize = 1;
385 pub const VEOL2: usize = 2;
386 pub const VERASE: usize = 3;
387 pub const VWERASE: usize = 4;
388 pub const VKILL: usize = 5;
389 pub const VREPRINT: usize = 6;
390 pub const VINTR: usize = 8;
391 pub const VQUIT: usize = 9;
392 pub const VSUSP: usize = 10;
393 pub const VDSUSP: usize = 11;
394 pub const VSTART: usize = 12;
395 pub const VSTOP: usize = 13;
396 pub const VLNEXT: usize = 14;
397 pub const VDISCARD: usize = 15;
398 pub const VMIN: usize = 16;
399 pub const VTIME: usize = 17;
400 pub const VSTATUS: usize = 18;
401 pub const _POSIX_VDISABLE: crate::cc_t = 0xff;
402 pub const IGNBRK: crate::tcflag_t = 0x00000001;
403 pub const BRKINT: crate::tcflag_t = 0x00000002;
404 pub const IGNPAR: crate::tcflag_t = 0x00000004;
405 pub const PARMRK: crate::tcflag_t = 0x00000008;
406 pub const INPCK: crate::tcflag_t = 0x00000010;
407 pub const ISTRIP: crate::tcflag_t = 0x00000020;
408 pub const INLCR: crate::tcflag_t = 0x00000040;
409 pub const IGNCR: crate::tcflag_t = 0x00000080;
410 pub const ICRNL: crate::tcflag_t = 0x00000100;
411 pub const IXON: crate::tcflag_t = 0x00000200;
412 pub const IXOFF: crate::tcflag_t = 0x00000400;
413 pub const IXANY: crate::tcflag_t = 0x00000800;
414 pub const IMAXBEL: crate::tcflag_t = 0x00002000;
415 pub const OPOST: crate::tcflag_t = 0x1;
416 pub const ONLCR: crate::tcflag_t = 0x2;
417 pub const OXTABS: crate::tcflag_t = 0x4;
418 pub const ONOEOT: crate::tcflag_t = 0x8;
419 pub const CIGNORE: crate::tcflag_t = 0x00000001;
420 pub const CSIZE: crate::tcflag_t = 0x00000300;
421 pub const CS5: crate::tcflag_t = 0x00000000;
422 pub const CS6: crate::tcflag_t = 0x00000100;
423 pub const CS7: crate::tcflag_t = 0x00000200;
424 pub const CS8: crate::tcflag_t = 0x00000300;
425 pub const CSTOPB: crate::tcflag_t = 0x00000400;
426 pub const CREAD: crate::tcflag_t = 0x00000800;
427 pub const PARENB: crate::tcflag_t = 0x00001000;
428 pub const PARODD: crate::tcflag_t = 0x00002000;
429 pub const HUPCL: crate::tcflag_t = 0x00004000;
430 pub const CLOCAL: crate::tcflag_t = 0x00008000;
431 pub const ECHOKE: crate::tcflag_t = 0x00000001;
432 pub const ECHOE: crate::tcflag_t = 0x00000002;
433 pub const ECHOK: crate::tcflag_t = 0x00000004;
434 pub const ECHO: crate::tcflag_t = 0x00000008;
435 pub const ECHONL: crate::tcflag_t = 0x00000010;
436 pub const ECHOPRT: crate::tcflag_t = 0x00000020;
437 pub const ECHOCTL: crate::tcflag_t = 0x00000040;
438 pub const ISIG: crate::tcflag_t = 0x00000080;
439 pub const ICANON: crate::tcflag_t = 0x00000100;
440 pub const ALTWERASE: crate::tcflag_t = 0x00000200;
441 pub const IEXTEN: crate::tcflag_t = 0x00000400;
442 pub const EXTPROC: crate::tcflag_t = 0x00000800;
443 pub const TOSTOP: crate::tcflag_t = 0x00400000;
444 pub const FLUSHO: crate::tcflag_t = 0x00800000;
445 pub const NOKERNINFO: crate::tcflag_t = 0x02000000;
446 pub const PENDIN: crate::tcflag_t = 0x20000000;
447 pub const NOFLSH: crate::tcflag_t = 0x80000000;
448 pub const MDMBUF: crate::tcflag_t = 0x00100000;
449 
450 pub const WNOHANG: c_int = 0x00000001;
451 pub const WUNTRACED: c_int = 0x00000002;
452 
453 pub const RTLD_LAZY: c_int = 0x1;
454 pub const RTLD_NOW: c_int = 0x2;
455 pub const RTLD_NEXT: *mut c_void = -1isize as *mut c_void;
456 pub const RTLD_DEFAULT: *mut c_void = -2isize as *mut c_void;
457 pub const RTLD_SELF: *mut c_void = -3isize as *mut c_void;
458 
459 pub const LOG_CRON: c_int = 9 << 3;
460 pub const LOG_AUTHPRIV: c_int = 10 << 3;
461 pub const LOG_FTP: c_int = 11 << 3;
462 pub const LOG_PERROR: c_int = 0x20;
463 
464 pub const TCP_NODELAY: c_int = 1;
465 pub const TCP_MAXSEG: c_int = 2;
466 
467 pub const PIPE_BUF: usize = 512;
468 
469 // si_code values for SIGBUS signal
470 pub const BUS_ADRALN: c_int = 1;
471 pub const BUS_ADRERR: c_int = 2;
472 pub const BUS_OBJERR: c_int = 3;
473 
474 // si_code values for SIGCHLD signal
475 pub const CLD_EXITED: c_int = 1;
476 pub const CLD_KILLED: c_int = 2;
477 pub const CLD_DUMPED: c_int = 3;
478 pub const CLD_TRAPPED: c_int = 4;
479 pub const CLD_STOPPED: c_int = 5;
480 pub const CLD_CONTINUED: c_int = 6;
481 
482 pub const POLLIN: c_short = 0x1;
483 pub const POLLPRI: c_short = 0x2;
484 pub const POLLOUT: c_short = 0x4;
485 pub const POLLERR: c_short = 0x8;
486 pub const POLLHUP: c_short = 0x10;
487 pub const POLLNVAL: c_short = 0x20;
488 pub const POLLRDNORM: c_short = 0x040;
489 pub const POLLWRNORM: c_short = 0x004;
490 pub const POLLRDBAND: c_short = 0x080;
491 pub const POLLWRBAND: c_short = 0x100;
492 
493 pub const BIOCGBLEN: c_ulong = 0x40044266;
494 pub const BIOCSBLEN: c_ulong = 0xc0044266;
495 pub const BIOCFLUSH: c_uint = 0x20004268;
496 pub const BIOCPROMISC: c_uint = 0x20004269;
497 pub const BIOCGDLT: c_ulong = 0x4004426a;
498 pub const BIOCGETIF: c_ulong = 0x4020426b;
499 pub const BIOCSETIF: c_ulong = 0x8020426c;
500 pub const BIOCGSTATS: c_ulong = 0x4008426f;
501 pub const BIOCIMMEDIATE: c_ulong = 0x80044270;
502 pub const BIOCVERSION: c_ulong = 0x40044271;
503 pub const BIOCGHDRCMPLT: c_ulong = 0x40044274;
504 pub const BIOCSHDRCMPLT: c_ulong = 0x80044275;
505 pub const SIOCGIFADDR: c_ulong = 0xc0206921;
506 
507 pub const REG_BASIC: c_int = 0o0000;
508 pub const REG_EXTENDED: c_int = 0o0001;
509 pub const REG_ICASE: c_int = 0o0002;
510 pub const REG_NOSUB: c_int = 0o0004;
511 pub const REG_NEWLINE: c_int = 0o0010;
512 pub const REG_NOSPEC: c_int = 0o0020;
513 pub const REG_PEND: c_int = 0o0040;
514 pub const REG_DUMP: c_int = 0o0200;
515 
516 pub const REG_NOMATCH: c_int = 1;
517 pub const REG_BADPAT: c_int = 2;
518 pub const REG_ECOLLATE: c_int = 3;
519 pub const REG_ECTYPE: c_int = 4;
520 pub const REG_EESCAPE: c_int = 5;
521 pub const REG_ESUBREG: c_int = 6;
522 pub const REG_EBRACK: c_int = 7;
523 pub const REG_EPAREN: c_int = 8;
524 pub const REG_EBRACE: c_int = 9;
525 pub const REG_BADBR: c_int = 10;
526 pub const REG_ERANGE: c_int = 11;
527 pub const REG_ESPACE: c_int = 12;
528 pub const REG_BADRPT: c_int = 13;
529 pub const REG_EMPTY: c_int = 14;
530 pub const REG_ASSERT: c_int = 15;
531 pub const REG_INVARG: c_int = 16;
532 pub const REG_ATOI: c_int = 255;
533 pub const REG_ITOA: c_int = 0o0400;
534 
535 pub const REG_NOTBOL: c_int = 0o00001;
536 pub const REG_NOTEOL: c_int = 0o00002;
537 pub const REG_STARTEND: c_int = 0o00004;
538 pub const REG_TRACE: c_int = 0o00400;
539 pub const REG_LARGE: c_int = 0o01000;
540 pub const REG_BACKR: c_int = 0o02000;
541 
542 pub const TIOCCBRK: c_uint = 0x2000747a;
543 pub const TIOCSBRK: c_uint = 0x2000747b;
544 
545 pub const PRIO_PROCESS: c_int = 0;
546 pub const PRIO_PGRP: c_int = 1;
547 pub const PRIO_USER: c_int = 2;
548 
549 pub const ITIMER_REAL: c_int = 0;
550 pub const ITIMER_VIRTUAL: c_int = 1;
551 pub const ITIMER_PROF: c_int = 2;
552 
553 // net/route.h
554 
555 pub const RTF_UP: c_int = 0x1;
556 pub const RTF_GATEWAY: c_int = 0x2;
557 pub const RTF_HOST: c_int = 0x4;
558 pub const RTF_REJECT: c_int = 0x8;
559 pub const RTF_DYNAMIC: c_int = 0x10;
560 pub const RTF_MODIFIED: c_int = 0x20;
561 pub const RTF_DONE: c_int = 0x40;
562 pub const RTF_STATIC: c_int = 0x800;
563 pub const RTF_BLACKHOLE: c_int = 0x1000;
564 pub const RTF_PROTO2: c_int = 0x4000;
565 pub const RTF_PROTO1: c_int = 0x8000;
566 
567 // Message types
568 pub const RTM_ADD: c_int = 0x1;
569 pub const RTM_DELETE: c_int = 0x2;
570 pub const RTM_CHANGE: c_int = 0x3;
571 pub const RTM_GET: c_int = 0x4;
572 pub const RTM_LOSING: c_int = 0x5;
573 pub const RTM_REDIRECT: c_int = 0x6;
574 pub const RTM_MISS: c_int = 0x7;
575 
576 // Bitmask values for rtm_addrs.
577 pub const RTA_DST: c_int = 0x1;
578 pub const RTA_GATEWAY: c_int = 0x2;
579 pub const RTA_NETMASK: c_int = 0x4;
580 pub const RTA_GENMASK: c_int = 0x8;
581 pub const RTA_IFP: c_int = 0x10;
582 pub const RTA_IFA: c_int = 0x20;
583 pub const RTA_AUTHOR: c_int = 0x40;
584 pub const RTA_BRD: c_int = 0x80;
585 
586 // Index offsets for sockaddr array for alternate internal encoding.
587 pub const RTAX_DST: c_int = 0;
588 pub const RTAX_GATEWAY: c_int = 1;
589 pub const RTAX_NETMASK: c_int = 2;
590 pub const RTAX_GENMASK: c_int = 3;
591 pub const RTAX_IFP: c_int = 4;
592 pub const RTAX_IFA: c_int = 5;
593 pub const RTAX_AUTHOR: c_int = 6;
594 pub const RTAX_BRD: c_int = 7;
595 
596 f! {
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 
632 safe_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) & 0x00ff
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 
654 extern "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     pub fn devname(dev: crate::dev_t, mode_t: crate::mode_t) -> *mut c_char;
970 }
971 
972 cfg_if! {
973     if #[cfg(any(
974         target_os = "macos",
975         target_os = "ios",
976         target_os = "tvos",
977         target_os = "watchos",
978         target_os = "visionos"
979     ))] {
980         mod apple;
981         pub use self::apple::*;
982     } else if #[cfg(any(target_os = "openbsd", target_os = "netbsd"))] {
983         mod netbsdlike;
984         pub use self::netbsdlike::*;
985     } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] {
986         mod freebsdlike;
987         pub use self::freebsdlike::*;
988     } else {
989         // Unknown target_os
990     }
991 }
992