Skip to content
Unknown2077Public

About

Lightweight Linux low-memory and OOM prevention daemon written in Rust

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

oxihang

CI Crates.io License: MIT

Lightweight Linux low-memory and OOM prevention daemon written in Rust (inspired by hakavlad/nohang).

oxihang monitors RAM, swap, zram, and Linux PSI pressure before the kernel stalls under memory thrashing, terminates or runs custom corrective actions against the highest-badness process, and notifies active Wayland/X11 desktop sessions over D-Bus.

Features

  • Minimal resident footprint: ~2.1 MiB stripped LTO binary, ~2.5–4.0 MiB resident memory (VmRSS), persistent /proc file handles, and mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) so resident pages stay locked under pressure without faulting unused virtual mappings.
  • Declarative configuration: Supports @BADNESS_ADJ_RE_*, @SOFT_ACTION_RE_*, @check_kmsg, % and M thresholds, PSI, and zram rules.
  • Host-aware config generator: --generate-config probes RAM, swap, zram, PSI, ZFS ARC, and running desktop/audio sessions to emit a self-validated configuration tailored to the current machine.
  • Multi-signal pressure detection: Combines MemAvailable (plus reclaimable ZFS ARC), SwapFree, zram usage (mm_stat / mem_used_total), and Linux PSI (/proc/pressure/memory when SwapTotal > 0) with sustained exceedance tracking.
  • Race-free victim signaling: Identifies victims by (pid, starttime) and signals via pidfd_open + pidfd_send_signal (falling back to verified kill(2) on older kernels), escalating SIGTERM → SIGKILL after max_soft_exit_time.
  • Kernel OOM watcher (@check_kmsg): Non-blocking /dev/kmsg reader thread with lock-free atomics to coordinate post-OOM cooldowns and avoid duplicate kills.
  • Safe custom actions: Direct argv execution (no /bin/sh) with bounded workers, process-group isolation, and hard timeouts for @SOFT_ACTION_RE_* / post_kill_exe ($PID, $NAME, $SERVICE) and warning_exe.
  • Zero-dependency desktop notifications: Pure-Rust org.freedesktop.Notifications.Notify client over Unix path and abstract D-Bus sockets for both Wayland and X11 (no sudo, doas, notify-send, or zbus).

Requirements

  • OS: Linux 3.14+ (MemAvailable in /proc/meminfo; Linux 4.20+ for PSI, Linux 5.3+ for pidfd_open / pidfd_send_signal)
  • Build: Rust 1.88+ (edition 2024)

Installation

From crates.io

cargo install oxihang

From source

git clone https://github.com/Unknown2077/oxihang.git
cd oxihang
cargo build --release

The compiled binary is placed at target/release/oxihang.

Quick Start

# 1. Generate a config tuned for the current machine
oxihang --generate-config -c /tmp/oxihang.conf

# 2. Inspect how current processes are scored
oxihang --tasks -c /tmp/oxihang.conf

# 3. Test monitoring safely without sending signals, commands, or notifications
oxihang --monitor --dry-run -c /tmp/oxihang.conf

CLI Reference

Option Description
-h, --help Show help text and exit
-v, --version Show version and exit
-c, --config PATH Config file path (-c=PATH, -cPATH, --config=PATH also accepted; optional output path for --generate-config)
--generate-config [auto|default|desktop] Generate a host-tuned config to stdout or -c PATH (never overwrites an existing path; --generate-config=PROFILE also accepted)
--check Validate and print the parsed configuration, then exit
--tasks Print the scored process table and exit
--monitor Start live memory pressure monitoring
--dry-run Log decisions without signaling processes, spawning commands, or sending notifications (valid only with --monitor)

Examples

# Print an auto-detected config to stdout
oxihang --generate-config

# Write an explicit desktop config (refuses to overwrite existing paths)
oxihang --generate-config desktop -c /etc/oxihang/oxihang-desktop.conf

# Validate a configuration against current host memory/swap
oxihang --check -c /etc/oxihang/oxihang-desktop.conf

# Run the live daemon in the foreground
sudo oxihang --monitor -c /etc/oxihang/oxihang-desktop.conf

Configuration

Two baseline profiles are shipped under config/:

  • config/oxihang.conf — headless/server profile (notifications disabled by default).
  • config/oxihang-desktop.conf — desktop/laptop profile (@check_kmsg, PSI, low-memory warnings, D-Bus notifications, and browser/Electron renderer scoring).

Running oxihang --generate-config starts from the matching profile and automatically adjusts zram_checking_enabled, psi_checking_enabled, and negative badness protection (-300) for active desktop compositors, shells, and audio daemons.

Note on zram: On systems where swap is backed primarily by zram, set zram_checking_enabled = True (handled automatically by --generate-config) so memory exhaustion is still caught when SwapFree remains above its threshold.

Systemd Installation

Two mutually conflicting, hardened service units are provided in systemd/:

sudo install -Dm755 target/release/oxihang /usr/local/bin/oxihang
sudo mkdir -p /etc/oxihang

# Option A: generate a host-tuned config directly
sudo /usr/local/bin/oxihang --generate-config desktop -c /etc/oxihang/oxihang-desktop.conf

# Option B: install the shipped static configs instead
# sudo install -Dm644 config/oxihang.conf /etc/oxihang/oxihang.conf
# sudo install -Dm644 config/oxihang-desktop.conf /etc/oxihang/oxihang-desktop.conf

# Enable ONE service unit (desktop example shown; use oxihang.service on headless systems)
sudo install -Dm644 systemd/oxihang-desktop.service /etc/systemd/system/oxihang-desktop.service
sudo systemctl daemon-reload
sudo systemctl enable --now oxihang-desktop.service

Note on systemd-oomd: Both units set ManagedOOMPreference=omit so systemd-oomd does not kill oxihang itself. If you want oxihang to be the sole low-memory handler, disable systemd-oomd or tune thresholds so the two daemons do not compete.

Security & Hardening

  • Privilege isolation: Systemd units apply OOMScoreAdjust=-1000, OOMPolicy=continue, MemoryMax=64M, MemorySwapMax=0, TasksMax=64, ProtectSystem=strict, InaccessiblePaths=/home /root, MemoryDenyWriteExecute=true, RestrictNamespaces=true, DevicePolicy=closed (/dev/kmsg r), and a minimal CapabilityBoundingSet (CAP_KILL, CAP_IPC_LOCK, CAP_SYS_PTRACE, CAP_DAC_READ_SEARCH, CAP_SETUID, CAP_SETGID, CAP_SYSLOG).
  • Per-thread D-Bus credential drop: Notification worker threads permanently drop UID/GID/supplementary groups via raw Linux syscalls before connecting to user session buses and verify SO_PEERCRED peer UID ownership.
  • No shell injection: Custom actions tokenize and expand $PID, $NAME, and $SERVICE directly into argv without invoking /bin/sh.

License

MIT — see LICENSE.

About

Lightweight Linux low-memory and OOM prevention daemon written in Rust

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages