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

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 directory entries.
  • Get directory entries using the new 64-bit structure type.
  • Get the effective group ID of the current process.
  • Get the effective user ID of the current process.
  • Get the real group ID of the current process.
  • Get the supplementary group IDs of the current process.
  • Get the address of the peer connected to a socket.
  • Get the process id (PID) of the current process.
  • Get the process id (PID) of the parent of the current process.
  • Get random bytes from the kernel.
  • Get the real GID, the effective GID, and the saved set-group-ID of the current process.
  • Get the real UID, the effective UID, and the saved set-user-ID of the current process.
  • Get the session ID of a process, or of the current process if pid is zero.
  • Get the address that a socket is bound to.
  • Get a socket option.
  • Get the thread id (TID) of the current process.
  • Get the real user ID of the current process.
  • Adds a new watch, or modifies an existing watch, to an inotify event queue.
  • Initializes a new inotify instance and returns a file descriptor associated with a new inotify event queue.
  • Initializes a new inotify instance and returns a file descriptor associated with a new inotify event queue.
  • Removes an existing watch from an inotify event queue.
  • Initiate and complete I/O using the shared submission and completion queues for an io_uring instance.
  • Registers resources for use with an io_uring instance.
  • Sets up an io_uring instance.
  • Arbitrary requests for file descriptors representing devices.
  • kill
    Send a signal to a process.
  • Change ownership of a file without dereferencing symbolic links.
  • link
    Create a new link (a “hard link”) for an existing file.
  • Create a new link (a “hard link”) for an existing file relative to directory file descriptors.
  • Listen for connections on a socket.
  • Reposition the read/write offset for a file.
  • mmap
    Map a file or device into memory.
  • Change a memory mapping previously created with mmap.
  • Remove a mapping previously created with mmap.
  • open
    Open a file.
  • Pause the current process until a signal is delivered.
  • Create a file descriptor representing a process.
  • pipe
    Create pipe.
  • Create pipe.
  • Changes the root mount in the mount namespace of the calling process.
  • poll
    Wait for events on one or more file descriptors.
  • Wait for events on one or more file descriptors while also awaiting signals.
  • Manipulates various aspects of the behavior of the calling thread or process.
  • read
    Read from a file descriptor.
  • Read from a file descriptor into multiple buffers.
  • Copies data between one file descriptor and another.
  • Set a socket option.
  • Create a socket endpoint for communication.
  • Create an unnamed pair of connected sockets.
  • Moves data between two file descriptors without copying between kernel address space and user address space.
  • sync
    Commit all filesystem caches to disk.
  • 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 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 signed long long integer type for the current platform.
  • The type used to represent file modes on the current platform.
  • 64-bit offset.
  • 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 signed size type (or “pointer difference” type) for the current platform.
  • Used for time in microseconds.
  • Used for time in seconds.
  • The type used for unsigned characters on 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 unsigned long 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