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