1 /* $NetBSD: kern_sig_43.c,v 1.21 2005/12/11 12:19:56 christos 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.21 2005/12/11 12:19:56 christos 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 65 #include <sys/mount.h> 66 #include <sys/sa.h> 67 #include <sys/syscallargs.h> 68 69 #include <machine/cpu.h> 70 71 #include <sys/user.h> /* for coredump */ 72 73 #include <compat/sys/signal.h> 74 75 void compat_43_sigmask_to_sigset __P((const int *, sigset_t *)); 76 void compat_43_sigset_to_sigmask __P((const sigset_t *, int *)); 77 void compat_43_sigvec_to_sigaction __P((const struct sigvec *, struct sigaction *)); 78 void compat_43_sigaction_to_sigvec __P((const struct sigaction *, struct sigvec *)); 79 void compat_43_sigstack_to_sigaltstack __P((const struct sigstack *, struct sigaltstack *)); 80 void compat_43_sigaltstack_to_sigstack __P((const struct sigaltstack *, struct sigstack *)); 81 82 void 83 compat_43_sigmask_to_sigset(sm, ss) 84 const int *sm; 85 sigset_t *ss; 86 { 87 88 ss->__bits[0] = *sm; 89 ss->__bits[1] = 0; 90 ss->__bits[2] = 0; 91 ss->__bits[3] = 0; 92 } 93 94 void 95 compat_43_sigset_to_sigmask(ss, sm) 96 const sigset_t *ss; 97 int *sm; 98 { 99 100 *sm = ss->__bits[0]; 101 } 102 103 void 104 compat_43_sigvec_to_sigaction(sv, sa) 105 const struct sigvec *sv; 106 struct sigaction *sa; 107 { 108 sa->sa_handler = sv->sv_handler; 109 compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask); 110 sa->sa_flags = sv->sv_flags ^ SA_RESTART; 111 } 112 113 void 114 compat_43_sigaction_to_sigvec(sa, sv) 115 const struct sigaction *sa; 116 struct sigvec *sv; 117 { 118 sv->sv_handler = sa->sa_handler; 119 compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask); 120 sv->sv_flags = sa->sa_flags ^ SA_RESTART; 121 } 122 123 void 124 compat_43_sigstack_to_sigaltstack(ss, sa) 125 const struct sigstack *ss; 126 struct sigaltstack *sa; 127 { 128 sa->ss_sp = ss->ss_sp; 129 sa->ss_size = SIGSTKSZ; /* Use the recommended size */ 130 sa->ss_flags = 0; 131 if (ss->ss_onstack) 132 sa->ss_flags |= SS_ONSTACK; 133 } 134 135 void 136 compat_43_sigaltstack_to_sigstack(sa, ss) 137 const struct sigaltstack *sa; 138 struct sigstack *ss; 139 { 140 ss->ss_sp = sa->ss_sp; 141 if (sa->ss_flags & SS_ONSTACK) 142 ss->ss_onstack = 1; 143 else 144 ss->ss_onstack = 0; 145 } 146 147 int 148 compat_43_sys_sigblock(struct lwp *l, void *v, register_t *retval) 149 { 150 struct compat_43_sys_sigblock_args /* { 151 syscallarg(int) mask; 152 } */ *uap = v; 153 struct proc *p = l->l_proc; 154 int nsm, osm; 155 sigset_t nss, oss; 156 int error; 157 158 nsm = SCARG(uap, mask); 159 compat_43_sigmask_to_sigset(&nsm, &nss); 160 error = sigprocmask1(p, SIG_BLOCK, &nss, &oss); 161 if (error) 162 return (error); 163 compat_43_sigset_to_sigmask(&oss, &osm); 164 *retval = osm; 165 return (0); 166 } 167 168 int 169 compat_43_sys_sigsetmask(struct lwp *l, void *v, register_t *retval) 170 { 171 struct compat_43_sys_sigsetmask_args /* { 172 syscallarg(int) mask; 173 } */ *uap = v; 174 struct proc *p = l->l_proc; 175 int nsm, osm; 176 sigset_t nss, oss; 177 int error; 178 179 nsm = SCARG(uap, mask); 180 compat_43_sigmask_to_sigset(&nsm, &nss); 181 error = sigprocmask1(p, SIG_SETMASK, &nss, &oss); 182 if (error) 183 return (error); 184 compat_43_sigset_to_sigmask(&oss, &osm); 185 *retval = osm; 186 return (0); 187 } 188 189 /* ARGSUSED */ 190 int 191 compat_43_sys_sigstack(struct lwp *l, void *v, register_t *retval) 192 { 193 struct compat_43_sys_sigstack_args /* { 194 syscallarg(struct sigstack *) nss; 195 syscallarg(struct sigstack *) oss; 196 } */ *uap = v; 197 struct proc *p = l->l_proc; 198 struct sigstack nss, oss; 199 struct sigaltstack nsa, osa; 200 int error; 201 202 if (SCARG(uap, nss)) { 203 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 204 if (error) 205 return (error); 206 compat_43_sigstack_to_sigaltstack(&nss, &nsa); 207 } 208 error = sigaltstack1(p, 209 SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0); 210 if (error) 211 return (error); 212 if (SCARG(uap, oss)) { 213 compat_43_sigaltstack_to_sigstack(&osa, &oss); 214 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 215 if (error) 216 return (error); 217 } 218 return (0); 219 } 220 221 /* 222 * Generalized interface signal handler, 4.3-compatible. 223 */ 224 /* ARGSUSED */ 225 int 226 compat_43_sys_sigvec(struct lwp *l, void *v, register_t *retval) 227 { 228 struct compat_43_sys_sigvec_args /* { 229 syscallarg(int) signum; 230 syscallarg(const struct sigvec *) nsv; 231 syscallarg(struct sigvec *) osv; 232 } */ *uap = v; 233 struct proc *p = l->l_proc; 234 struct sigvec nsv, osv; 235 struct sigaction nsa, osa; 236 int error; 237 238 if (SCARG(uap, nsv)) { 239 error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv)); 240 if (error) 241 return (error); 242 compat_43_sigvec_to_sigaction(&nsv, &nsa); 243 } 244 error = sigaction1(p, SCARG(uap, signum), 245 SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0, 246 NULL, 0); 247 if (error) 248 return (error); 249 if (SCARG(uap, osv)) { 250 compat_43_sigaction_to_sigvec(&osa, &osv); 251 error = copyout(&osv, SCARG(uap, osv), sizeof(osv)); 252 if (error) 253 return (error); 254 } 255 return (0); 256 } 257 258 259 /* ARGSUSED */ 260 int 261 compat_43_sys_killpg(struct lwp *l, void *v, register_t *retval) 262 { 263 struct compat_43_sys_killpg_args /* { 264 syscallarg(int) pgid; 265 syscallarg(int) signum; 266 } */ *uap = v; 267 struct proc *p = l->l_proc; 268 ksiginfo_t ksi; 269 270 #ifdef COMPAT_09 271 SCARG(uap, pgid) = (short) SCARG(uap, pgid); 272 #endif 273 274 if ((u_int)SCARG(uap, signum) >= NSIG) 275 return (EINVAL); 276 memset(&ksi, 0, sizeof(ksi)); 277 ksi.ksi_signo = SCARG(uap, signum); 278 ksi.ksi_code = SI_USER; 279 ksi.ksi_pid = p->p_pid; 280 ksi.ksi_uid = p->p_ucred->cr_uid; 281 return (killpg1(p, &ksi, SCARG(uap, pgid), 0)); 282 } 283