linux_signal.c revision 38344
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 * $Id: linux_signal.c,v 1.11 1998/07/29 16:43:00 bde Exp $ 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 41static sigset_t 42linux_to_bsd_sigset(linux_sigset_t mask) { 43 int b, l; 44 sigset_t new = 0; 45 46 for (l = 1; l <= LINUX_NSIG; l++) { 47 if (mask & (1 << (l - 1))) { 48 if ((b = linux_to_bsd_signal[l])) 49 new |= (1 << (b - 1)); 50 } 51 } 52 return new; 53} 54 55static linux_sigset_t 56bsd_to_linux_sigset(sigset_t mask) { 57 int b, l; 58 sigset_t new = 0; 59 60 for (b = 1; b <= NSIG; b++) { 61 if (mask & (1 << (b - 1))) { 62 if ((l = bsd_to_linux_signal[b])) 63 new |= (1 << (l - 1)); 64 } 65 } 66 return new; 67} 68 69static void 70linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa) 71{ 72 bsa->sa_mask = linux_to_bsd_sigset(lsa->sa_mask); 73 bsa->sa_handler = lsa->sa_handler; 74 bsa->sa_flags = 0; 75 if (lsa->sa_flags & LINUX_SA_NOCLDSTOP) 76 bsa->sa_flags |= SA_NOCLDSTOP; 77 if (lsa->sa_flags & LINUX_SA_ONSTACK) 78 bsa->sa_flags |= SA_ONSTACK; 79 if (lsa->sa_flags & LINUX_SA_RESTART) 80 bsa->sa_flags |= SA_RESTART; 81 if (lsa->sa_flags & LINUX_SA_ONESHOT) 82 bsa->sa_flags |= SA_RESETHAND; 83 if (lsa->sa_flags & LINUX_SA_NOMASK) 84 bsa->sa_flags |= SA_NODEFER; 85} 86 87static void 88bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa) 89{ 90 lsa->sa_handler = bsa->sa_handler; 91 lsa->sa_restorer = NULL; /* unsupported */ 92 lsa->sa_mask = bsd_to_linux_sigset(bsa->sa_mask); 93 lsa->sa_flags = 0; 94 if (bsa->sa_flags & SA_NOCLDSTOP) 95 lsa->sa_flags |= LINUX_SA_NOCLDSTOP; 96 if (bsa->sa_flags & SA_ONSTACK) 97 lsa->sa_flags |= LINUX_SA_ONSTACK; 98 if (bsa->sa_flags & SA_RESTART) 99 lsa->sa_flags |= LINUX_SA_RESTART; 100 if (bsa->sa_flags & SA_RESETHAND) 101 lsa->sa_flags |= LINUX_SA_ONESHOT; 102 if (bsa->sa_flags & SA_NODEFER) 103 lsa->sa_flags |= LINUX_SA_NOMASK; 104} 105 106int 107linux_sigaction(struct proc *p, struct linux_sigaction_args *args) 108{ 109 linux_sigaction_t linux_sa; 110 struct sigaction *nsa = NULL, *osa = NULL, bsd_sa; 111 struct sigaction_args sa; 112 int error; 113 caddr_t sg = stackgap_init(); 114 115#ifdef DEBUG 116 printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n", 117 (long)p->p_pid, args->sig, (void *)args->nsa, (void *)args->osa); 118#endif 119 120 if (args->osa) 121 osa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction)); 122 123 if (args->nsa) { 124 nsa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction)); 125 if (error = copyin(args->nsa, &linux_sa, sizeof(linux_sigaction_t))) 126 return error; 127 linux_to_bsd_sigaction(&linux_sa, &bsd_sa); 128 if (error = copyout(&bsd_sa, nsa, sizeof(struct sigaction))) 129 return error; 130 } 131 sa.signum = linux_to_bsd_signal[args->sig]; 132 sa.nsa = nsa; 133 sa.osa = osa; 134 if ((error = sigaction(p, &sa))) 135 return error; 136 137 if (args->osa) { 138 if (error = copyin(osa, &bsd_sa, sizeof(struct sigaction))) 139 return error; 140 bsd_to_linux_sigaction(&bsd_sa, &linux_sa); 141 if (error = copyout(&linux_sa, args->osa, sizeof(linux_sigaction_t))) 142 return error; 143 } 144 return 0; 145} 146 147int 148linux_signal(struct proc *p, struct linux_signal_args *args) 149{ 150 caddr_t sg; 151 struct sigaction_args sa_args; 152 struct sigaction *osa, *nsa, tmpsa; 153 int error; 154 155#ifdef DEBUG 156 printf("Linux-emul(%ld): signal(%d, %p)\n", 157 (long)p->p_pid, args->sig, (void *)args->handler); 158#endif 159 sg = stackgap_init(); 160 nsa = stackgap_alloc(&sg, sizeof *nsa); 161 osa = stackgap_alloc(&sg, sizeof *osa); 162 163 tmpsa.sa_handler = args->handler; 164 tmpsa.sa_mask = (sigset_t) 0; 165 tmpsa.sa_flags = SA_RESETHAND | SA_NODEFER; 166 if ((error = copyout(&tmpsa, nsa, sizeof tmpsa))) 167 return error; 168 169 sa_args.signum = linux_to_bsd_signal[args->sig]; 170 sa_args.osa = osa; 171 sa_args.nsa = nsa; 172 if ((error = sigaction(p, &sa_args))) 173 return error; 174 175 if ((error = copyin(osa, &tmpsa, sizeof *osa))) 176 return error; 177 178 p->p_retval[0] = (int)tmpsa.sa_handler; 179 180 return 0; 181} 182 183 184int 185linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args) 186{ 187 int error, s; 188 sigset_t mask; 189 sigset_t omask; 190 191#ifdef DEBUG 192 printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how); 193#endif 194 195 p->p_retval[0] = 0; 196 197 if (args->omask != NULL) { 198 omask = bsd_to_linux_sigset(p->p_sigmask); 199 if (error = copyout(&omask, args->omask, sizeof(sigset_t))) 200 return error; 201 } 202 if (!(args->mask)) 203 return 0; 204 if (error = copyin(args->mask, &mask, sizeof(linux_sigset_t))) 205 return error; 206 207 mask = linux_to_bsd_sigset(mask); 208 s = splhigh(); 209 switch (args->how) { 210 case LINUX_SIG_BLOCK: 211 p->p_sigmask |= (mask & ~sigcantmask); 212 break; 213 case LINUX_SIG_UNBLOCK: 214 p->p_sigmask &= ~mask; 215 break; 216 case LINUX_SIG_SETMASK: 217 p->p_sigmask = (mask & ~sigcantmask); 218 break; 219 default: 220 error = EINVAL; 221 break; 222 } 223 splx(s); 224 return error; 225} 226 227int 228linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args) 229{ 230#ifdef DEBUG 231 printf("Linux-emul(%d): siggetmask()\n", p->p_pid); 232#endif 233 p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask); 234 return 0; 235} 236 237int 238linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args) 239{ 240 int s; 241 sigset_t mask; 242 243#ifdef DEBUG 244 printf("Linux-emul(%ld): sigsetmask(%08lx)\n", 245 (long)p->p_pid, (unsigned long)args->mask); 246#endif 247 p->p_retval[0] = bsd_to_linux_sigset(p->p_sigmask); 248 249 mask = linux_to_bsd_sigset(args->mask); 250 s = splhigh(); 251 p->p_sigmask = mask & ~sigcantmask; 252 splx(s); 253 return 0; 254} 255 256int 257linux_sigpending(struct proc *p, struct linux_sigpending_args *args) 258{ 259 linux_sigset_t linux_sig; 260 261#ifdef DEBUG 262 printf("Linux-emul(%d): sigpending(*)\n", p->p_pid); 263#endif 264 linux_sig = bsd_to_linux_sigset(p->p_siglist & p->p_sigmask); 265 return copyout(&linux_sig, args->mask, sizeof(linux_sig)); 266} 267 268/* 269 * Linux has two extra args, restart and oldmask. We dont use these, 270 * but it seems that "restart" is actually a context pointer that 271 * enables the signal to happen with a different register set. 272 */ 273int 274linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args) 275{ 276 struct sigsuspend_args tmp; 277 278#ifdef DEBUG 279 printf("Linux-emul(%ld): sigsuspend(%08lx)\n", 280 (long)p->p_pid, (unsigned long)args->mask); 281#endif 282 tmp.mask = linux_to_bsd_sigset(args->mask); 283 return sigsuspend(p, &tmp); 284} 285 286int 287linux_pause(struct proc *p, struct linux_pause_args *args) 288{ 289 struct sigsuspend_args tmp; 290 291#ifdef DEBUG 292 printf("Linux-emul(%d): pause()\n", p->p_pid); 293#endif 294 tmp.mask = p->p_sigmask; 295 return sigsuspend(p, &tmp); 296} 297 298int 299linux_kill(struct proc *p, struct linux_kill_args *args) 300{ 301 struct kill_args /* { 302 int pid; 303 int signum; 304 } */ tmp; 305 306#ifdef DEBUG 307 printf("Linux-emul(%d): kill(%d, %d)\n", 308 p->p_pid, args->pid, args->signum); 309#endif 310 tmp.pid = args->pid; 311 tmp.signum = linux_to_bsd_signal[args->signum]; 312 return kill(p, &tmp); 313} 314