Crate linux_unsafe

source ·
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

Supporting traits for preparing values to be system call arguments.
Thin wrappers around the CPU instructions for making system calls on x86_64.
Types representing results from system call wrapper functions.

Structs

A type used with crate::epoll_ctl.
A type used with some crate::fcntl commands.
A type used with crate::readv and crate::writev.
A file descriptor request object for use with crate::poll.

Enums

The type for representing socket communication model types.

Constants

Functions

Accept a connection on a socket.
Accept a connection on a socket with additional flags.
Check user’s permissions for a file.
acct
Switch process accounting on or off.
Set an alarm clock for delivery of a signal.
bind
Bind a name to a socket.
brk
Set the program break.
Change working directory.
Change permissions of a file.
Change ownership of a file.
Change the root directory.
Close a file.
Close all file descriptors in a given range.
Initiate a connection on a socket.
Create a file.
dup
Duplicate a file descriptor.
dup2
Duplicate a file descriptor.
dup3
Duplicate a file descriptor.
Open an epoll file descriptor.
Open an epoll file descriptor.
Control interface for an epoll file descriptor.
Wait for an I/O event on an epoll file descriptor.
Create a file descriptor for event notification.
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.
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.
Check user’s permissions for a file.
Check user’s permissions for a file.
Change working directory.
Change permissions of a file.
Change permissions of a file.
Change ownership of a file.
Change ownership of a file.
Manipulate characteristics of a file descriptor.
Synchronize a file’s in-core state with storage device.
Synchronize a file’s in-core state with storage device.
Truncate a file to a specified length.
Determine CPU and NUMA node on which the calling thread is running.
Get current working directory.
Get the process id (PID) of the current process.
Get a socket option.
Arbitrary requests for file descriptors representing devices.
Listen for connections on a socket.
Reposition the read/write offset for a file.
mmap
Map a file or device into memory.
Remove a mapping previously created with mmap.
open
Open a file.
pipe
Create pipe.
Create pipe.
poll
Wait for events on one or more file descriptors.
read
Read from a file descriptor.
Read from a file descriptor into multiple buffers.
Set a socket option.
Create a socket endpoint for communication.
sync
Commit all filesystem caches to disk.
Commit filesystem caches to disk for the filesystem containing a particular file.
Truncate a file to a specified length.
Write to a file descriptor.
Write to a file descriptor from multiple buffers.

Type Definitions

The type used for characters on the current platform.
The type used to represent group ids.
The primary signed integer type for the current platform.
The type used to represent larger file sizes and offsets into files on the current platform.
The signed long integer type for the current platform.
The type used to represent file modes on the current platform.
The type used to represent file sizes and offsets into files on the current platform.
The type used for process identifiers (PIDs) on the current platform.
The type for representing socket address families.
The short signed integer type for the current platform.
The unsigned size type for the current platform.
The type used for representing the length of a socket address.
The type used for representing the length of a socket address.
The signed size type (or “pointer difference” type) for the current platform.
The type used to represent user ids.
The primary unsigned integer type for the current platform.
The unsigned long integer type for the current platform.
The short unsigned integer type for the current platform.
The type used for void pointers on the current platform.

Unions

A type used with crate::epoll_ctl.