1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2015 Joyent, Inc.
26 */
27
28 #include <sys/signal.h>
29 #include <sys/lx_siginfo.h>
30 #include <lx_signum.h>
31 #include <sys/debug.h>
32
33 /*
34 * Delivering signals to a Linux process is complicated by differences in
35 * signal numbering, stack structure and contents, and the action taken when a
36 * signal handler exits. In addition, many signal-related structures, such as
37 * sigset_ts, vary between Solaris and Linux.
38 *
39 * The simplest transformation that must be done when sending signals is to
40 * translate between Linux and Solaris signal numbers.
41 *
42 * These are the major signal number differences between Linux and Solaris:
43 *
44 * ====================================
45 * | Number | Linux | Solaris |
46 * | ====== | ========= | ========== |
47 * | 7 | SIGBUS | SIGEMT |
48 * | 10 | SIGUSR1 | SIGBUS |
49 * | 12 | SIGUSR2 | SIGSYS |
50 * | 16 | SIGSTKFLT | SIGUSR1 |
51 * | 17 | SIGCHLD | SIGUSR2 |
52 * | 18 | SIGCONT | SIGCHLD |
53 * | 19 | SIGSTOP | SIGPWR |
54 * | 20 | SIGTSTP | SIGWINCH |
55 * | 21 | SIGTTIN | SIGURG |
56 * | 22 | SIGTTOU | SIGPOLL |
57 * | 23 | SIGURG | SIGSTOP |
58 * | 24 | SIGXCPU | SIGTSTP |
59 * | 25 | SIGXFSZ | SIGCONT |
60 * | 26 | SIGVTALARM | SIGTTIN |
61 * | 27 | SIGPROF | SIGTTOU |
62 * | 28 | SIGWINCH | SIGVTALARM |
63 * | 29 | SIGPOLL | SIGPROF |
64 * | 30 | SIGPWR | SIGXCPU |
65 * | 31 | SIGSYS | SIGXFSZ |
66 * ====================================
67 *
68 * Not every Linux signal maps to a Solaris signal, nor does every Solaris
69 * signal map to a Linux counterpart. However, when signals do map, the
70 * mapping is unique.
71 *
72 * One mapping issue is that Linux supports 33 real time signals, with SIGRTMIN
73 * typically starting at or near 32 (SIGRTMIN) and proceeding to 64 (SIGRTMAX)
74 * (SIGRTMIN is "at or near" 32 because glibc usually "steals" one ore more of
75 * these signals for its own internal use, adjusting SIGRTMIN and SIGRTMAX as
76 * needed.) Conversely, Solaris actively uses signals 32-40 for other purposes
77 * and supports exactly 32 real time signals, in the range 41 (SIGRTMIN)
78 * to 72 (SIGRTMAX).
79 *
80 * At present, attempting to translate a Linux signal equal to 63
81 * will generate an error (we allow SIGRTMAX because a program
82 * should be able to send SIGRTMAX without getting an EINVAL, though obviously
83 * anything that loops through the signals from SIGRTMIN to SIGRTMAX will
84 * fail.)
85 *
86 * Similarly, attempting to translate a native Solaris signal in the range
87 * 32-40 will also generate an error as we don't want to support the receipt of
88 * those signals from the Solaris global zone.
89 */
90
91 /*
92 * Linux to Solaris signal map
93 *
94 * Usage: solaris_signal = ltos_signum[lx_signal];
95 */
96 const int
97 ltos_signo[LX_NSIG + 1] = {
98 0,
99 SIGHUP,
100 SIGINT,
101 SIGQUIT,
102 SIGILL,
103 SIGTRAP,
104 SIGABRT,
105 SIGBUS,
106 SIGFPE,
107 SIGKILL,
108 SIGUSR1,
109 SIGSEGV,
110 SIGUSR2,
111 SIGPIPE,
112 SIGALRM,
113 SIGTERM,
114 SIGEMT, /* 16: Linux SIGSTKFLT; use Solaris SIGEMT */
115 SIGCHLD,
116 SIGCONT,
117 SIGSTOP,
118 SIGTSTP,
119 SIGTTIN,
120 SIGTTOU,
121 SIGURG,
122 SIGXCPU,
123 SIGXFSZ,
124 SIGVTALRM,
125 SIGPROF,
126 SIGWINCH,
127 SIGPOLL,
128 SIGPWR,
129 SIGSYS,
130 _SIGRTMIN, /* 32: Linux SIGRTMIN */
131 _SIGRTMIN + 1,
132 _SIGRTMIN + 2,
133 _SIGRTMIN + 3,
134 _SIGRTMIN + 4,
135 _SIGRTMIN + 5,
136 _SIGRTMIN + 6,
137 _SIGRTMIN + 7,
138 _SIGRTMIN + 8,
139 _SIGRTMIN + 9,
140 _SIGRTMIN + 10,
141 _SIGRTMIN + 11,
142 _SIGRTMIN + 12,
143 _SIGRTMIN + 13,
144 _SIGRTMIN + 14,
145 _SIGRTMIN + 15,
146 _SIGRTMIN + 16,
147 _SIGRTMIN + 17,
148 _SIGRTMIN + 18,
149 _SIGRTMIN + 19,
150 _SIGRTMIN + 20,
151 _SIGRTMIN + 21,
152 _SIGRTMIN + 22,
153 _SIGRTMIN + 23,
154 _SIGRTMIN + 24,
155 _SIGRTMIN + 25,
156 _SIGRTMIN + 26,
157 _SIGRTMIN + 27,
158 _SIGRTMIN + 28,
159 _SIGRTMIN + 29,
160 _SIGRTMIN + 30,
161 _SIGRTMIN + 31,
162 _SIGRTMAX, /* 64: Linux SIGRTMAX */
163 };
164
165 /*
166 * Solaris to Linux signal map
167 *
168 * Usage: lx_signal = stol_signo[solaris_signal];
169 */
170 const int
171 stol_signo[NSIG] = {
172 0,
173 LX_SIGHUP,
174 LX_SIGINT,
175 LX_SIGQUIT,
176 LX_SIGILL,
177 LX_SIGTRAP,
178 LX_SIGABRT,
179 LX_SIGSTKFLT, /* 7: Solaris SIGEMT; use for LX_SIGSTKFLT */
180 LX_SIGFPE,
181 LX_SIGKILL,
182 LX_SIGBUS,
183 LX_SIGSEGV,
184 LX_SIGSYS,
185 LX_SIGPIPE,
186 LX_SIGALRM,
187 LX_SIGTERM,
188 LX_SIGUSR1,
189 LX_SIGUSR2,
190 LX_SIGCHLD,
191 LX_SIGPWR,
192 LX_SIGWINCH,
193 LX_SIGURG,
194 LX_SIGPOLL,
195 LX_SIGSTOP,
196 LX_SIGTSTP,
197 LX_SIGCONT,
198 LX_SIGTTIN,
199 LX_SIGTTOU,
200 LX_SIGVTALRM,
201 LX_SIGPROF,
202 LX_SIGXCPU,
203 LX_SIGXFSZ,
204 -1, /* 32: Solaris SIGWAITING */
205 -1, /* 33: Solaris SIGLWP */
206 -1, /* 34: Solaris SIGFREEZE */
207 -1, /* 35: Solaris SIGTHAW */
208 -1, /* 36: Solaris SIGCANCEL */
209 -1, /* 37: Solaris SIGLOST */
210 -1, /* 38: Solaris SIGXRES */
211 -1, /* 39: Solaris SIGJVM1 */
212 -1, /* 40: Solaris SIGJVM2 */
213 -1, /* 41: Solaris SIGINFO */
214 LX_SIGRTMIN, /* 42: Solaris _SIGRTMIN */
215 LX_SIGRTMIN + 1,
216 LX_SIGRTMIN + 2,
217 LX_SIGRTMIN + 3,
218 LX_SIGRTMIN + 4,
219 LX_SIGRTMIN + 5,
220 LX_SIGRTMIN + 6,
221 LX_SIGRTMIN + 7,
222 LX_SIGRTMIN + 8,
223 LX_SIGRTMIN + 9,
224 LX_SIGRTMIN + 10,
225 LX_SIGRTMIN + 11,
226 LX_SIGRTMIN + 12,
227 LX_SIGRTMIN + 13,
228 LX_SIGRTMIN + 14,
229 LX_SIGRTMIN + 15,
230 LX_SIGRTMIN + 16,
231 LX_SIGRTMIN + 17,
232 LX_SIGRTMIN + 18,
233 LX_SIGRTMIN + 19,
234 LX_SIGRTMIN + 20,
235 LX_SIGRTMIN + 21,
236 LX_SIGRTMIN + 22,
237 LX_SIGRTMIN + 23,
238 LX_SIGRTMIN + 24,
239 LX_SIGRTMIN + 25,
240 LX_SIGRTMIN + 26,
241 LX_SIGRTMIN + 27,
242 LX_SIGRTMIN + 28,
243 LX_SIGRTMIN + 29,
244 LX_SIGRTMIN + 30,
245 LX_SIGRTMIN + 31,
246 LX_SIGRTMAX, /* 74: Solaris _SIGRTMAX */
247 };
248
249 /*
250 * Convert an illumos native signal number to a Linux signal number and return
251 * it. If no valid conversion is possible, the function fails back to the
252 * value of "defsig". In userland, passing a default signal number of "-1"
253 * will abort the program if the signal number could not be converted.
254 */
255 int
256 lx_stol_signo(int signo, int defsig)
257 {
258 int rval;
259
260 #ifdef _KERNEL
261 VERIFY3S(defsig, >=, 0);
262 #endif
263
264 if (signo < 0 || signo >= NSIG || (rval = stol_signo[signo]) < 1) {
265 #ifndef _KERNEL
266 VERIFY3S(defsig, >=, 0);
267 #endif
268 return (defsig);
269 }
270
271 return (rval);
272 }
273
274
275 /*
276 * Convert a Linux signal number to an illumos signal number and return it.
277 * Error behavior is identical to lx_stol_signo.
278 */
279 int
280 lx_ltos_signo(int signo, int defsig)
281 {
282 #ifdef _KERNEL
283 VERIFY3S(defsig, >=, 0);
284 #endif
285
286 if (signo < 1 || signo >= NSIG) {
287 #ifndef _KERNEL
288 VERIFY3S(defsig, >=, 0);
289 #endif
290 return (defsig);
291 }
292
293 return (ltos_signo[signo]);
294 }
295
296 /*
297 * Convert the "status" field of a SIGCLD siginfo_t. We need to extract the
298 * illumos signal number and convert it to a Linux signal number while leaving
299 * the ptrace(2) event bits intact. In userland, passing a default signal
300 * number of "-1" will abort the program if the signal number could not be
301 * converted, as for lx_stol_signo().
302 */
303 int
304 lx_stol_status(int s, int defsig)
305 {
306 /*
307 * We mask out the top bit here in case PTRACE_O_TRACESYSGOOD
308 * is in use and 0x80 has been ORed with the signal number.
309 */
310 int stat = lx_stol_signo(s & 0x7f, defsig);
311
312 /*
313 * We must mix in the ptrace(2) event which may be stored in
314 * the second byte of the status code. We also re-include the
315 * PTRACE_O_TRACESYSGOOD bit.
316 */
317 return ((s & 0xff80) | stat);
318 }
319
320 int
321 lx_stol_sigcode(int code)
322 {
323 switch (code) {
324 case SI_USER:
325 return (LX_SI_USER);
326 case SI_LWP:
327 return (LX_SI_TKILL);
328 case SI_QUEUE:
329 return (LX_SI_QUEUE);
330 case SI_TIMER:
331 return (LX_SI_TIMER);
332 case SI_ASYNCIO:
333 return (LX_SI_ASYNCIO);
334 case SI_MESGQ:
335 return (LX_SI_MESGQ);
336 default:
337 return (code);
338 }
339 }