linux_unsafe/
types.rs

1#![allow(non_camel_case_types)]
2
3use core::ffi;
4
5/// The primary signed integer type for the current platform.
6pub type int = ffi::c_int;
7
8/// The primary unsigned integer type for the current platform.
9pub type uint = ffi::c_uint;
10
11/// The short signed integer type for the current platform.
12pub type short = ffi::c_short;
13
14/// The short unsigned integer type for the current platform.
15pub type ushort = ffi::c_ushort;
16
17/// The signed long integer type for the current platform.
18pub type long = ffi::c_long;
19
20/// The unsigned long integer type for the current platform.
21pub type ulong = ffi::c_ulong;
22
23/// The signed long long integer type for the current platform.
24pub type longlong = ffi::c_long;
25
26/// The unsigned long long integer type for the current platform.
27pub type ulonglong = ffi::c_ulong;
28
29/// The signed size type (or "pointer difference" type) for the current platform.
30pub type ssize_t = isize;
31
32/// The unsigned size type for the current platform.
33pub type size_t = usize;
34
35/// The type used for characters on the current platform.
36pub type char = ffi::c_char;
37
38/// The type used for unsigned characters on the current platform.
39pub type uchar = ffi::c_uchar;
40
41/// The type used for void pointers on the current platform.
42pub type void = ffi::c_void;
43
44/// The type used to represent file modes on the current platform.
45pub type mode_t = uint;
46
47/// The type used to represent file sizes and offsets into files on the current platform.
48pub type off_t = long;
49
50/// The type used to represent larger file sizes and offsets into files on the current platform.
51pub type loff_t = ffi::c_longlong;
52
53/// The type used for process identifiers (PIDs) on the current platform.
54pub type pid_t = int;
55
56/// The type used for representing socket addresses in the raw system calls.
57///
58/// This is a type of unknown length, because the actual length of a socket
59/// address depends on the address family. When reading a socket address of
60/// an unknown address family, use a pointer to a [`sockaddr_storage`] as a
61/// placeholder type and then convert based on the returned `family`.
62///
63/// **Warning:** It is not meaningful to ask the compiler for the size of
64/// this type; it will return an arbitrary placeholder value. If you need
65/// sufficient storage for an arbitrary address, use [`sockaddr_storage`]
66/// instead.
67#[repr(C)]
68pub struct sockaddr {
69    pub family: sa_family_t,
70
71    // Intentionally not public to discourage using values of this type;
72    // it's here primarily as a placeholder type for arguments that are
73    // pointers to arbitrary addresses.
74    data: [u8; 14],
75}
76
77/// Represents the upper limit for the size of any [`sockaddr`] value, across
78/// all address families.
79///
80/// This is a reasonable default type to use when retrieving a socket address
81/// from the kernel without knowledge of its address family. It is guaranteed
82/// at least as large as the largest address type the kernel can return.
83/// After the value is populated, use `family` to convert to a more specific
84/// address type.
85#[repr(C, align(8))]
86pub struct sockaddr_storage {
87    pub family: sa_family_t,
88    pub data: [u8; 128 - core::mem::size_of::<sa_family_t>()],
89}
90
91/// The type used for representing the length of a socket address.
92pub type socklen_t = int;
93
94/// The type used to represent user ids.
95pub type uid_t = uint;
96
97/// The type used to represent group ids.
98pub type gid_t = uint;
99
100/// Seek relative to the beginning of the file.
101pub const SEEK_SET: int = 0;
102
103/// Seek relative to the current file position.
104pub const SEEK_CUR: int = 1;
105
106/// Seek relative to the end of the file.
107pub const SEEK_END: int = 2;
108
109/// Seek to the next data.
110pub const SEEK_DATA: int = 3;
111
112/// Seek to the next hole.
113pub const SEEK_HOLE: int = 4;
114
115pub const O_ACCMODE: int = 0o00000003;
116pub const O_RDONLY: int = 0o00000000;
117pub const O_WRONLY: int = 0o00000001;
118pub const O_RDWR: int = 0o00000002;
119pub const O_CREAT: int = 0o00000100;
120pub const O_EXCL: int = 0o00000200;
121pub const O_NOCTTY: int = 0o00000400;
122pub const O_TRUNC: int = 0o00001000;
123pub const O_APPEND: int = 0o00002000;
124pub const O_NONBLOCK: int = 0o00004000;
125pub const O_DSYNC: int = 0o00010000;
126pub const O_DIRECT: int = 0o00040000;
127pub const O_LARGEFILE: int = 0o00100000;
128pub const O_DIRECTORY: int = 0o00200000;
129pub const O_NOFOLLOW: int = 0o00400000;
130pub const O_NOATIME: int = 0o01000000;
131pub const O_CLOEXEC: int = 0o02000000;
132pub const O_SYNC: int = 0o04000000 | O_DSYNC;
133pub const O_PATH: int = 0o010000000;
134pub const O_TMPFILE: int = 0o020000000 | O_DIRECTORY;
135pub const O_TMPFILE_MASK: int = 0o020000000 | O_DIRECTORY | O_CREAT;
136pub const O_NDELAY: int = O_NONBLOCK;
137
138pub const AT_FDCWD: int = -100;
139pub const AT_EMPTY_PATH: int = 0x1000;
140pub const AT_SYMLINK_NOFOLLOW: int = 0x100;
141pub const AT_EACCESS: int = 0x200;
142pub const AT_REMOVEDIR: int = 0x200;
143pub const AT_SYMLINK_FOLLOW: int = 0x400;
144pub const AT_NO_AUTOMOUNT: int = 0x800;
145pub const AT_STATX_SYNC_TYPE: int = 0x6000;
146pub const AT_STATX_SYNC_AS_STAT: int = 0x0000;
147pub const AT_STATX_FORCE_SYNC: int = 0x2000;
148pub const AT_STATX_DONT_SYNC: int = 0x4000;
149pub const AT_RECURSIVE: int = 0x8000;
150pub const AT_HANDLE_FID: int = AT_REMOVEDIR;
151
152/// A file descriptor request object for use with [`crate::poll`].
153#[derive(Clone, Copy, Debug)]
154#[repr(C)]
155pub struct pollfd {
156    pub fd: int,
157    pub events: short,
158    pub revents: short,
159}
160
161// The type used to specify the number of file descriptors when calling [`crate::poll`].
162pub type nfds_t = uint;
163
164pub const POLLIN: short = 0x0001;
165pub const POLLPRI: short = 0x0002;
166pub const POLLOUT: short = 0x0004;
167pub const POLLERR: short = 0x0008;
168pub const POLLHUP: short = 0x0010;
169pub const POLLNVAL: short = 0x0020;
170
171/// A type used with [`crate::readv`] and [`crate::writev`].
172#[derive(Clone, Copy, Debug)]
173#[repr(C)]
174pub struct iovec {
175    pub iov_base: *mut void,
176    pub iov_len: size_t,
177}
178
179/// A type used with [`crate::epoll_ctl`].
180#[derive(Clone, Copy, Debug)]
181#[repr(C)]
182pub struct epoll_event {
183    pub events: u32,
184    pub data: epoll_data,
185}
186
187/// A type used with [`crate::epoll_ctl`].
188#[derive(Clone, Copy)]
189#[repr(C)]
190pub union epoll_data {
191    pub ptr: *mut void,
192    pub fd: int,
193    pub u32: u32,
194    pub u64: u64,
195}
196
197impl core::fmt::Debug for epoll_data {
198    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
199        f.debug_struct("epoll_data").finish_non_exhaustive()
200    }
201}
202
203/// A type used with some [`crate::fcntl`] commands.
204#[derive(Clone, Copy, Debug)]
205#[repr(C)]
206pub struct flock {
207    pub l_type: short,
208    pub l_whence: short,
209    pub l_start: off_t,
210    pub l_len: off_t,
211    pub l_pid: pid_t,
212    // TODO: MIPS Linux has an extra field l_sysid and some padding.
213    // We don't support MIPS yet so we're ignoring that, but we'll
214    // need to deal with that if we add MIPS support later.
215    // Sparc also has padding, but no other extra fields.
216}
217
218/// The type for representing socket address families.
219pub type sa_family_t = ushort;
220
221/// The type for representing socket communication model types.
222#[derive(Clone, Copy, Debug)]
223#[repr(C)]
224pub enum sock_type {
225    SOCK_STREAM = 1,
226    SOCK_DGRAM = 2,
227    SOCK_RAW = 3,
228    SOCK_RDM = 4,
229    SOCK_SEQPACKET = 5,
230    SOCK_DCCP = 6,
231    SOCK_PACKET = 10,
232}
233
234/// Used for time in seconds.
235pub type time_t = long;
236
237/// Used for time in microseconds.
238pub type suseconds_t = long;
239
240/// Representation of time as separate seconds and nanoseconds.
241#[derive(Clone, Copy, Debug)]
242#[repr(C)]
243pub struct timespec {
244    pub tv_sec: long,
245    pub tv_nsec: long,
246}
247
248/// Representation of time as separate seconds and microseconds.
249#[derive(Clone, Copy, Debug)]
250#[repr(C)]
251pub struct timeval {
252    pub tv_sec: long,
253    pub tv_usec: suseconds_t,
254}
255
256/// Used for [`crate::getdents`].
257#[derive(Debug)]
258#[repr(C)]
259pub struct linux_dirent {
260    pub d_ino: ulong,
261    pub d_off: ulong,
262    pub d_reclen: ushort,
263    pub d_name: [char],
264}
265
266/// 64-bit offset.
267pub type off64_t = longlong;
268
269// 64-bit inode number.
270pub type ino64_t = ulonglong;
271
272/// Used for [`crate::getdents64`].
273#[derive(Debug)]
274#[repr(C)]
275pub struct linux_dirent64 {
276    pub d_ino: ino64_t,
277    pub d_off: off64_t,
278    pub d_reclen: ushort,
279    pub d_type: uchar,
280    pub d_name: [char],
281}
282
283pub const DT_UNKNOWN: uchar = 0;
284pub const DT_FIFO: uchar = 1;
285pub const DT_CHR: uchar = 2;
286pub const DT_DIR: uchar = 4;
287pub const DT_BLK: uchar = 6;
288pub const DT_REG: uchar = 8;
289pub const DT_LNK: uchar = 10;
290pub const DT_SOCK: uchar = 12;
291pub const DT_WHT: uchar = 14;
292
293#[repr(C)]
294#[derive(Clone, Copy, Debug)]
295pub struct statx {
296    pub stx_mask: u32,
297    pub stx_blksize: u32,
298    pub stx_attributes: u64,
299    pub stx_nlink: u32,
300    pub stx_uid: u32,
301    pub stx_gid: u32,
302    pub stx_mode: u16,
303    pub stx_ino: u64,
304    pub stx_size: u64,
305    pub stx_blocks: u64,
306    pub stx_attributes_mask: u64,
307    pub stx_atime: statx_timestamp,
308    pub stx_btime: statx_timestamp,
309    pub stx_ctime: statx_timestamp,
310    pub stx_mtime: statx_timestamp,
311    pub stx_rdev_major: u32,
312    pub stx_rdev_minor: u32,
313    pub stx_dev_major: u32,
314    pub stx_dev_minor: u32,
315    pub stx_mnt_id: u64,
316    pub stx_dio_mem_align: u32,
317    pub stx_dio_offset_align: u32,
318}
319
320#[repr(C)]
321#[derive(Clone, Copy, Debug)]
322pub struct statx_timestamp {
323    pub tv_sec: i64,
324    pub tv_nsec: u32,
325}
326
327pub const STATX_TYPE: u32 = 1;
328pub const STATX_MODE: u32 = 2;
329pub const STATX_NLINK: u32 = 4;
330pub const STATX_UID: u32 = 8;
331pub const STATX_GID: u32 = 0x10;
332pub const STATX_ATIME: u32 = 0x20;
333pub const STATX_MTIME: u32 = 0x40;
334pub const STATX_CTIME: u32 = 0x80;
335pub const STATX_INO: u32 = 0x100;
336pub const STATX_SIZE: u32 = 0x200;
337pub const STATX_BLOCKS: u32 = 0x400;
338pub const STATX_BASIC_STATS: u32 = 0x7ff;
339pub const STATX_BTIME: u32 = 0x800;
340pub const STATX_ALL: u32 = 0xfff;
341pub const STATX_ATTR_COMPRESSED: u64 = 0x4;
342pub const STATX_ATTR_IMMUTABLE: u64 = 0x10;
343pub const STATX_ATTR_APPEND: u64 = 0x20;
344pub const STATX_ATTR_NODUMP: u64 = 0x40;
345pub const STATX_ATTR_ENCRYPTED: u64 = 0x800;
346pub const STATX_ATTR_AUTOMOUNT: u64 = 0x1000;
347pub const STATX_ATTR_MOUNT_ROOT: u64 = 0x2000;
348pub const STATX_ATTR_VERITY: u64 = 0x100000;
349pub const STATX_ATTR_DAX: u64 = 0x200000;
350
351/// Used for [`crate::io_uring_setup`].
352#[derive(Clone, Copy, Debug)]
353#[repr(C)]
354pub struct io_uring_params {
355    pub sq_entries: u32,
356    pub cq_entries: u32,
357    pub flags: u32,
358    pub sq_thread_cpu: u32,
359    pub sq_thread_idle: u32,
360    pub features: u32,
361    pub wq_fd: u32,
362    pub resv: [u32; 3],
363    pub sq_off: io_sqring_offsets,
364    pub cq_off: io_cqring_offsets,
365}
366
367/// Used with [`io_uring_params`].
368#[derive(Clone, Copy, Debug)]
369#[repr(C)]
370pub struct io_sqring_offsets {
371    pub head: u32,
372    pub tail: u32,
373    pub ring_mask: u32,
374    pub ring_entries: u32,
375    pub flags: u32,
376    pub dropped: u32,
377    pub array: u32,
378    pub resv: [u32; 3],
379}
380
381/// Used with [`io_uring_params`].
382#[derive(Clone, Copy, Debug)]
383#[repr(C)]
384pub struct io_cqring_offsets {
385    pub head: u32,
386    pub tail: u32,
387    pub ring_mask: u32,
388    pub ring_entries: u32,
389    pub overflow: u32,
390    pub cqes: u32,
391    pub flags: u32,
392    pub resv: [u32; 3],
393}
394
395pub use crate::sigset::sigset_t;
396
397// Also include architecture-specific types.
398#[allow(unused_imports)]
399pub use crate::raw::types::*;
400
401pub const FUTEX_WAIT: int = 0;
402pub const FUTEX_WAKE: int = 1;
403pub const FUTEX_FD: int = 2;
404pub const FUTEX_REQUEUE: int = 3;
405pub const FUTEX_CMP_REQUEUE: int = 4;
406pub const FUTEX_WAKE_OP: int = 5;
407pub const FUTEX_LOCK_PI: int = 6;
408pub const FUTEX_UNLOCK_PI: int = 7;
409pub const FUTEX_TRYLOCK_PI: int = 8;
410pub const FUTEX_WAIT_BITSET: int = 9;
411pub const FUTEX_PRIVATE: int = 128;
412pub const FUTEX_CLOCK_REALTIME: int = 256;