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