Home | History | Annotate | Line # | Download | only in common
kern_sig_43.c revision 1.37
      1  1.37  riastrad /*	$NetBSD: kern_sig_43.c,v 1.37 2021/09/07 11:43:02 riastradh 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.37  riastrad __KERNEL_RCSID(0, "$NetBSD: kern_sig_43.c,v 1.37 2021/09/07 11:43:02 riastradh 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.35  pgoyette #include <sys/syscall.h>
     60  1.35  pgoyette #include <sys/syscallvar.h>
     61   1.1  christos #include <sys/syscallargs.h>
     62  1.35  pgoyette #include <sys/compat_stub.h>
     63   1.1  christos 
     64  1.27        ad #include <sys/cpu.h>
     65   1.1  christos 
     66  1.20  christos #include <compat/sys/signal.h>
     67  1.35  pgoyette #include <compat/common/compat_mod.h>
     68  1.20  christos 
     69  1.28       dsl void compat_43_sigmask_to_sigset(const int *, sigset_t *);
     70  1.28       dsl void compat_43_sigset_to_sigmask(const sigset_t *, int *);
     71  1.28       dsl void compat_43_sigvec_to_sigaction(const struct sigvec *, struct sigaction *);
     72  1.28       dsl void compat_43_sigaction_to_sigvec(const struct sigaction *, struct sigvec *);
     73  1.28       dsl void compat_43_sigstack_to_sigaltstack(const struct sigstack *, struct sigaltstack *);
     74  1.28       dsl void compat_43_sigaltstack_to_sigstack(const struct sigaltstack *, struct sigstack *);
     75   1.9   mycroft 
     76  1.35  pgoyette static struct syscall_package kern_sig_43_syscalls[] = {
     77  1.35  pgoyette 	{ SYS_compat_43_osigblock, 0, (sy_call_t *)compat_43_sys_sigblock },
     78  1.35  pgoyette 	{ SYS_compat_43_osigsetmask, 0, (sy_call_t *)compat_43_sys_sigsetmask },
     79  1.35  pgoyette 	{ SYS_compat_43_osigstack, 0, (sy_call_t *)compat_43_sys_sigstack },
     80  1.35  pgoyette 	{ SYS_compat_43_osigvec, 0, (sy_call_t *)compat_43_sys_sigvec },
     81  1.35  pgoyette 	{ SYS_compat_43_okillpg, 0, (sy_call_t *)compat_43_sys_killpg },
     82  1.35  pgoyette 	{ 0, 0, NULL }
     83  1.35  pgoyette };
     84  1.35  pgoyette 
     85   1.9   mycroft void
     86  1.29       dsl compat_43_sigmask_to_sigset(const int *sm, sigset_t *ss)
     87   1.9   mycroft {
     88   1.9   mycroft 
     89  1.37  riastrad 	memset(ss, 0, sizeof(*ss));
     90   1.9   mycroft 	ss->__bits[0] = *sm;
     91   1.9   mycroft 	ss->__bits[1] = 0;
     92   1.9   mycroft 	ss->__bits[2] = 0;
     93   1.9   mycroft 	ss->__bits[3] = 0;
     94   1.9   mycroft }
     95   1.9   mycroft 
     96   1.9   mycroft void
     97  1.29       dsl compat_43_sigset_to_sigmask(const sigset_t *ss, int *sm)
     98   1.9   mycroft {
     99   1.9   mycroft 
    100   1.9   mycroft 	*sm = ss->__bits[0];
    101   1.9   mycroft }
    102   1.9   mycroft 
    103   1.9   mycroft void
    104  1.29       dsl compat_43_sigvec_to_sigaction(const struct sigvec *sv, struct sigaction *sa)
    105   1.9   mycroft {
    106  1.37  riastrad 
    107  1.37  riastrad 	memset(sa, 0, sizeof(*sa));
    108   1.9   mycroft 	sa->sa_handler = sv->sv_handler;
    109   1.9   mycroft 	compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
    110   1.9   mycroft 	sa->sa_flags = sv->sv_flags ^ SA_RESTART;
    111   1.9   mycroft }
    112   1.9   mycroft 
    113   1.9   mycroft void
    114  1.29       dsl compat_43_sigaction_to_sigvec(const struct sigaction *sa, struct sigvec *sv)
    115   1.9   mycroft {
    116  1.37  riastrad 
    117  1.37  riastrad 	memset(sv, 0, sizeof(*sv));
    118   1.9   mycroft 	sv->sv_handler = sa->sa_handler;
    119   1.9   mycroft 	compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
    120   1.9   mycroft 	sv->sv_flags = sa->sa_flags ^ SA_RESTART;
    121   1.9   mycroft }
    122   1.9   mycroft 
    123   1.9   mycroft void
    124  1.29       dsl compat_43_sigstack_to_sigaltstack(const struct sigstack *ss, struct sigaltstack *sa)
    125   1.9   mycroft {
    126  1.37  riastrad 	memset(sa, 0, sizeof(*sa));
    127   1.9   mycroft 	sa->ss_sp = ss->ss_sp;
    128  1.11  christos 	sa->ss_size = SIGSTKSZ;	/* Use the recommended size */
    129   1.9   mycroft 	sa->ss_flags = 0;
    130   1.9   mycroft 	if (ss->ss_onstack)
    131   1.9   mycroft 		sa->ss_flags |= SS_ONSTACK;
    132   1.9   mycroft }
    133   1.9   mycroft 
    134   1.9   mycroft void
    135  1.29       dsl compat_43_sigaltstack_to_sigstack(const struct sigaltstack *sa, struct sigstack *ss)
    136   1.9   mycroft {
    137  1.36      maxv 	memset(ss, 0, sizeof(*ss));
    138   1.9   mycroft 	ss->ss_sp = sa->ss_sp;
    139   1.9   mycroft 	if (sa->ss_flags & SS_ONSTACK)
    140   1.9   mycroft 		ss->ss_onstack = 1;
    141   1.9   mycroft 	else
    142   1.9   mycroft 		ss->ss_onstack = 0;
    143   1.9   mycroft }
    144   1.9   mycroft 
    145   1.1  christos int
    146  1.30       dsl compat_43_sys_sigblock(struct lwp *l, const struct compat_43_sys_sigblock_args *uap, register_t *retval)
    147   1.4   thorpej {
    148  1.30       dsl 	/* {
    149   1.1  christos 		syscallarg(int) mask;
    150  1.30       dsl 	} */
    151  1.18   thorpej 	struct proc *p = l->l_proc;
    152   1.9   mycroft 	int nsm, osm;
    153   1.9   mycroft 	sigset_t nss, oss;
    154   1.9   mycroft 	int error;
    155   1.9   mycroft 
    156   1.9   mycroft 	nsm = SCARG(uap, mask);
    157   1.9   mycroft 	compat_43_sigmask_to_sigset(&nsm, &nss);
    158  1.31        ad 	mutex_enter(p->p_lock);
    159  1.26        ad 	error = sigprocmask1(l, SIG_BLOCK, &nss, &oss);
    160  1.31        ad 	mutex_exit(p->p_lock);
    161   1.9   mycroft 	if (error)
    162   1.9   mycroft 		return (error);
    163   1.9   mycroft 	compat_43_sigset_to_sigmask(&oss, &osm);
    164   1.9   mycroft 	*retval = osm;
    165   1.1  christos 	return (0);
    166   1.1  christos }
    167   1.1  christos 
    168   1.1  christos int
    169  1.30       dsl compat_43_sys_sigsetmask(struct lwp *l, const struct compat_43_sys_sigsetmask_args *uap, register_t *retval)
    170   1.4   thorpej {
    171  1.30       dsl 	/* {
    172   1.1  christos 		syscallarg(int) mask;
    173  1.30       dsl 	} */
    174  1.18   thorpej 	struct proc *p = l->l_proc;
    175   1.9   mycroft 	int nsm, osm;
    176   1.9   mycroft 	sigset_t nss, oss;
    177   1.9   mycroft 	int error;
    178   1.9   mycroft 
    179   1.9   mycroft 	nsm = SCARG(uap, mask);
    180   1.9   mycroft 	compat_43_sigmask_to_sigset(&nsm, &nss);
    181  1.31        ad 	mutex_enter(p->p_lock);
    182  1.26        ad 	error = sigprocmask1(l, SIG_SETMASK, &nss, &oss);
    183  1.31        ad 	mutex_exit(p->p_lock);
    184   1.9   mycroft 	if (error)
    185   1.9   mycroft 		return (error);
    186   1.9   mycroft 	compat_43_sigset_to_sigmask(&oss, &osm);
    187   1.9   mycroft 	*retval = osm;
    188   1.1  christos 	return (0);
    189   1.1  christos }
    190   1.1  christos 
    191   1.1  christos /* ARGSUSED */
    192   1.1  christos int
    193  1.30       dsl compat_43_sys_sigstack(struct lwp *l, const struct compat_43_sys_sigstack_args *uap, register_t *retval)
    194   1.4   thorpej {
    195  1.30       dsl 	/* {
    196   1.1  christos 		syscallarg(struct sigstack *) nss;
    197   1.1  christos 		syscallarg(struct sigstack *) oss;
    198  1.30       dsl 	} */
    199   1.9   mycroft 	struct sigstack nss, oss;
    200   1.9   mycroft 	struct sigaltstack nsa, osa;
    201   1.9   mycroft 	int error;
    202   1.9   mycroft 
    203   1.9   mycroft 	if (SCARG(uap, nss)) {
    204   1.9   mycroft 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
    205   1.9   mycroft 		if (error)
    206   1.9   mycroft 			return (error);
    207   1.9   mycroft 		compat_43_sigstack_to_sigaltstack(&nss, &nsa);
    208   1.9   mycroft 	}
    209  1.26        ad 	error = sigaltstack1(l,
    210   1.9   mycroft 	    SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0);
    211   1.7  christos 	if (error)
    212   1.3   mycroft 		return (error);
    213   1.9   mycroft 	if (SCARG(uap, oss)) {
    214   1.9   mycroft 		compat_43_sigaltstack_to_sigstack(&osa, &oss);
    215  1.10        pk 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
    216   1.9   mycroft 		if (error)
    217   1.9   mycroft 			return (error);
    218   1.9   mycroft 	}
    219   1.3   mycroft 	return (0);
    220   1.1  christos }
    221   1.1  christos 
    222   1.1  christos /*
    223   1.1  christos  * Generalized interface signal handler, 4.3-compatible.
    224   1.1  christos  */
    225   1.1  christos /* ARGSUSED */
    226   1.1  christos int
    227  1.30       dsl compat_43_sys_sigvec(struct lwp *l, const struct compat_43_sys_sigvec_args *uap, register_t *retval)
    228   1.4   thorpej {
    229  1.30       dsl 	/* {
    230   1.1  christos 		syscallarg(int) signum;
    231   1.9   mycroft 		syscallarg(const struct sigvec *) nsv;
    232   1.1  christos 		syscallarg(struct sigvec *) osv;
    233  1.30       dsl 	} */
    234   1.9   mycroft 	struct sigvec nsv, osv;
    235   1.9   mycroft 	struct sigaction nsa, osa;
    236   1.9   mycroft 	int error;
    237   1.9   mycroft 
    238   1.9   mycroft 	if (SCARG(uap, nsv)) {
    239   1.9   mycroft 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
    240   1.7  christos 		if (error)
    241   1.1  christos 			return (error);
    242   1.9   mycroft 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
    243   1.1  christos 	}
    244  1.26        ad 	error = sigaction1(l, SCARG(uap, signum),
    245  1.17   thorpej 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
    246  1.17   thorpej 	    NULL, 0);
    247   1.9   mycroft 	if (error)
    248   1.9   mycroft 		return (error);
    249   1.9   mycroft 	if (SCARG(uap, osv)) {
    250   1.9   mycroft 		compat_43_sigaction_to_sigvec(&osa, &osv);
    251  1.10        pk 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
    252   1.7  christos 		if (error)
    253   1.1  christos 			return (error);
    254   1.1  christos 	}
    255   1.1  christos 	return (0);
    256   1.1  christos }
    257   1.1  christos 
    258   1.1  christos 
    259   1.1  christos /* ARGSUSED */
    260   1.1  christos int
    261  1.30       dsl compat_43_sys_killpg(struct lwp *l, const struct compat_43_sys_killpg_args *uap, register_t *retval)
    262   1.4   thorpej {
    263  1.30       dsl 	/* {
    264   1.1  christos 		syscallarg(int) pgid;
    265   1.1  christos 		syscallarg(int) signum;
    266  1.30       dsl 	} */
    267  1.19  christos 	ksiginfo_t ksi;
    268  1.30       dsl 	int pgid = SCARG(uap, pgid);
    269   1.1  christos 
    270  1.35  pgoyette 	pgid &= kern_sig_43_pgid_mask;
    271   1.1  christos 
    272   1.1  christos 	if ((u_int)SCARG(uap, signum) >= NSIG)
    273   1.1  christos 		return (EINVAL);
    274  1.19  christos 	memset(&ksi, 0, sizeof(ksi));
    275  1.19  christos 	ksi.ksi_signo = SCARG(uap, signum);
    276  1.19  christos 	ksi.ksi_code = SI_USER;
    277  1.23        ad 	ksi.ksi_pid = l->l_proc->p_pid;
    278  1.23        ad 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    279  1.30       dsl 	return killpg1(l, &ksi, pgid, 0);
    280   1.1  christos }
    281  1.35  pgoyette 
    282  1.35  pgoyette int
    283  1.35  pgoyette kern_sig_43_init(void)
    284  1.35  pgoyette {
    285  1.35  pgoyette 
    286  1.35  pgoyette 	return syscall_establish(NULL, kern_sig_43_syscalls);
    287  1.35  pgoyette }
    288  1.35  pgoyette 
    289  1.35  pgoyette int
    290  1.35  pgoyette kern_sig_43_fini(void)
    291  1.35  pgoyette {
    292  1.35  pgoyette 
    293  1.35  pgoyette 	return syscall_disestablish(NULL, kern_sig_43_syscalls);
    294  1.35  pgoyette }
    295