From 536b52430addcfc8109b8d0e855272d416964e40 Mon Sep 17 00:00:00 2001 From: HimbeerserverDE Date: Thu, 29 Dec 2022 13:11:32 +0100 Subject: refuse to run as pid != 1 --- src/main.rs | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 75acff4..059f23f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use anyhow::bail; use std::fs; use std::io::Write; -use std::process::Command; +use std::process::{self, Command, ExitCode}; use std::thread; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; @@ -43,22 +43,34 @@ fn start() -> anyhow::Result<()> { Ok(()) } -fn main() { - match start() { - Ok(_) => {} - Err(e) => { - let mut stdout = StandardStream::stdout(ColorChoice::Always); +fn main() -> ExitCode { + let mut stdout = StandardStream::stdout(ColorChoice::Always); - match stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))) { - Ok(_) => match writeln!(&mut stdout, "[ ERROR ] {}", e) { - Ok(_) => {} - Err(_) => println!("[ ERROR ] {}", e), - }, - Err(_) => { - println!("[ ERROR ] {}", e); - } + if process::id() != 1 { + match stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))) { + Ok(_) => match writeln!(&mut stdout, "Must be run as PID 1") { + Ok(_) => {} + Err(_) => println!("Must be run as PID 1"), + }, + Err(_) => { + println!("Must be run as PID 1"); } } + + return ExitCode::FAILURE; + } + + match start() { + Ok(_) => {} + Err(e) => match stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))) { + Ok(_) => match writeln!(&mut stdout, "[ ERROR ] {}", e) { + Ok(_) => {} + Err(_) => println!("[ ERROR ] {}", e), + }, + Err(_) => { + println!("[ ERROR ] {}", e); + } + }, } loop { -- cgit v1.2.3