libc_interface/unix/bsd/
mod.rs

1use dox::{mem, Option};
2
3pub type wchar_t = i32;
4pub type off_t = i64;
5pub type useconds_t = u32;
6pub type blkcnt_t = i64;
7pub type socklen_t = u32;
8pub type sa_family_t = u8;
9pub type pthread_t = ::uintptr_t;
10pub type nfds_t = ::c_uint;
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: ::in_port_t,
23        pub sin6_flowinfo: u32,
24        pub sin6_addr: ::in6_addr,
25        pub sin6_scope_id: u32,
26    }
27
28    pub struct sockaddr_un {
29        pub sun_len: u8,
30        pub sun_family: sa_family_t,
31        pub sun_path: [c_char; 104]
32    }
33
34    pub struct passwd {
35        pub pw_name: *mut ::c_char,
36        pub pw_passwd: *mut ::c_char,
37        pub pw_uid: ::uid_t,
38        pub pw_gid: ::gid_t,
39        pub pw_change: ::time_t,
40        pub pw_class: *mut ::c_char,
41        pub pw_gecos: *mut ::c_char,
42        pub pw_dir: *mut ::c_char,
43        pub pw_shell: *mut ::c_char,
44        pub pw_expire: ::time_t,
45
46        #[cfg(not(any(target_os = "macos",
47                      target_os = "ios",
48                      target_os = "netbsd",
49                      target_os = "openbsd")))]
50        pub pw_fields: ::c_int,
51    }
52
53    pub struct ifaddrs {
54        pub ifa_next: *mut ifaddrs,
55        pub ifa_name: *mut ::c_char,
56        pub ifa_flags: ::c_uint,
57        pub ifa_addr: *mut ::sockaddr,
58        pub ifa_netmask: *mut ::sockaddr,
59        pub ifa_dstaddr: *mut ::sockaddr,
60        pub ifa_data: *mut ::c_void
61    }
62
63    pub struct fd_set {
64        #[cfg(all(target_pointer_width = "64",
65                  any(target_os = "freebsd", target_os = "dragonfly")))]
66        fds_bits: [i64; FD_SETSIZE / 64],
67        #[cfg(not(all(target_pointer_width = "64",
68                      any(target_os = "freebsd", target_os = "dragonfly"))))]
69        fds_bits: [i32; FD_SETSIZE / 32],
70    }
71
72    pub struct tm {
73        pub tm_sec: ::c_int,
74        pub tm_min: ::c_int,
75        pub tm_hour: ::c_int,
76        pub tm_mday: ::c_int,
77        pub tm_mon: ::c_int,
78        pub tm_year: ::c_int,
79        pub tm_wday: ::c_int,
80        pub tm_yday: ::c_int,
81        pub tm_isdst: ::c_int,
82        pub tm_gmtoff: ::c_long,
83        pub tm_zone: *mut ::c_char,
84    }
85
86    pub struct utsname {
87        #[cfg(not(target_os = "dragonfly"))]
88        pub sysname: [::c_char; 256],
89        #[cfg(target_os = "dragonfly")]
90        pub sysname: [::c_char; 32],
91        #[cfg(not(target_os = "dragonfly"))]
92        pub nodename: [::c_char; 256],
93        #[cfg(target_os = "dragonfly")]
94        pub nodename: [::c_char; 32],
95        #[cfg(not(target_os = "dragonfly"))]
96        pub release: [::c_char; 256],
97        #[cfg(target_os = "dragonfly")]
98        pub release: [::c_char; 32],
99        #[cfg(not(target_os = "dragonfly"))]
100        pub version: [::c_char; 256],
101        #[cfg(target_os = "dragonfly")]
102        pub version: [::c_char; 32],
103        #[cfg(not(target_os = "dragonfly"))]
104        pub machine: [::c_char; 256],
105        #[cfg(target_os = "dragonfly")]
106        pub machine: [::c_char; 32],
107    }
108
109    pub struct msghdr {
110        pub msg_name: *mut ::c_void,
111        pub msg_namelen: ::socklen_t,
112        pub msg_iov: *mut ::iovec,
113        pub msg_iovlen: ::c_int,
114        pub msg_control: *mut ::c_void,
115        pub msg_controllen: ::socklen_t,
116        pub msg_flags: ::c_int,
117    }
118
119    pub struct cmsghdr {
120        pub cmsg_len: ::socklen_t,
121        pub cmsg_level: ::c_int,
122        pub cmsg_type: ::c_int,
123    }
124
125    pub struct fsid_t {
126        __fsid_val: [::int32_t; 2],
127    }
128
129    pub struct if_nameindex {
130        pub if_index: ::c_uint,
131        pub if_name: *mut ::c_char,
132    }
133}
134
135pub const LC_ALL: ::c_int = 0;
136pub const LC_COLLATE: ::c_int = 1;
137pub const LC_CTYPE: ::c_int = 2;
138pub const LC_MONETARY: ::c_int = 3;
139pub const LC_NUMERIC: ::c_int = 4;
140pub const LC_TIME: ::c_int = 5;
141pub const LC_MESSAGES: ::c_int = 6;
142
143pub const FIOCLEX: ::c_ulong = 0x20006601;
144pub const FIONBIO: ::c_ulong = 0x8004667e;
145
146pub const PATH_MAX: ::c_int = 1024;
147
148pub const SA_ONSTACK: ::c_int = 0x0001;
149pub const SA_SIGINFO: ::c_int = 0x0040;
150pub const SA_RESTART: ::c_int = 0x0002;
151pub const SA_RESETHAND: ::c_int = 0x0004;
152pub const SA_NOCLDSTOP: ::c_int = 0x0008;
153pub const SA_NODEFER: ::c_int = 0x0010;
154pub const SA_NOCLDWAIT: ::c_int = 0x0020;
155
156pub const SS_ONSTACK: ::c_int = 1;
157pub const SS_DISABLE: ::c_int = 4;
158
159pub const SIGCHLD: ::c_int = 20;
160pub const SIGBUS: ::c_int = 10;
161pub const SIGUSR1: ::c_int = 30;
162pub const SIGUSR2: ::c_int = 31;
163pub const SIGCONT: ::c_int = 19;
164pub const SIGSTOP: ::c_int = 17;
165pub const SIGTSTP: ::c_int = 18;
166pub const SIGURG: ::c_int = 16;
167pub const SIGIO: ::c_int = 23;
168pub const SIGSYS: ::c_int = 12;
169pub const SIGTTIN: ::c_int = 21;
170pub const SIGTTOU: ::c_int = 22;
171pub const SIGXCPU: ::c_int = 24;
172pub const SIGXFSZ: ::c_int = 25;
173pub const SIGVTALRM: ::c_int = 26;
174pub const SIGPROF: ::c_int = 27;
175pub const SIGWINCH: ::c_int = 28;
176pub const SIGINFO: ::c_int = 29;
177
178pub const SIG_SETMASK: ::c_int = 3;
179pub const SIG_BLOCK: ::c_int = 0x1;
180pub const SIG_UNBLOCK: ::c_int = 0x2;
181
182pub const IP_MULTICAST_IF: ::c_int = 9;
183pub const IP_MULTICAST_TTL: ::c_int = 10;
184pub const IP_MULTICAST_LOOP: ::c_int = 11;
185
186pub const IPV6_UNICAST_HOPS: ::c_int = 4;
187pub const IPV6_MULTICAST_IF: ::c_int = 9;
188pub const IPV6_MULTICAST_HOPS: ::c_int = 10;
189pub const IPV6_MULTICAST_LOOP: ::c_int = 11;
190pub const IPV6_V6ONLY: ::c_int = 27;
191
192pub const ST_RDONLY: ::c_ulong = 1;
193
194pub const SCM_RIGHTS: ::c_int = 0x01;
195
196pub const NCCS: usize = 20;
197
198pub const O_ACCMODE: ::c_int = 0x3;
199pub const O_RDONLY: ::c_int = 0;
200pub const O_WRONLY: ::c_int = 1;
201pub const O_RDWR: ::c_int = 2;
202pub const O_APPEND: ::c_int = 8;
203pub const O_CREAT: ::c_int = 512;
204pub const O_TRUNC: ::c_int = 1024;
205pub const O_EXCL: ::c_int = 2048;
206pub const O_ASYNC: ::c_int = 0x40;
207pub const O_SYNC: ::c_int = 0x80;
208pub const O_NONBLOCK: ::c_int = 0x4;
209pub const O_NOFOLLOW: ::c_int = 0x100;
210pub const O_SHLOCK: ::c_int = 0x10;
211pub const O_EXLOCK: ::c_int = 0x20;
212pub const O_FSYNC: ::c_int = O_SYNC;
213pub const O_NDELAY: ::c_int = O_NONBLOCK;
214
215pub const F_GETOWN: ::c_int = 5;
216pub const F_SETOWN: ::c_int = 6;
217
218pub const MNT_FORCE: ::c_int = 0x80000;
219
220pub const Q_SYNC: ::c_int = 0x600;
221pub const Q_QUOTAON: ::c_int = 0x100;
222pub const Q_QUOTAOFF: ::c_int = 0x200;
223
224pub const TCIOFF: ::c_int = 3;
225pub const TCION: ::c_int = 4;
226pub const TCOOFF: ::c_int = 1;
227pub const TCOON: ::c_int = 2;
228pub const TCIFLUSH: ::c_int = 1;
229pub const TCOFLUSH: ::c_int = 2;
230pub const TCIOFLUSH: ::c_int = 3;
231pub const TCSANOW: ::c_int = 0;
232pub const TCSADRAIN: ::c_int = 1;
233pub const TCSAFLUSH: ::c_int = 2;
234pub const VEOF: usize = 0;
235pub const VEOL: usize = 1;
236pub const VEOL2: usize = 2;
237pub const VERASE: usize = 3;
238pub const VWERASE: usize = 4;
239pub const VKILL: usize = 5;
240pub const VREPRINT: usize = 6;
241pub const VINTR: usize = 8;
242pub const VQUIT: usize = 9;
243pub const VSUSP: usize = 10;
244pub const VDSUSP: usize = 11;
245pub const VSTART: usize = 12;
246pub const VSTOP: usize = 13;
247pub const VLNEXT: usize = 14;
248pub const VDISCARD: usize = 15;
249pub const VMIN: usize = 16;
250pub const VTIME: usize = 17;
251pub const VSTATUS: usize = 18;
252pub const _POSIX_VDISABLE: ::cc_t = 0xff;
253pub const IGNBRK: ::tcflag_t = 0x00000001;
254pub const BRKINT: ::tcflag_t = 0x00000002;
255pub const IGNPAR: ::tcflag_t = 0x00000004;
256pub const PARMRK: ::tcflag_t = 0x00000008;
257pub const INPCK: ::tcflag_t = 0x00000010;
258pub const ISTRIP: ::tcflag_t = 0x00000020;
259pub const INLCR: ::tcflag_t = 0x00000040;
260pub const IGNCR: ::tcflag_t = 0x00000080;
261pub const ICRNL: ::tcflag_t = 0x00000100;
262pub const IXON: ::tcflag_t = 0x00000200;
263pub const IXOFF: ::tcflag_t = 0x00000400;
264pub const IXANY: ::tcflag_t = 0x00000800;
265pub const IMAXBEL: ::tcflag_t = 0x00002000;
266pub const OPOST: ::tcflag_t = 0x1;
267pub const ONLCR: ::tcflag_t = 0x2;
268pub const OXTABS: ::tcflag_t = 0x4;
269pub const ONOEOT: ::tcflag_t = 0x8;
270pub const CIGNORE: ::tcflag_t = 0x00000001;
271pub const CSIZE: ::tcflag_t = 0x00000300;
272pub const CS5: ::tcflag_t = 0x00000000;
273pub const CS6: ::tcflag_t = 0x00000100;
274pub const CS7: ::tcflag_t = 0x00000200;
275pub const CS8: ::tcflag_t = 0x00000300;
276pub const CSTOPB: ::tcflag_t = 0x00000400;
277pub const CREAD: ::tcflag_t = 0x00000800;
278pub const PARENB: ::tcflag_t = 0x00001000;
279pub const PARODD: ::tcflag_t = 0x00002000;
280pub const HUPCL: ::tcflag_t = 0x00004000;
281pub const CLOCAL: ::tcflag_t = 0x00008000;
282pub const ECHOKE: ::tcflag_t = 0x00000001;
283pub const ECHOE: ::tcflag_t = 0x00000002;
284pub const ECHOK: ::tcflag_t = 0x00000004;
285pub const ECHO: ::tcflag_t = 0x00000008;
286pub const ECHONL: ::tcflag_t = 0x00000010;
287pub const ECHOPRT: ::tcflag_t = 0x00000020;
288pub const ECHOCTL: ::tcflag_t = 0x00000040;
289pub const ISIG: ::tcflag_t = 0x00000080;
290pub const ICANON: ::tcflag_t = 0x00000100;
291pub const ALTWERASE: ::tcflag_t = 0x00000200;
292pub const IEXTEN: ::tcflag_t = 0x00000400;
293pub const EXTPROC: ::tcflag_t = 0x00000800;
294pub const TOSTOP: ::tcflag_t = 0x00400000;
295pub const FLUSHO: ::tcflag_t = 0x00800000;
296pub const NOKERNINFO: ::tcflag_t = 0x02000000;
297pub const PENDIN: ::tcflag_t = 0x20000000;
298pub const NOFLSH: ::tcflag_t = 0x80000000;
299pub const MDMBUF: ::tcflag_t = 0x00100000;
300
301pub const WNOHANG: ::c_int = 0x00000001;
302pub const WUNTRACED: ::c_int = 0x00000002;
303
304pub const RTLD_LAZY: ::c_int = 0x1;
305pub const RTLD_NOW: ::c_int = 0x2;
306pub const RTLD_NEXT: *mut ::c_void = -1isize as *mut ::c_void;
307pub const RTLD_DEFAULT: *mut ::c_void = -2isize as *mut ::c_void;
308pub const RTLD_SELF: *mut ::c_void = -3isize as *mut ::c_void;
309
310pub const LOG_CRON: ::c_int = 9 << 3;
311pub const LOG_AUTHPRIV: ::c_int = 10 << 3;
312pub const LOG_FTP: ::c_int = 11 << 3;
313pub const LOG_PERROR: ::c_int = 0x20;
314
315pub const TCP_MAXSEG: ::c_int = 2;
316
317pub const PIPE_BUF: usize = 512;
318
319pub const POLLIN: ::c_short = 0x1;
320pub const POLLPRI: ::c_short = 0x2;
321pub const POLLOUT: ::c_short = 0x4;
322pub const POLLERR: ::c_short = 0x8;
323pub const POLLHUP: ::c_short = 0x10;
324pub const POLLNVAL: ::c_short = 0x20;
325pub const POLLRDNORM: ::c_short = 0x040;
326pub const POLLWRNORM: ::c_short = 0x004;
327pub const POLLRDBAND: ::c_short = 0x080;
328pub const POLLWRBAND: ::c_short = 0x100;
329
330f! {
331    pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
332        let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
333        let fd = fd as usize;
334        (*set).fds_bits[fd / bits] &= !(1 << (fd % bits));
335        return
336    }
337
338    pub fn FD_ISSET(fd: ::c_int, set: *mut fd_set) -> bool {
339        let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
340        let fd = fd as usize;
341        return ((*set).fds_bits[fd / bits] & (1 << (fd % bits))) != 0
342    }
343
344    pub fn FD_SET(fd: ::c_int, set: *mut fd_set) -> () {
345        let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
346        let fd = fd as usize;
347        (*set).fds_bits[fd / bits] |= 1 << (fd % bits);
348        return
349    }
350
351    pub fn FD_ZERO(set: *mut fd_set) -> () {
352        for slot in (*set).fds_bits.iter_mut() {
353            *slot = 0;
354        }
355    }
356
357    pub fn WTERMSIG(status: ::c_int) -> ::c_int {
358        status & 0o177
359    }
360
361    pub fn WIFEXITED(status: ::c_int) -> bool {
362        (status & 0o177) == 0
363    }
364
365    pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
366        status >> 8
367    }
368
369    pub fn WCOREDUMP(status: ::c_int) -> bool {
370        (status & 0o200) != 0
371    }
372
373    pub fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
374        (cmd << 8) | (type_ & 0x00ff)
375    }
376}
377
378cfg_if! {
379    if #[cfg(any(target_os = "macos", target_os = "ios"))] {
380        mod apple;
381        pub use self::apple::*;
382    } else if #[cfg(any(target_os = "openbsd", target_os = "netbsd",
383                        target_os = "bitrig"))] {
384        mod netbsdlike;
385        pub use self::netbsdlike::*;
386    } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] {
387        mod freebsdlike;
388        pub use self::freebsdlike::*;
389    } else {
390        // Unknown target_os
391    }
392}