xref: /netbsd/sys/compat/linux/common/linux_signal.c (revision eb96f2eb)
1 /*	$NetBSD: linux_signal.c,v 1.88 2021/11/01 05:07:16 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden and Eric Haszlakiewicz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*
32  * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp
33  */
34 
35 /*
36  *   Functions in multiarch:
37  *	linux_sys_signal	: linux_sig_notalpha.c
38  *	linux_sys_siggetmask	: linux_sig_notalpha.c
39  *	linux_sys_sigsetmask	: linux_sig_notalpha.c
40  *	linux_sys_pause		: linux_sig_notalpha.c
41  *	linux_sys_sigaction	: linux_sigaction.c
42  *
43  */
44 
45 /*
46  *   Unimplemented:
47  *	linux_sys_rt_sigtimedwait	: sigsuspend w/timeout.
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.88 2021/11/01 05:07:16 thorpej Exp $");
52 
53 #define COMPAT_LINUX 1
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/namei.h>
58 #include <sys/proc.h>
59 #include <sys/filedesc.h>
60 #include <sys/ioctl.h>
61 #include <sys/mount.h>
62 #include <sys/kernel.h>
63 #include <sys/signal.h>
64 #include <sys/signalvar.h>
65 #include <sys/wait.h>
66 
67 #include <sys/syscallargs.h>
68 
69 #include <compat/linux/common/linux_types.h>
70 #include <compat/linux/common/linux_signal.h>
71 #include <compat/linux/common/linux_emuldata.h>
72 #include <compat/linux/common/linux_siginfo.h>
73 #include <compat/linux/common/linux_sigevent.h>
74 #include <compat/linux/common/linux_util.h>
75 #include <compat/linux/common/linux_ipc.h>
76 #include <compat/linux/common/linux_sem.h>
77 #include <compat/linux/common/linux_errno.h>
78 #include <compat/linux/common/linux_sched.h>
79 
80 #include <compat/linux/linux_syscallargs.h>
81 
82 /* Locally used defines (in bsd<->linux conversion functions): */
83 #define	linux_sigemptyset(s)	memset((s), 0, sizeof(*(s)))
84 #define	linux_sigismember(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
85 					& (1L << ((n) - 1) % LINUX__NSIG_BPW))
86 #define	linux_sigaddset(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
87 					|= (1L << ((n) - 1) % LINUX__NSIG_BPW))
88 
89 #ifdef DEBUG_LINUX
90 #define DPRINTF(a)	uprintf a
91 #else
92 #define DPRINTF(a)
93 #endif
94 
95 extern const int native_to_linux_signo[];
96 extern const int linux_to_native_signo[];
97 
98 /*
99  * Convert between Linux and BSD signal sets.
100  */
101 #if LINUX__NSIG_WORDS > 1
102 void
linux_old_extra_to_native_sigset(sigset_t * bss,const linux_old_sigset_t * lss,const unsigned long * extra)103 linux_old_extra_to_native_sigset(sigset_t *bss, const linux_old_sigset_t *lss, const unsigned long *extra)
104 {
105 	linux_sigset_t lsnew;
106 
107 	/* convert old sigset to new sigset */
108 	linux_sigemptyset(&lsnew);
109 	lsnew.sig[0] = *lss;
110 	if (extra)
111 		memcpy(&lsnew.sig[1], extra,
112 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
113 
114 	linux_to_native_sigset(bss, &lsnew);
115 }
116 
117 void
native_to_linux_old_extra_sigset(linux_old_sigset_t * lss,unsigned long * extra,const sigset_t * bss)118 native_to_linux_old_extra_sigset(linux_old_sigset_t *lss, unsigned long *extra, const sigset_t *bss)
119 {
120 	linux_sigset_t lsnew;
121 
122 	native_to_linux_sigset(&lsnew, bss);
123 
124 	/* convert new sigset to old sigset */
125 	*lss = lsnew.sig[0];
126 	if (extra)
127 		memcpy(extra, &lsnew.sig[1],
128 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
129 }
130 #endif /* LINUX__NSIG_WORDS > 1 */
131 
132 void
linux_to_native_sigset(sigset_t * bss,const linux_sigset_t * lss)133 linux_to_native_sigset(sigset_t *bss, const linux_sigset_t *lss)
134 {
135 	int i, newsig;
136 
137 	sigemptyset(bss);
138 	for (i = 1; i < LINUX__NSIG; i++) {
139 		if (linux_sigismember(lss, i)) {
140 			newsig = linux_to_native_signo[i];
141 			if (newsig)
142 				sigaddset(bss, newsig);
143 		}
144 	}
145 }
146 
147 void
native_to_linux_sigset(linux_sigset_t * lss,const sigset_t * bss)148 native_to_linux_sigset(linux_sigset_t *lss, const sigset_t *bss)
149 {
150 	int i, newsig;
151 
152 	linux_sigemptyset(lss);
153 	for (i = 1; i < NSIG; i++) {
154 		if (sigismember(bss, i)) {
155 			newsig = native_to_linux_signo[i];
156 			if (newsig)
157 				linux_sigaddset(lss, newsig);
158 		}
159 	}
160 }
161 
162 void
native_to_linux_siginfo(linux_siginfo_t * lsi,const struct _ksiginfo * ksi)163 native_to_linux_siginfo(linux_siginfo_t *lsi, const struct _ksiginfo *ksi)
164 {
165 	memset(lsi, 0, sizeof(*lsi));
166 
167 	lsi->lsi_signo = native_to_linux_signo[ksi->_signo];
168 	lsi->lsi_errno = native_to_linux_errno[ksi->_errno];
169 	lsi->lsi_code = native_to_linux_si_code(ksi->_code);
170 
171 	switch (ksi->_code) {
172 	case SI_NOINFO:
173 		break;
174 
175 	case SI_USER:
176 		lsi->lsi_pid = ksi->_reason._rt._pid;
177 		lsi->lsi_uid = ksi->_reason._rt._uid;
178 		if (lsi->lsi_signo == LINUX_SIGALRM ||
179 		    lsi->lsi_signo >= LINUX_SIGRTMIN)
180 			lsi->lsi_value.sival_ptr =
181 			    ksi->_reason._rt._value.sival_ptr;
182 		break;
183 
184 	case SI_TIMER:
185 	case SI_QUEUE:
186 		lsi->lsi_uid = ksi->_reason._rt._uid;
187 		lsi->lsi_uid = ksi->_reason._rt._uid;
188 		lsi->lsi_value.sival_ptr = ksi->_reason._rt._value.sival_ptr;
189 		break;
190 
191 	case SI_ASYNCIO:
192 	case SI_MESGQ:
193 		lsi->lsi_value.sival_ptr = ksi->_reason._rt._value.sival_ptr;
194 		break;
195 
196 	default:
197 		switch (ksi->_signo) {
198 		case SIGCHLD:
199 			lsi->lsi_uid = ksi->_reason._child._uid;
200 			lsi->lsi_pid = ksi->_reason._child._pid;
201 			lsi->lsi_status = native_to_linux_si_status(
202 			    ksi->_code, ksi->_reason._child._status);
203 			lsi->lsi_utime = ksi->_reason._child._utime;
204 			lsi->lsi_stime = ksi->_reason._child._stime;
205 			break;
206 
207 		case SIGILL:
208 		case SIGFPE:
209 		case SIGSEGV:
210 		case SIGBUS:
211 		case SIGTRAP:
212 			lsi->lsi_addr = ksi->_reason._fault._addr;
213 			break;
214 
215 		case SIGIO:
216 			lsi->lsi_fd = ksi->_reason._poll._fd;
217 			lsi->lsi_band = ksi->_reason._poll._band;
218 			break;
219 		default:
220 			break;
221 		}
222 	}
223 }
224 
225 unsigned int
native_to_linux_sigflags(const int bsf)226 native_to_linux_sigflags(const int bsf)
227 {
228 	unsigned int lsf = 0;
229 	if ((bsf & SA_NOCLDSTOP) != 0)
230 		lsf |= LINUX_SA_NOCLDSTOP;
231 	if ((bsf & SA_NOCLDWAIT) != 0)
232 		lsf |= LINUX_SA_NOCLDWAIT;
233 	if ((bsf & SA_ONSTACK) != 0)
234 		lsf |= LINUX_SA_ONSTACK;
235 	if ((bsf & SA_RESTART) != 0)
236 		lsf |= LINUX_SA_RESTART;
237 	if ((bsf & SA_NODEFER) != 0)
238 		lsf |= LINUX_SA_NOMASK;
239 	if ((bsf & SA_RESETHAND) != 0)
240 		lsf |= LINUX_SA_ONESHOT;
241 	if ((bsf & SA_SIGINFO) != 0)
242 		lsf |= LINUX_SA_SIGINFO;
243 	return lsf;
244 }
245 
246 int
linux_to_native_sigflags(const unsigned long lsf)247 linux_to_native_sigflags(const unsigned long lsf)
248 {
249 	int bsf = 0;
250 	if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
251 		bsf |= SA_NOCLDSTOP;
252 	if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
253 		bsf |= SA_NOCLDWAIT;
254 	if ((lsf & LINUX_SA_ONSTACK) != 0)
255 		bsf |= SA_ONSTACK;
256 	if ((lsf & LINUX_SA_RESTART) != 0)
257 		bsf |= SA_RESTART;
258 	if ((lsf & LINUX_SA_ONESHOT) != 0)
259 		bsf |= SA_RESETHAND;
260 	if ((lsf & LINUX_SA_NOMASK) != 0)
261 		bsf |= SA_NODEFER;
262 	if ((lsf & LINUX_SA_SIGINFO) != 0)
263 		bsf |= SA_SIGINFO;
264 	if ((lsf & ~LINUX_SA_ALLBITS) != 0) {
265 		DPRINTF(("linux_old_to_native_sigflags: "
266 		    "%lx extra bits ignored\n", lsf));
267 	}
268 	return bsf;
269 }
270 
271 #if !defined(__aarch64__)
272 /*
273  * Convert between Linux and BSD sigaction structures.
274  */
275 void
linux_old_to_native_sigaction(struct sigaction * bsa,const struct linux_old_sigaction * lsa)276 linux_old_to_native_sigaction(struct sigaction *bsa, const struct linux_old_sigaction *lsa)
277 {
278 
279 	memset(bsa, 0, sizeof(*bsa));
280 	bsa->sa_handler = lsa->linux_sa_handler;
281 	linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
282 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
283 }
284 
285 void
native_to_linux_old_sigaction(struct linux_old_sigaction * lsa,const struct sigaction * bsa)286 native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa)
287 {
288 
289 	memset(lsa, 0, sizeof(*lsa));
290 	lsa->linux_sa_handler = bsa->sa_handler;
291 	native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
292 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
293 #ifndef __alpha__
294 	lsa->linux_sa_restorer = NULL;
295 #endif
296 }
297 #endif
298 
299 /* ...and the new sigaction conversion funcs. */
300 void
linux_to_native_sigaction(struct sigaction * bsa,const struct linux_sigaction * lsa)301 linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa)
302 {
303 
304 	memset(bsa, 0, sizeof(*bsa));
305 	bsa->sa_handler = lsa->linux_sa_handler;
306 	linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
307 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
308 }
309 
310 void
native_to_linux_sigaction(struct linux_sigaction * lsa,const struct sigaction * bsa)311 native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa)
312 {
313 
314 	memset(lsa, 0, sizeof(*lsa));
315 	lsa->linux_sa_handler = bsa->sa_handler;
316 	native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
317 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
318 #ifndef __alpha__
319 	lsa->linux_sa_restorer = NULL;
320 #endif
321 }
322 
323 /* ----------------------------------------------------------------------- */
324 
325 /*
326  * The Linux sigaction() system call. Do the usual conversions,
327  * and just call sigaction(). Some flags and values are silently
328  * ignored (see above).
329  */
330 int
linux_sys_rt_sigaction(struct lwp * l,const struct linux_sys_rt_sigaction_args * uap,register_t * retval)331 linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval)
332 {
333 	/* {
334 		syscallarg(int) signum;
335 		syscallarg(const struct linux_sigaction *) nsa;
336 		syscallarg(struct linux_sigaction *) osa;
337 		syscallarg(size_t) sigsetsize;
338 	} */
339 	struct linux_sigaction nlsa, olsa;
340 	struct sigaction nbsa, obsa;
341 	int error, sig;
342 	void *tramp = NULL;
343 	int vers = 0;
344 #ifdef LINUX_SA_RESTORER
345 	struct sigacts *ps = l->l_proc->p_sigacts;
346 #endif
347 
348 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
349 		return EINVAL;
350 
351 	if (SCARG(uap, nsa)) {
352 		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
353 		if (error)
354 			return error;
355 		linux_to_native_sigaction(&nbsa, &nlsa);
356 	}
357 
358 	sig = SCARG(uap, signum);
359 	/*
360 	 * XXX: Linux has 33 realtime signals, the go binary wants to
361 	 * reset all of them; nothing else uses the last RT signal, so for
362 	 * now ignore it.
363 	 */
364 	if (sig == LINUX__NSIG) {
365 		uprintf("%s: setting signal %d ignored\n", __func__, sig);
366 		sig--;	/* back to 63 which is ignored */
367 	}
368 	if (sig < 0 || sig >= LINUX__NSIG)
369 		return EINVAL;
370 	if (sig > 0 && !linux_to_native_signo[sig]) {
371 		/* Pretend that we did something useful for unknown signals. */
372 		obsa.sa_handler = SIG_IGN;
373 		sigemptyset(&obsa.sa_mask);
374 		obsa.sa_flags = 0;
375 	} else {
376 #ifdef LINUX_SA_RESTORER
377 		if (SCARG(uap, nsa) &&
378 		    (nlsa.linux_sa_flags & LINUX_SA_RESTORER) &&
379 		    (tramp = nlsa.linux_sa_restorer) != NULL)
380 			vers = 2;
381 #endif
382 
383 		error = sigaction1(l, linux_to_native_signo[sig],
384 		    SCARG(uap, nsa) ? &nbsa : NULL,
385 		    SCARG(uap, osa) ? &obsa : NULL,
386 		    tramp, vers);
387 		if (error)
388 			return error;
389 	}
390 	if (SCARG(uap, osa)) {
391 		native_to_linux_sigaction(&olsa, &obsa);
392 
393 #ifdef LINUX_SA_RESTORER
394 		if (ps->sa_sigdesc[sig].sd_vers != __SIGTRAMP_SIGCODE_VERSION) {
395 			olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp;
396 			olsa.linux_sa_flags |= LINUX_SA_RESTORER;
397 		}
398 #endif
399 
400 		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
401 		if (error)
402 			return error;
403 	}
404 	return 0;
405 }
406 
407 #if !defined(__aarch64__)
408 int
linux_sigprocmask1(struct lwp * l,int how,const linux_old_sigset_t * set,linux_old_sigset_t * oset)409 linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset)
410 {
411 	struct proc *p = l->l_proc;
412 	linux_old_sigset_t nlss, olss;
413 	sigset_t nbss, obss;
414 	int error;
415 
416 	switch (how) {
417 	case LINUX_SIG_BLOCK:
418 		how = SIG_BLOCK;
419 		break;
420 	case LINUX_SIG_UNBLOCK:
421 		how = SIG_UNBLOCK;
422 		break;
423 	case LINUX_SIG_SETMASK:
424 		how = SIG_SETMASK;
425 		break;
426 	default:
427 		return EINVAL;
428 	}
429 
430 	if (set) {
431 		error = copyin(set, &nlss, sizeof(nlss));
432 		if (error)
433 			return error;
434 		linux_old_to_native_sigset(&nbss, &nlss);
435 	}
436 	mutex_enter(p->p_lock);
437 	error = sigprocmask1(l, how,
438 	    set ? &nbss : NULL, oset ? &obss : NULL);
439 	mutex_exit(p->p_lock);
440 	if (error)
441 		return error;
442 	if (oset) {
443 		native_to_linux_old_sigset(&olss, &obss);
444 		error = copyout(&olss, oset, sizeof(olss));
445 		if (error)
446 			return error;
447 	}
448 	return error;
449 }
450 #endif
451 
452 int
linux_sys_rt_sigprocmask(struct lwp * l,const struct linux_sys_rt_sigprocmask_args * uap,register_t * retval)453 linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval)
454 {
455 	/* {
456 		syscallarg(int) how;
457 		syscallarg(const linux_sigset_t *) set;
458 		syscallarg(linux_sigset_t *) oset;
459 		syscallarg(size_t) sigsetsize;
460 	} */
461 	linux_sigset_t nlss, olss, *oset;
462 	const linux_sigset_t *set;
463 	struct proc *p = l->l_proc;
464 	sigset_t nbss, obss;
465 	int error, how;
466 
467 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
468 		return EINVAL;
469 
470 	switch (SCARG(uap, how)) {
471 	case LINUX_SIG_BLOCK:
472 		how = SIG_BLOCK;
473 		break;
474 	case LINUX_SIG_UNBLOCK:
475 		how = SIG_UNBLOCK;
476 		break;
477 	case LINUX_SIG_SETMASK:
478 		how = SIG_SETMASK;
479 		break;
480 	default:
481 		return EINVAL;
482 	}
483 
484 	set = SCARG(uap, set);
485 	oset = SCARG(uap, oset);
486 
487 	if (set) {
488 		error = copyin(set, &nlss, sizeof(nlss));
489 		if (error)
490 			return error;
491 		linux_to_native_sigset(&nbss, &nlss);
492 	}
493 	mutex_enter(p->p_lock);
494 	error = sigprocmask1(l, how,
495 	    set ? &nbss : NULL, oset ? &obss : NULL);
496 	mutex_exit(p->p_lock);
497 	if (!error && oset) {
498 		native_to_linux_sigset(&olss, &obss);
499 		error = copyout(&olss, oset, sizeof(olss));
500 	}
501 	return error;
502 }
503 
504 int
linux_sys_rt_sigpending(struct lwp * l,const struct linux_sys_rt_sigpending_args * uap,register_t * retval)505 linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval)
506 {
507 	/* {
508 		syscallarg(linux_sigset_t *) set;
509 		syscallarg(size_t) sigsetsize;
510 	} */
511 	sigset_t bss;
512 	linux_sigset_t lss;
513 
514 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
515 		return EINVAL;
516 
517 	sigpending1(l, &bss);
518 	native_to_linux_sigset(&lss, &bss);
519 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
520 }
521 
522 #if !defined(__aarch64__) && !defined(__amd64__)
523 int
linux_sys_sigpending(struct lwp * l,const struct linux_sys_sigpending_args * uap,register_t * retval)524 linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval)
525 {
526 	/* {
527 		syscallarg(linux_old_sigset_t *) mask;
528 	} */
529 	sigset_t bss;
530 	linux_old_sigset_t lss;
531 
532 	sigpending1(l, &bss);
533 	native_to_linux_old_sigset(&lss, &bss);
534 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
535 }
536 
537 int
linux_sys_sigsuspend(struct lwp * l,const struct linux_sys_sigsuspend_args * uap,register_t * retval)538 linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval)
539 {
540 	/* {
541 		syscallarg(void *) restart;
542 		syscallarg(int) oldmask;
543 		syscallarg(int) mask;
544 	} */
545 	linux_old_sigset_t lss;
546 	sigset_t bss;
547 
548 	lss = SCARG(uap, mask);
549 	linux_old_to_native_sigset(&bss, &lss);
550 	return sigsuspend1(l, &bss);
551 }
552 #endif /* !__aarch64__ && !__amd64__ */
553 
554 int
linux_sys_rt_sigsuspend(struct lwp * l,const struct linux_sys_rt_sigsuspend_args * uap,register_t * retval)555 linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval)
556 {
557 	/* {
558 		syscallarg(linux_sigset_t *) unewset;
559 		syscallarg(size_t) sigsetsize;
560 	} */
561 	linux_sigset_t lss;
562 	sigset_t bss;
563 	int error;
564 
565 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
566 		return EINVAL;
567 
568 	error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
569 	if (error)
570 		return error;
571 
572 	linux_to_native_sigset(&bss, &lss);
573 
574 	return sigsuspend1(l, &bss);
575 }
576 
577 static int
fetchss(const void * u,void * s,size_t len)578 fetchss(const void *u, void *s, size_t len)
579 {
580 	int error;
581 	linux_sigset_t lss;
582 
583 	if ((error = copyin(u, &lss, sizeof(lss))) != 0)
584 		return error;
585 
586 	linux_to_native_sigset(s, &lss);
587 	return 0;
588 }
589 
590 static int
fetchts(const void * u,void * s,size_t len)591 fetchts(const void *u, void *s, size_t len)
592 {
593 	int error;
594 	struct linux_timespec lts;
595 
596 	if ((error = copyin(u, &lts, sizeof(lts))) != 0)
597 		return error;
598 
599 	linux_to_native_timespec(s, &lts);
600 	return 0;
601 }
602 
603 static int
fakestorets(const void * u,void * s,size_t len)604 fakestorets(const void *u, void *s, size_t len)
605 {
606 	/* Do nothing, sigtimedwait does not alter timeout like ours */
607 	return 0;
608 }
609 
610 static int
storeinfo(const void * s,void * u,size_t len)611 storeinfo(const void *s, void *u, size_t len)
612 {
613 	struct linux_siginfo lsi;
614 
615 	native_to_linux_siginfo(&lsi, &((const siginfo_t *)s)->_info);
616 	return copyout(&lsi, u, sizeof(lsi));
617 }
618 
619 int
linux_sys_rt_sigtimedwait(struct lwp * l,const struct linux_sys_rt_sigtimedwait_args * uap,register_t * retval)620 linux_sys_rt_sigtimedwait(struct lwp *l,
621     const struct linux_sys_rt_sigtimedwait_args *uap, register_t *retval)
622 {
623 	/* {
624 		syscallarg(const linux_sigset_t *) set;
625 		syscallarg(linux_siginfo_t *) info);
626 		syscallarg(const struct linux_timespec *) timeout;
627 	} */
628 
629 	return sigtimedwait1(l, (const struct sys_____sigtimedwait50_args *)uap,
630 	    retval, fetchss, storeinfo, fetchts, fakestorets);
631 }
632 
633 #if !defined(__aarch64__)
634 /*
635  * Once more: only a signal conversion is needed.
636  * Note: also used as sys_rt_queueinfo.  The info field is ignored.
637  */
638 int
linux_sys_rt_queueinfo(struct lwp * l,const struct linux_sys_rt_queueinfo_args * uap,register_t * retval)639 linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval)
640 {
641 	/*
642 		syscallarg(int) pid;
643 		syscallarg(int) signum;
644 		syscallarg(linix_siginfo_t *) uinfo;
645 	*/
646 	int error;
647 	linux_siginfo_t info;
648 
649 	error = copyin(SCARG(uap, uinfo), &info, sizeof(info));
650 	if (error)
651 		return error;
652 	if (info.lsi_code >= 0)
653 		return EPERM;
654 
655 	/* XXX To really implement this we need to	*/
656 	/* XXX keep a list of queued signals somewhere.	*/
657 	return linux_sys_kill(l, (const void *)uap, retval);
658 }
659 #endif
660 
661 int
linux_sys_kill(struct lwp * l,const struct linux_sys_kill_args * uap,register_t * retval)662 linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval)
663 {
664 	/* {
665 		syscallarg(int) pid;
666 		syscallarg(int) signum;
667 	} */
668 
669 	struct sys_kill_args ka;
670 	int sig;
671 
672 	SCARG(&ka, pid) = SCARG(uap, pid);
673 	sig = SCARG(uap, signum);
674 	if (sig < 0 || sig >= LINUX__NSIG)
675 		return EINVAL;
676 	SCARG(&ka, signum) = linux_to_native_signo[sig];
677 	return sys_kill(l, &ka, retval);
678 }
679 
680 #ifdef LINUX_SS_ONSTACK
681 static void linux_to_native_sigaltstack(stack_t *,
682     const struct linux_sigaltstack *);
683 
684 static void
linux_to_native_sigaltstack(stack_t * bss,const struct linux_sigaltstack * lss)685 linux_to_native_sigaltstack(stack_t *bss, const struct linux_sigaltstack *lss)
686 {
687 	bss->ss_sp = lss->ss_sp;
688 	bss->ss_size = lss->ss_size;
689 	if (lss->ss_flags & LINUX_SS_ONSTACK)
690 	    bss->ss_flags = SS_ONSTACK;
691 	else if (lss->ss_flags & LINUX_SS_DISABLE)
692 	    bss->ss_flags = SS_DISABLE;
693 	else
694 	    bss->ss_flags = 0;
695 }
696 
697 void
native_to_linux_sigaltstack(struct linux_sigaltstack * lss,const stack_t * bss)698 native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const stack_t *bss)
699 {
700 	memset(lss, 0, sizeof(*lss));
701 	lss->ss_sp = bss->ss_sp;
702 	lss->ss_size = bss->ss_size;
703 	if (bss->ss_flags & SS_ONSTACK)
704 	    lss->ss_flags = LINUX_SS_ONSTACK;
705 	else if (bss->ss_flags & SS_DISABLE)
706 	    lss->ss_flags = LINUX_SS_DISABLE;
707 	else
708 	    lss->ss_flags = 0;
709 }
710 
711 int
linux_sys_sigaltstack(struct lwp * l,const struct linux_sys_sigaltstack_args * uap,register_t * retval)712 linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval)
713 {
714 	/* {
715 		syscallarg(const struct linux_sigaltstack *) ss;
716 		syscallarg(struct linux_sigaltstack *) oss;
717 	} */
718 	struct linux_sigaltstack ss;
719 	stack_t nss;
720 	struct proc *p = l->l_proc;
721 	int error = 0;
722 
723 	if (SCARG(uap, oss)) {
724 		native_to_linux_sigaltstack(&ss, &l->l_sigstk);
725 		if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
726 			return error;
727 	}
728 
729 	if (SCARG(uap, ss) != NULL) {
730 		if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
731 			return error;
732 		linux_to_native_sigaltstack(&nss, &ss);
733 
734 		mutex_enter(p->p_lock);
735 
736 		if (nss.ss_flags & ~SS_ALLBITS)
737 			error = EINVAL;
738 		else if (nss.ss_flags & SS_DISABLE) {
739 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
740 				error = EINVAL;
741 		} else if (nss.ss_size < LINUX_MINSIGSTKSZ)
742 			error = ENOMEM;
743 
744 		if (error == 0)
745 			l->l_sigstk = nss;
746 
747 		mutex_exit(p->p_lock);
748 	}
749 
750 	return error;
751 }
752 #endif /* LINUX_SS_ONSTACK */
753 
754 static int
linux_do_tkill(struct lwp * l,int tgid,int tid,int signum)755 linux_do_tkill(struct lwp *l, int tgid, int tid, int signum)
756 {
757 	struct proc *p;
758 	struct lwp *t;
759 	ksiginfo_t ksi;
760 	int error;
761 
762 	if (signum < 0 || signum >= LINUX__NSIG)
763 		return EINVAL;
764 	signum = linux_to_native_signo[signum];
765 
766 	KSI_INIT(&ksi);
767 	ksi.ksi_signo = signum;
768 	ksi.ksi_code = SI_LWP;
769 	ksi.ksi_pid = l->l_proc->p_pid;
770 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
771 	ksi.ksi_lid = tid;
772 
773 	mutex_enter(&proc_lock);
774 	if (tgid != -1)
775 		p = proc_find(tgid);
776 	else
777 		p = proc_find_lwpid(tid);
778 	if (p == NULL) {
779 		mutex_exit(&proc_lock);
780 		return ESRCH;
781 	}
782 	mutex_enter(p->p_lock);
783 	error = kauth_authorize_process(l->l_cred,
784 	    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL);
785 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
786 		error = ESRCH;
787 	else if (signum != 0)
788 		kpsignal2(p, &ksi);
789 	mutex_exit(p->p_lock);
790 	mutex_exit(&proc_lock);
791 
792 	return error;
793 }
794 
795 int
linux_sys_tkill(struct lwp * l,const struct linux_sys_tkill_args * uap,register_t * retval)796 linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval)
797 {
798 	/* {
799 		syscallarg(int) tid;
800 		syscallarg(int) sig;
801 	} */
802 
803 	if (SCARG(uap, tid) <= 0)
804 		return EINVAL;
805 
806 	return linux_do_tkill(l, -1, SCARG(uap, tid), SCARG(uap, sig));
807 }
808 
809 int
linux_sys_tgkill(struct lwp * l,const struct linux_sys_tgkill_args * uap,register_t * retval)810 linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval)
811 {
812 	/* {
813 		syscallarg(int) tgid;
814 		syscallarg(int) tid;
815 		syscallarg(int) sig;
816 	} */
817 
818 	if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) < -1)
819 		return EINVAL;
820 
821 	return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig));
822 }
823 
824 int
native_to_linux_si_code(int code)825 native_to_linux_si_code(int code)
826 {
827 	int si_codes[] = {
828 	    LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO,
829 	    LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */
830 	};
831 
832 	if (code <= 0 && -code < __arraycount(si_codes))
833 		return si_codes[-code];
834 
835 	return code;
836 }
837 
838 int
native_to_linux_si_status(int code,int status)839 native_to_linux_si_status(int code, int status)
840 {
841 	int sts;
842 
843 	switch (code) {
844 	case CLD_CONTINUED:
845 		sts = LINUX_SIGCONT;
846 		break;
847 	case CLD_EXITED:
848 		sts = WEXITSTATUS(status);
849 		break;
850 	case CLD_STOPPED:
851 	case CLD_TRAPPED:
852 	case CLD_DUMPED:
853 	case CLD_KILLED:
854 	default:
855 		sts = native_to_linux_signo[WTERMSIG(status)];
856 		break;
857 	}
858 
859 	return sts;
860 }
861 
862 int
linux_to_native_sigevent(struct sigevent * nsep,const struct linux_sigevent * lsep)863 linux_to_native_sigevent(struct sigevent *nsep,
864     const struct linux_sigevent *lsep)
865 {
866 	memset(nsep, 0, sizeof(*nsep));
867 
868 	switch (lsep->sigev_notify) {
869 	case LINUX_SIGEV_SIGNAL:
870 		nsep->sigev_notify = SIGEV_SIGNAL;
871 		break;
872 
873 	case LINUX_SIGEV_NONE:
874 		nsep->sigev_notify = SIGEV_NONE;
875 		break;
876 
877 	case LINUX_SIGEV_THREAD:
878 	case LINUX_SIGEV_THREAD_ID:
879 	default:
880 		return ENOTSUP;
881 	}
882 
883 	nsep->sigev_value = lsep->sigev_value;
884 	if (lsep->sigev_signo < 0 || lsep->sigev_signo >= LINUX__NSIG) {
885 		return EINVAL;
886 	}
887 	nsep->sigev_signo = linux_to_native_signo[lsep->sigev_signo];
888 
889 	return 0;
890 }
891 
892 int
linux_sigevent_copyin(const void * src,void * dst,size_t size)893 linux_sigevent_copyin(const void *src, void *dst, size_t size)
894 {
895 	struct linux_sigevent lse;
896 	struct sigevent *sep = dst;
897 	int error;
898 
899 	KASSERT(size == sizeof(*sep));
900 
901 	error = copyin(src, &lse, sizeof(lse));
902 	if (error) {
903 		return error;
904 	}
905 
906 	return linux_to_native_sigevent(sep, &lse);
907 }
908