Deleted Added
full compact
1/* Derived from:
2 * $NetBSD: svr4_resource.c,v 1.3 1998/12/13 18:00:52 christos Exp $
3 */
4
5/*-
6 * Original copyright:
7 *
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * All rights reserved.
10 *
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Christos Zoulas.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 *
42 * $FreeBSD: head/sys/compat/svr4/svr4_resource.c 107839 2002-12-13 22:41:47Z alfred $
42 * $FreeBSD: head/sys/compat/svr4/svr4_resource.c 107849 2002-12-14 01:56:26Z alfred $
43 */
44
45/*
46 * Portions of this software have been derived from software contributed
47 * to the FreeBSD Project by Mark Newton.
48 *
49 * Copyright (c) 1999 Mark Newton
50 * All rights reserved.
51 *
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
60 * 3. The name of the author may not be used to endorse or promote products
61 * derived from this software without specific prior written permission
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
64 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
65 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
66 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
67 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
68 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
69 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
70 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
72 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 */
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/file.h>
78#include <sys/lock.h>
79#include <sys/mutex.h>
80#include <sys/proc.h>
81#include <sys/resource.h>
82#include <sys/resourcevar.h>
83
84#include <compat/svr4/svr4.h>
85#include <compat/svr4/svr4_types.h>
86#include <compat/svr4/svr4_resource.h>
87#include <compat/svr4/svr4_signal.h>
88#include <compat/svr4/svr4_proto.h>
89#include <compat/svr4/svr4_util.h>
90
91static __inline int svr4_to_native_rl(int);
92
93static __inline int
94svr4_to_native_rl(rl)
95 int rl;
96{
97 switch (rl) {
98 case SVR4_RLIMIT_CPU:
99 return RLIMIT_CPU;
100 case SVR4_RLIMIT_FSIZE:
101 return RLIMIT_FSIZE;
102 case SVR4_RLIMIT_DATA:
103 return RLIMIT_DATA;
104 case SVR4_RLIMIT_STACK:
105 return RLIMIT_STACK;
106 case SVR4_RLIMIT_CORE:
107 return RLIMIT_CORE;
108 case SVR4_RLIMIT_NOFILE:
109 return RLIMIT_NOFILE;
110 case SVR4_RLIMIT_VMEM:
111 return RLIMIT_VMEM;
112 default:
113 return -1;
114 }
115}
116
117/*
118 * Check if the resource limit fits within the BSD range and it is not
119 * one of the magic SVR4 limit values
120 */
121#define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
122 ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
123 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
124 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
125
126#define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
127 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
128 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
129 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
130
131int
132svr4_sys_getrlimit(td, uap)
133 register struct thread *td;
134 struct svr4_sys_getrlimit_args *uap;
135{
136 int rl = svr4_to_native_rl(SCARG(uap, which));
136 int rl = svr4_to_native_rl(uap->which);
137 struct rlimit blim;
138 struct svr4_rlimit slim;
139
140 if (rl == -1)
141 return EINVAL;
142
143 /* For p_rlimit. */
144 mtx_assert(&Giant, MA_OWNED);
145 blim = td->td_proc->p_rlimit[rl];
146
147 /*
148 * Our infinity, is their maxfiles.
149 */
150 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
151 blim.rlim_max = maxfiles;
152
153 /*
154 * If the limit can be be represented, it is returned.
155 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
156 * else return RLIM_SAVED_CUR
157 */
158 if (blim.rlim_max == RLIM_INFINITY)
159 slim.rlim_max = SVR4_RLIM_INFINITY;
160 else if (OKLIMIT(blim.rlim_max))
161 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
162 else
163 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
164
165 if (blim.rlim_cur == RLIM_INFINITY)
166 slim.rlim_cur = SVR4_RLIM_INFINITY;
167 else if (OKLIMIT(blim.rlim_cur))
168 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
169 else if (blim.rlim_max == blim.rlim_cur)
170 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
171 else
172 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
173
174 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
174 return copyout(&slim, uap->rlp, sizeof(*uap->rlp));
175}
176
177
178int
179svr4_sys_setrlimit(td, uap)
180 register struct thread *td;
181 struct svr4_sys_setrlimit_args *uap;
182{
183 int rl = svr4_to_native_rl(SCARG(uap, which));
183 int rl = svr4_to_native_rl(uap->which);
184 struct rlimit blim, *limp;
185 struct svr4_rlimit slim;
186 int error;
187
188 if (rl == -1)
189 return EINVAL;
190
191 /* For p_rlimit. */
192 mtx_assert(&Giant, MA_OWNED);
193 limp = &td->td_proc->p_rlimit[rl];
194
195 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
195 if ((error = copyin(uap->rlp, &slim, sizeof(slim))) != 0)
196 return error;
197
198 /*
199 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
200 * unlimited.
201 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
202 * new limit to the corresponding saved hard limit, and if
203 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
204 * corresponding saved soft limit.
205 *
206 */
207 if (slim.rlim_max == SVR4_RLIM_INFINITY)
208 blim.rlim_max = RLIM_INFINITY;
209 else if (OKLIMIT(slim.rlim_max))
210 blim.rlim_max = (rlim_t) slim.rlim_max;
211 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
212 blim.rlim_max = limp->rlim_max;
213 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
214 blim.rlim_max = limp->rlim_cur;
215
216 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
217 blim.rlim_cur = RLIM_INFINITY;
218 else if (OKLIMIT(slim.rlim_cur))
219 blim.rlim_cur = (rlim_t) slim.rlim_cur;
220 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
221 blim.rlim_cur = limp->rlim_max;
222 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
223 blim.rlim_cur = limp->rlim_cur;
224
225 return dosetrlimit(td, rl, &blim);
226}
227
228
229int
230svr4_sys_getrlimit64(td, uap)
231 register struct thread *td;
232 struct svr4_sys_getrlimit64_args *uap;
233{
234 int rl = svr4_to_native_rl(SCARG(uap, which));
234 int rl = svr4_to_native_rl(uap->which);
235 struct rlimit blim;
236 struct svr4_rlimit64 slim;
237
238 if (rl == -1)
239 return EINVAL;
240
241 /* For p_rlimit. */
242 mtx_assert(&Giant, MA_OWNED);
243 blim = td->td_proc->p_rlimit[rl];
244
245 /*
246 * Our infinity, is their maxfiles.
247 */
248 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
249 blim.rlim_max = maxfiles;
250
251 /*
252 * If the limit can be be represented, it is returned.
253 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
254 * else return SVR4_RLIM_SAVED_CUR
255 */
256 if (blim.rlim_max == RLIM_INFINITY)
257 slim.rlim_max = SVR4_RLIM64_INFINITY;
258 else if (OKLIMIT64(blim.rlim_max))
259 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
260 else
261 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
262
263 if (blim.rlim_cur == RLIM_INFINITY)
264 slim.rlim_cur = SVR4_RLIM64_INFINITY;
265 else if (OKLIMIT64(blim.rlim_cur))
266 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
267 else if (blim.rlim_max == blim.rlim_cur)
268 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
269 else
270 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
271
272 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
272 return copyout(&slim, uap->rlp, sizeof(*uap->rlp));
273}
274
275
276int
277svr4_sys_setrlimit64(td, uap)
278 register struct thread *td;
279 struct svr4_sys_setrlimit64_args *uap;
280{
281 int rl = svr4_to_native_rl(SCARG(uap, which));
281 int rl = svr4_to_native_rl(uap->which);
282 struct rlimit blim, *limp;
283 struct svr4_rlimit64 slim;
284 int error;
285
286 if (rl == -1)
287 return EINVAL;
288
289 /* For p_rlimit. */
290 mtx_assert(&Giant, MA_OWNED);
291 limp = &td->td_proc->p_rlimit[rl];
292
293 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
293 if ((error = copyin(uap->rlp, &slim, sizeof(slim))) != 0)
294 return error;
295
296 /*
297 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
298 * unlimited.
299 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
300 * new limit to the corresponding saved hard limit, and if
301 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
302 * corresponding saved soft limit.
303 *
304 */
305 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
306 blim.rlim_max = RLIM_INFINITY;
307 else if (OKLIMIT64(slim.rlim_max))
308 blim.rlim_max = (rlim_t) slim.rlim_max;
309 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
310 blim.rlim_max = limp->rlim_max;
311 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
312 blim.rlim_max = limp->rlim_cur;
313
314 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
315 blim.rlim_cur = RLIM_INFINITY;
316 else if (OKLIMIT64(slim.rlim_cur))
317 blim.rlim_cur = (rlim_t) slim.rlim_cur;
318 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
319 blim.rlim_cur = limp->rlim_max;
320 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
321 blim.rlim_cur = limp->rlim_cur;
322
323 return dosetrlimit(td, rl, &blim);
324}