1#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4#[cfg(feature = "std")]
5pub use std::os::raw as ctypes;
6
7#[cfg(all(not(feature = "std"), feature = "no_std"))]
8pub mod ctypes {
9 // The signedness of `char` is platform-specific, however a consequence
10 // of it being platform-specific is that any code which depends on the
11 // signedness of `char` is already non-portable. So we can just use `u8`
12 // here and no portable code will notice.
13 pub type c_char = u8;
14
15 // The following assumes that Linux is always either ILP32 or LP64,
16 // and char is always 8-bit.
17 //
18 // In theory, `c_long` and `c_ulong` could be `isize` and `usize`
19 // respectively, however in practice Linux doesn't use them in that way
20 // consistently. So stick with the convention followed by `libc` and
21 // others and use the fixed-width types.
22 pub type c_schar = i8;
23 pub type c_uchar = u8;
24 pub type c_short = i16;
25 pub type c_ushort = u16;
26 pub type c_int = i32;
27 pub type c_uint = u32;
28 #[cfg(target_pointer_width = "32")]
29 pub type c_long = i32;
30 #[cfg(target_pointer_width = "32")]
31 pub type c_ulong = u32;
32 #[cfg(target_pointer_width = "64")]
33 pub type c_long = i64;
34 #[cfg(target_pointer_width = "64")]
35 pub type c_ulong = u64;
36 pub type c_longlong = i64;
37 pub type c_ulonglong = u64;
38 pub type c_float = f32;
39 pub type c_double = f64;
40
41 pub use core::ffi::c_void;
42}
43
44// Confirm that our type definitions above match the actual type definitions.
45#[cfg(test)]
46mod assertions {
47 use super::ctypes;
48 static_assertions::assert_eq_size!(ctypes::c_char, libc::c_char);
49 static_assertions::assert_type_eq_all!(ctypes::c_schar, libc::c_schar);
50 static_assertions::assert_type_eq_all!(ctypes::c_uchar, libc::c_uchar);
51 static_assertions::assert_type_eq_all!(ctypes::c_short, libc::c_short);
52 static_assertions::assert_type_eq_all!(ctypes::c_ushort, libc::c_ushort);
53 static_assertions::assert_type_eq_all!(ctypes::c_int, libc::c_int);
54 static_assertions::assert_type_eq_all!(ctypes::c_uint, libc::c_uint);
55 static_assertions::assert_type_eq_all!(ctypes::c_long, libc::c_long);
56 static_assertions::assert_type_eq_all!(ctypes::c_ulong, libc::c_ulong);
57 static_assertions::assert_type_eq_all!(ctypes::c_longlong, libc::c_longlong);
58 static_assertions::assert_type_eq_all!(ctypes::c_ulonglong, libc::c_ulonglong);
59 static_assertions::assert_type_eq_all!(ctypes::c_float, libc::c_float);
60 static_assertions::assert_type_eq_all!(ctypes::c_double, libc::c_double);
61}
62
63// We don't enable `derive_eq` in bindgen because adding `PartialEq`/`Eq` to
64// *all* structs noticeably increases compile times. But we can add a few
65// manual impls where they're especially useful.
66#[cfg(feature = "general")]
67impl PartialEq for general::__kernel_timespec {
68 fn eq(&self, other: &Self) -> bool {
69 ({
70 let Self { tv_sec: &i64, tv_nsec: &i64 } = self;
71 (tv_sec, tv_nsec)
72 }) == ({
73 let Self { tv_sec: &i64, tv_nsec: &i64 } = other;
74 (tv_sec, tv_nsec)
75 })
76 }
77}
78#[cfg(feature = "general")]
79impl Eq for general::__kernel_timespec {}
80
81#[cfg(feature = "general")]
82pub mod cmsg_macros {
83 use crate::ctypes::{c_long, c_uchar, c_uint};
84 use crate::general::{cmsghdr, msghdr};
85 use core::mem::size_of;
86 use core::ptr;
87
88 pub const unsafe fn CMSG_ALIGN(len: c_uint) -> c_uint {
89 let c_long_size = size_of::<c_long>() as c_uint;
90 (len + c_long_size - 1) & !(c_long_size - 1)
91 }
92
93 // TODO: In Rust 1.63 we can make this a `const fn`.
94 pub unsafe fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
95 (cmsg as *mut c_uchar).add(size_of::<cmsghdr>())
96 }
97
98 pub const unsafe fn CMSG_SPACE(len: c_uint) -> c_uint {
99 size_of::<cmsghdr>() as c_uint + CMSG_ALIGN(len)
100 }
101
102 pub const unsafe fn CMSG_LEN(len: c_uint) -> c_uint {
103 size_of::<cmsghdr>() as c_uint + len
104 }
105
106 // TODO: In Rust 1.63 we can make this a `const fn`.
107 pub unsafe fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
108 if (*mhdr).msg_controllen < size_of::<cmsghdr>() as _ {
109 return ptr::null_mut();
110 }
111
112 (*mhdr).msg_control as *mut cmsghdr
113 }
114
115 pub unsafe fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
116 // We convert from raw pointers to usize here, which may not be sound in a
117 // future version of Rust. Once the provenance rules are set in stone,
118 // it will be a good idea to give this function a once-over.
119
120 let cmsg_len = (*cmsg).cmsg_len;
121 let next_cmsg = (cmsg as *mut u8).add(CMSG_ALIGN(cmsg_len as _) as usize) as *mut cmsghdr;
122 let max = ((*mhdr).msg_control as usize) + ((*mhdr).msg_controllen as usize);
123
124 if cmsg_len < size_of::<cmsghdr>() as _ {
125 return ptr::null_mut();
126 }
127
128 if next_cmsg.add(1) as usize > max
129 || next_cmsg as usize + CMSG_ALIGN(cmsg_len as _) as usize > max
130 {
131 return ptr::null_mut();
132 }
133
134 next_cmsg
135 }
136}
137
138#[cfg(feature = "general")]
139pub mod select_macros {
140 use crate::ctypes::c_int;
141 use crate::general::__kernel_fd_set;
142 use core::mem::size_of;
143
144 pub unsafe fn FD_CLR(fd: c_int, set: *mut __kernel_fd_set) {
145 let bytes = set as *mut u8;
146 if fd >= 0 {
147 *bytes.add((fd / 8) as usize) &= !(1 << (fd % 8));
148 }
149 }
150
151 pub unsafe fn FD_SET(fd: c_int, set: *mut __kernel_fd_set) {
152 let bytes = set as *mut u8;
153 if fd >= 0 {
154 *bytes.add((fd / 8) as usize) |= 1 << (fd % 8);
155 }
156 }
157
158 pub unsafe fn FD_ISSET(fd: c_int, set: *const __kernel_fd_set) -> bool {
159 let bytes = set as *const u8;
160 if fd >= 0 {
161 *bytes.add((fd / 8) as usize) & (1 << (fd % 8)) != 0
162 } else {
163 false
164 }
165 }
166
167 pub unsafe fn FD_ZERO(set: *mut __kernel_fd_set) {
168 let bytes = set as *mut u8;
169 core::ptr::write_bytes(bytes, 0, size_of::<__kernel_fd_set>());
170 }
171}
172
173#[cfg(feature = "general")]
174pub mod signal_macros {
175 pub const SIG_DFL: super::general::__kernel_sighandler_t = None;
176
177 /// Rust doesn't currently permit us to use `transmute` to convert the
178 /// `SIG_IGN` value into a function pointer in a `const` initializer, so
179 /// we make it a function instead.
180 ///
181 // TODO: In Rust 1.56 we can make this a `const fn`.
182 #[inline]
183 pub fn sig_ign() -> super::general::__kernel_sighandler_t {
184 // Safety: This creates an invalid pointer, but the pointer type
185 // includes `unsafe`, which covers the safety of calling it.
186 Some(unsafe {
187 core::mem::transmute::<usize, unsafe extern "C" fn(crate::ctypes::c_int)>(src:1)
188 })
189 }
190}
191
192// The rest of this file is auto-generated!
193#[cfg(feature = "errno")]
194#[cfg(target_arch = "arm")]
195#[path = "arm/errno.rs"]
196pub mod errno;
197#[cfg(feature = "general")]
198#[cfg(target_arch = "arm")]
199#[path = "arm/general.rs"]
200pub mod general;
201#[cfg(feature = "ioctl")]
202#[cfg(target_arch = "arm")]
203#[path = "arm/ioctl.rs"]
204pub mod ioctl;
205#[cfg(feature = "netlink")]
206#[cfg(target_arch = "arm")]
207#[path = "arm/netlink.rs"]
208pub mod netlink;
209#[cfg(feature = "errno")]
210#[cfg(target_arch = "aarch64")]
211#[path = "aarch64/errno.rs"]
212pub mod errno;
213#[cfg(feature = "general")]
214#[cfg(target_arch = "aarch64")]
215#[path = "aarch64/general.rs"]
216pub mod general;
217#[cfg(feature = "ioctl")]
218#[cfg(target_arch = "aarch64")]
219#[path = "aarch64/ioctl.rs"]
220pub mod ioctl;
221#[cfg(feature = "netlink")]
222#[cfg(target_arch = "aarch64")]
223#[path = "aarch64/netlink.rs"]
224pub mod netlink;
225#[cfg(feature = "errno")]
226#[cfg(target_arch = "loongarch64")]
227#[path = "loongarch64/errno.rs"]
228pub mod errno;
229#[cfg(feature = "general")]
230#[cfg(target_arch = "loongarch64")]
231#[path = "loongarch64/general.rs"]
232pub mod general;
233#[cfg(feature = "ioctl")]
234#[cfg(target_arch = "loongarch64")]
235#[path = "loongarch64/ioctl.rs"]
236pub mod ioctl;
237#[cfg(feature = "netlink")]
238#[cfg(target_arch = "loongarch64")]
239#[path = "loongarch64/netlink.rs"]
240pub mod netlink;
241#[cfg(feature = "errno")]
242#[cfg(target_arch = "mips")]
243#[path = "mips/errno.rs"]
244pub mod errno;
245#[cfg(feature = "general")]
246#[cfg(target_arch = "mips")]
247#[path = "mips/general.rs"]
248pub mod general;
249#[cfg(feature = "ioctl")]
250#[cfg(target_arch = "mips")]
251#[path = "mips/ioctl.rs"]
252pub mod ioctl;
253#[cfg(feature = "netlink")]
254#[cfg(target_arch = "mips")]
255#[path = "mips/netlink.rs"]
256pub mod netlink;
257#[cfg(feature = "errno")]
258#[cfg(target_arch = "mips64")]
259#[path = "mips64/errno.rs"]
260pub mod errno;
261#[cfg(feature = "general")]
262#[cfg(target_arch = "mips64")]
263#[path = "mips64/general.rs"]
264pub mod general;
265#[cfg(feature = "ioctl")]
266#[cfg(target_arch = "mips64")]
267#[path = "mips64/ioctl.rs"]
268pub mod ioctl;
269#[cfg(feature = "netlink")]
270#[cfg(target_arch = "mips64")]
271#[path = "mips64/netlink.rs"]
272pub mod netlink;
273#[cfg(feature = "errno")]
274#[cfg(target_arch = "powerpc")]
275#[path = "powerpc/errno.rs"]
276pub mod errno;
277#[cfg(feature = "general")]
278#[cfg(target_arch = "powerpc")]
279#[path = "powerpc/general.rs"]
280pub mod general;
281#[cfg(feature = "ioctl")]
282#[cfg(target_arch = "powerpc")]
283#[path = "powerpc/ioctl.rs"]
284pub mod ioctl;
285#[cfg(feature = "netlink")]
286#[cfg(target_arch = "powerpc")]
287#[path = "powerpc/netlink.rs"]
288pub mod netlink;
289#[cfg(feature = "errno")]
290#[cfg(target_arch = "powerpc64")]
291#[path = "powerpc64/errno.rs"]
292pub mod errno;
293#[cfg(feature = "general")]
294#[cfg(target_arch = "powerpc64")]
295#[path = "powerpc64/general.rs"]
296pub mod general;
297#[cfg(feature = "ioctl")]
298#[cfg(target_arch = "powerpc64")]
299#[path = "powerpc64/ioctl.rs"]
300pub mod ioctl;
301#[cfg(feature = "netlink")]
302#[cfg(target_arch = "powerpc64")]
303#[path = "powerpc64/netlink.rs"]
304pub mod netlink;
305#[cfg(feature = "errno")]
306#[cfg(target_arch = "riscv32")]
307#[path = "riscv32/errno.rs"]
308pub mod errno;
309#[cfg(feature = "general")]
310#[cfg(target_arch = "riscv32")]
311#[path = "riscv32/general.rs"]
312pub mod general;
313#[cfg(feature = "ioctl")]
314#[cfg(target_arch = "riscv32")]
315#[path = "riscv32/ioctl.rs"]
316pub mod ioctl;
317#[cfg(feature = "netlink")]
318#[cfg(target_arch = "riscv32")]
319#[path = "riscv32/netlink.rs"]
320pub mod netlink;
321#[cfg(feature = "errno")]
322#[cfg(target_arch = "riscv64")]
323#[path = "riscv64/errno.rs"]
324pub mod errno;
325#[cfg(feature = "general")]
326#[cfg(target_arch = "riscv64")]
327#[path = "riscv64/general.rs"]
328pub mod general;
329#[cfg(feature = "ioctl")]
330#[cfg(target_arch = "riscv64")]
331#[path = "riscv64/ioctl.rs"]
332pub mod ioctl;
333#[cfg(feature = "netlink")]
334#[cfg(target_arch = "riscv64")]
335#[path = "riscv64/netlink.rs"]
336pub mod netlink;
337#[cfg(feature = "errno")]
338#[cfg(target_arch = "s390x")]
339#[path = "s390x/errno.rs"]
340pub mod errno;
341#[cfg(feature = "general")]
342#[cfg(target_arch = "s390x")]
343#[path = "s390x/general.rs"]
344pub mod general;
345#[cfg(feature = "ioctl")]
346#[cfg(target_arch = "s390x")]
347#[path = "s390x/ioctl.rs"]
348pub mod ioctl;
349#[cfg(feature = "netlink")]
350#[cfg(target_arch = "s390x")]
351#[path = "s390x/netlink.rs"]
352pub mod netlink;
353#[cfg(feature = "errno")]
354#[cfg(target_arch = "sparc")]
355#[path = "sparc/errno.rs"]
356pub mod errno;
357#[cfg(feature = "general")]
358#[cfg(target_arch = "sparc")]
359#[path = "sparc/general.rs"]
360pub mod general;
361#[cfg(feature = "ioctl")]
362#[cfg(target_arch = "sparc")]
363#[path = "sparc/ioctl.rs"]
364pub mod ioctl;
365#[cfg(feature = "netlink")]
366#[cfg(target_arch = "sparc")]
367#[path = "sparc/netlink.rs"]
368pub mod netlink;
369#[cfg(feature = "errno")]
370#[cfg(target_arch = "sparc64")]
371#[path = "sparc64/errno.rs"]
372pub mod errno;
373#[cfg(feature = "general")]
374#[cfg(target_arch = "sparc64")]
375#[path = "sparc64/general.rs"]
376pub mod general;
377#[cfg(feature = "ioctl")]
378#[cfg(target_arch = "sparc64")]
379#[path = "sparc64/ioctl.rs"]
380pub mod ioctl;
381#[cfg(feature = "netlink")]
382#[cfg(target_arch = "sparc64")]
383#[path = "sparc64/netlink.rs"]
384pub mod netlink;
385#[cfg(feature = "errno")]
386#[cfg(target_arch = "x86")]
387#[path = "x86/errno.rs"]
388pub mod errno;
389#[cfg(feature = "general")]
390#[cfg(target_arch = "x86")]
391#[path = "x86/general.rs"]
392pub mod general;
393#[cfg(feature = "ioctl")]
394#[cfg(target_arch = "x86")]
395#[path = "x86/ioctl.rs"]
396pub mod ioctl;
397#[cfg(feature = "netlink")]
398#[cfg(target_arch = "x86")]
399#[path = "x86/netlink.rs"]
400pub mod netlink;
401#[cfg(feature = "errno")]
402#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
403#[path = "x86_64/errno.rs"]
404pub mod errno;
405#[cfg(feature = "general")]
406#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
407#[path = "x86_64/general.rs"]
408pub mod general;
409#[cfg(feature = "ioctl")]
410#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
411#[path = "x86_64/ioctl.rs"]
412pub mod ioctl;
413#[cfg(feature = "netlink")]
414#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
415#[path = "x86_64/netlink.rs"]
416pub mod netlink;
417#[cfg(feature = "errno")]
418#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
419#[path = "x32/errno.rs"]
420pub mod errno;
421#[cfg(feature = "general")]
422#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
423#[path = "x32/general.rs"]
424pub mod general;
425#[cfg(feature = "ioctl")]
426#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
427#[path = "x32/ioctl.rs"]
428pub mod ioctl;
429#[cfg(feature = "netlink")]
430#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
431#[path = "x32/netlink.rs"]
432pub mod netlink;
433