Home | History | Annotate | Line # | Download | only in common
kern_sig_43.c revision 1.23.4.2
      1 /*	$NetBSD: kern_sig_43.c,v 1.23.4.2 2007/01/11 22:22:59 ad 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.23.4.2 2007/01/11 22:22:59 ad 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 	mutex_enter(&p->p_smutex);
    162 	error = sigprocmask1(l, SIG_BLOCK, &nss, &oss);
    163 	mutex_exit(&p->p_smutex);
    164 	if (error)
    165 		return (error);
    166 	compat_43_sigset_to_sigmask(&oss, &osm);
    167 	*retval = osm;
    168 	return (0);
    169 }
    170 
    171 int
    172 compat_43_sys_sigsetmask(struct lwp *l, void *v, register_t *retval)
    173 {
    174 	struct compat_43_sys_sigsetmask_args /* {
    175 		syscallarg(int) mask;
    176 	} */ *uap = v;
    177 	struct proc *p = l->l_proc;
    178 	int nsm, osm;
    179 	sigset_t nss, oss;
    180 	int error;
    181 
    182 	nsm = SCARG(uap, mask);
    183 	compat_43_sigmask_to_sigset(&nsm, &nss);
    184 	mutex_enter(&p->p_smutex);
    185 	error = sigprocmask1(l, SIG_SETMASK, &nss, &oss);
    186 	mutex_exit(&p->p_smutex);
    187 	if (error)
    188 		return (error);
    189 	compat_43_sigset_to_sigmask(&oss, &osm);
    190 	*retval = osm;
    191 	return (0);
    192 }
    193 
    194 /* ARGSUSED */
    195 int
    196 compat_43_sys_sigstack(struct lwp *l, void *v, register_t *retval)
    197 {
    198 	struct compat_43_sys_sigstack_args /* {
    199 		syscallarg(struct sigstack *) nss;
    200 		syscallarg(struct sigstack *) oss;
    201 	} */ *uap = v;
    202 	struct sigstack nss, oss;
    203 	struct sigaltstack nsa, osa;
    204 	int error;
    205 
    206 	if (SCARG(uap, nss)) {
    207 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    208 		if (error)
    209 			return (error);
    210 		compat_43_sigstack_to_sigaltstack(&nss, &nsa);
    211 	}
    212 	error = sigaltstack1(l,
    213 	    SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0);
    214 	if (error)
    215 		return (error);
    216 	if (SCARG(uap, oss)) {
    217 		compat_43_sigaltstack_to_sigstack(&osa, &oss);
    218 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    219 		if (error)
    220 			return (error);
    221 	}
    222 	return (0);
    223 }
    224 
    225 /*
    226  * Generalized interface signal handler, 4.3-compatible.
    227  */
    228 /* ARGSUSED */
    229 int
    230 compat_43_sys_sigvec(struct lwp *l, void *v, register_t *retval)
    231 {
    232 	struct compat_43_sys_sigvec_args /* {
    233 		syscallarg(int) signum;
    234 		syscallarg(const struct sigvec *) nsv;
    235 		syscallarg(struct sigvec *) osv;
    236 	} */ *uap = v;
    237 	struct sigvec nsv, osv;
    238 	struct sigaction nsa, osa;
    239 	int error;
    240 
    241 	if (SCARG(uap, nsv)) {
    242 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
    243 		if (error)
    244 			return (error);
    245 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
    246 	}
    247 	error = sigaction1(l, SCARG(uap, signum),
    248 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
    249 	    NULL, 0);
    250 	if (error)
    251 		return (error);
    252 	if (SCARG(uap, osv)) {
    253 		compat_43_sigaction_to_sigvec(&osa, &osv);
    254 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
    255 		if (error)
    256 			return (error);
    257 	}
    258 	return (0);
    259 }
    260 
    261 
    262 /* ARGSUSED */
    263 int
    264 compat_43_sys_killpg(struct lwp *l, void *v, register_t *retval)
    265 {
    266 	struct compat_43_sys_killpg_args /* {
    267 		syscallarg(int) pgid;
    268 		syscallarg(int) signum;
    269 	} */ *uap = v;
    270 	ksiginfo_t ksi;
    271 
    272 #ifdef COMPAT_09
    273 	SCARG(uap, pgid) = (short) SCARG(uap, pgid);
    274 #endif
    275 
    276 	if ((u_int)SCARG(uap, signum) >= NSIG)
    277 		return (EINVAL);
    278 	memset(&ksi, 0, sizeof(ksi));
    279 	ksi.ksi_signo = SCARG(uap, signum);
    280 	ksi.ksi_code = SI_USER;
    281 	ksi.ksi_pid = l->l_proc->p_pid;
    282 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    283 	return (killpg1(l, &ksi, SCARG(uap, pgid), 0));
    284 }
    285