safe_libc/posix/signal.rs
1//
2// Created: Mon 22 Dec 2025 06:12:39 PM PST
3// Modified: Tue 23 Dec 2025 02:45:40 PM PST
4//
5// Copyright (C) 2025 Robert Gill <rtgill82@gmail.com>
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to
9// deal in the Software without restriction, including without limitation the
10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11// sell copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies of the Software, its documentation and marketing & publicity
16// materials, and acknowledgment shall be given in the documentation, materials
17// and software packages that this Software was used.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25//
26
27use std::mem::MaybeUninit;
28use libc::{pid_t,c_int};
29use libc::sighandler_t;
30use crate::errno::{Error,Result};
31
32pub fn kill(pid: pid_t, sig: c_int) -> Result<()> {
33 unsafe {
34 if libc::kill(pid, sig) == 0 {
35 Ok(())
36 } else {
37 Err(Error::errno())
38 }
39 }
40}
41
42pub fn sigaction(signum: c_int, act: &libc::sigaction)
43 -> Result<libc::sigaction>
44{
45 unsafe {
46 let mut oldact: libc::sigaction = MaybeUninit::zeroed().assume_init();
47 let oldact_p: *mut libc::sigaction = &mut oldact;
48 if libc::sigaction(signum, act, oldact_p) == 0 {
49 Ok(oldact)
50 } else {
51 Err(Error::errno())
52 }
53 }
54}
55
56pub fn signal(signum: c_int, handler: sighandler_t)
57 -> Result<sighandler_t>
58{
59 unsafe {
60 let rv = libc::signal(signum, handler);
61 if rv == libc::SIG_ERR {
62 Err(Error::errno())
63 } else {
64 Ok(rv)
65 }
66 }
67}