diff options
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/platform/linux.rs | 29 |
4 files changed, 28 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f8f2578..5db885b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [Unreleased] +- linux: move pid file path to `/run/dpibreak.pid` and log file path (on daemon) to `/var/log/dpibreak.log`. (Fixes #16) +- linux: add guard check if euid is root. (#16) + ## [DPIBreak v0.4.1] - 2026-02-15 Linux only hotfix: @@ -36,7 +36,7 @@ etherparse = "0.18" [target.'cfg(target_os = "linux")'.dependencies] nfq = { package = "nfq-updated", version = "0.2.6" } # nfq-updated: to us as_raw_fd socket2 = { version = "0.6", features = ["all"] } -nix = { version = "0.27.1", features = ["fs", "poll"] } +nix = { version = "0.27.1", features = ["fs", "poll", "user"] } serde_json = "1.0.145" daemonize = "0.5.0" diff --git a/src/main.rs b/src/main.rs index f458e24..23d8e09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,9 +70,7 @@ fn main_0() -> Result<()> { } opt.set_opt()?; splash_banner(); - if !opt::daemon() { - platform::bootstrap()?; - } + platform::bootstrap()?; let _guard = EnsureCleanup; diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 3668fe8..eb550dd 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -8,7 +8,7 @@ use std::process::{Command, Stdio}; use std::io::Write; use anyhow::{Result, Context, anyhow}; -use crate::{log::LogLevel, log_println, splash, MESSAGE_AT_RUN}; +use crate::{log::LogLevel, log_println, splash, MESSAGE_AT_RUN, opt}; mod iptables; mod nftables; @@ -20,7 +20,7 @@ pub static IS_U32_SUPPORTED: AtomicBool = AtomicBool::new(false); pub static IS_NFT_NOT_SUPPORTED: AtomicBool = AtomicBool::new(false); const INJECT_MARK: u32 = 0xD001; -const PID_FILE: &str = "/tmp/dpibreak.pid"; // TODO: unmagic this +const PID_FILE: &str = "/run/dpibreak.pid"; // TODO: unmagic this const PKG_NAME: &str = env!("CARGO_PKG_NAME"); fn exec_process(args: &[&str], input: Option<&str>) -> Result<()> { @@ -98,10 +98,7 @@ pub fn cleanup() -> Result<()> { Ok(()) } - -/// Only called on non-daemon run. Fail if running dpibreak is -/// existing. -pub fn bootstrap() -> Result<()> { +fn lock_pid_file() -> Result<()> { use nix::fcntl::{flock, FlockArg}; use std::fs::OpenOptions; @@ -125,6 +122,23 @@ pub fn bootstrap() -> Result<()> { Ok(()) } +fn exit_if_not_root() { + if !nix::unistd::geteuid().is_root() { + log_println!(LogLevel::Error, "{PKG_NAME} must be run as root. Try sudo."); + std::process::exit(3); + } +} + +/// Bootstraps that don't require cleanup after load global opts +pub fn bootstrap() -> Result<()> { + exit_if_not_root(); + if !opt::daemon() { + lock_pid_file()?; + } + + Ok(()) +} + use socket2::{Domain, Protocol, Socket, Type}; static RAW4: LazyLock<Mutex<Socket>> = LazyLock::new(|| { @@ -246,12 +260,13 @@ pub fn run() -> Result<()> { Ok(()) } -const DAEMON_PREFIX: &str = "/tmp"; +const DAEMON_PREFIX: &str = "/var/log"; fn daemonize() -> Result<()> { use std::fs; use daemonize::Daemonize; + fs::create_dir_all(DAEMON_PREFIX).context("daemonize")?; let log_file = fs::File::create(format!("{DAEMON_PREFIX}/{PKG_NAME}.log"))?; let daemonize = Daemonize::new() |
