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