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