1 /*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/proc.h> 36 #include <sys/signalvar.h> 37 #include <sys/syscallsubr.h> 38 #include <sys/sysproto.h> 39 40 #include <machine/../linux/linux.h> 41 #include <machine/../linux/linux_proto.h> 42 #include <compat/linux/linux_signal.h> 43 #include <compat/linux/linux_util.h> 44 45 void 46 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss) 47 { 48 int b, l; 49 50 SIGEMPTYSET(*bss); 51 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1); 52 bss->__bits[1] = lss->__bits[1]; 53 for (l = 1; l <= LINUX_SIGTBLSZ; l++) { 54 if (LINUX_SIGISMEMBER(*lss, l)) { 55 #ifdef __alpha__ 56 b = _SIG_IDX(l); 57 #else 58 b = linux_to_bsd_signal[_SIG_IDX(l)]; 59 #endif 60 if (b) 61 SIGADDSET(*bss, b); 62 } 63 } 64 } 65 66 void 67 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss) 68 { 69 int b, l; 70 71 LINUX_SIGEMPTYSET(*lss); 72 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1); 73 lss->__bits[1] = bss->__bits[1]; 74 for (b = 1; b <= LINUX_SIGTBLSZ; b++) { 75 if (SIGISMEMBER(*bss, b)) { 76 #if __alpha__ 77 l = _SIG_IDX(b); 78 #else 79 l = bsd_to_linux_signal[_SIG_IDX(b)]; 80 #endif 81 if (l) 82 LINUX_SIGADDSET(*lss, l); 83 } 84 } 85 } 86 87 static void 88 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa) 89 { 90 91 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask); 92 bsa->sa_handler = lsa->lsa_handler; 93 bsa->sa_flags = 0; 94 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) 95 bsa->sa_flags |= SA_NOCLDSTOP; 96 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) 97 bsa->sa_flags |= SA_NOCLDWAIT; 98 if (lsa->lsa_flags & LINUX_SA_SIGINFO) 99 bsa->sa_flags |= SA_SIGINFO; 100 if (lsa->lsa_flags & LINUX_SA_ONSTACK) 101 bsa->sa_flags |= SA_ONSTACK; 102 if (lsa->lsa_flags & LINUX_SA_RESTART) 103 bsa->sa_flags |= SA_RESTART; 104 if (lsa->lsa_flags & LINUX_SA_ONESHOT) 105 bsa->sa_flags |= SA_RESETHAND; 106 if (lsa->lsa_flags & LINUX_SA_NOMASK) 107 bsa->sa_flags |= SA_NODEFER; 108 } 109 110 static void 111 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa) 112 { 113 114 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask); 115 lsa->lsa_handler = bsa->sa_handler; 116 lsa->lsa_restorer = NULL; /* unsupported */ 117 lsa->lsa_flags = 0; 118 if (bsa->sa_flags & SA_NOCLDSTOP) 119 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP; 120 if (bsa->sa_flags & SA_NOCLDWAIT) 121 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT; 122 if (bsa->sa_flags & SA_SIGINFO) 123 lsa->lsa_flags |= LINUX_SA_SIGINFO; 124 if (bsa->sa_flags & SA_ONSTACK) 125 lsa->lsa_flags |= LINUX_SA_ONSTACK; 126 if (bsa->sa_flags & SA_RESTART) 127 lsa->lsa_flags |= LINUX_SA_RESTART; 128 if (bsa->sa_flags & SA_RESETHAND) 129 lsa->lsa_flags |= LINUX_SA_ONESHOT; 130 if (bsa->sa_flags & SA_NODEFER) 131 lsa->lsa_flags |= LINUX_SA_NOMASK; 132 } 133 134 int 135 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa, 136 l_sigaction_t *linux_osa) 137 { 138 struct sigaction act, oact, *nsa, *osa; 139 int error, sig; 140 141 if (linux_sig <= 0 || linux_sig > LINUX_NSIG) 142 return (EINVAL); 143 144 osa = (linux_osa != NULL) ? &oact : NULL; 145 if (linux_nsa != NULL) { 146 nsa = &act; 147 linux_to_bsd_sigaction(linux_nsa, nsa); 148 } else 149 nsa = NULL; 150 151 #ifndef __alpha__ 152 if (linux_sig <= LINUX_SIGTBLSZ) 153 sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)]; 154 else 155 #endif 156 sig = linux_sig; 157 158 error = kern_sigaction(td, sig, nsa, osa, 0); 159 if (error) 160 return (error); 161 162 if (linux_osa != NULL) 163 bsd_to_linux_sigaction(osa, linux_osa); 164 165 return (0); 166 } 167 168 169 #ifndef __alpha__ 170 int 171 linux_signal(struct thread *td, struct linux_signal_args *args) 172 { 173 l_sigaction_t nsa, osa; 174 int error; 175 176 #ifdef DEBUG 177 if (ldebug(signal)) 178 printf(ARGS(signal, "%d, %p"), 179 args->sig, (void *)args->handler); 180 #endif 181 182 nsa.lsa_handler = args->handler; 183 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK; 184 LINUX_SIGEMPTYSET(nsa.lsa_mask); 185 186 error = linux_do_sigaction(td, args->sig, &nsa, &osa); 187 td->td_retval[0] = (int)osa.lsa_handler; 188 189 return (error); 190 } 191 #endif /*!__alpha__*/ 192 193 int 194 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args) 195 { 196 l_sigaction_t nsa, osa; 197 int error; 198 199 #ifdef DEBUG 200 if (ldebug(rt_sigaction)) 201 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"), 202 (long)args->sig, (void *)args->act, 203 (void *)args->oact, (long)args->sigsetsize); 204 #endif 205 206 if (args->sigsetsize != sizeof(l_sigset_t)) 207 return (EINVAL); 208 209 if (args->act != NULL) { 210 error = copyin(args->act, &nsa, sizeof(l_sigaction_t)); 211 if (error) 212 return (error); 213 } 214 215 error = linux_do_sigaction(td, args->sig, 216 args->act ? &nsa : NULL, 217 args->oact ? &osa : NULL); 218 219 if (args->oact != NULL && !error) { 220 error = copyout(&osa, args->oact, sizeof(l_sigaction_t)); 221 } 222 223 return (error); 224 } 225 226 static int 227 linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new, 228 l_sigset_t *old) 229 { 230 int error; 231 sigset_t mask; 232 struct proc *p = td->td_proc; 233 234 error = 0; 235 td->td_retval[0] = 0; 236 237 PROC_LOCK(p); 238 if (old != NULL) 239 bsd_to_linux_sigset(&p->p_sigmask, old); 240 241 if (new != NULL) { 242 linux_to_bsd_sigset(new, &mask); 243 244 switch (how) { 245 case LINUX_SIG_BLOCK: 246 SIGSETOR(p->p_sigmask, mask); 247 SIG_CANTMASK(p->p_sigmask); 248 break; 249 case LINUX_SIG_UNBLOCK: 250 SIGSETNAND(p->p_sigmask, mask); 251 signotify(p); 252 break; 253 case LINUX_SIG_SETMASK: 254 p->p_sigmask = mask; 255 SIG_CANTMASK(p->p_sigmask); 256 signotify(p); 257 break; 258 default: 259 error = EINVAL; 260 break; 261 } 262 } 263 PROC_UNLOCK(p); 264 265 return (error); 266 } 267 268 #ifndef __alpha__ 269 int 270 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args) 271 { 272 l_osigset_t mask; 273 l_sigset_t set, oset; 274 int error; 275 276 #ifdef DEBUG 277 if (ldebug(sigprocmask)) 278 printf(ARGS(sigprocmask, "%d, *, *"), args->how); 279 #endif 280 281 if (args->mask != NULL) { 282 error = copyin(args->mask, &mask, sizeof(l_osigset_t)); 283 if (error) 284 return (error); 285 LINUX_SIGEMPTYSET(set); 286 set.__bits[0] = mask; 287 } 288 289 error = linux_do_sigprocmask(td, args->how, 290 args->mask ? &set : NULL, 291 args->omask ? &oset : NULL); 292 293 if (args->omask != NULL && !error) { 294 mask = oset.__bits[0]; 295 error = copyout(&mask, args->omask, sizeof(l_osigset_t)); 296 } 297 298 return (error); 299 } 300 #endif /*!__alpha__*/ 301 302 int 303 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args) 304 { 305 l_sigset_t set, oset; 306 int error; 307 308 #ifdef DEBUG 309 if (ldebug(rt_sigprocmask)) 310 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"), 311 args->how, (void *)args->mask, 312 (void *)args->omask, (long)args->sigsetsize); 313 #endif 314 315 if (args->sigsetsize != sizeof(l_sigset_t)) 316 return EINVAL; 317 318 if (args->mask != NULL) { 319 error = copyin(args->mask, &set, sizeof(l_sigset_t)); 320 if (error) 321 return (error); 322 } 323 324 error = linux_do_sigprocmask(td, args->how, 325 args->mask ? &set : NULL, 326 args->omask ? &oset : NULL); 327 328 if (args->omask != NULL && !error) { 329 error = copyout(&oset, args->omask, sizeof(l_sigset_t)); 330 } 331 332 return (error); 333 } 334 335 #ifndef __alpha__ 336 int 337 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args) 338 { 339 struct proc *p = td->td_proc; 340 l_sigset_t mask; 341 342 #ifdef DEBUG 343 if (ldebug(sgetmask)) 344 printf(ARGS(sgetmask, "")); 345 #endif 346 347 PROC_LOCK(p); 348 bsd_to_linux_sigset(&p->p_sigmask, &mask); 349 PROC_UNLOCK(p); 350 td->td_retval[0] = mask.__bits[0]; 351 return (0); 352 } 353 354 int 355 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args) 356 { 357 struct proc *p = td->td_proc; 358 l_sigset_t lset; 359 sigset_t bset; 360 361 #ifdef DEBUG 362 if (ldebug(ssetmask)) 363 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask); 364 #endif 365 366 PROC_LOCK(p); 367 bsd_to_linux_sigset(&p->p_sigmask, &lset); 368 td->td_retval[0] = lset.__bits[0]; 369 LINUX_SIGEMPTYSET(lset); 370 lset.__bits[0] = args->mask; 371 linux_to_bsd_sigset(&lset, &bset); 372 p->p_sigmask = bset; 373 SIG_CANTMASK(p->p_sigmask); 374 signotify(p); 375 PROC_UNLOCK(p); 376 return (0); 377 } 378 379 /* 380 * MPSAFE 381 */ 382 int 383 linux_sigpending(struct thread *td, struct linux_sigpending_args *args) 384 { 385 struct proc *p = td->td_proc; 386 sigset_t bset; 387 l_sigset_t lset; 388 l_osigset_t mask; 389 390 #ifdef DEBUG 391 if (ldebug(sigpending)) 392 printf(ARGS(sigpending, "*")); 393 #endif 394 395 PROC_LOCK(p); 396 bset = p->p_siglist; 397 SIGSETAND(bset, p->p_sigmask); 398 bsd_to_linux_sigset(&bset, &lset); 399 PROC_UNLOCK(p); 400 mask = lset.__bits[0]; 401 return (copyout(&mask, args->mask, sizeof(mask))); 402 } 403 #endif /*!__alpha__*/ 404 405 int 406 linux_kill(struct thread *td, struct linux_kill_args *args) 407 { 408 struct kill_args /* { 409 int pid; 410 int signum; 411 } */ tmp; 412 413 #ifdef DEBUG 414 if (ldebug(kill)) 415 printf(ARGS(kill, "%d, %d"), args->pid, args->signum); 416 #endif 417 418 /* 419 * Allow signal 0 as a means to check for privileges 420 */ 421 if (args->signum < 0 || args->signum > LINUX_NSIG) 422 return EINVAL; 423 424 #ifndef __alpha__ 425 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ) 426 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)]; 427 else 428 #endif 429 tmp.signum = args->signum; 430 431 tmp.pid = args->pid; 432 return (kill(td, &tmp)); 433 } 434