diff options
| author | namilsk <namilsk@namilsk.tech> | 2026-03-06 22:42:10 +0300 |
|---|---|---|
| committer | namilsk <namilsk@namilsk.tech> | 2026-03-06 22:42:10 +0300 |
| commit | 772093279e1dd162a47e2dfe50d9a9ae0192d750 (patch) | |
| tree | 49b10a7543cb43fe1c024f327bf7a55d791ccd0b /init/src/main.rs | |
| parent | 733fa015a0499139a2dc171503fa48701fcba1d0 (diff) | |
implemented `SIGUSER/SIGINT/SIGTERM` handlers, graceful shutdown, logging to file
Diffstat (limited to 'init/src/main.rs')
| -rw-r--r-- | init/src/main.rs | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/init/src/main.rs b/init/src/main.rs index 6821bd6..a3492d1 100644 --- a/init/src/main.rs +++ b/init/src/main.rs @@ -10,26 +10,31 @@ mod signals; use crate::host::locale; use crate::host::set::set_hostname; use crate::kmods::load::load_modules; -use crate::log::{log_critical_error, log_success, log_warning}; +use crate::log::{init_logging, log_critical_error, log_success, log_warning}; use crate::mounts::fstab::FstabEntry; use crate::mounts::rescue; use crate::pid_one::check_pid; use crate::processes::udev::spawn_udev; use crate::services::units::{Runlevel, services_mainloop}; use crate::signals::sigchld; +use crate::signals::sigterm::{self, RELOAD_REQUESTED, DEBUG_DUMP_REQUESTED}; // RULE: I will not use .expect() and .unwrap() in this project. This causes panic, // which will affect stability. use std::sync::Mutex; use std::thread; +use std::time::Duration; /// Variable which show current runlevel; pub static RUNLEVEL_STATE: Mutex<Runlevel> = Mutex::new(Runlevel::Undefined); // TODO: Add proper RUNLEVEL_STATE switching fn main() -> Result<(), Box<dyn std::error::Error>> { - println!("Initializing your system."); + if let Err(e) = init_logging() { + eprintln!("Failed to initialize logging: {}", e); + } + if let Err(e) = check_pid() { log_critical_error(&format!( "Runned not as first process. init/src/pid_one.rs:8:8 - {}", @@ -38,6 +43,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { return Err(e); } + sigchld::setup_sigchld_handler()?; + sigterm::setup_signal_handlers()?; + match FstabEntry::parse_fstab("/etc/fstab") { Ok(entries) => { log_success("Sucessfully parsed /etc/fstab."); @@ -77,9 +85,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { Err(e) => log_warning(&format!("Failed to set localization: {}", e)), } - let _ = sigchld::setup_sigchld_handler(); - thread::spawn(services_mainloop); - Ok(()) + log_success("System initialization complete. Entering main loop."); + + loop { + if sigterm::SHUTDOWN_REQUESTED.load(std::sync::atomic::Ordering::SeqCst) { + log_warning("Shutdown signal received."); + let _ = sigterm::graceful_shutdown(); + break Ok(()); + } + + if RELOAD_REQUESTED.swap(false, std::sync::atomic::Ordering::SeqCst) { + log_warning("SIGUSR1 received - config reload requested (not implemented yet)."); + // TODO: Configs reload + } + + // SIGUSR2 (debug dump) + if DEBUG_DUMP_REQUESTED.swap(false, std::sync::atomic::Ordering::SeqCst) { + log_warning("SIGUSR2 received - debug dump requested (not implemented yet)."); + // TODO: debug dump + } + + // Sleep to not disturb CPU + // TODO: io_uring / epolls + thread::sleep(Duration::from_secs(1)); + } } |
