1 use crate::backend::c;
2 
3 /// A signal number for use with [`kill_process`], [`kill_process_group`],
4 /// and [`kill_current_process_group`].
5 ///
6 /// [`kill_process`]: crate::process::kill_process
7 /// [`kill_process_group`]: crate::process::kill_process_group
8 /// [`kill_current_process_group`]: crate::process::kill_current_process_group
9 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
10 #[repr(i32)]
11 pub enum Signal {
12     /// `SIGHUP`
13     Hup = c::SIGHUP,
14     /// `SIGINT`
15     Int = c::SIGINT,
16     /// `SIGQUIT`
17     Quit = c::SIGQUIT,
18     /// `SIGILL`
19     Ill = c::SIGILL,
20     /// `SIGTRAP`
21     Trap = c::SIGTRAP,
22     /// `SIGABRT`, aka `SIGIOT`
23     #[doc(alias = "Iot")]
24     #[doc(alias = "Abrt")]
25     Abort = c::SIGABRT,
26     /// `SIGBUS`
27     Bus = c::SIGBUS,
28     /// `SIGFPE`
29     Fpe = c::SIGFPE,
30     /// `SIGKILL`
31     Kill = c::SIGKILL,
32     /// `SIGUSR1`
33     #[cfg(not(target_os = "vita"))]
34     Usr1 = c::SIGUSR1,
35     /// `SIGSEGV`
36     Segv = c::SIGSEGV,
37     /// `SIGUSR2`
38     #[cfg(not(target_os = "vita"))]
39     Usr2 = c::SIGUSR2,
40     /// `SIGPIPE`
41     Pipe = c::SIGPIPE,
42     /// `SIGALRM`
43     #[doc(alias = "Alrm")]
44     Alarm = c::SIGALRM,
45     /// `SIGTERM`
46     Term = c::SIGTERM,
47     /// `SIGSTKFLT`
48     #[cfg(not(any(
49         bsd,
50         solarish,
51         target_os = "aix",
52         target_os = "haiku",
53         target_os = "nto",
54         target_os = "vita",
55         all(
56             linux_kernel,
57             any(
58                 target_arch = "mips",
59                 target_arch = "mips32r6",
60                 target_arch = "mips64",
61                 target_arch = "mips64r6",
62                 target_arch = "sparc",
63                 target_arch = "sparc64"
64             ),
65         )
66     )))]
67     Stkflt = c::SIGSTKFLT,
68     /// `SIGCHLD`
69     #[cfg(not(target_os = "vita"))]
70     #[doc(alias = "Chld")]
71     Child = c::SIGCHLD,
72     /// `SIGCONT`
73     #[cfg(not(target_os = "vita"))]
74     Cont = c::SIGCONT,
75     /// `SIGSTOP`
76     #[cfg(not(target_os = "vita"))]
77     Stop = c::SIGSTOP,
78     /// `SIGTSTP`
79     #[cfg(not(target_os = "vita"))]
80     Tstp = c::SIGTSTP,
81     /// `SIGTTIN`
82     #[cfg(not(target_os = "vita"))]
83     Ttin = c::SIGTTIN,
84     /// `SIGTTOU`
85     #[cfg(not(target_os = "vita"))]
86     Ttou = c::SIGTTOU,
87     /// `SIGURG`
88     #[cfg(not(target_os = "vita"))]
89     Urg = c::SIGURG,
90     /// `SIGXCPU`
91     #[cfg(not(target_os = "vita"))]
92     Xcpu = c::SIGXCPU,
93     /// `SIGXFSZ`
94     #[cfg(not(target_os = "vita"))]
95     Xfsz = c::SIGXFSZ,
96     /// `SIGVTALRM`
97     #[cfg(not(target_os = "vita"))]
98     #[doc(alias = "Vtalrm")]
99     Vtalarm = c::SIGVTALRM,
100     /// `SIGPROF`
101     #[cfg(not(target_os = "vita"))]
102     Prof = c::SIGPROF,
103     /// `SIGWINCH`
104     #[cfg(not(target_os = "vita"))]
105     Winch = c::SIGWINCH,
106     /// `SIGIO`, aka `SIGPOLL`
107     #[doc(alias = "Poll")]
108     #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
109     Io = c::SIGIO,
110     /// `SIGPWR`
111     #[cfg(not(any(bsd, target_os = "haiku", target_os = "vita")))]
112     #[doc(alias = "Pwr")]
113     Power = c::SIGPWR,
114     /// `SIGSYS`, aka `SIGUNUSED`
115     #[doc(alias = "Unused")]
116     Sys = c::SIGSYS,
117     /// `SIGEMT`
118     #[cfg(any(
119         bsd,
120         solarish,
121         target_os = "aix",
122         target_os = "hermit",
123         all(
124             linux_kernel,
125             any(
126                 target_arch = "mips",
127                 target_arch = "mips32r6",
128                 target_arch = "mips64",
129                 target_arch = "mips64r6",
130                 target_arch = "sparc",
131                 target_arch = "sparc64"
132             )
133         )
134     ))]
135     Emt = c::SIGEMT,
136     /// `SIGINFO`
137     #[cfg(bsd)]
138     Info = c::SIGINFO,
139     /// `SIGTHR`
140     #[cfg(target_os = "freebsd")]
141     #[doc(alias = "Lwp")]
142     Thr = c::SIGTHR,
143     /// `SIGLIBRT`
144     #[cfg(target_os = "freebsd")]
145     Librt = c::SIGLIBRT,
146 }
147 
148 impl Signal {
149     /// Convert a raw signal number into a `Signal`, if possible.
from_raw(sig: c::c_int) -> Option<Self>150     pub fn from_raw(sig: c::c_int) -> Option<Self> {
151         match sig {
152             c::SIGHUP => Some(Self::Hup),
153             c::SIGINT => Some(Self::Int),
154             c::SIGQUIT => Some(Self::Quit),
155             c::SIGILL => Some(Self::Ill),
156             c::SIGTRAP => Some(Self::Trap),
157             c::SIGABRT => Some(Self::Abort),
158             c::SIGBUS => Some(Self::Bus),
159             c::SIGFPE => Some(Self::Fpe),
160             c::SIGKILL => Some(Self::Kill),
161             #[cfg(not(target_os = "vita"))]
162             c::SIGUSR1 => Some(Self::Usr1),
163             c::SIGSEGV => Some(Self::Segv),
164             #[cfg(not(target_os = "vita"))]
165             c::SIGUSR2 => Some(Self::Usr2),
166             c::SIGPIPE => Some(Self::Pipe),
167             c::SIGALRM => Some(Self::Alarm),
168             c::SIGTERM => Some(Self::Term),
169             #[cfg(not(any(
170                 bsd,
171                 solarish,
172                 target_os = "aix",
173                 target_os = "haiku",
174                 target_os = "nto",
175                 target_os = "vita",
176                 all(
177                     linux_kernel,
178                     any(
179                         target_arch = "mips",
180                         target_arch = "mips32r6",
181                         target_arch = "mips64",
182                         target_arch = "mips64r6",
183                         target_arch = "sparc",
184                         target_arch = "sparc64"
185                     ),
186                 )
187             )))]
188             c::SIGSTKFLT => Some(Self::Stkflt),
189             #[cfg(not(target_os = "vita"))]
190             c::SIGCHLD => Some(Self::Child),
191             #[cfg(not(target_os = "vita"))]
192             c::SIGCONT => Some(Self::Cont),
193             #[cfg(not(target_os = "vita"))]
194             c::SIGSTOP => Some(Self::Stop),
195             #[cfg(not(target_os = "vita"))]
196             c::SIGTSTP => Some(Self::Tstp),
197             #[cfg(not(target_os = "vita"))]
198             c::SIGTTIN => Some(Self::Ttin),
199             #[cfg(not(target_os = "vita"))]
200             c::SIGTTOU => Some(Self::Ttou),
201             #[cfg(not(target_os = "vita"))]
202             c::SIGURG => Some(Self::Urg),
203             #[cfg(not(target_os = "vita"))]
204             c::SIGXCPU => Some(Self::Xcpu),
205             #[cfg(not(target_os = "vita"))]
206             c::SIGXFSZ => Some(Self::Xfsz),
207             #[cfg(not(target_os = "vita"))]
208             c::SIGVTALRM => Some(Self::Vtalarm),
209             #[cfg(not(target_os = "vita"))]
210             c::SIGPROF => Some(Self::Prof),
211             #[cfg(not(target_os = "vita"))]
212             c::SIGWINCH => Some(Self::Winch),
213             #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
214             c::SIGIO => Some(Self::Io),
215             #[cfg(not(any(bsd, target_os = "haiku", target_os = "vita")))]
216             c::SIGPWR => Some(Self::Power),
217             c::SIGSYS => Some(Self::Sys),
218             #[cfg(any(
219                 bsd,
220                 solarish,
221                 target_os = "aix",
222                 target_os = "hermit",
223                 all(
224                     linux_kernel,
225                     any(
226                         target_arch = "mips",
227                         target_arch = "mips32r6",
228                         target_arch = "mips64",
229                         target_arch = "mips64r6",
230                         target_arch = "sparc",
231                         target_arch = "sparc64"
232                     )
233                 )
234             ))]
235             c::SIGEMT => Some(Self::Emt),
236             #[cfg(bsd)]
237             c::SIGINFO => Some(Self::Info),
238             #[cfg(target_os = "freebsd")]
239             c::SIGTHR => Some(Self::Thr),
240             #[cfg(target_os = "freebsd")]
241             c::SIGLIBRT => Some(Self::Librt),
242             _ => None,
243         }
244     }
245 }
246 
247 #[test]
test_sizes()248 fn test_sizes() {
249     assert_eq_size!(Signal, c::c_int);
250 }
251