Skip to main content

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