1 /* $NetBSD: kern_sig_43.c,v 1.22 2006/05/14 21:24:49 elad Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: kern_sig_43.c,v 1.22 2006/05/14 21:24:49 elad Exp $"); 41 42 #if defined(_KERNEL_OPT) 43 #include "opt_compat_netbsd.h" 44 #endif 45 46 #include <sys/param.h> 47 #include <sys/signalvar.h> 48 #include <sys/resourcevar.h> 49 #include <sys/namei.h> 50 #include <sys/vnode.h> 51 #include <sys/proc.h> 52 #include <sys/systm.h> 53 #include <sys/timeb.h> 54 #include <sys/times.h> 55 #include <sys/buf.h> 56 #include <sys/acct.h> 57 #include <sys/file.h> 58 #include <sys/kernel.h> 59 #include <sys/wait.h> 60 #include <sys/ktrace.h> 61 #include <sys/syslog.h> 62 #include <sys/stat.h> 63 #include <sys/core.h> 64 #include <sys/kauth.h> 65 66 #include <sys/mount.h> 67 #include <sys/sa.h> 68 #include <sys/syscallargs.h> 69 70 #include <machine/cpu.h> 71 72 #include <sys/user.h> /* for coredump */ 73 74 #include <compat/sys/signal.h> 75 76 void compat_43_sigmask_to_sigset __P((const int *, sigset_t *)); 77 void compat_43_sigset_to_sigmask __P((const sigset_t *, int *)); 78 void compat_43_sigvec_to_sigaction __P((const struct sigvec *, struct sigaction *)); 79 void compat_43_sigaction_to_sigvec __P((const struct sigaction *, struct sigvec *)); 80 void compat_43_sigstack_to_sigaltstack __P((const struct sigstack *, struct sigaltstack *)); 81 void compat_43_sigaltstack_to_sigstack __P((const struct sigaltstack *, struct sigstack *)); 82 83 void 84 compat_43_sigmask_to_sigset(sm, ss) 85 const int *sm; 86 sigset_t *ss; 87 { 88 89 ss->__bits[0] = *sm; 90 ss->__bits[1] = 0; 91 ss->__bits[2] = 0; 92 ss->__bits[3] = 0; 93 } 94 95 void 96 compat_43_sigset_to_sigmask(ss, sm) 97 const sigset_t *ss; 98 int *sm; 99 { 100 101 *sm = ss->__bits[0]; 102 } 103 104 void 105 compat_43_sigvec_to_sigaction(sv, sa) 106 const struct sigvec *sv; 107 struct sigaction *sa; 108 { 109 sa->sa_handler = sv->sv_handler; 110 compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask); 111 sa->sa_flags = sv->sv_flags ^ SA_RESTART; 112 } 113 114 void 115 compat_43_sigaction_to_sigvec(sa, sv) 116 const struct sigaction *sa; 117 struct sigvec *sv; 118 { 119 sv->sv_handler = sa->sa_handler; 120 compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask); 121 sv->sv_flags = sa->sa_flags ^ SA_RESTART; 122 } 123 124 void 125 compat_43_sigstack_to_sigaltstack(ss, sa) 126 const struct sigstack *ss; 127 struct sigaltstack *sa; 128 { 129 sa->ss_sp = ss->ss_sp; 130 sa->ss_size = SIGSTKSZ; /* Use the recommended size */ 131 sa->ss_flags = 0; 132 if (ss->ss_onstack) 133 sa->ss_flags |= SS_ONSTACK; 134 } 135 136 void 137 compat_43_sigaltstack_to_sigstack(sa, ss) 138 const struct sigaltstack *sa; 139 struct sigstack *ss; 140 { 141 ss->ss_sp = sa->ss_sp; 142 if (sa->ss_flags & SS_ONSTACK) 143 ss->ss_onstack = 1; 144 else 145 ss->ss_onstack = 0; 146 } 147 148 int 149 compat_43_sys_sigblock(struct lwp *l, void *v, register_t *retval) 150 { 151 struct compat_43_sys_sigblock_args /* { 152 syscallarg(int) mask; 153 } */ *uap = v; 154 struct proc *p = l->l_proc; 155 int nsm, osm; 156 sigset_t nss, oss; 157 int error; 158 159 nsm = SCARG(uap, mask); 160 compat_43_sigmask_to_sigset(&nsm, &nss); 161 error = sigprocmask1(p, SIG_BLOCK, &nss, &oss); 162 if (error) 163 return (error); 164 compat_43_sigset_to_sigmask(&oss, &osm); 165 *retval = osm; 166 return (0); 167 } 168 169 int 170 compat_43_sys_sigsetmask(struct lwp *l, void *v, register_t *retval) 171 { 172 struct compat_43_sys_sigsetmask_args /* { 173 syscallarg(int) mask; 174 } */ *uap = v; 175 struct proc *p = l->l_proc; 176 int nsm, osm; 177 sigset_t nss, oss; 178 int error; 179 180 nsm = SCARG(uap, mask); 181 compat_43_sigmask_to_sigset(&nsm, &nss); 182 error = sigprocmask1(p, SIG_SETMASK, &nss, &oss); 183 if (error) 184 return (error); 185 compat_43_sigset_to_sigmask(&oss, &osm); 186 *retval = osm; 187 return (0); 188 } 189 190 /* ARGSUSED */ 191 int 192 compat_43_sys_sigstack(struct lwp *l, void *v, register_t *retval) 193 { 194 struct compat_43_sys_sigstack_args /* { 195 syscallarg(struct sigstack *) nss; 196 syscallarg(struct sigstack *) oss; 197 } */ *uap = v; 198 struct proc *p = l->l_proc; 199 struct sigstack nss, oss; 200 struct sigaltstack nsa, osa; 201 int error; 202 203 if (SCARG(uap, nss)) { 204 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 205 if (error) 206 return (error); 207 compat_43_sigstack_to_sigaltstack(&nss, &nsa); 208 } 209 error = sigaltstack1(p, 210 SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0); 211 if (error) 212 return (error); 213 if (SCARG(uap, oss)) { 214 compat_43_sigaltstack_to_sigstack(&osa, &oss); 215 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 216 if (error) 217 return (error); 218 } 219 return (0); 220 } 221 222 /* 223 * Generalized interface signal handler, 4.3-compatible. 224 */ 225 /* ARGSUSED */ 226 int 227 compat_43_sys_sigvec(struct lwp *l, void *v, register_t *retval) 228 { 229 struct compat_43_sys_sigvec_args /* { 230 syscallarg(int) signum; 231 syscallarg(const struct sigvec *) nsv; 232 syscallarg(struct sigvec *) osv; 233 } */ *uap = v; 234 struct proc *p = l->l_proc; 235 struct sigvec nsv, osv; 236 struct sigaction nsa, osa; 237 int error; 238 239 if (SCARG(uap, nsv)) { 240 error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv)); 241 if (error) 242 return (error); 243 compat_43_sigvec_to_sigaction(&nsv, &nsa); 244 } 245 error = sigaction1(p, SCARG(uap, signum), 246 SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0, 247 NULL, 0); 248 if (error) 249 return (error); 250 if (SCARG(uap, osv)) { 251 compat_43_sigaction_to_sigvec(&osa, &osv); 252 error = copyout(&osv, SCARG(uap, osv), sizeof(osv)); 253 if (error) 254 return (error); 255 } 256 return (0); 257 } 258 259 260 /* ARGSUSED */ 261 int 262 compat_43_sys_killpg(struct lwp *l, void *v, register_t *retval) 263 { 264 struct compat_43_sys_killpg_args /* { 265 syscallarg(int) pgid; 266 syscallarg(int) signum; 267 } */ *uap = v; 268 struct proc *p = l->l_proc; 269 ksiginfo_t ksi; 270 271 #ifdef COMPAT_09 272 SCARG(uap, pgid) = (short) SCARG(uap, pgid); 273 #endif 274 275 if ((u_int)SCARG(uap, signum) >= NSIG) 276 return (EINVAL); 277 memset(&ksi, 0, sizeof(ksi)); 278 ksi.ksi_signo = SCARG(uap, signum); 279 ksi.ksi_code = SI_USER; 280 ksi.ksi_pid = p->p_pid; 281 ksi.ksi_uid = kauth_cred_geteuid(p->p_cred); 282 return (killpg1(p, &ksi, SCARG(uap, pgid), 0)); 283 } 284