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, tv_nsec } = self;
71            (tv_sec, tv_nsec)
72        }) == ({
73            let Self { tv_sec, tv_nsec } = 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_uint, c_uchar};
84    use crate::general::{cmsghdr, msghdr};
85    use core::mem::size_of;
86    use core::ptr;
87
88    pub 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    pub unsafe fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
94        (cmsg as *mut c_uchar).offset(size_of::<cmsghdr>() as isize)
95    }
96
97    pub unsafe fn CMSG_SPACE(len: c_uint) -> c_uint {
98        size_of::<cmsghdr>() as c_uint + CMSG_ALIGN(len)
99    }
100
101    pub unsafe fn CMSG_LEN(len: c_uint) -> c_uint {
102        size_of::<cmsghdr>() as c_uint + len
103    }
104
105    pub unsafe fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
106        if (*mhdr).msg_controllen < size_of::<cmsghdr>() as _ {
107            return ptr::null_mut();
108        }
109
110        (*mhdr).msg_control as *mut cmsghdr
111    }
112
113    pub unsafe fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
114        // We convert from raw pointers to isize here, which may not be sound in a future version of Rust.
115        // Once the provenance rules are set in stone, it will be a good idea to give this function a once-over.
116
117        let cmsg_len = (*cmsg).cmsg_len;
118        let next_cmsg = (cmsg as *mut u8).offset(CMSG_ALIGN(cmsg_len as _) as isize) as *mut cmsghdr;
119        let max = ((*mhdr).msg_control as usize) + ((*mhdr).msg_controllen as usize);
120
121        if cmsg_len < size_of::<cmsghdr>() as _ {
122            return ptr::null_mut();
123        }
124
125        if next_cmsg.offset(1) as usize > max || next_cmsg as usize + CMSG_ALIGN(cmsg_len as _) as usize > max
126        {
127            return ptr::null_mut();
128        }
129
130        next_cmsg
131    }
132}
133
134// The rest of this file is auto-generated!
135#[cfg(feature = "errno")]
136#[cfg(target_arch = "arm")]
137#[path = "arm/errno.rs"]
138pub mod errno;
139#[cfg(feature = "general")]
140#[cfg(target_arch = "arm")]
141#[path = "arm/general.rs"]
142pub mod general;
143#[cfg(feature = "ioctl")]
144#[cfg(target_arch = "arm")]
145#[path = "arm/ioctl.rs"]
146pub mod ioctl;
147#[cfg(feature = "netlink")]
148#[cfg(target_arch = "arm")]
149#[path = "arm/netlink.rs"]
150pub mod netlink;
151#[cfg(feature = "errno")]
152#[cfg(target_arch = "aarch64")]
153#[path = "aarch64/errno.rs"]
154pub mod errno;
155#[cfg(feature = "general")]
156#[cfg(target_arch = "aarch64")]
157#[path = "aarch64/general.rs"]
158pub mod general;
159#[cfg(feature = "ioctl")]
160#[cfg(target_arch = "aarch64")]
161#[path = "aarch64/ioctl.rs"]
162pub mod ioctl;
163#[cfg(feature = "netlink")]
164#[cfg(target_arch = "aarch64")]
165#[path = "aarch64/netlink.rs"]
166pub mod netlink;
167#[cfg(feature = "errno")]
168#[cfg(target_arch = "mips")]
169#[path = "mips/errno.rs"]
170pub mod errno;
171#[cfg(feature = "general")]
172#[cfg(target_arch = "mips")]
173#[path = "mips/general.rs"]
174pub mod general;
175#[cfg(feature = "ioctl")]
176#[cfg(target_arch = "mips")]
177#[path = "mips/ioctl.rs"]
178pub mod ioctl;
179#[cfg(feature = "netlink")]
180#[cfg(target_arch = "mips")]
181#[path = "mips/netlink.rs"]
182pub mod netlink;
183#[cfg(feature = "errno")]
184#[cfg(target_arch = "mips64")]
185#[path = "mips64/errno.rs"]
186pub mod errno;
187#[cfg(feature = "general")]
188#[cfg(target_arch = "mips64")]
189#[path = "mips64/general.rs"]
190pub mod general;
191#[cfg(feature = "ioctl")]
192#[cfg(target_arch = "mips64")]
193#[path = "mips64/ioctl.rs"]
194pub mod ioctl;
195#[cfg(feature = "netlink")]
196#[cfg(target_arch = "mips64")]
197#[path = "mips64/netlink.rs"]
198pub mod netlink;
199#[cfg(feature = "errno")]
200#[cfg(target_arch = "powerpc")]
201#[path = "powerpc/errno.rs"]
202pub mod errno;
203#[cfg(feature = "general")]
204#[cfg(target_arch = "powerpc")]
205#[path = "powerpc/general.rs"]
206pub mod general;
207#[cfg(feature = "ioctl")]
208#[cfg(target_arch = "powerpc")]
209#[path = "powerpc/ioctl.rs"]
210pub mod ioctl;
211#[cfg(feature = "netlink")]
212#[cfg(target_arch = "powerpc")]
213#[path = "powerpc/netlink.rs"]
214pub mod netlink;
215#[cfg(feature = "errno")]
216#[cfg(target_arch = "powerpc64")]
217#[path = "powerpc64/errno.rs"]
218pub mod errno;
219#[cfg(feature = "general")]
220#[cfg(target_arch = "powerpc64")]
221#[path = "powerpc64/general.rs"]
222pub mod general;
223#[cfg(feature = "ioctl")]
224#[cfg(target_arch = "powerpc64")]
225#[path = "powerpc64/ioctl.rs"]
226pub mod ioctl;
227#[cfg(feature = "netlink")]
228#[cfg(target_arch = "powerpc64")]
229#[path = "powerpc64/netlink.rs"]
230pub mod netlink;
231#[cfg(feature = "errno")]
232#[cfg(target_arch = "riscv32")]
233#[path = "riscv32/errno.rs"]
234pub mod errno;
235#[cfg(feature = "general")]
236#[cfg(target_arch = "riscv32")]
237#[path = "riscv32/general.rs"]
238pub mod general;
239#[cfg(feature = "ioctl")]
240#[cfg(target_arch = "riscv32")]
241#[path = "riscv32/ioctl.rs"]
242pub mod ioctl;
243#[cfg(feature = "netlink")]
244#[cfg(target_arch = "riscv32")]
245#[path = "riscv32/netlink.rs"]
246pub mod netlink;
247#[cfg(feature = "errno")]
248#[cfg(target_arch = "riscv64")]
249#[path = "riscv64/errno.rs"]
250pub mod errno;
251#[cfg(feature = "general")]
252#[cfg(target_arch = "riscv64")]
253#[path = "riscv64/general.rs"]
254pub mod general;
255#[cfg(feature = "ioctl")]
256#[cfg(target_arch = "riscv64")]
257#[path = "riscv64/ioctl.rs"]
258pub mod ioctl;
259#[cfg(feature = "netlink")]
260#[cfg(target_arch = "riscv64")]
261#[path = "riscv64/netlink.rs"]
262pub mod netlink;
263#[cfg(feature = "errno")]
264#[cfg(target_arch = "s390x")]
265#[path = "s390x/errno.rs"]
266pub mod errno;
267#[cfg(feature = "general")]
268#[cfg(target_arch = "s390x")]
269#[path = "s390x/general.rs"]
270pub mod general;
271#[cfg(feature = "ioctl")]
272#[cfg(target_arch = "s390x")]
273#[path = "s390x/ioctl.rs"]
274pub mod ioctl;
275#[cfg(feature = "netlink")]
276#[cfg(target_arch = "s390x")]
277#[path = "s390x/netlink.rs"]
278pub mod netlink;
279#[cfg(feature = "errno")]
280#[cfg(target_arch = "sparc")]
281#[path = "sparc/errno.rs"]
282pub mod errno;
283#[cfg(feature = "general")]
284#[cfg(target_arch = "sparc")]
285#[path = "sparc/general.rs"]
286pub mod general;
287#[cfg(feature = "ioctl")]
288#[cfg(target_arch = "sparc")]
289#[path = "sparc/ioctl.rs"]
290pub mod ioctl;
291#[cfg(feature = "netlink")]
292#[cfg(target_arch = "sparc")]
293#[path = "sparc/netlink.rs"]
294pub mod netlink;
295#[cfg(feature = "errno")]
296#[cfg(target_arch = "sparc64")]
297#[path = "sparc64/errno.rs"]
298pub mod errno;
299#[cfg(feature = "general")]
300#[cfg(target_arch = "sparc64")]
301#[path = "sparc64/general.rs"]
302pub mod general;
303#[cfg(feature = "ioctl")]
304#[cfg(target_arch = "sparc64")]
305#[path = "sparc64/ioctl.rs"]
306pub mod ioctl;
307#[cfg(feature = "netlink")]
308#[cfg(target_arch = "sparc64")]
309#[path = "sparc64/netlink.rs"]
310pub mod netlink;
311#[cfg(feature = "errno")]
312#[cfg(target_arch = "x86")]
313#[path = "x86/errno.rs"]
314pub mod errno;
315#[cfg(feature = "general")]
316#[cfg(target_arch = "x86")]
317#[path = "x86/general.rs"]
318pub mod general;
319#[cfg(feature = "ioctl")]
320#[cfg(target_arch = "x86")]
321#[path = "x86/ioctl.rs"]
322pub mod ioctl;
323#[cfg(feature = "netlink")]
324#[cfg(target_arch = "x86")]
325#[path = "x86/netlink.rs"]
326pub mod netlink;
327#[cfg(feature = "errno")]
328#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
329#[path = "x86_64/errno.rs"]
330pub mod errno;
331#[cfg(feature = "general")]
332#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
333#[path = "x86_64/general.rs"]
334pub mod general;
335#[cfg(feature = "ioctl")]
336#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
337#[path = "x86_64/ioctl.rs"]
338pub mod ioctl;
339#[cfg(feature = "netlink")]
340#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
341#[path = "x86_64/netlink.rs"]
342pub mod netlink;
343#[cfg(feature = "errno")]
344#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
345#[path = "x32/errno.rs"]
346pub mod errno;
347#[cfg(feature = "general")]
348#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
349#[path = "x32/general.rs"]
350pub mod general;
351#[cfg(feature = "ioctl")]
352#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
353#[path = "x32/ioctl.rs"]
354pub mod ioctl;
355#[cfg(feature = "netlink")]
356#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
357#[path = "x32/netlink.rs"]
358pub mod netlink;
359