linux_signal.c revision 1.68 1 1.68 njoly /* $NetBSD: linux_signal.c,v 1.68 2009/05/29 14:19:13 njoly Exp $ */
2 1.53 ad
3 1.16 fvdl /*-
4 1.16 fvdl * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.16 fvdl * This code is derived from software contributed to The NetBSD Foundation
8 1.16 fvdl * by Frank van der Linden and Eric Haszlakiewicz.
9 1.16 fvdl *
10 1.1 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1 fvdl * modification, are permitted provided that the following conditions
12 1.1 fvdl * are met:
13 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
17 1.1 fvdl * documentation and/or other materials provided with the distribution.
18 1.1 fvdl *
19 1.16 fvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.16 fvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.16 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.16 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.16 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.16 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.16 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.16 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.16 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.16 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.16 fvdl * POSSIBILITY OF SUCH DAMAGE.
30 1.16 fvdl */
31 1.16 fvdl /*
32 1.1 fvdl * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp
33 1.1 fvdl */
34 1.1 fvdl
35 1.14 erh /*
36 1.14 erh * Functions in multiarch:
37 1.14 erh * linux_sys_signal : linux_sig_notalpha.c
38 1.14 erh * linux_sys_siggetmask : linux_sig_notalpha.c
39 1.14 erh * linux_sys_sigsetmask : linux_sig_notalpha.c
40 1.14 erh * linux_sys_pause : linux_sig_notalpha.c
41 1.14 erh * linux_sys_sigaction : linux_sigaction.c
42 1.14 erh *
43 1.14 erh */
44 1.14 erh
45 1.14 erh /*
46 1.14 erh * Unimplemented:
47 1.14 erh * linux_sys_rt_sigtimedwait : sigsuspend w/timeout.
48 1.14 erh */
49 1.32 lukem
50 1.32 lukem #include <sys/cdefs.h>
51 1.68 njoly __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.68 2009/05/29 14:19:13 njoly Exp $");
52 1.21 drochner
53 1.21 drochner #define COMPAT_LINUX 1
54 1.14 erh
55 1.1 fvdl #include <sys/param.h>
56 1.1 fvdl #include <sys/systm.h>
57 1.1 fvdl #include <sys/namei.h>
58 1.1 fvdl #include <sys/proc.h>
59 1.1 fvdl #include <sys/filedesc.h>
60 1.1 fvdl #include <sys/ioctl.h>
61 1.1 fvdl #include <sys/mount.h>
62 1.1 fvdl #include <sys/kernel.h>
63 1.1 fvdl #include <sys/signal.h>
64 1.1 fvdl #include <sys/signalvar.h>
65 1.1 fvdl #include <sys/malloc.h>
66 1.68 njoly #include <sys/wait.h>
67 1.1 fvdl
68 1.1 fvdl #include <sys/syscallargs.h>
69 1.1 fvdl
70 1.15 christos #include <compat/linux/common/linux_types.h>
71 1.15 christos #include <compat/linux/common/linux_signal.h>
72 1.49 manu #include <compat/linux/common/linux_exec.h> /* For emul_linux */
73 1.49 manu #include <compat/linux/common/linux_machdep.h> /* For LINUX_NPTL */
74 1.49 manu #include <compat/linux/common/linux_emuldata.h> /* for linux_emuldata */
75 1.17 erh #include <compat/linux/common/linux_siginfo.h>
76 1.45 fvdl #include <compat/linux/common/linux_sigevent.h>
77 1.15 christos #include <compat/linux/common/linux_util.h>
78 1.55 njoly #include <compat/linux/common/linux_ipc.h>
79 1.55 njoly #include <compat/linux/common/linux_sem.h>
80 1.15 christos
81 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
82 1.1 fvdl
83 1.14 erh /* Locally used defines (in bsd<->linux conversion functions): */
84 1.11 perry #define linux_sigemptyset(s) memset((s), 0, sizeof(*(s)))
85 1.20 itohy #define linux_sigismember(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \
86 1.63 njoly & (1L << ((n) - 1) % LINUX__NSIG_BPW))
87 1.20 itohy #define linux_sigaddset(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \
88 1.63 njoly |= (1L << ((n) - 1) % LINUX__NSIG_BPW))
89 1.6 mycroft
90 1.36 christos #ifdef DEBUG_LINUX
91 1.36 christos #define DPRINTF(a) uprintf a
92 1.36 christos #else
93 1.36 christos #define DPRINTF(a)
94 1.36 christos #endif
95 1.36 christos
96 1.37 christos extern const int native_to_linux_signo[];
97 1.37 christos extern const int linux_to_native_signo[];
98 1.6 mycroft
99 1.14 erh /*
100 1.20 itohy * Convert between Linux and BSD signal sets.
101 1.14 erh */
102 1.20 itohy #if LINUX__NSIG_WORDS > 1
103 1.6 mycroft void
104 1.57 dsl linux_old_extra_to_native_sigset(sigset_t *bss, const linux_old_sigset_t *lss, const unsigned long *extra)
105 1.20 itohy {
106 1.20 itohy linux_sigset_t lsnew;
107 1.20 itohy
108 1.20 itohy /* convert old sigset to new sigset */
109 1.20 itohy linux_sigemptyset(&lsnew);
110 1.20 itohy lsnew.sig[0] = *lss;
111 1.20 itohy if (extra)
112 1.33 christos memcpy(&lsnew.sig[1], extra,
113 1.33 christos sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
114 1.20 itohy
115 1.33 christos linux_to_native_sigset(bss, &lsnew);
116 1.20 itohy }
117 1.20 itohy
118 1.20 itohy void
119 1.57 dsl native_to_linux_old_extra_sigset(linux_old_sigset_t *lss, unsigned long *extra, const sigset_t *bss)
120 1.20 itohy {
121 1.20 itohy linux_sigset_t lsnew;
122 1.20 itohy
123 1.33 christos native_to_linux_sigset(&lsnew, bss);
124 1.20 itohy
125 1.20 itohy /* convert new sigset to old sigset */
126 1.20 itohy *lss = lsnew.sig[0];
127 1.20 itohy if (extra)
128 1.33 christos memcpy(extra, &lsnew.sig[1],
129 1.33 christos sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
130 1.20 itohy }
131 1.43 manu #endif /* LINUX__NSIG_WORDS > 1 */
132 1.20 itohy
133 1.20 itohy void
134 1.57 dsl linux_to_native_sigset(sigset_t *bss, const linux_sigset_t *lss)
135 1.1 fvdl {
136 1.1 fvdl int i, newsig;
137 1.1 fvdl
138 1.6 mycroft sigemptyset(bss);
139 1.20 itohy for (i = 1; i < LINUX__NSIG; i++) {
140 1.6 mycroft if (linux_sigismember(lss, i)) {
141 1.37 christos newsig = linux_to_native_signo[i];
142 1.1 fvdl if (newsig)
143 1.6 mycroft sigaddset(bss, newsig);
144 1.1 fvdl }
145 1.1 fvdl }
146 1.1 fvdl }
147 1.1 fvdl
148 1.1 fvdl void
149 1.57 dsl native_to_linux_sigset(linux_sigset_t *lss, const sigset_t *bss)
150 1.1 fvdl {
151 1.1 fvdl int i, newsig;
152 1.20 itohy
153 1.6 mycroft linux_sigemptyset(lss);
154 1.6 mycroft for (i = 1; i < NSIG; i++) {
155 1.6 mycroft if (sigismember(bss, i)) {
156 1.37 christos newsig = native_to_linux_signo[i];
157 1.1 fvdl if (newsig)
158 1.6 mycroft linux_sigaddset(lss, newsig);
159 1.1 fvdl }
160 1.1 fvdl }
161 1.1 fvdl }
162 1.1 fvdl
163 1.34 christos unsigned int
164 1.57 dsl native_to_linux_sigflags(const int bsf)
165 1.33 christos {
166 1.34 christos unsigned int lsf = 0;
167 1.34 christos if ((bsf & SA_NOCLDSTOP) != 0)
168 1.34 christos lsf |= LINUX_SA_NOCLDSTOP;
169 1.34 christos if ((bsf & SA_NOCLDWAIT) != 0)
170 1.34 christos lsf |= LINUX_SA_NOCLDWAIT;
171 1.34 christos if ((bsf & SA_ONSTACK) != 0)
172 1.34 christos lsf |= LINUX_SA_ONSTACK;
173 1.34 christos if ((bsf & SA_RESTART) != 0)
174 1.34 christos lsf |= LINUX_SA_RESTART;
175 1.34 christos if ((bsf & SA_NODEFER) != 0)
176 1.34 christos lsf |= LINUX_SA_NOMASK;
177 1.34 christos if ((bsf & SA_RESETHAND) != 0)
178 1.34 christos lsf |= LINUX_SA_ONESHOT;
179 1.34 christos if ((bsf & SA_SIGINFO) != 0)
180 1.34 christos lsf |= LINUX_SA_SIGINFO;
181 1.34 christos return lsf;
182 1.33 christos }
183 1.33 christos
184 1.34 christos int
185 1.57 dsl linux_to_native_sigflags(const unsigned long lsf)
186 1.33 christos {
187 1.34 christos int bsf = 0;
188 1.34 christos if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
189 1.34 christos bsf |= SA_NOCLDSTOP;
190 1.34 christos if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
191 1.34 christos bsf |= SA_NOCLDWAIT;
192 1.34 christos if ((lsf & LINUX_SA_ONSTACK) != 0)
193 1.34 christos bsf |= SA_ONSTACK;
194 1.34 christos if ((lsf & LINUX_SA_RESTART) != 0)
195 1.34 christos bsf |= SA_RESTART;
196 1.34 christos if ((lsf & LINUX_SA_ONESHOT) != 0)
197 1.34 christos bsf |= SA_RESETHAND;
198 1.34 christos if ((lsf & LINUX_SA_NOMASK) != 0)
199 1.34 christos bsf |= SA_NODEFER;
200 1.34 christos if ((lsf & LINUX_SA_SIGINFO) != 0)
201 1.34 christos bsf |= SA_SIGINFO;
202 1.50 christos if ((lsf & ~LINUX_SA_ALLBITS) != 0) {
203 1.36 christos DPRINTF(("linux_old_to_native_sigflags: "
204 1.36 christos "%lx extra bits ignored\n", lsf));
205 1.50 christos }
206 1.34 christos return bsf;
207 1.33 christos }
208 1.33 christos
209 1.1 fvdl /*
210 1.46 manu * Convert between Linux and BSD sigaction structures.
211 1.1 fvdl */
212 1.1 fvdl void
213 1.57 dsl linux_old_to_native_sigaction(struct sigaction *bsa, const struct linux_old_sigaction *lsa)
214 1.14 erh {
215 1.39 christos bsa->sa_handler = lsa->linux_sa_handler;
216 1.39 christos linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
217 1.39 christos bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
218 1.14 erh }
219 1.14 erh
220 1.14 erh void
221 1.57 dsl native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa)
222 1.14 erh {
223 1.39 christos lsa->linux_sa_handler = bsa->sa_handler;
224 1.39 christos native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
225 1.39 christos lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
226 1.34 christos #ifndef __alpha__
227 1.39 christos lsa->linux_sa_restorer = NULL;
228 1.34 christos #endif
229 1.14 erh }
230 1.14 erh
231 1.14 erh /* ...and the new sigaction conversion funcs. */
232 1.14 erh void
233 1.57 dsl linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa)
234 1.1 fvdl {
235 1.39 christos bsa->sa_handler = lsa->linux_sa_handler;
236 1.39 christos linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
237 1.39 christos bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
238 1.1 fvdl }
239 1.1 fvdl
240 1.1 fvdl void
241 1.57 dsl native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa)
242 1.1 fvdl {
243 1.39 christos lsa->linux_sa_handler = bsa->sa_handler;
244 1.39 christos native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
245 1.39 christos lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
246 1.34 christos #ifndef __alpha__
247 1.39 christos lsa->linux_sa_restorer = NULL;
248 1.34 christos #endif
249 1.1 fvdl }
250 1.1 fvdl
251 1.14 erh /* ----------------------------------------------------------------------- */
252 1.1 fvdl
253 1.1 fvdl /*
254 1.1 fvdl * The Linux sigaction() system call. Do the usual conversions,
255 1.1 fvdl * and just call sigaction(). Some flags and values are silently
256 1.1 fvdl * ignored (see above).
257 1.1 fvdl */
258 1.1 fvdl int
259 1.58 dsl linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval)
260 1.8 thorpej {
261 1.58 dsl /* {
262 1.1 fvdl syscallarg(int) signum;
263 1.12 mycroft syscallarg(const struct linux_sigaction *) nsa;
264 1.1 fvdl syscallarg(struct linux_sigaction *) osa;
265 1.14 erh syscallarg(size_t) sigsetsize;
266 1.58 dsl } */
267 1.12 mycroft struct linux_sigaction nlsa, olsa;
268 1.12 mycroft struct sigaction nbsa, obsa;
269 1.25 tron int error, sig;
270 1.46 manu void *tramp = NULL;
271 1.46 manu int vers = 0;
272 1.47 mrg #if defined __amd64__
273 1.53 ad struct sigacts *ps = l->l_proc->p_sigacts;
274 1.46 manu #endif
275 1.1 fvdl
276 1.14 erh if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
277 1.14 erh return (EINVAL);
278 1.14 erh
279 1.12 mycroft if (SCARG(uap, nsa)) {
280 1.12 mycroft error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
281 1.12 mycroft if (error)
282 1.12 mycroft return (error);
283 1.33 christos linux_to_native_sigaction(&nbsa, &nlsa);
284 1.12 mycroft }
285 1.46 manu
286 1.25 tron sig = SCARG(uap, signum);
287 1.25 tron if (sig < 0 || sig >= LINUX__NSIG)
288 1.25 tron return (EINVAL);
289 1.37 christos if (sig > 0 && !linux_to_native_signo[sig]) {
290 1.29 tv /* Pretend that we did something useful for unknown signals. */
291 1.29 tv obsa.sa_handler = SIG_IGN;
292 1.29 tv sigemptyset(&obsa.sa_mask);
293 1.29 tv obsa.sa_flags = 0;
294 1.29 tv } else {
295 1.46 manu #if defined __amd64__
296 1.46 manu if (nlsa.linux_sa_flags & LINUX_SA_RESTORER) {
297 1.46 manu if ((tramp = nlsa.linux_sa_restorer) != NULL)
298 1.46 manu vers = 2; /* XXX arch dependant */
299 1.46 manu }
300 1.46 manu #endif
301 1.46 manu
302 1.53 ad error = sigaction1(l, linux_to_native_signo[sig],
303 1.38 thorpej SCARG(uap, nsa) ? &nbsa : NULL,
304 1.38 thorpej SCARG(uap, osa) ? &obsa : NULL,
305 1.46 manu tramp, vers);
306 1.29 tv if (error)
307 1.29 tv return (error);
308 1.29 tv }
309 1.12 mycroft if (SCARG(uap, osa)) {
310 1.33 christos native_to_linux_sigaction(&olsa, &obsa);
311 1.46 manu
312 1.46 manu #if defined __amd64__
313 1.46 manu if (ps->sa_sigdesc[sig].sd_vers != 0) {
314 1.46 manu olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp;
315 1.46 manu olsa.linux_sa_flags |= LINUX_SA_RESTORER;
316 1.46 manu }
317 1.46 manu #endif
318 1.46 manu
319 1.12 mycroft error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
320 1.12 mycroft if (error)
321 1.12 mycroft return (error);
322 1.1 fvdl }
323 1.12 mycroft return (0);
324 1.1 fvdl }
325 1.1 fvdl
326 1.1 fvdl int
327 1.57 dsl linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset)
328 1.8 thorpej {
329 1.53 ad struct proc *p = l->l_proc;
330 1.14 erh linux_old_sigset_t nlss, olss;
331 1.12 mycroft sigset_t nbss, obss;
332 1.12 mycroft int error;
333 1.1 fvdl
334 1.17 erh switch (how) {
335 1.1 fvdl case LINUX_SIG_BLOCK:
336 1.12 mycroft how = SIG_BLOCK;
337 1.1 fvdl break;
338 1.1 fvdl case LINUX_SIG_UNBLOCK:
339 1.12 mycroft how = SIG_UNBLOCK;
340 1.1 fvdl break;
341 1.1 fvdl case LINUX_SIG_SETMASK:
342 1.12 mycroft how = SIG_SETMASK;
343 1.1 fvdl break;
344 1.1 fvdl default:
345 1.12 mycroft return (EINVAL);
346 1.1 fvdl }
347 1.1 fvdl
348 1.17 erh if (set) {
349 1.17 erh error = copyin(set, &nlss, sizeof(nlss));
350 1.12 mycroft if (error)
351 1.12 mycroft return (error);
352 1.33 christos linux_old_to_native_sigset(&nbss, &nlss);
353 1.12 mycroft }
354 1.61 ad mutex_enter(p->p_lock);
355 1.53 ad error = sigprocmask1(l, how,
356 1.27 tron set ? &nbss : NULL, oset ? &obss : NULL);
357 1.61 ad mutex_exit(p->p_lock);
358 1.12 mycroft if (error)
359 1.42 perry return (error);
360 1.17 erh if (oset) {
361 1.33 christos native_to_linux_old_sigset(&olss, &obss);
362 1.17 erh error = copyout(&olss, oset, sizeof(olss));
363 1.12 mycroft if (error)
364 1.12 mycroft return (error);
365 1.42 perry }
366 1.12 mycroft return (error);
367 1.17 erh }
368 1.17 erh
369 1.17 erh int
370 1.58 dsl linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval)
371 1.17 erh {
372 1.58 dsl /* {
373 1.17 erh syscallarg(int) how;
374 1.17 erh syscallarg(const linux_sigset_t *) set;
375 1.17 erh syscallarg(linux_sigset_t *) oset;
376 1.17 erh syscallarg(size_t) sigsetsize;
377 1.58 dsl } */
378 1.24 fvdl linux_sigset_t nlss, olss, *oset;
379 1.24 fvdl const linux_sigset_t *set;
380 1.53 ad struct proc *p = l->l_proc;
381 1.24 fvdl sigset_t nbss, obss;
382 1.24 fvdl int error, how;
383 1.24 fvdl
384 1.24 fvdl if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
385 1.24 fvdl return (EINVAL);
386 1.24 fvdl
387 1.24 fvdl switch (SCARG(uap, how)) {
388 1.24 fvdl case LINUX_SIG_BLOCK:
389 1.24 fvdl how = SIG_BLOCK;
390 1.24 fvdl break;
391 1.24 fvdl case LINUX_SIG_UNBLOCK:
392 1.24 fvdl how = SIG_UNBLOCK;
393 1.24 fvdl break;
394 1.24 fvdl case LINUX_SIG_SETMASK:
395 1.24 fvdl how = SIG_SETMASK;
396 1.24 fvdl break;
397 1.24 fvdl default:
398 1.24 fvdl return (EINVAL);
399 1.17 erh }
400 1.17 erh
401 1.24 fvdl set = SCARG(uap, set);
402 1.24 fvdl oset = SCARG(uap, oset);
403 1.24 fvdl
404 1.24 fvdl if (set) {
405 1.24 fvdl error = copyin(set, &nlss, sizeof(nlss));
406 1.24 fvdl if (error)
407 1.24 fvdl return (error);
408 1.33 christos linux_to_native_sigset(&nbss, &nlss);
409 1.24 fvdl }
410 1.61 ad mutex_enter(p->p_lock);
411 1.53 ad error = sigprocmask1(l, how,
412 1.27 tron set ? &nbss : NULL, oset ? &obss : NULL);
413 1.61 ad mutex_exit(p->p_lock);
414 1.28 tron if (!error && oset) {
415 1.33 christos native_to_linux_sigset(&olss, &obss);
416 1.24 fvdl error = copyout(&olss, oset, sizeof(olss));
417 1.42 perry }
418 1.24 fvdl return (error);
419 1.1 fvdl }
420 1.1 fvdl
421 1.1 fvdl int
422 1.58 dsl linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval)
423 1.1 fvdl {
424 1.58 dsl /* {
425 1.14 erh syscallarg(linux_sigset_t *) set;
426 1.14 erh syscallarg(size_t) sigsetsize;
427 1.58 dsl } */
428 1.12 mycroft sigset_t bss;
429 1.12 mycroft linux_sigset_t lss;
430 1.6 mycroft
431 1.14 erh if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
432 1.14 erh return (EINVAL);
433 1.14 erh
434 1.53 ad sigpending1(l, &bss);
435 1.33 christos native_to_linux_sigset(&lss, &bss);
436 1.14 erh return copyout(&lss, SCARG(uap, set), sizeof(lss));
437 1.1 fvdl }
438 1.24 fvdl
439 1.43 manu #ifndef __amd64__
440 1.1 fvdl int
441 1.58 dsl linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval)
442 1.8 thorpej {
443 1.58 dsl /* {
444 1.14 erh syscallarg(linux_old_sigset_t *) mask;
445 1.58 dsl } */
446 1.12 mycroft sigset_t bss;
447 1.14 erh linux_old_sigset_t lss;
448 1.1 fvdl
449 1.53 ad sigpending1(l, &bss);
450 1.33 christos native_to_linux_old_sigset(&lss, &bss);
451 1.12 mycroft return copyout(&lss, SCARG(uap, set), sizeof(lss));
452 1.1 fvdl }
453 1.1 fvdl
454 1.1 fvdl int
455 1.58 dsl linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval)
456 1.8 thorpej {
457 1.58 dsl /* {
458 1.54 christos syscallarg(void *) restart;
459 1.3 fvdl syscallarg(int) oldmask;
460 1.1 fvdl syscallarg(int) mask;
461 1.58 dsl } */
462 1.14 erh linux_old_sigset_t lss;
463 1.14 erh sigset_t bss;
464 1.14 erh
465 1.14 erh lss = SCARG(uap, mask);
466 1.33 christos linux_old_to_native_sigset(&bss, &lss);
467 1.53 ad return (sigsuspend1(l, &bss));
468 1.14 erh }
469 1.43 manu #endif /* __amd64__ */
470 1.43 manu
471 1.14 erh int
472 1.58 dsl linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval)
473 1.14 erh {
474 1.58 dsl /* {
475 1.14 erh syscallarg(linux_sigset_t *) unewset;
476 1.14 erh syscallarg(size_t) sigsetsize;
477 1.58 dsl } */
478 1.12 mycroft linux_sigset_t lss;
479 1.12 mycroft sigset_t bss;
480 1.14 erh int error;
481 1.14 erh
482 1.14 erh if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
483 1.14 erh return (EINVAL);
484 1.14 erh
485 1.14 erh error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
486 1.14 erh if (error)
487 1.14 erh return (error);
488 1.14 erh
489 1.33 christos linux_to_native_sigset(&bss, &lss);
490 1.14 erh
491 1.53 ad return (sigsuspend1(l, &bss));
492 1.3 fvdl }
493 1.3 fvdl
494 1.3 fvdl /*
495 1.14 erh * Once more: only a signal conversion is needed.
496 1.14 erh * Note: also used as sys_rt_queueinfo. The info field is ignored.
497 1.3 fvdl */
498 1.3 fvdl int
499 1.58 dsl linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval)
500 1.14 erh {
501 1.14 erh /* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
502 1.14 erh #if 0
503 1.14 erh struct linux_sys_rt_queueinfo_args /* {
504 1.14 erh syscallarg(int) pid;
505 1.14 erh syscallarg(int) signum;
506 1.14 erh syscallarg(siginfo_t *) uinfo;
507 1.14 erh } */ *uap = v;
508 1.14 erh #endif
509 1.3 fvdl
510 1.14 erh /* XXX To really implement this we need to */
511 1.14 erh /* XXX keep a list of queued signals somewhere. */
512 1.58 dsl return (linux_sys_kill(l, (const void *)uap, retval));
513 1.1 fvdl }
514 1.1 fvdl
515 1.1 fvdl int
516 1.58 dsl linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval)
517 1.8 thorpej {
518 1.58 dsl /* {
519 1.1 fvdl syscallarg(int) pid;
520 1.1 fvdl syscallarg(int) signum;
521 1.58 dsl } */
522 1.40 thorpej
523 1.9 mycroft struct sys_kill_args ka;
524 1.25 tron int sig;
525 1.6 mycroft
526 1.6 mycroft SCARG(&ka, pid) = SCARG(uap, pid);
527 1.25 tron sig = SCARG(uap, signum);
528 1.25 tron if (sig < 0 || sig >= LINUX__NSIG)
529 1.25 tron return (EINVAL);
530 1.37 christos SCARG(&ka, signum) = linux_to_native_signo[sig];
531 1.40 thorpej return sys_kill(l, &ka, retval);
532 1.1 fvdl }
533 1.30 christos
534 1.30 christos #ifdef LINUX_SS_ONSTACK
535 1.56 dsl static void linux_to_native_sigaltstack(struct sigaltstack *,
536 1.56 dsl const struct linux_sigaltstack *);
537 1.30 christos
538 1.30 christos static void
539 1.57 dsl linux_to_native_sigaltstack(struct sigaltstack *bss, const struct linux_sigaltstack *lss)
540 1.30 christos {
541 1.30 christos bss->ss_sp = lss->ss_sp;
542 1.30 christos bss->ss_size = lss->ss_size;
543 1.30 christos if (lss->ss_flags & LINUX_SS_ONSTACK)
544 1.30 christos bss->ss_flags = SS_ONSTACK;
545 1.30 christos else if (lss->ss_flags & LINUX_SS_DISABLE)
546 1.30 christos bss->ss_flags = SS_DISABLE;
547 1.30 christos else
548 1.30 christos bss->ss_flags = 0;
549 1.30 christos }
550 1.30 christos
551 1.41 christos void
552 1.57 dsl native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const struct sigaltstack *bss)
553 1.30 christos {
554 1.30 christos lss->ss_sp = bss->ss_sp;
555 1.30 christos lss->ss_size = bss->ss_size;
556 1.30 christos if (bss->ss_flags & SS_ONSTACK)
557 1.30 christos lss->ss_flags = LINUX_SS_ONSTACK;
558 1.30 christos else if (bss->ss_flags & SS_DISABLE)
559 1.30 christos lss->ss_flags = LINUX_SS_DISABLE;
560 1.30 christos else
561 1.30 christos lss->ss_flags = 0;
562 1.30 christos }
563 1.30 christos
564 1.30 christos int
565 1.58 dsl linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval)
566 1.30 christos {
567 1.58 dsl /* {
568 1.30 christos syscallarg(const struct linux_sigaltstack *) ss;
569 1.30 christos syscallarg(struct linux_sigaltstack *) oss;
570 1.58 dsl } */
571 1.30 christos struct linux_sigaltstack ss;
572 1.48 christos struct sigaltstack nss;
573 1.53 ad struct proc *p = l->l_proc;
574 1.53 ad int error = 0;
575 1.30 christos
576 1.48 christos if (SCARG(uap, oss)) {
577 1.53 ad native_to_linux_sigaltstack(&ss, &l->l_sigstk);
578 1.48 christos if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
579 1.48 christos return error;
580 1.48 christos }
581 1.48 christos
582 1.30 christos if (SCARG(uap, ss) != NULL) {
583 1.30 christos if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
584 1.30 christos return error;
585 1.30 christos linux_to_native_sigaltstack(&nss, &ss);
586 1.30 christos
587 1.61 ad mutex_enter(p->p_lock);
588 1.53 ad
589 1.48 christos if (nss.ss_flags & ~SS_ALLBITS)
590 1.53 ad error = EINVAL;
591 1.53 ad else if (nss.ss_flags & SS_DISABLE) {
592 1.53 ad if (l->l_sigstk.ss_flags & SS_ONSTACK)
593 1.53 ad error = EINVAL;
594 1.53 ad } else if (nss.ss_size < LINUX_MINSIGSTKSZ)
595 1.53 ad error = ENOMEM;
596 1.30 christos
597 1.53 ad if (error == 0)
598 1.53 ad l->l_sigstk = nss;
599 1.53 ad
600 1.61 ad mutex_exit(p->p_lock);
601 1.30 christos }
602 1.48 christos
603 1.53 ad return error;
604 1.30 christos }
605 1.44 jmc #endif /* LINUX_SS_ONSTACK */
606 1.49 manu
607 1.49 manu #ifdef LINUX_NPTL
608 1.65 njoly static int
609 1.65 njoly linux_do_tkill(struct lwp *l, int tgid, int tid, int signum)
610 1.65 njoly {
611 1.65 njoly struct proc *p;
612 1.65 njoly int error;
613 1.65 njoly ksiginfo_t ksi;
614 1.65 njoly struct linux_emuldata *led;
615 1.65 njoly
616 1.65 njoly if (signum < 0 || signum >= LINUX__NSIG)
617 1.65 njoly return EINVAL;
618 1.65 njoly signum = linux_to_native_signo[signum];
619 1.65 njoly
620 1.65 njoly KSI_INIT(&ksi);
621 1.65 njoly ksi.ksi_signo = signum;
622 1.65 njoly ksi.ksi_code = SI_LWP;
623 1.65 njoly ksi.ksi_pid = l->l_proc->p_pid;
624 1.65 njoly ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
625 1.65 njoly
626 1.65 njoly mutex_enter(proc_lock);
627 1.65 njoly if ((p = p_find(tid, PFIND_LOCKED)) == NULL) {
628 1.65 njoly mutex_exit(proc_lock);
629 1.65 njoly return ESRCH;
630 1.65 njoly }
631 1.65 njoly led = p->p_emuldata;
632 1.65 njoly if (tgid > 0 && led->s->group_pid != tgid) {
633 1.65 njoly mutex_exit(proc_lock);
634 1.65 njoly return ESRCH;
635 1.65 njoly }
636 1.65 njoly mutex_enter(p->p_lock);
637 1.65 njoly error = kauth_authorize_process(l->l_cred,
638 1.65 njoly KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL);
639 1.65 njoly if (!error && signum)
640 1.65 njoly kpsignal2(p, &ksi);
641 1.65 njoly mutex_exit(p->p_lock);
642 1.65 njoly mutex_exit(proc_lock);
643 1.65 njoly
644 1.65 njoly return error;
645 1.65 njoly }
646 1.65 njoly
647 1.49 manu int
648 1.58 dsl linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval)
649 1.49 manu {
650 1.58 dsl /* {
651 1.49 manu syscallarg(int) tid;
652 1.49 manu syscallarg(int) sig;
653 1.58 dsl } */
654 1.49 manu
655 1.65 njoly if (SCARG(uap, tid) <= 0)
656 1.65 njoly return EINVAL;
657 1.49 manu
658 1.65 njoly return linux_do_tkill(l, 0, SCARG(uap, tid), SCARG(uap, sig));
659 1.49 manu }
660 1.49 manu
661 1.49 manu int
662 1.58 dsl linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval)
663 1.49 manu {
664 1.58 dsl /* {
665 1.49 manu syscallarg(int) tgid;
666 1.49 manu syscallarg(int) tid;
667 1.49 manu syscallarg(int) sig;
668 1.58 dsl } */
669 1.49 manu
670 1.65 njoly if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) <= 0)
671 1.65 njoly return EINVAL;
672 1.49 manu
673 1.65 njoly return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig));
674 1.49 manu }
675 1.67 njoly #endif /* LINUX_NPTL */
676 1.64 njoly
677 1.64 njoly int
678 1.64 njoly native_to_linux_si_code(int code)
679 1.64 njoly {
680 1.64 njoly int si_codes[] = {
681 1.64 njoly LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO,
682 1.64 njoly LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */
683 1.64 njoly };
684 1.64 njoly
685 1.64 njoly if (code <= 0 && -code < __arraycount(si_codes))
686 1.64 njoly return si_codes[-code];
687 1.64 njoly
688 1.64 njoly return code;
689 1.64 njoly }
690 1.68 njoly
691 1.68 njoly int
692 1.68 njoly native_to_linux_si_status(int code, int status)
693 1.68 njoly {
694 1.68 njoly int sts;
695 1.68 njoly
696 1.68 njoly switch (code) {
697 1.68 njoly case CLD_CONTINUED:
698 1.68 njoly sts = LINUX_SIGCONT;
699 1.68 njoly break;
700 1.68 njoly case CLD_EXITED:
701 1.68 njoly sts = WEXITSTATUS(status);
702 1.68 njoly break;
703 1.68 njoly case CLD_STOPPED:
704 1.68 njoly case CLD_TRAPPED:
705 1.68 njoly case CLD_DUMPED:
706 1.68 njoly case CLD_KILLED:
707 1.68 njoly default:
708 1.68 njoly sts = native_to_linux_signo[WTERMSIG(status)];
709 1.68 njoly break;
710 1.68 njoly }
711 1.68 njoly
712 1.68 njoly return sts;
713 1.68 njoly }
714