Expand description
A low-level, unsafe Rust interface to Linux system calls.
The raw
module provides functions wrapping platform-specific assembly
language stubs for making arbitrary system calls by providing a system
call number and arbitrary number of arguments.
This crate currently supports the following architectures:
- x86_64
- x86 (32-bit)
- arm
- riscv64
For this initial release, x86_64 has seen some limited testing and the other platforms have been barely tested at all. Over time I intend to support all architectures that Linux supports that are also supported by Rust inline assembly, but we’ll see how it goes.
The functions in the root of the crate then wrap those stubs with thin
wrappers that just lightly convert their arguments to what the kernel
expects for a particular system call and then delegate to one of the system
call stubs in raw
.
This crate also includes a number of types and type aliases representing the memory layout of objects the kernel will interpret. For those which are aliases, calling code must always use the aliases rather than their underlying types because their exact definitions may vary on different platforms and in future versions of this crate.
Where possible the wrapping functions and types are portable across architectures, as long as callers use the argument types and type aliases defined in this crate. The raw system call interface has considerable overlap between platforms but is ultimately architecture-specific and this crate does not attempt to hide differences at that layer.
§Be careful mixing with std
The Rust std
crate has lots of functionality that wraps the target’s
libc functions. On Linux systems libc is a wrapper around the same system
call interface this crate is exposing, but also adds other state and
abstractions such as buffers and error codes. Making direct system calls
may violate the assumptions being made by libc.
To avoid strange problems, avoid interacting with the same system resources through both the standard library and though direct system calls.
Modules§
- args
- Supporting traits for preparing values to be system call arguments.
- raw
- Thin wrappers around the CPU instructions for making system calls on x86_64.
- result
- Types representing results from system call wrapper functions.
Structs§
- epoll_
event - A type used with
crate::epoll_ctl
. - flock
- A type used with some
crate::fcntl
commands. - io_
cqring_ offsets - Used with
io_uring_params
. - io_
sqring_ offsets - Used with
io_uring_params
. - io_
uring_ params - Used for
crate::io_uring_setup
. - iovec
- A type used with
crate::readv
andcrate::writev
. - linux_
dirent - Used for
crate::getdents
. - linux_
dirent64 - Used for
crate::getdents64
. - pollfd
- A file descriptor request object for use with
crate::poll
. - sigset_
t - A set of signals for use with signal blocking functions.
- sockaddr
- The type used for representing socket addresses in the raw system calls.
- sockaddr_
storage - Represents the upper limit for the size of any
sockaddr
value, across all address families. - statx
- statx_
timestamp - timespec
- Representation of time as separate seconds and nanoseconds.
- timeval
- Representation of time as separate seconds and microseconds.
Enums§
- sock_
type - The type for representing socket communication model types.
Constants§
- AT_
EACCESS - AT_
EMPTY_ PATH - AT_
FDCWD - AT_
HANDLE_ FID - AT_
NO_ AUTOMOUNT - AT_
RECURSIVE - AT_
REMOVEDIR - AT_
STATX_ DONT_ SYNC - AT_
STATX_ FORCE_ SYNC - AT_
STATX_ SYNC_ AS_ STAT - AT_
STATX_ SYNC_ TYPE - AT_
SYMLINK_ FOLLOW - AT_
SYMLINK_ NOFOLLOW - DT_BLK
- DT_CHR
- DT_DIR
- DT_FIFO
- DT_LNK
- DT_REG
- DT_SOCK
- DT_
UNKNOWN - DT_WHT
- FUTEX_
CLOCK_ REALTIME - FUTEX_
CMP_ REQUEUE - FUTEX_
FD - FUTEX_
LOCK_ PI - FUTEX_
PRIVATE - FUTEX_
REQUEUE - FUTEX_
TRYLOCK_ PI - FUTEX_
UNLOCK_ PI - FUTEX_
WAIT - FUTEX_
WAIT_ BITSET - FUTEX_
WAKE - FUTEX_
WAKE_ OP - O_
ACCMODE - O_
APPEND - O_
CLOEXEC - O_CREAT
- O_
DIRECT - O_
DIRECTORY - O_DSYNC
- O_EXCL
- O_
LARGEFILE - O_
NDELAY - O_
NOATIME - O_
NOCTTY - O_
NOFOLLOW - O_
NONBLOCK - O_PATH
- O_
RDONLY - O_RDWR
- O_SYNC
- O_
TMPFILE - O_
TMPFILE_ MASK - O_TRUNC
- O_
WRONLY - POLLERR
- POLLHUP
- POLLIN
- POLLNVAL
- POLLOUT
- POLLPRI
- SEEK_
CUR - Seek relative to the current file position.
- SEEK_
DATA - Seek to the next data.
- SEEK_
END - Seek relative to the end of the file.
- SEEK_
HOLE - Seek to the next hole.
- SEEK_
SET - Seek relative to the beginning of the file.
- STATX_
ALL - STATX_
ATIME - STATX_
ATTR_ APPEND - STATX_
ATTR_ AUTOMOUNT - STATX_
ATTR_ COMPRESSED - STATX_
ATTR_ DAX - STATX_
ATTR_ ENCRYPTED - STATX_
ATTR_ IMMUTABLE - STATX_
ATTR_ MOUNT_ ROOT - STATX_
ATTR_ NODUMP - STATX_
ATTR_ VERITY - STATX_
BASIC_ STATS - STATX_
BLOCKS - STATX_
BTIME - STATX_
CTIME - STATX_
GID - STATX_
INO - STATX_
MODE - STATX_
MTIME - STATX_
NLINK - STATX_
SIZE - STATX_
TYPE - STATX_
UID
Functions§
- accept⚠
- Accept a connection on a socket.
- accept4⚠
- Accept a connection on a socket with additional flags.
- access⚠
- Check user’s permissions for a file.
- acct⚠
- Switch process accounting on or off.
- alarm⚠
- Set an alarm clock for delivery of a signal.
- bind⚠
- Bind a name to a socket.
- brk⚠
- Set the program break.
- chdir⚠
- Change working directory.
- chmod⚠
- Change permissions of a file.
- chown⚠
- Change ownership of a file.
- chroot⚠
- Change the root directory.
- close⚠
- Close a file.
- close_
range ⚠ - Close all file descriptors in a given range.
- connect⚠
- Initiate a connection on a socket.
- creat⚠
- Create a file.
- dup⚠
- Duplicate a file descriptor.
- dup2⚠
- Duplicate a file descriptor.
- dup3⚠
- Duplicate a file descriptor.
- epoll_
create ⚠ - Open an epoll file descriptor.
- epoll_
create1 ⚠ - Open an epoll file descriptor.
- epoll_
ctl ⚠ - Control interface for an epoll file descriptor.
- epoll_
wait ⚠ - Wait for an I/O event on an epoll file descriptor.
- eventfd⚠
- Create a file descriptor for event notification.
- eventfd2⚠
- Create a file descriptor for event notification.
- exit⚠
- Immediately terminate the current thread, without giving Rust or libc any opportunity to run destructors or other cleanup code.
- exit_
group ⚠ - Immediately terminate all threads in the current process’s thread group, without giving Rust or libc any opportunity to run destructors or other cleanup code.
- faccessat⚠
- Check user’s permissions for a file.
- faccessat2⚠
- Check user’s permissions for a file.
- fchdir⚠
- Change working directory.
- fchmod⚠
- Change permissions of a file.
- fchmodat⚠
- Change permissions of a file.
- fchown⚠
- Change ownership of a file.
- fchownat⚠
- Change ownership of a file.
- fcntl⚠
- Manipulate characteristics of a file descriptor.
- fdatasync⚠
- Synchronize a file’s in-core state with storage device.
- fsync⚠
- Synchronize a file’s in-core state with storage device.
- ftruncate⚠
- Truncate a file to a specified length.
- futex⚠
- Interact with the kernel’s “fast userspace locking” mechanism.
- getcpu⚠
- Determine CPU and NUMA node on which the calling thread is running.
- getcwd⚠
- Get current working directory.
- getdents⚠
- Get directory entries.
- getdents64⚠
- Get directory entries using the new 64-bit structure type.
- getegid⚠
- Get the effective group ID of the current process.
- geteuid⚠
- Get the effective user ID of the current process.
- getgid⚠
- Get the real group ID of the current process.
- getgroups⚠
- Get the supplementary group IDs of the current process.
- getpeername⚠
- Get the address of the peer connected to a socket.
- getpid⚠
- Get the process id (PID) of the current process.
- getppid⚠
- Get the process id (PID) of the parent of the current process.
- getrandom⚠
- Get random bytes from the kernel.
- getresgid⚠
- Get the real GID, the effective GID, and the saved set-group-ID of the current process.
- getresuid⚠
- Get the real UID, the effective UID, and the saved set-user-ID of the current process.
- getsid⚠
- Get the session ID of a process, or of the current process if
pid
is zero. - getsockname⚠
- Get the address that a socket is bound to.
- getsockopt⚠
- Get a socket option.
- gettid⚠
- Get the thread id (TID) of the current process.
- getuid⚠
- Get the real user ID of the current process.
- inotify_
add_ ⚠watch - Adds a new watch, or modifies an existing watch, to an inotify event queue.
- inotify_
init ⚠ - Initializes a new inotify instance and returns a file descriptor associated with a new inotify event queue.
- inotify_
init1 ⚠ - Initializes a new inotify instance and returns a file descriptor associated with a new inotify event queue.
- inotify_
rm_ ⚠watch - Removes an existing watch from an inotify event queue.
- io_
uring_ ⚠enter - Initiate and complete I/O using the shared submission and completion queues for an io_uring instance.
- io_
uring_ ⚠register - Registers resources for use with an io_uring instance.
- io_
uring_ ⚠setup - Sets up an io_uring instance.
- ioctl⚠
- Arbitrary requests for file descriptors representing devices.
- kill⚠
- Send a signal to a process.
- lchown⚠
- Change ownership of a file without dereferencing symbolic links.
- link⚠
- Create a new link (a “hard link”) for an existing file.
- linkat⚠
- Create a new link (a “hard link”) for an existing file relative to directory file descriptors.
- listen⚠
- Listen for connections on a socket.
- lseek⚠
- Reposition the read/write offset for a file.
- mmap⚠
- Map a file or device into memory.
- mremap⚠
- Change a memory mapping previously created with
mmap
. - munmap⚠
- Remove a mapping previously created with
mmap
. - open⚠
- Open a file.
- openat⚠
- Open a file.
- pause⚠
- Pause the current process until a signal is delivered.
- pidfd_
open ⚠ - Create a file descriptor representing a process.
- pipe⚠
- Create pipe.
- pipe2⚠
- Create pipe.
- pivot_
root ⚠ - Changes the root mount in the mount namespace of the calling process.
- poll⚠
- Wait for events on one or more file descriptors.
- ppoll⚠
- Wait for events on one or more file descriptors while also awaiting signals.
- prctl⚠
- Manipulates various aspects of the behavior of the calling thread or process.
- read⚠
- Read from a file descriptor.
- readlink⚠
- Read value of a symbolic link.
- readlinkat⚠
- Read value of a symbolic link relative to a directory file descriptor.
- readv⚠
- Read from a file descriptor into multiple buffers.
- sendfile⚠
- Copies data between one file descriptor and another.
- setsockopt⚠
- Set a socket option.
- socket⚠
- Create a socket endpoint for communication.
- socketpair⚠
- Create an unnamed pair of connected sockets.
- splice⚠
- Moves data between two file descriptors without copying between kernel address space and user address space.
- statx⚠
- Get file status, extended.
- sync⚠
- Commit all filesystem caches to disk.
- syncfs⚠
- Commit filesystem caches to disk for the filesystem containing a particular file.
- tee⚠
- Duplicates data between file descriptors without consuming the data at the source.
- truncate⚠
- Truncate a file to a specified length.
- write⚠
- Write to a file descriptor.
- writev⚠
- Write to a file descriptor from multiple buffers.
Type Aliases§
- char
- The type used for characters on the current platform.
- gid_t
- The type used to represent group ids.
- ino64_t
- int
- The primary signed integer type for the current platform.
- loff_t
- The type used to represent larger file sizes and offsets into files on the current platform.
- long
- The signed long integer type for the current platform.
- longlong
- The signed long long integer type for the current platform.
- mode_t
- The type used to represent file modes on the current platform.
- nfds_t
- off64_t
- 64-bit offset.
- off_t
- The type used to represent file sizes and offsets into files on the current platform.
- pid_t
- The type used for process identifiers (PIDs) on the current platform.
- sa_
family_ t - The type for representing socket address families.
- short
- The short signed integer type for the current platform.
- size_t
- The unsigned size type for the current platform.
- socklen_
t - The type used for representing the length of a socket address.
- ssize_t
- The signed size type (or “pointer difference” type) for the current platform.
- suseconds_
t - Used for time in microseconds.
- time_t
- Used for time in seconds.
- uchar
- The type used for unsigned characters on the current platform.
- uid_t
- The type used to represent user ids.
- uint
- The primary unsigned integer type for the current platform.
- ulong
- The unsigned long integer type for the current platform.
- ulonglong
- The unsigned long long integer type for the current platform.
- ushort
- The short unsigned integer type for the current platform.
- void
- The type used for void pointers on the current platform.
Unions§
- epoll_
data - A type used with
crate::epoll_ctl
.