From caa5ef8afbb38bbd5db0a23116262f90f6c08f07 Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 25 Mar 2026 07:40:42 +0200 Subject: [PATCH] chore(signal): Move alarm function and adding the todo --- src/platform/linux/signal.rs | 26 ++++++++++++++++++++++++-- src/platform/pal/signal.rs | 29 ++--------------------------- src/platform/redox/signal.rs | 4 ++++ 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/platform/linux/signal.rs b/src/platform/linux/signal.rs index da2119d47b..3459ef62ee 100644 --- a/src/platform/linux/signal.rs +++ b/src/platform/linux/signal.rs @@ -6,7 +6,7 @@ use core::{ use super::{ super::{ PalSignal, - types::{c_int, pid_t}, + types::{c_int, c_uint, pid_t, time_t}, }, Sys, e_raw, }; @@ -15,8 +15,10 @@ use crate::{ header::{ bits_time::timespec, signal::{SA_RESTORER, SI_QUEUE, sigaction, siginfo_t, sigset_t, sigval, stack_t}, - sys_time::itimerval, + sys_select::timeval, + sys_time::{self, itimerval}, }, + platform, }; impl PalSignal for Sys { @@ -68,6 +70,26 @@ impl PalSignal for Sys { Ok(()) } + /// See . + fn alarm(seconds: c_uint) -> c_uint { + let timer = itimerval { + it_value: timeval { + tv_sec: time_t::from(seconds), + tv_usec: 0, + }, + ..Default::default() + }; + let mut otimer = itimerval::default(); + let errno_backup = platform::ERRNO.get(); + let secs = if Self::setitimer(sys_time::ITIMER_REAL, &timer, Some(&mut otimer)).is_err() { + 0 + } else { + otimer.it_value.tv_sec as c_uint + if otimer.it_value.tv_usec > 0 { 1 } else { 0 } + }; + platform::ERRNO.set(errno_backup); + secs + } + fn sigaction( sig: c_int, act: Option<&sigaction>, diff --git a/src/platform/pal/signal.rs b/src/platform/pal/signal.rs index b8ed26acea..e703358d72 100644 --- a/src/platform/pal/signal.rs +++ b/src/platform/pal/signal.rs @@ -4,9 +4,8 @@ use crate::{ header::{ bits_time::timespec, signal::{sigaction, siginfo_t, sigset_t, sigval, stack_t}, - sys_time::{self, itimerval}, + sys_time::itimerval, }, - platform, }; pub trait PalSignal: Pal { @@ -22,31 +21,7 @@ pub trait PalSignal: Pal { fn setitimer(which: c_int, new: &itimerval, old: Option<&mut itimerval>) -> Result<()>; - /// See . - /// - /// Default implementation uses setitimer(ITIMER_REAL). Platforms that do not - /// support setitimer (e.g. Redox) must override this. - fn alarm(seconds: c_uint) -> c_uint { - use crate::header::sys_select::timeval; - - let timer = itimerval { - it_value: timeval { - tv_sec: time_t::from(seconds), - tv_usec: 0, - }, - ..Default::default() - }; - let mut otimer = itimerval::default(); - - let errno_backup = platform::ERRNO.get(); - let secs = if Self::setitimer(sys_time::ITIMER_REAL, &timer, Some(&mut otimer)).is_err() { - 0 - } else { - otimer.it_value.tv_sec as c_uint + if otimer.it_value.tv_usec > 0 { 1 } else { 0 } - }; - platform::ERRNO.set(errno_backup); - secs - } + fn alarm(seconds: c_uint) -> c_uint; fn sigaction(sig: c_int, act: Option<&sigaction>, oact: Option<&mut sigaction>) -> Result<()>; diff --git a/src/platform/redox/signal.rs b/src/platform/redox/signal.rs index dd47fa6c80..c7a36819cb 100644 --- a/src/platform/redox/signal.rs +++ b/src/platform/redox/signal.rs @@ -278,6 +278,10 @@ impl PalSignal for Sys { /// Accepts a full timespec so sub-second timers (ualarm) can reuse this later. /// Returns the number of seconds remaining on the previous alarm (rounded up), /// or 0 if there was no previous alarm. +/// +/// TODO: This implementation does not survive `exec()`. POSIX requires that a +/// pending alarm be preserved across exec (the timer continues counting down +/// in the new process image as i understand). fn alarm_timespec(duration: timespec) -> c_uint { let mut guard = ALARM_TIMER.lock();