]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/c.rs
1e68eac5a67354b880b5a7e678a7ae8e67fda3ea
[rustc.git] / src / libstd / sys / unix / c.rs
1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! C definitions used by libnative that don't belong in liblibc
12
13 #![allow(dead_code)]
14 #![allow(non_camel_case_types)]
15
16 pub use self::select::fd_set;
17 pub use self::signal::{sigaction, siginfo, sigset_t};
18 pub use self::signal::{SA_ONSTACK, SA_RESTART, SA_RESETHAND, SA_NOCLDSTOP};
19 pub use self::signal::{SA_NODEFER, SA_NOCLDWAIT, SA_SIGINFO, SIGCHLD};
20
21 use libc;
22
23 #[cfg(any(target_os = "macos",
24 target_os = "ios",
25 target_os = "freebsd",
26 target_os = "dragonfly",
27 target_os = "bitrig",
28 target_os = "openbsd"))]
29 mod consts {
30 use libc;
31 pub const FIONBIO: libc::c_ulong = 0x8004667e;
32 pub const FIOCLEX: libc::c_ulong = 0x20006601;
33 pub const FIONCLEX: libc::c_ulong = 0x20006602;
34 }
35 #[cfg(any(all(target_os = "linux",
36 any(target_arch = "x86",
37 target_arch = "x86_64",
38 target_arch = "arm",
39 target_arch = "aarch64")),
40 target_os = "android"))]
41 mod consts {
42 use libc;
43 pub const FIONBIO: libc::c_ulong = 0x5421;
44 pub const FIOCLEX: libc::c_ulong = 0x5451;
45 pub const FIONCLEX: libc::c_ulong = 0x5450;
46 }
47 #[cfg(all(target_os = "linux",
48 any(target_arch = "mips",
49 target_arch = "mipsel",
50 target_arch = "powerpc")))]
51 mod consts {
52 use libc;
53 pub const FIONBIO: libc::c_ulong = 0x667e;
54 pub const FIOCLEX: libc::c_ulong = 0x6601;
55 pub const FIONCLEX: libc::c_ulong = 0x6600;
56 }
57 pub use self::consts::*;
58
59 #[cfg(any(target_os = "macos",
60 target_os = "ios",
61 target_os = "freebsd",
62 target_os = "dragonfly",
63 target_os = "bitrig",
64 target_os = "openbsd"))]
65 pub const MSG_DONTWAIT: libc::c_int = 0x80;
66 #[cfg(any(target_os = "linux", target_os = "android"))]
67 pub const MSG_DONTWAIT: libc::c_int = 0x40;
68
69 pub const WNOHANG: libc::c_int = 1;
70
71 #[cfg(target_os = "linux")]
72 pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 70;
73 #[cfg(any(target_os = "macos",
74 target_os = "freebsd",
75 target_os = "dragonfly"))]
76 pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 71;
77 #[cfg(any(target_os = "bitrig",
78 target_os = "openbsd"))]
79 pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 101;
80 #[cfg(target_os = "android")]
81 pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 0x0048;
82
83 #[repr(C)]
84 #[cfg(target_os = "linux")]
85 pub struct passwd {
86 pub pw_name: *mut libc::c_char,
87 pub pw_passwd: *mut libc::c_char,
88 pub pw_uid: libc::uid_t,
89 pub pw_gid: libc::gid_t,
90 pub pw_gecos: *mut libc::c_char,
91 pub pw_dir: *mut libc::c_char,
92 pub pw_shell: *mut libc::c_char,
93 }
94
95 #[repr(C)]
96 #[cfg(any(target_os = "macos",
97 target_os = "freebsd",
98 target_os = "dragonfly",
99 target_os = "bitrig",
100 target_os = "openbsd"))]
101 pub struct passwd {
102 pub pw_name: *mut libc::c_char,
103 pub pw_passwd: *mut libc::c_char,
104 pub pw_uid: libc::uid_t,
105 pub pw_gid: libc::gid_t,
106 pub pw_change: libc::time_t,
107 pub pw_class: *mut libc::c_char,
108 pub pw_gecos: *mut libc::c_char,
109 pub pw_dir: *mut libc::c_char,
110 pub pw_shell: *mut libc::c_char,
111 pub pw_expire: libc::time_t,
112 }
113
114 #[repr(C)]
115 #[cfg(target_os = "android")]
116 pub struct passwd {
117 pub pw_name: *mut libc::c_char,
118 pub pw_passwd: *mut libc::c_char,
119 pub pw_uid: libc::uid_t,
120 pub pw_gid: libc::gid_t,
121 pub pw_dir: *mut libc::c_char,
122 pub pw_shell: *mut libc::c_char,
123 }
124
125 extern {
126 pub fn gettimeofday(timeval: *mut libc::timeval,
127 tzp: *mut libc::c_void) -> libc::c_int;
128 pub fn select(nfds: libc::c_int,
129 readfds: *mut fd_set,
130 writefds: *mut fd_set,
131 errorfds: *mut fd_set,
132 timeout: *mut libc::timeval) -> libc::c_int;
133 pub fn getsockopt(sockfd: libc::c_int,
134 level: libc::c_int,
135 optname: libc::c_int,
136 optval: *mut libc::c_void,
137 optlen: *mut libc::socklen_t) -> libc::c_int;
138 pub fn ioctl(fd: libc::c_int, req: libc::c_ulong, ...) -> libc::c_int;
139
140
141 pub fn waitpid(pid: libc::pid_t, status: *mut libc::c_int,
142 options: libc::c_int) -> libc::pid_t;
143
144 pub fn sigaction(signum: libc::c_int,
145 act: *const sigaction,
146 oldact: *mut sigaction) -> libc::c_int;
147
148 pub fn sigaddset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
149 pub fn sigdelset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
150 pub fn sigemptyset(set: *mut sigset_t) -> libc::c_int;
151
152 #[cfg(not(target_os = "ios"))]
153 pub fn getpwuid_r(uid: libc::uid_t,
154 pwd: *mut passwd,
155 buf: *mut libc::c_char,
156 buflen: libc::size_t,
157 result: *mut *mut passwd) -> libc::c_int;
158
159 pub fn utimes(filename: *const libc::c_char,
160 times: *const libc::timeval) -> libc::c_int;
161 pub fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char;
162 pub fn setgroups(ngroups: libc::c_int,
163 ptr: *const libc::c_void) -> libc::c_int;
164 pub fn realpath(pathname: *const libc::c_char, resolved: *mut libc::c_char)
165 -> *mut libc::c_char;
166 }
167
168 #[cfg(any(target_os = "macos", target_os = "ios"))]
169 mod select {
170 pub const FD_SETSIZE: usize = 1024;
171
172 #[repr(C)]
173 pub struct fd_set {
174 fds_bits: [i32; (FD_SETSIZE / 32)]
175 }
176
177 pub fn fd_set(set: &mut fd_set, fd: i32) {
178 set.fds_bits[(fd / 32) as usize] |= 1 << ((fd % 32) as usize);
179 }
180 }
181
182 #[cfg(any(target_os = "android",
183 target_os = "freebsd",
184 target_os = "dragonfly",
185 target_os = "bitrig",
186 target_os = "openbsd",
187 target_os = "linux"))]
188 mod select {
189 use usize;
190 use libc;
191
192 pub const FD_SETSIZE: usize = 1024;
193
194 #[repr(C)]
195 pub struct fd_set {
196 // FIXME: shouldn't this be a c_ulong?
197 fds_bits: [libc::uintptr_t; (FD_SETSIZE / usize::BITS)]
198 }
199
200 pub fn fd_set(set: &mut fd_set, fd: i32) {
201 let fd = fd as usize;
202 set.fds_bits[fd / usize::BITS] |= 1 << (fd % usize::BITS);
203 }
204 }
205
206 #[cfg(any(all(target_os = "linux",
207 any(target_arch = "x86",
208 target_arch = "x86_64",
209 target_arch = "arm",
210 target_arch = "aarch64")),
211 target_os = "android"))]
212 mod signal {
213 use libc;
214
215 pub const SA_NOCLDSTOP: libc::c_ulong = 0x00000001;
216 pub const SA_NOCLDWAIT: libc::c_ulong = 0x00000002;
217 pub const SA_NODEFER: libc::c_ulong = 0x40000000;
218 pub const SA_ONSTACK: libc::c_ulong = 0x08000000;
219 pub const SA_RESETHAND: libc::c_ulong = 0x80000000;
220 pub const SA_RESTART: libc::c_ulong = 0x10000000;
221 pub const SA_SIGINFO: libc::c_ulong = 0x00000004;
222 pub const SIGCHLD: libc::c_int = 17;
223
224 // This definition is not as accurate as it could be, {pid, uid, status} is
225 // actually a giant union. Currently we're only interested in these fields,
226 // however.
227 #[repr(C)]
228 pub struct siginfo {
229 si_signo: libc::c_int,
230 si_errno: libc::c_int,
231 si_code: libc::c_int,
232 pub pid: libc::pid_t,
233 pub uid: libc::uid_t,
234 pub status: libc::c_int,
235 }
236
237 #[repr(C)]
238 pub struct sigaction {
239 pub sa_handler: extern fn(libc::c_int),
240 pub sa_mask: sigset_t,
241 pub sa_flags: libc::c_ulong,
242 sa_restorer: *mut libc::c_void,
243 }
244
245 unsafe impl ::marker::Send for sigaction { }
246 unsafe impl ::marker::Sync for sigaction { }
247
248 #[repr(C)]
249 #[cfg(target_pointer_width = "32")]
250 pub struct sigset_t {
251 __val: [libc::c_ulong; 32],
252 }
253
254 #[repr(C)]
255 #[cfg(target_pointer_width = "64")]
256 pub struct sigset_t {
257 __val: [libc::c_ulong; 16],
258 }
259 }
260
261 #[cfg(all(target_os = "linux",
262 any(target_arch = "mips",
263 target_arch = "mipsel",
264 target_arch = "powerpc")))]
265 mod signal {
266 use libc;
267
268 pub const SA_NOCLDSTOP: libc::c_ulong = 0x00000001;
269 pub const SA_NOCLDWAIT: libc::c_ulong = 0x00010000;
270 pub const SA_NODEFER: libc::c_ulong = 0x40000000;
271 pub const SA_ONSTACK: libc::c_ulong = 0x08000000;
272 pub const SA_RESETHAND: libc::c_ulong = 0x80000000;
273 pub const SA_RESTART: libc::c_ulong = 0x10000000;
274 pub const SA_SIGINFO: libc::c_ulong = 0x00000008;
275 pub const SIGCHLD: libc::c_int = 18;
276
277 // This definition is not as accurate as it could be, {pid, uid, status} is
278 // actually a giant union. Currently we're only interested in these fields,
279 // however.
280 #[repr(C)]
281 pub struct siginfo {
282 si_signo: libc::c_int,
283 si_code: libc::c_int,
284 si_errno: libc::c_int,
285 pub pid: libc::pid_t,
286 pub uid: libc::uid_t,
287 pub status: libc::c_int,
288 }
289
290 #[repr(C)]
291 pub struct sigaction {
292 pub sa_flags: libc::c_uint,
293 pub sa_handler: extern fn(libc::c_int),
294 pub sa_mask: sigset_t,
295 sa_restorer: *mut libc::c_void,
296 sa_resv: [libc::c_int; 1],
297 }
298
299 unsafe impl ::marker::Send for sigaction { }
300 unsafe impl ::marker::Sync for sigaction { }
301
302 #[repr(C)]
303 pub struct sigset_t {
304 __val: [libc::c_ulong; 32],
305 }
306 }
307
308 #[cfg(any(target_os = "macos",
309 target_os = "ios",
310 target_os = "freebsd",
311 target_os = "dragonfly"))]
312 mod signal {
313 use libc;
314
315 pub const SA_ONSTACK: libc::c_int = 0x0001;
316 pub const SA_RESTART: libc::c_int = 0x0002;
317 pub const SA_RESETHAND: libc::c_int = 0x0004;
318 pub const SA_NOCLDSTOP: libc::c_int = 0x0008;
319 pub const SA_NODEFER: libc::c_int = 0x0010;
320 pub const SA_NOCLDWAIT: libc::c_int = 0x0020;
321 pub const SA_SIGINFO: libc::c_int = 0x0040;
322 pub const SIGCHLD: libc::c_int = 20;
323
324 #[cfg(any(target_os = "macos",
325 target_os = "ios"))]
326 pub type sigset_t = u32;
327 #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
328 #[repr(C)]
329 pub struct sigset_t {
330 bits: [u32; 4],
331 }
332
333 // This structure has more fields, but we're not all that interested in
334 // them.
335 #[repr(C)]
336 pub struct siginfo {
337 pub si_signo: libc::c_int,
338 pub si_errno: libc::c_int,
339 pub si_code: libc::c_int,
340 pub pid: libc::pid_t,
341 pub uid: libc::uid_t,
342 pub status: libc::c_int,
343 }
344
345 #[repr(C)]
346 pub struct sigaction {
347 pub sa_handler: extern fn(libc::c_int),
348 pub sa_flags: libc::c_int,
349 pub sa_mask: sigset_t,
350 }
351 }
352
353 #[cfg(any(target_os = "bitrig", target_os = "openbsd"))]
354 mod signal {
355 use libc;
356
357 pub const SA_ONSTACK: libc::c_int = 0x0001;
358 pub const SA_RESTART: libc::c_int = 0x0002;
359 pub const SA_RESETHAND: libc::c_int = 0x0004;
360 pub const SA_NOCLDSTOP: libc::c_int = 0x0008;
361 pub const SA_NODEFER: libc::c_int = 0x0010;
362 pub const SA_NOCLDWAIT: libc::c_int = 0x0020;
363 pub const SA_SIGINFO: libc::c_int = 0x0040;
364 pub const SIGCHLD: libc::c_int = 20;
365
366 pub type sigset_t = libc::c_uint;
367
368 // This structure has more fields, but we're not all that interested in
369 // them.
370 #[repr(C)]
371 pub struct siginfo {
372 pub si_signo: libc::c_int,
373 pub si_code: libc::c_int,
374 pub si_errno: libc::c_int,
375 // FIXME: Bitrig has a crazy union here in the siginfo, I think this
376 // layout will still work tho. The status might be off by the size of
377 // a clock_t by my reading, but we can fix this later.
378 pub pid: libc::pid_t,
379 pub uid: libc::uid_t,
380 pub status: libc::c_int,
381 }
382
383 #[repr(C)]
384 pub struct sigaction {
385 pub sa_handler: extern fn(libc::c_int),
386 pub sa_mask: sigset_t,
387 pub sa_flags: libc::c_int,
388 }
389 }