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