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 sigset_t omask, nmask; 231 sigset_t *nmaskp; 232 int error; 233 234 td->td_retval[0] = 0; 235 236 switch (how) { 237 case LINUX_SIG_BLOCK: 238 how = SIG_BLOCK; 239 break; 240 case LINUX_SIG_UNBLOCK: 241 how = SIG_UNBLOCK; 242 break; 243 case LINUX_SIG_SETMASK: 244 how = SIG_SETMASK; 245 break; 246 default: 247 return (EINVAL); 248 } 249 if (new != NULL) { 250 linux_to_bsd_sigset(new, &nmask); 251 nmaskp = &nmask; 252 } else 253 nmaskp = NULL; 254 error = kern_sigprocmask(td, how, nmaskp, &omask, 0); 255 if (error == 0 && old != NULL) 256 bsd_to_linux_sigset(&omask, old); 257 258 return (error); 259 } 260 261 #ifndef __alpha__ 262 int 263 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args) 264 { 265 l_osigset_t mask; 266 l_sigset_t set, oset; 267 int error; 268 269 #ifdef DEBUG 270 if (ldebug(sigprocmask)) 271 printf(ARGS(sigprocmask, "%d, *, *"), args->how); 272 #endif 273 274 if (args->mask != NULL) { 275 error = copyin(args->mask, &mask, sizeof(l_osigset_t)); 276 if (error) 277 return (error); 278 LINUX_SIGEMPTYSET(set); 279 set.__bits[0] = mask; 280 } 281 282 error = linux_do_sigprocmask(td, args->how, 283 args->mask ? &set : NULL, 284 args->omask ? &oset : NULL); 285 286 if (args->omask != NULL && !error) { 287 mask = oset.__bits[0]; 288 error = copyout(&mask, args->omask, sizeof(l_osigset_t)); 289 } 290 291 return (error); 292 } 293 #endif /*!__alpha__*/ 294 295 int 296 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args) 297 { 298 l_sigset_t set, oset; 299 int error; 300 301 #ifdef DEBUG 302 if (ldebug(rt_sigprocmask)) 303 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"), 304 args->how, (void *)args->mask, 305 (void *)args->omask, (long)args->sigsetsize); 306 #endif 307 308 if (args->sigsetsize != sizeof(l_sigset_t)) 309 return EINVAL; 310 311 if (args->mask != NULL) { 312 error = copyin(args->mask, &set, sizeof(l_sigset_t)); 313 if (error) 314 return (error); 315 } 316 317 error = linux_do_sigprocmask(td, args->how, 318 args->mask ? &set : NULL, 319 args->omask ? &oset : NULL); 320 321 if (args->omask != NULL && !error) { 322 error = copyout(&oset, args->omask, sizeof(l_sigset_t)); 323 } 324 325 return (error); 326 } 327 328 #ifndef __alpha__ 329 int 330 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args) 331 { 332 struct proc *p = td->td_proc; 333 l_sigset_t mask; 334 335 #ifdef DEBUG 336 if (ldebug(sgetmask)) 337 printf(ARGS(sgetmask, "")); 338 #endif 339 340 PROC_LOCK(p); 341 bsd_to_linux_sigset(&td->td_sigmask, &mask); 342 PROC_UNLOCK(p); 343 td->td_retval[0] = mask.__bits[0]; 344 return (0); 345 } 346 347 int 348 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args) 349 { 350 struct proc *p = td->td_proc; 351 l_sigset_t lset; 352 sigset_t bset; 353 354 #ifdef DEBUG 355 if (ldebug(ssetmask)) 356 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask); 357 #endif 358 359 PROC_LOCK(p); 360 bsd_to_linux_sigset(&td->td_sigmask, &lset); 361 td->td_retval[0] = lset.__bits[0]; 362 LINUX_SIGEMPTYSET(lset); 363 lset.__bits[0] = args->mask; 364 linux_to_bsd_sigset(&lset, &bset); 365 td->td_sigmask = bset; 366 SIG_CANTMASK(td->td_sigmask); 367 signotify(td); 368 PROC_UNLOCK(p); 369 return (0); 370 } 371 372 /* 373 * MPSAFE 374 */ 375 int 376 linux_sigpending(struct thread *td, struct linux_sigpending_args *args) 377 { 378 struct proc *p = td->td_proc; 379 sigset_t bset; 380 l_sigset_t lset; 381 l_osigset_t mask; 382 383 #ifdef DEBUG 384 if (ldebug(sigpending)) 385 printf(ARGS(sigpending, "*")); 386 #endif 387 388 PROC_LOCK(p); 389 bset = p->p_siglist; 390 SIGSETOR(bset, td->td_siglist); 391 SIGSETAND(bset, td->td_sigmask); 392 PROC_UNLOCK(p); 393 bsd_to_linux_sigset(&bset, &lset); 394 mask = lset.__bits[0]; 395 return (copyout(&mask, args->mask, sizeof(mask))); 396 } 397 #endif /*!__alpha__*/ 398 399 int 400 linux_kill(struct thread *td, struct linux_kill_args *args) 401 { 402 struct kill_args /* { 403 int pid; 404 int signum; 405 } */ tmp; 406 407 #ifdef DEBUG 408 if (ldebug(kill)) 409 printf(ARGS(kill, "%d, %d"), args->pid, args->signum); 410 #endif 411 412 /* 413 * Allow signal 0 as a means to check for privileges 414 */ 415 if (args->signum < 0 || args->signum > LINUX_NSIG) 416 return EINVAL; 417 418 #ifndef __alpha__ 419 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ) 420 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)]; 421 else 422 #endif 423 tmp.signum = args->signum; 424 425 tmp.pid = args->pid; 426 return (kill(td, &tmp)); 427 } 428