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 withough 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/sysproto.h> 34 #include <sys/proc.h> 35 #include <sys/signalvar.h> 36 37 #include <i386/linux/linux.h> 38 #include <i386/linux/linux_proto.h> 39 #include <i386/linux/linux_util.h> 40 41 static void 42 linux_to_bsd_sigset(linux_sigset_t *lss, sigset_t *bss) 43 { 44 int b, l; 45 46 SIGEMPTYSET(*bss); 47 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1); 48 bss->__bits[1] = lss->__bits[1]; 49 for (l = 1; l <= LINUX_SIGTBLSZ; l++) { 50 if (LINUX_SIGISMEMBER(*lss, l)) { 51 b = linux_to_bsd_signal[_SIG_IDX(l)]; 52 if (b) 53 SIGADDSET(*bss, b); 54 } 55 } 56 } 57 58 static void 59 bsd_to_linux_sigset(sigset_t *bss, linux_sigset_t *lss) 60 { 61 int b, l; 62 63 LINUX_SIGEMPTYSET(*lss); 64 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1); 65 lss->__bits[1] = bss->__bits[1]; 66 for (b = 1; b <= LINUX_SIGTBLSZ; b++) { 67 if (SIGISMEMBER(*bss, b)) { 68 l = bsd_to_linux_signal[_SIG_IDX(b)]; 69 if (l) 70 LINUX_SIGADDSET(*lss, l); 71 } 72 } 73 } 74 75 static void 76 linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa) 77 { 78 79 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask); 80 bsa->sa_handler = lsa->lsa_handler; 81 bsa->sa_flags = 0; 82 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) 83 bsa->sa_flags |= SA_NOCLDSTOP; 84 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) 85 bsa->sa_flags |= SA_NOCLDWAIT; 86 if (lsa->lsa_flags & LINUX_SA_SIGINFO) 87 bsa->sa_flags |= SA_SIGINFO; 88 if (lsa->lsa_flags & LINUX_SA_ONSTACK) 89 bsa->sa_flags |= SA_ONSTACK; 90 if (lsa->lsa_flags & LINUX_SA_RESTART) 91 bsa->sa_flags |= SA_RESTART; 92 if (lsa->lsa_flags & LINUX_SA_ONESHOT) 93 bsa->sa_flags |= SA_RESETHAND; 94 if (lsa->lsa_flags & LINUX_SA_NOMASK) 95 bsa->sa_flags |= SA_NODEFER; 96 } 97 98 static void 99 bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa) 100 { 101 102 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask); 103 lsa->lsa_handler = bsa->sa_handler; 104 lsa->lsa_restorer = NULL; /* unsupported */ 105 lsa->lsa_flags = 0; 106 if (bsa->sa_flags & SA_NOCLDSTOP) 107 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP; 108 if (bsa->sa_flags & SA_NOCLDWAIT) 109 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT; 110 if (bsa->sa_flags & SA_SIGINFO) 111 lsa->lsa_flags |= LINUX_SA_SIGINFO; 112 if (bsa->sa_flags & SA_ONSTACK) 113 lsa->lsa_flags |= LINUX_SA_ONSTACK; 114 if (bsa->sa_flags & SA_RESTART) 115 lsa->lsa_flags |= LINUX_SA_RESTART; 116 if (bsa->sa_flags & SA_RESETHAND) 117 lsa->lsa_flags |= LINUX_SA_ONESHOT; 118 if (bsa->sa_flags & SA_NODEFER) 119 lsa->lsa_flags |= LINUX_SA_NOMASK; 120 } 121 122 static int 123 linux_do_sigaction(struct proc *p, int linux_sig, linux_sigaction_t *linux_nsa, 124 linux_sigaction_t *linux_osa) 125 { 126 struct sigaction *nsa, *osa, sa; 127 struct sigaction_args sa_args; 128 int error; 129 caddr_t sg = stackgap_init(); 130 131 if (linux_sig <= 0 || linux_sig > LINUX_NSIG) 132 return (EINVAL); 133 134 if (linux_osa != NULL) 135 osa = stackgap_alloc(&sg, sizeof(struct sigaction)); 136 else 137 osa = NULL; 138 139 if (linux_nsa != NULL) { 140 nsa = stackgap_alloc(&sg, sizeof(struct sigaction)); 141 linux_to_bsd_sigaction(linux_nsa, &sa); 142 error = copyout(&sa, nsa, sizeof(struct sigaction)); 143 if (error) 144 return (error); 145 } 146 else 147 nsa = NULL; 148 149 if (linux_sig <= LINUX_SIGTBLSZ) 150 sa_args.sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)]; 151 else 152 sa_args.sig = linux_sig; 153 154 sa_args.act = nsa; 155 sa_args.oact = osa; 156 error = sigaction(p, &sa_args); 157 if (error) 158 return (error); 159 160 if (linux_osa != NULL) { 161 error = copyin(osa, &sa, sizeof(struct sigaction)); 162 if (error) 163 return (error); 164 bsd_to_linux_sigaction(&sa, linux_osa); 165 } 166 167 return (0); 168 } 169 170 int 171 linux_sigaction(struct proc *p, struct linux_sigaction_args *args) 172 { 173 linux_osigaction_t osa; 174 linux_sigaction_t act, oact; 175 int error; 176 177 #ifdef DEBUG 178 printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n", (long)p->p_pid, 179 args->sig, (void *)args->nsa, (void *)args->osa); 180 #endif 181 182 if (args->nsa != NULL) { 183 error = copyin(args->nsa, &osa, sizeof(linux_osigaction_t)); 184 if (error) 185 return (error); 186 act.lsa_handler = osa.lsa_handler; 187 act.lsa_flags = osa.lsa_flags; 188 act.lsa_restorer = osa.lsa_restorer; 189 LINUX_SIGEMPTYSET(act.lsa_mask); 190 act.lsa_mask.__bits[0] = osa.lsa_mask; 191 } 192 193 error = linux_do_sigaction(p, args->sig, 194 args->nsa ? &act : NULL, 195 args->osa ? &oact : NULL); 196 197 if (args->osa != NULL && !error) { 198 osa.lsa_handler = oact.lsa_handler; 199 osa.lsa_flags = oact.lsa_flags; 200 osa.lsa_restorer = oact.lsa_restorer; 201 osa.lsa_mask = oact.lsa_mask.__bits[0]; 202 error = copyout(&osa, args->osa, sizeof(linux_osigaction_t)); 203 } 204 205 return (error); 206 } 207 208 int 209 linux_signal(struct proc *p, struct linux_signal_args *args) 210 { 211 linux_sigaction_t nsa, osa; 212 int error; 213 214 #ifdef DEBUG 215 printf("Linux-emul(%ld): signal(%d, %p)\n", 216 (long)p->p_pid, args->sig, (void *)args->handler); 217 #endif 218 219 nsa.lsa_handler = args->handler; 220 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK; 221 LINUX_SIGEMPTYSET(nsa.lsa_mask); 222 223 error = linux_do_sigaction(p, args->sig, &nsa, &osa); 224 p->p_retval[0] = (int)osa.lsa_handler; 225 226 return (error); 227 } 228 229 int 230 linux_rt_sigaction(struct proc *p, struct linux_rt_sigaction_args *args) 231 { 232 linux_sigaction_t nsa, osa; 233 int error; 234 235 #ifdef DEBUG 236 printf("Linux-emul(%ld): rt_sigaction(%d, %p, %p, %d)\n", 237 (long)p->p_pid, args->sig, (void *)args->act, 238 (void *)args->oact, args->sigsetsize); 239 #endif 240 241 if (args->sigsetsize != sizeof(linux_sigset_t)) 242 return (EINVAL); 243 244 if (args->act != NULL) { 245 error = copyin(args->act, &nsa, sizeof(linux_sigaction_t)); 246 if (error) 247 return (error); 248 } 249 250 error = linux_do_sigaction(p, args->sig, 251 args->act ? &nsa : NULL, 252 args->oact ? &osa : NULL); 253 254 if (args->oact != NULL && !error) { 255 error = copyout(&osa, args->oact, sizeof(linux_sigaction_t)); 256 } 257 258 return (error); 259 } 260 261 static int 262 linux_do_sigprocmask(struct proc *p, int how, linux_sigset_t *new, 263 linux_sigset_t *old) 264 { 265 int error, s; 266 sigset_t mask; 267 268 error = 0; 269 p->p_retval[0] = 0; 270 271 if (old != NULL) 272 bsd_to_linux_sigset(&p->p_sigmask, old); 273 274 if (new != NULL) { 275 linux_to_bsd_sigset(new, &mask); 276 277 s = splhigh(); 278 279 switch (how) { 280 case LINUX_SIG_BLOCK: 281 SIGSETOR(p->p_sigmask, mask); 282 SIG_CANTMASK(p->p_sigmask); 283 break; 284 case LINUX_SIG_UNBLOCK: 285 SIGSETNAND(p->p_sigmask, mask); 286 break; 287 case LINUX_SIG_SETMASK: 288 p->p_sigmask = mask; 289 SIG_CANTMASK(p->p_sigmask); 290 break; 291 default: 292 error = EINVAL; 293 break; 294 } 295 296 splx(s); 297 } 298 299 return (error); 300 } 301 302 int 303 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args) 304 { 305 linux_osigset_t mask; 306 linux_sigset_t set, oset; 307 int error; 308 309 #ifdef DEBUG 310 printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how); 311 #endif 312 313 if (args->mask != NULL) { 314 error = copyin(args->mask, &mask, sizeof(linux_osigset_t)); 315 if (error) 316 return (error); 317 LINUX_SIGEMPTYSET(set); 318 set.__bits[0] = mask; 319 } 320 321 error = linux_do_sigprocmask(p, args->how, 322 args->mask ? &set : NULL, 323 args->omask ? &oset : NULL); 324 325 if (args->omask != NULL && !error) { 326 mask = oset.__bits[0]; 327 error = copyout(&mask, args->omask, sizeof(linux_osigset_t)); 328 } 329 330 return (error); 331 } 332 333 int 334 linux_rt_sigprocmask(struct proc *p, struct linux_rt_sigprocmask_args *args) 335 { 336 linux_sigset_t set, oset; 337 int error; 338 339 #ifdef DEBUG 340 printf("Linux-emul(%ld): rt_sigprocmask(%d, %p, %p, %d)\n", 341 (long)p->p_pid, args->how, (void *)args->mask, 342 (void *)args->omask, args->sigsetsize); 343 #endif 344 345 if (args->sigsetsize != sizeof(linux_sigset_t)) 346 return EINVAL; 347 348 if (args->mask != NULL) { 349 error = copyin(args->mask, &set, sizeof(linux_sigset_t)); 350 if (error) 351 return (error); 352 } 353 354 error = linux_do_sigprocmask(p, args->how, 355 args->mask ? &set : NULL, 356 args->omask ? &oset : NULL); 357 358 if (args->omask != NULL && !error) { 359 error = copyout(&oset, args->omask, sizeof(linux_sigset_t)); 360 } 361 362 return (error); 363 } 364 365 int 366 linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args) 367 { 368 linux_sigset_t mask; 369 370 #ifdef DEBUG 371 printf("Linux-emul(%d): siggetmask()\n", p->p_pid); 372 #endif 373 374 bsd_to_linux_sigset(&p->p_sigmask, &mask); 375 p->p_retval[0] = mask.__bits[0]; 376 return (0); 377 } 378 379 int 380 linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args) 381 { 382 linux_sigset_t lset; 383 sigset_t bset; 384 int s; 385 386 #ifdef DEBUG 387 printf("Linux-emul(%ld): sigsetmask(%08lx)\n", 388 (long)p->p_pid, (unsigned long)args->mask); 389 #endif 390 391 bsd_to_linux_sigset(&p->p_sigmask, &lset); 392 p->p_retval[0] = lset.__bits[0]; 393 LINUX_SIGEMPTYSET(lset); 394 lset.__bits[0] = args->mask; 395 linux_to_bsd_sigset(&lset, &bset); 396 s = splhigh(); 397 p->p_sigmask = bset; 398 SIG_CANTMASK(p->p_sigmask); 399 splx(s); 400 return (0); 401 } 402 403 int 404 linux_sigpending(struct proc *p, struct linux_sigpending_args *args) 405 { 406 sigset_t bset; 407 linux_sigset_t lset; 408 linux_osigset_t mask; 409 410 #ifdef DEBUG 411 printf("Linux-emul(%d): sigpending(*)\n", p->p_pid); 412 #endif 413 414 bset = p->p_siglist; 415 SIGSETAND(bset, p->p_sigmask); 416 bsd_to_linux_sigset(&bset, &lset); 417 mask = lset.__bits[0]; 418 return (copyout(&mask, args->mask, sizeof(mask))); 419 } 420 421 /* 422 * Linux has two extra args, restart and oldmask. We dont use these, 423 * but it seems that "restart" is actually a context pointer that 424 * enables the signal to happen with a different register set. 425 */ 426 int 427 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args) 428 { 429 struct sigsuspend_args bsd; 430 sigset_t *sigmask; 431 linux_sigset_t mask; 432 caddr_t sg = stackgap_init(); 433 434 #ifdef DEBUG 435 printf("Linux-emul(%ld): sigsuspend(%08lx)\n", 436 (long)p->p_pid, (unsigned long)args->mask); 437 #endif 438 439 sigmask = stackgap_alloc(&sg, sizeof(sigset_t)); 440 LINUX_SIGEMPTYSET(mask); 441 mask.__bits[0] = args->mask; 442 linux_to_bsd_sigset(&mask, sigmask); 443 bsd.sigmask = sigmask; 444 return (sigsuspend(p, &bsd)); 445 } 446 447 int 448 linux_rt_sigsuspend(p, uap) 449 struct proc *p; 450 struct linux_rt_sigsuspend_args *uap; 451 { 452 linux_sigset_t lmask; 453 sigset_t *bmask; 454 struct sigsuspend_args bsd; 455 caddr_t sg = stackgap_init(); 456 int error; 457 458 #ifdef DEBUG 459 printf("Linux-emul(%ld): rt_sigsuspend(%p, %d)\n", (long)p->p_pid, 460 (void *)uap->newset, uap->sigsetsize); 461 #endif 462 463 if (uap->sigsetsize != sizeof(linux_sigset_t)) 464 return (EINVAL); 465 466 error = copyin(uap->newset, &lmask, sizeof(linux_sigset_t)); 467 if (error) 468 return (error); 469 470 bmask = stackgap_alloc(&sg, sizeof(sigset_t)); 471 linux_to_bsd_sigset(&lmask, bmask); 472 bsd.sigmask = bmask; 473 return (sigsuspend(p, &bsd)); 474 } 475 476 int 477 linux_pause(struct proc *p, struct linux_pause_args *args) 478 { 479 struct sigsuspend_args bsd; 480 sigset_t *sigmask; 481 caddr_t sg = stackgap_init(); 482 483 #ifdef DEBUG 484 printf("Linux-emul(%d): pause()\n", p->p_pid); 485 #endif 486 487 sigmask = stackgap_alloc(&sg, sizeof(sigset_t)); 488 *sigmask = p->p_sigmask; 489 bsd.sigmask = sigmask; 490 return sigsuspend(p, &bsd); 491 } 492 493 int 494 linux_kill(struct proc *p, struct linux_kill_args *args) 495 { 496 struct kill_args /* { 497 int pid; 498 int signum; 499 } */ tmp; 500 501 #ifdef DEBUG 502 printf("Linux-emul(%d): kill(%d, %d)\n", 503 p->p_pid, args->pid, args->signum); 504 #endif 505 506 /* 507 * Allow signal 0 as a means to check for privileges 508 */ 509 if (args->signum < 0 || args->signum > LINUX_NSIG) 510 return EINVAL; 511 512 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ) 513 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)]; 514 else 515 tmp.signum = args->signum; 516 517 tmp.pid = args->pid; 518 return (kill(p, &tmp)); 519 } 520