1 /*	$NetBSD: linux_signal.c,v 1.77 2015/11/14 13:29:35 christos 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.77 2015/11/14 13:29:35 christos 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 /*
272  * Convert between Linux and BSD sigaction structures.
273  */
274 void
linux_old_to_native_sigaction(struct sigaction * bsa,const struct linux_old_sigaction * lsa)275 linux_old_to_native_sigaction(struct sigaction *bsa, const struct linux_old_sigaction *lsa)
276 {
277 	bsa->sa_handler = lsa->linux_sa_handler;
278 	linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
279 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
280 }
281 
282 void
native_to_linux_old_sigaction(struct linux_old_sigaction * lsa,const struct sigaction * bsa)283 native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa)
284 {
285 	lsa->linux_sa_handler = bsa->sa_handler;
286 	native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
287 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
288 #ifndef __alpha__
289 	lsa->linux_sa_restorer = NULL;
290 #endif
291 }
292 
293 /* ...and the new sigaction conversion funcs. */
294 void
linux_to_native_sigaction(struct sigaction * bsa,const struct linux_sigaction * lsa)295 linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa)
296 {
297 	bsa->sa_handler = lsa->linux_sa_handler;
298 	linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
299 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
300 }
301 
302 void
native_to_linux_sigaction(struct linux_sigaction * lsa,const struct sigaction * bsa)303 native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa)
304 {
305 	lsa->linux_sa_handler = bsa->sa_handler;
306 	native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
307 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
308 #ifndef __alpha__
309 	lsa->linux_sa_restorer = NULL;
310 #endif
311 }
312 
313 /* ----------------------------------------------------------------------- */
314 
315 /*
316  * The Linux sigaction() system call. Do the usual conversions,
317  * and just call sigaction(). Some flags and values are silently
318  * ignored (see above).
319  */
320 int
linux_sys_rt_sigaction(struct lwp * l,const struct linux_sys_rt_sigaction_args * uap,register_t * retval)321 linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval)
322 {
323 	/* {
324 		syscallarg(int) signum;
325 		syscallarg(const struct linux_sigaction *) nsa;
326 		syscallarg(struct linux_sigaction *) osa;
327 		syscallarg(size_t) sigsetsize;
328 	} */
329 	struct linux_sigaction nlsa, olsa;
330 	struct sigaction nbsa, obsa;
331 	int error, sig;
332 	void *tramp = NULL;
333 	int vers = 0;
334 #ifdef LINUX_SA_RESTORER
335 	struct sigacts *ps = l->l_proc->p_sigacts;
336 #endif
337 
338 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
339 		return (EINVAL);
340 
341 	if (SCARG(uap, nsa)) {
342 		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
343 		if (error)
344 			return (error);
345 		linux_to_native_sigaction(&nbsa, &nlsa);
346 	}
347 
348 	sig = SCARG(uap, signum);
349 	if (sig < 0 || sig >= LINUX__NSIG)
350 		return (EINVAL);
351 	if (sig > 0 && !linux_to_native_signo[sig]) {
352 		/* Pretend that we did something useful for unknown signals. */
353 		obsa.sa_handler = SIG_IGN;
354 		sigemptyset(&obsa.sa_mask);
355 		obsa.sa_flags = 0;
356 	} else {
357 #ifdef LINUX_SA_RESTORER
358 		if ((nlsa.linux_sa_flags & LINUX_SA_RESTORER) &&
359 		    (tramp = nlsa.linux_sa_restorer) != NULL)
360 				vers = 2;
361 #endif
362 
363 		error = sigaction1(l, linux_to_native_signo[sig],
364 		    SCARG(uap, nsa) ? &nbsa : NULL,
365 		    SCARG(uap, osa) ? &obsa : NULL,
366 		    tramp, vers);
367 		if (error)
368 			return (error);
369 	}
370 	if (SCARG(uap, osa)) {
371 		native_to_linux_sigaction(&olsa, &obsa);
372 
373 #ifdef LINUX_SA_RESTORER
374 		if (ps->sa_sigdesc[sig].sd_vers != 0) {
375 			olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp;
376 			olsa.linux_sa_flags |= LINUX_SA_RESTORER;
377 		}
378 #endif
379 
380 		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
381 		if (error)
382 			return (error);
383 	}
384 	return (0);
385 }
386 
387 int
linux_sigprocmask1(struct lwp * l,int how,const linux_old_sigset_t * set,linux_old_sigset_t * oset)388 linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset)
389 {
390 	struct proc *p = l->l_proc;
391 	linux_old_sigset_t nlss, olss;
392 	sigset_t nbss, obss;
393 	int error;
394 
395 	switch (how) {
396 	case LINUX_SIG_BLOCK:
397 		how = SIG_BLOCK;
398 		break;
399 	case LINUX_SIG_UNBLOCK:
400 		how = SIG_UNBLOCK;
401 		break;
402 	case LINUX_SIG_SETMASK:
403 		how = SIG_SETMASK;
404 		break;
405 	default:
406 		return (EINVAL);
407 	}
408 
409 	if (set) {
410 		error = copyin(set, &nlss, sizeof(nlss));
411 		if (error)
412 			return (error);
413 		linux_old_to_native_sigset(&nbss, &nlss);
414 	}
415 	mutex_enter(p->p_lock);
416 	error = sigprocmask1(l, how,
417 	    set ? &nbss : NULL, oset ? &obss : NULL);
418 	mutex_exit(p->p_lock);
419 	if (error)
420 		return (error);
421 	if (oset) {
422 		native_to_linux_old_sigset(&olss, &obss);
423 		error = copyout(&olss, oset, sizeof(olss));
424 		if (error)
425 			return (error);
426 	}
427 	return (error);
428 }
429 
430 int
linux_sys_rt_sigprocmask(struct lwp * l,const struct linux_sys_rt_sigprocmask_args * uap,register_t * retval)431 linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval)
432 {
433 	/* {
434 		syscallarg(int) how;
435 		syscallarg(const linux_sigset_t *) set;
436 		syscallarg(linux_sigset_t *) oset;
437 		syscallarg(size_t) sigsetsize;
438 	} */
439 	linux_sigset_t nlss, olss, *oset;
440 	const linux_sigset_t *set;
441 	struct proc *p = l->l_proc;
442 	sigset_t nbss, obss;
443 	int error, how;
444 
445 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
446 		return (EINVAL);
447 
448 	switch (SCARG(uap, how)) {
449 	case LINUX_SIG_BLOCK:
450 		how = SIG_BLOCK;
451 		break;
452 	case LINUX_SIG_UNBLOCK:
453 		how = SIG_UNBLOCK;
454 		break;
455 	case LINUX_SIG_SETMASK:
456 		how = SIG_SETMASK;
457 		break;
458 	default:
459 		return (EINVAL);
460 	}
461 
462 	set = SCARG(uap, set);
463 	oset = SCARG(uap, oset);
464 
465 	if (set) {
466 		error = copyin(set, &nlss, sizeof(nlss));
467 		if (error)
468 			return (error);
469 		linux_to_native_sigset(&nbss, &nlss);
470 	}
471 	mutex_enter(p->p_lock);
472 	error = sigprocmask1(l, how,
473 	    set ? &nbss : NULL, oset ? &obss : NULL);
474 	mutex_exit(p->p_lock);
475 	if (!error && oset) {
476 		native_to_linux_sigset(&olss, &obss);
477 		error = copyout(&olss, oset, sizeof(olss));
478 	}
479 	return (error);
480 }
481 
482 int
linux_sys_rt_sigpending(struct lwp * l,const struct linux_sys_rt_sigpending_args * uap,register_t * retval)483 linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval)
484 {
485 	/* {
486 		syscallarg(linux_sigset_t *) set;
487 		syscallarg(size_t) sigsetsize;
488 	} */
489 	sigset_t bss;
490 	linux_sigset_t lss;
491 
492 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
493 		return (EINVAL);
494 
495 	sigpending1(l, &bss);
496 	native_to_linux_sigset(&lss, &bss);
497 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
498 }
499 
500 #ifndef __amd64__
501 int
linux_sys_sigpending(struct lwp * l,const struct linux_sys_sigpending_args * uap,register_t * retval)502 linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval)
503 {
504 	/* {
505 		syscallarg(linux_old_sigset_t *) mask;
506 	} */
507 	sigset_t bss;
508 	linux_old_sigset_t lss;
509 
510 	sigpending1(l, &bss);
511 	native_to_linux_old_sigset(&lss, &bss);
512 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
513 }
514 
515 int
linux_sys_sigsuspend(struct lwp * l,const struct linux_sys_sigsuspend_args * uap,register_t * retval)516 linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval)
517 {
518 	/* {
519 		syscallarg(void *) restart;
520 		syscallarg(int) oldmask;
521 		syscallarg(int) mask;
522 	} */
523 	linux_old_sigset_t lss;
524 	sigset_t bss;
525 
526 	lss = SCARG(uap, mask);
527 	linux_old_to_native_sigset(&bss, &lss);
528 	return (sigsuspend1(l, &bss));
529 }
530 #endif /* __amd64__ */
531 
532 int
linux_sys_rt_sigsuspend(struct lwp * l,const struct linux_sys_rt_sigsuspend_args * uap,register_t * retval)533 linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval)
534 {
535 	/* {
536 		syscallarg(linux_sigset_t *) unewset;
537 		syscallarg(size_t) sigsetsize;
538 	} */
539 	linux_sigset_t lss;
540 	sigset_t bss;
541 	int error;
542 
543 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
544 		return (EINVAL);
545 
546 	error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
547 	if (error)
548 		return (error);
549 
550 	linux_to_native_sigset(&bss, &lss);
551 
552 	return (sigsuspend1(l, &bss));
553 }
554 
555 static int
fetchss(const void * u,void * s,size_t len)556 fetchss(const void *u, void *s, size_t len)
557 {
558 	int error;
559 	linux_sigset_t lss;
560 
561 	if ((error = copyin(u, &lss, sizeof(lss))) != 0)
562 		return error;
563 
564 	linux_to_native_sigset(s, &lss);
565 	return 0;
566 }
567 
568 static int
fetchts(const void * u,void * s,size_t len)569 fetchts(const void *u, void *s, size_t len)
570 {
571 	int error;
572 	struct linux_timespec lts;
573 
574 	if ((error = copyin(u, &lts, sizeof(lts))) != 0)
575 		return error;
576 
577 	linux_to_native_timespec(s, &lts);
578 	return 0;
579 }
580 
581 static int
fakestorets(const void * u,void * s,size_t len)582 fakestorets(const void *u, void *s, size_t len)
583 {
584 	/* Do nothing, sigtimedwait does not alter timeout like ours */
585 	return 0;
586 }
587 
588 static int
storeinfo(const void * s,void * u,size_t len)589 storeinfo(const void *s, void *u, size_t len)
590 {
591 	struct linux_siginfo lsi;
592 
593 	native_to_linux_siginfo(&lsi, &((const siginfo_t *)s)->_info);
594 	return copyout(&lsi, u, sizeof(lsi));
595 }
596 
597 int
linux_sys_rt_sigtimedwait(struct lwp * l,const struct linux_sys_rt_sigtimedwait_args * uap,register_t * retval)598 linux_sys_rt_sigtimedwait(struct lwp *l,
599     const struct linux_sys_rt_sigtimedwait_args *uap, register_t *retval)
600 {
601 	/* {
602 		syscallarg(const linux_sigset_t *) set;
603 		syscallarg(linux_siginfo_t *) info);
604 		syscallarg(const struct linux_timespec *) timeout;
605 	} */
606 
607 	return sigtimedwait1(l, (const struct sys_____sigtimedwait50_args *)uap,
608 	    retval, fetchss, storeinfo, fetchts, fakestorets);
609 }
610 
611 /*
612  * Once more: only a signal conversion is needed.
613  * Note: also used as sys_rt_queueinfo.  The info field is ignored.
614  */
615 int
linux_sys_rt_queueinfo(struct lwp * l,const struct linux_sys_rt_queueinfo_args * uap,register_t * retval)616 linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval)
617 {
618 	/*
619 		syscallarg(int) pid;
620 		syscallarg(int) signum;
621 		syscallarg(linix_siginfo_t *) uinfo;
622 	*/
623 	int error;
624 	linux_siginfo_t info;
625 
626 	error = copyin(SCARG(uap, uinfo), &info, sizeof(info));
627 	if (error)
628 		return error;
629 	if (info.lsi_code >= 0)
630 		return EPERM;
631 
632 	/* XXX To really implement this we need to	*/
633 	/* XXX keep a list of queued signals somewhere.	*/
634 	return (linux_sys_kill(l, (const void *)uap, retval));
635 }
636 
637 int
linux_sys_kill(struct lwp * l,const struct linux_sys_kill_args * uap,register_t * retval)638 linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval)
639 {
640 	/* {
641 		syscallarg(int) pid;
642 		syscallarg(int) signum;
643 	} */
644 
645 	struct sys_kill_args ka;
646 	int sig;
647 
648 	SCARG(&ka, pid) = SCARG(uap, pid);
649 	sig = SCARG(uap, signum);
650 	if (sig < 0 || sig >= LINUX__NSIG)
651 		return (EINVAL);
652 	SCARG(&ka, signum) = linux_to_native_signo[sig];
653 	return sys_kill(l, &ka, retval);
654 }
655 
656 #ifdef LINUX_SS_ONSTACK
657 static void linux_to_native_sigaltstack(struct sigaltstack *,
658     const struct linux_sigaltstack *);
659 
660 static void
linux_to_native_sigaltstack(struct sigaltstack * bss,const struct linux_sigaltstack * lss)661 linux_to_native_sigaltstack(struct sigaltstack *bss, const struct linux_sigaltstack *lss)
662 {
663 	bss->ss_sp = lss->ss_sp;
664 	bss->ss_size = lss->ss_size;
665 	if (lss->ss_flags & LINUX_SS_ONSTACK)
666 	    bss->ss_flags = SS_ONSTACK;
667 	else if (lss->ss_flags & LINUX_SS_DISABLE)
668 	    bss->ss_flags = SS_DISABLE;
669 	else
670 	    bss->ss_flags = 0;
671 }
672 
673 void
native_to_linux_sigaltstack(struct linux_sigaltstack * lss,const struct sigaltstack * bss)674 native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const struct sigaltstack *bss)
675 {
676 	lss->ss_sp = bss->ss_sp;
677 	lss->ss_size = bss->ss_size;
678 	if (bss->ss_flags & SS_ONSTACK)
679 	    lss->ss_flags = LINUX_SS_ONSTACK;
680 	else if (bss->ss_flags & SS_DISABLE)
681 	    lss->ss_flags = LINUX_SS_DISABLE;
682 	else
683 	    lss->ss_flags = 0;
684 }
685 
686 int
linux_sys_sigaltstack(struct lwp * l,const struct linux_sys_sigaltstack_args * uap,register_t * retval)687 linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval)
688 {
689 	/* {
690 		syscallarg(const struct linux_sigaltstack *) ss;
691 		syscallarg(struct linux_sigaltstack *) oss;
692 	} */
693 	struct linux_sigaltstack ss;
694 	struct sigaltstack nss;
695 	struct proc *p = l->l_proc;
696 	int error = 0;
697 
698 	if (SCARG(uap, oss)) {
699 		native_to_linux_sigaltstack(&ss, &l->l_sigstk);
700 		if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
701 			return error;
702 	}
703 
704 	if (SCARG(uap, ss) != NULL) {
705 		if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
706 			return error;
707 		linux_to_native_sigaltstack(&nss, &ss);
708 
709 		mutex_enter(p->p_lock);
710 
711 		if (nss.ss_flags & ~SS_ALLBITS)
712 			error = EINVAL;
713 		else if (nss.ss_flags & SS_DISABLE) {
714 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
715 				error = EINVAL;
716 		} else if (nss.ss_size < LINUX_MINSIGSTKSZ)
717 			error = ENOMEM;
718 
719 		if (error == 0)
720 			l->l_sigstk = nss;
721 
722 		mutex_exit(p->p_lock);
723 	}
724 
725 	return error;
726 }
727 #endif /* LINUX_SS_ONSTACK */
728 
729 static int
linux_do_tkill(struct lwp * l,int tgid,int tid,int signum)730 linux_do_tkill(struct lwp *l, int tgid, int tid, int signum)
731 {
732 	struct proc *p;
733 	struct lwp *t;
734 	ksiginfo_t ksi;
735 	int error;
736 
737 	if (signum < 0 || signum >= LINUX__NSIG)
738 		return EINVAL;
739 	signum = linux_to_native_signo[signum];
740 
741 	if (tgid == -1) {
742 		tgid = tid;
743 	}
744 
745 	KSI_INIT(&ksi);
746 	ksi.ksi_signo = signum;
747 	ksi.ksi_code = SI_LWP;
748 	ksi.ksi_pid = l->l_proc->p_pid;
749 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
750 	ksi.ksi_lid = tid;
751 
752 	mutex_enter(proc_lock);
753 	p = proc_find(tgid);
754 	if (p == NULL) {
755 		mutex_exit(proc_lock);
756 		return ESRCH;
757 	}
758 	mutex_enter(p->p_lock);
759 	error = kauth_authorize_process(l->l_cred,
760 	    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL);
761 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
762 		error = ESRCH;
763 	else if (signum != 0)
764 		kpsignal2(p, &ksi);
765 	mutex_exit(p->p_lock);
766 	mutex_exit(proc_lock);
767 
768 	return error;
769 }
770 
771 int
linux_sys_tkill(struct lwp * l,const struct linux_sys_tkill_args * uap,register_t * retval)772 linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval)
773 {
774 	/* {
775 		syscallarg(int) tid;
776 		syscallarg(int) sig;
777 	} */
778 
779 	if (SCARG(uap, tid) <= 0)
780 		return EINVAL;
781 
782 	return linux_do_tkill(l, -1, SCARG(uap, tid), SCARG(uap, sig));
783 }
784 
785 int
linux_sys_tgkill(struct lwp * l,const struct linux_sys_tgkill_args * uap,register_t * retval)786 linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval)
787 {
788 	/* {
789 		syscallarg(int) tgid;
790 		syscallarg(int) tid;
791 		syscallarg(int) sig;
792 	} */
793 
794 	if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) < -1)
795 		return EINVAL;
796 
797 	return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig));
798 }
799 
800 int
native_to_linux_si_code(int code)801 native_to_linux_si_code(int code)
802 {
803 	int si_codes[] = {
804 	    LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO,
805 	    LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */
806 	};
807 
808 	if (code <= 0 && -code < __arraycount(si_codes))
809 		return si_codes[-code];
810 
811 	return code;
812 }
813 
814 int
native_to_linux_si_status(int code,int status)815 native_to_linux_si_status(int code, int status)
816 {
817 	int sts;
818 
819 	switch (code) {
820 	case CLD_CONTINUED:
821 		sts = LINUX_SIGCONT;
822 		break;
823 	case CLD_EXITED:
824 		sts = WEXITSTATUS(status);
825 		break;
826 	case CLD_STOPPED:
827 	case CLD_TRAPPED:
828 	case CLD_DUMPED:
829 	case CLD_KILLED:
830 	default:
831 		sts = native_to_linux_signo[WTERMSIG(status)];
832 		break;
833 	}
834 
835 	return sts;
836 }
837