|
| 1 | +//! Signal numbers and constants following Linux x86_64 conventions |
| 2 | +
|
| 3 | +// Standard signals (1-31) |
| 4 | +pub const SIGHUP: u32 = 1; |
| 5 | +pub const SIGINT: u32 = 2; |
| 6 | +pub const SIGQUIT: u32 = 3; |
| 7 | +pub const SIGILL: u32 = 4; |
| 8 | +pub const SIGTRAP: u32 = 5; |
| 9 | +pub const SIGABRT: u32 = 6; |
| 10 | +pub const SIGBUS: u32 = 7; |
| 11 | +pub const SIGFPE: u32 = 8; |
| 12 | +pub const SIGKILL: u32 = 9; // Cannot be caught or blocked |
| 13 | +pub const SIGUSR1: u32 = 10; |
| 14 | +pub const SIGSEGV: u32 = 11; |
| 15 | +pub const SIGUSR2: u32 = 12; |
| 16 | +pub const SIGPIPE: u32 = 13; |
| 17 | +pub const SIGALRM: u32 = 14; |
| 18 | +pub const SIGTERM: u32 = 15; |
| 19 | +pub const SIGSTKFLT: u32 = 16; |
| 20 | +pub const SIGCHLD: u32 = 17; |
| 21 | +pub const SIGCONT: u32 = 18; |
| 22 | +pub const SIGSTOP: u32 = 19; // Cannot be caught or blocked |
| 23 | +pub const SIGTSTP: u32 = 20; |
| 24 | +pub const SIGTTIN: u32 = 21; |
| 25 | +pub const SIGTTOU: u32 = 22; |
| 26 | +pub const SIGURG: u32 = 23; |
| 27 | +pub const SIGXCPU: u32 = 24; |
| 28 | +pub const SIGXFSZ: u32 = 25; |
| 29 | +pub const SIGVTALRM: u32 = 26; |
| 30 | +pub const SIGPROF: u32 = 27; |
| 31 | +pub const SIGWINCH: u32 = 28; |
| 32 | +pub const SIGIO: u32 = 29; |
| 33 | +pub const SIGPWR: u32 = 30; |
| 34 | +pub const SIGSYS: u32 = 31; |
| 35 | + |
| 36 | +// Real-time signals (32-64) - for future use |
| 37 | +pub const SIGRTMIN: u32 = 32; |
| 38 | +pub const SIGRTMAX: u32 = 64; |
| 39 | + |
| 40 | +/// Maximum signal number supported |
| 41 | +pub const NSIG: u32 = 64; |
| 42 | + |
| 43 | +// Signal handler special values |
| 44 | +/// Default action for the signal |
| 45 | +pub const SIG_DFL: u64 = 0; |
| 46 | +/// Ignore the signal |
| 47 | +pub const SIG_IGN: u64 = 1; |
| 48 | + |
| 49 | +// sigprocmask "how" values |
| 50 | +/// Block signals in set |
| 51 | +pub const SIG_BLOCK: i32 = 0; |
| 52 | +/// Unblock signals in set |
| 53 | +pub const SIG_UNBLOCK: i32 = 1; |
| 54 | +/// Set blocked signals to set |
| 55 | +pub const SIG_SETMASK: i32 = 2; |
| 56 | + |
| 57 | +// sigaction flags |
| 58 | +/// Restart interrupted syscalls |
| 59 | +#[allow(dead_code)] // Part of POSIX sigaction API, used by userspace |
| 60 | +pub const SA_RESTART: u64 = 0x10000000; |
| 61 | +/// Don't block signal during handler |
| 62 | +pub const SA_NODEFER: u64 = 0x40000000; |
| 63 | +/// Provide siginfo_t to handler |
| 64 | +#[allow(dead_code)] // Part of POSIX sigaction API, used by userspace |
| 65 | +pub const SA_SIGINFO: u64 = 0x00000004; |
| 66 | +/// Use alternate signal stack |
| 67 | +#[allow(dead_code)] // Part of POSIX sigaction API, used by userspace |
| 68 | +pub const SA_ONSTACK: u64 = 0x08000000; |
| 69 | +/// Provide restorer function |
| 70 | +#[allow(dead_code)] // Part of POSIX sigaction API, used by userspace |
| 71 | +pub const SA_RESTORER: u64 = 0x04000000; |
| 72 | + |
| 73 | +/// Convert signal number to bit mask |
| 74 | +/// |
| 75 | +/// Returns 0 for invalid signal numbers (0 or > NSIG) |
| 76 | +#[inline] |
| 77 | +pub const fn sig_mask(sig: u32) -> u64 { |
| 78 | + if sig == 0 || sig > NSIG { |
| 79 | + 0 |
| 80 | + } else { |
| 81 | + 1u64 << (sig - 1) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Signals that cannot be caught, blocked, or ignored |
| 86 | +pub const UNCATCHABLE_SIGNALS: u64 = sig_mask(SIGKILL) | sig_mask(SIGSTOP); |
| 87 | + |
| 88 | +/// Check if a signal number is valid |
| 89 | +#[inline] |
| 90 | +pub const fn is_valid_signal(sig: u32) -> bool { |
| 91 | + sig > 0 && sig <= NSIG |
| 92 | +} |
| 93 | + |
| 94 | +/// Check if a signal can be caught/blocked |
| 95 | +#[inline] |
| 96 | +pub const fn is_catchable(sig: u32) -> bool { |
| 97 | + sig != SIGKILL && sig != SIGSTOP |
| 98 | +} |
| 99 | + |
| 100 | +/// Get signal name for debugging |
| 101 | +pub fn signal_name(sig: u32) -> &'static str { |
| 102 | + match sig { |
| 103 | + SIGHUP => "SIGHUP", |
| 104 | + SIGINT => "SIGINT", |
| 105 | + SIGQUIT => "SIGQUIT", |
| 106 | + SIGILL => "SIGILL", |
| 107 | + SIGTRAP => "SIGTRAP", |
| 108 | + SIGABRT => "SIGABRT", |
| 109 | + SIGBUS => "SIGBUS", |
| 110 | + SIGFPE => "SIGFPE", |
| 111 | + SIGKILL => "SIGKILL", |
| 112 | + SIGUSR1 => "SIGUSR1", |
| 113 | + SIGSEGV => "SIGSEGV", |
| 114 | + SIGUSR2 => "SIGUSR2", |
| 115 | + SIGPIPE => "SIGPIPE", |
| 116 | + SIGALRM => "SIGALRM", |
| 117 | + SIGTERM => "SIGTERM", |
| 118 | + SIGSTKFLT => "SIGSTKFLT", |
| 119 | + SIGCHLD => "SIGCHLD", |
| 120 | + SIGCONT => "SIGCONT", |
| 121 | + SIGSTOP => "SIGSTOP", |
| 122 | + SIGTSTP => "SIGTSTP", |
| 123 | + SIGTTIN => "SIGTTIN", |
| 124 | + SIGTTOU => "SIGTTOU", |
| 125 | + SIGURG => "SIGURG", |
| 126 | + SIGXCPU => "SIGXCPU", |
| 127 | + SIGXFSZ => "SIGXFSZ", |
| 128 | + SIGVTALRM => "SIGVTALRM", |
| 129 | + SIGPROF => "SIGPROF", |
| 130 | + SIGWINCH => "SIGWINCH", |
| 131 | + SIGIO => "SIGIO", |
| 132 | + SIGPWR => "SIGPWR", |
| 133 | + SIGSYS => "SIGSYS", |
| 134 | + _ if sig >= SIGRTMIN && sig <= SIGRTMAX => "SIGRT", |
| 135 | + _ => "UNKNOWN", |
| 136 | + } |
| 137 | +} |
0 commit comments