1 /* $NetBSD: kern_sig_43.c,v 1.33 2009/11/23 00:46:06 rmind 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: kern_sig_43.c,v 1.33 2009/11/23 00:46:06 rmind Exp $"); 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_compat_netbsd.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/signalvar.h> 41 #include <sys/resourcevar.h> 42 #include <sys/namei.h> 43 #include <sys/vnode.h> 44 #include <sys/proc.h> 45 #include <sys/systm.h> 46 #include <sys/timeb.h> 47 #include <sys/times.h> 48 #include <sys/buf.h> 49 #include <sys/acct.h> 50 #include <sys/file.h> 51 #include <sys/kernel.h> 52 #include <sys/wait.h> 53 #include <sys/ktrace.h> 54 #include <sys/syslog.h> 55 #include <sys/stat.h> 56 #include <sys/core.h> 57 #include <sys/kauth.h> 58 59 #include <sys/mount.h> 60 #include <sys/syscallargs.h> 61 62 #include <sys/cpu.h> 63 64 #include <compat/sys/signal.h> 65 66 void compat_43_sigmask_to_sigset(const int *, sigset_t *); 67 void compat_43_sigset_to_sigmask(const sigset_t *, int *); 68 void compat_43_sigvec_to_sigaction(const struct sigvec *, struct sigaction *); 69 void compat_43_sigaction_to_sigvec(const struct sigaction *, struct sigvec *); 70 void compat_43_sigstack_to_sigaltstack(const struct sigstack *, struct sigaltstack *); 71 void compat_43_sigaltstack_to_sigstack(const struct sigaltstack *, struct sigstack *); 72 73 void 74 compat_43_sigmask_to_sigset(const int *sm, sigset_t *ss) 75 { 76 77 ss->__bits[0] = *sm; 78 ss->__bits[1] = 0; 79 ss->__bits[2] = 0; 80 ss->__bits[3] = 0; 81 } 82 83 void 84 compat_43_sigset_to_sigmask(const sigset_t *ss, int *sm) 85 { 86 87 *sm = ss->__bits[0]; 88 } 89 90 void 91 compat_43_sigvec_to_sigaction(const struct sigvec *sv, struct sigaction *sa) 92 { 93 sa->sa_handler = sv->sv_handler; 94 compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask); 95 sa->sa_flags = sv->sv_flags ^ SA_RESTART; 96 } 97 98 void 99 compat_43_sigaction_to_sigvec(const struct sigaction *sa, struct sigvec *sv) 100 { 101 sv->sv_handler = sa->sa_handler; 102 compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask); 103 sv->sv_flags = sa->sa_flags ^ SA_RESTART; 104 } 105 106 void 107 compat_43_sigstack_to_sigaltstack(const struct sigstack *ss, struct sigaltstack *sa) 108 { 109 sa->ss_sp = ss->ss_sp; 110 sa->ss_size = SIGSTKSZ; /* Use the recommended size */ 111 sa->ss_flags = 0; 112 if (ss->ss_onstack) 113 sa->ss_flags |= SS_ONSTACK; 114 } 115 116 void 117 compat_43_sigaltstack_to_sigstack(const struct sigaltstack *sa, struct sigstack *ss) 118 { 119 ss->ss_sp = sa->ss_sp; 120 if (sa->ss_flags & SS_ONSTACK) 121 ss->ss_onstack = 1; 122 else 123 ss->ss_onstack = 0; 124 } 125 126 int 127 compat_43_sys_sigblock(struct lwp *l, const struct compat_43_sys_sigblock_args *uap, register_t *retval) 128 { 129 /* { 130 syscallarg(int) mask; 131 } */ 132 struct proc *p = l->l_proc; 133 int nsm, osm; 134 sigset_t nss, oss; 135 int error; 136 137 nsm = SCARG(uap, mask); 138 compat_43_sigmask_to_sigset(&nsm, &nss); 139 mutex_enter(p->p_lock); 140 error = sigprocmask1(l, SIG_BLOCK, &nss, &oss); 141 mutex_exit(p->p_lock); 142 if (error) 143 return (error); 144 compat_43_sigset_to_sigmask(&oss, &osm); 145 *retval = osm; 146 return (0); 147 } 148 149 int 150 compat_43_sys_sigsetmask(struct lwp *l, const struct compat_43_sys_sigsetmask_args *uap, register_t *retval) 151 { 152 /* { 153 syscallarg(int) mask; 154 } */ 155 struct proc *p = l->l_proc; 156 int nsm, osm; 157 sigset_t nss, oss; 158 int error; 159 160 nsm = SCARG(uap, mask); 161 compat_43_sigmask_to_sigset(&nsm, &nss); 162 mutex_enter(p->p_lock); 163 error = sigprocmask1(l, SIG_SETMASK, &nss, &oss); 164 mutex_exit(p->p_lock); 165 if (error) 166 return (error); 167 compat_43_sigset_to_sigmask(&oss, &osm); 168 *retval = osm; 169 return (0); 170 } 171 172 /* ARGSUSED */ 173 int 174 compat_43_sys_sigstack(struct lwp *l, const struct compat_43_sys_sigstack_args *uap, register_t *retval) 175 { 176 /* { 177 syscallarg(struct sigstack *) nss; 178 syscallarg(struct sigstack *) oss; 179 } */ 180 struct sigstack nss, oss; 181 struct sigaltstack nsa, osa; 182 int error; 183 184 if (SCARG(uap, nss)) { 185 error = copyin(SCARG(uap, nss), &nss, sizeof(nss)); 186 if (error) 187 return (error); 188 compat_43_sigstack_to_sigaltstack(&nss, &nsa); 189 } 190 error = sigaltstack1(l, 191 SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0); 192 if (error) 193 return (error); 194 if (SCARG(uap, oss)) { 195 compat_43_sigaltstack_to_sigstack(&osa, &oss); 196 error = copyout(&oss, SCARG(uap, oss), sizeof(oss)); 197 if (error) 198 return (error); 199 } 200 return (0); 201 } 202 203 /* 204 * Generalized interface signal handler, 4.3-compatible. 205 */ 206 /* ARGSUSED */ 207 int 208 compat_43_sys_sigvec(struct lwp *l, const struct compat_43_sys_sigvec_args *uap, register_t *retval) 209 { 210 /* { 211 syscallarg(int) signum; 212 syscallarg(const struct sigvec *) nsv; 213 syscallarg(struct sigvec *) osv; 214 } */ 215 struct sigvec nsv, osv; 216 struct sigaction nsa, osa; 217 int error; 218 219 if (SCARG(uap, nsv)) { 220 error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv)); 221 if (error) 222 return (error); 223 compat_43_sigvec_to_sigaction(&nsv, &nsa); 224 } 225 error = sigaction1(l, SCARG(uap, signum), 226 SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0, 227 NULL, 0); 228 if (error) 229 return (error); 230 if (SCARG(uap, osv)) { 231 compat_43_sigaction_to_sigvec(&osa, &osv); 232 error = copyout(&osv, SCARG(uap, osv), sizeof(osv)); 233 if (error) 234 return (error); 235 } 236 return (0); 237 } 238 239 240 /* ARGSUSED */ 241 int 242 compat_43_sys_killpg(struct lwp *l, const struct compat_43_sys_killpg_args *uap, register_t *retval) 243 { 244 /* { 245 syscallarg(int) pgid; 246 syscallarg(int) signum; 247 } */ 248 ksiginfo_t ksi; 249 int pgid = SCARG(uap, pgid); 250 251 #ifdef COMPAT_09 252 pgid &= 0xffff; 253 #endif 254 255 if ((u_int)SCARG(uap, signum) >= NSIG) 256 return (EINVAL); 257 memset(&ksi, 0, sizeof(ksi)); 258 ksi.ksi_signo = SCARG(uap, signum); 259 ksi.ksi_code = SI_USER; 260 ksi.ksi_pid = l->l_proc->p_pid; 261 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred); 262 return killpg1(l, &ksi, pgid, 0); 263 } 264