1 /* $NetBSD: svr4_32_resource.c,v 1.9 2006/07/28 13:02:56 hannken 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 Christos Zoulas.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: svr4_32_resource.c,v 1.9 2006/07/28 13:02:56 hannken Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/file.h>
46 #include <sys/resource.h>
47 #include <sys/resourcevar.h>
48
49 #include <machine/vmparam.h>
50
51 #include <compat/svr4_32/svr4_32_types.h>
52 #include <compat/svr4_32/svr4_32_resource.h>
53 #include <compat/svr4_32/svr4_32_signal.h>
54 #include <compat/svr4_32/svr4_32_lwp.h>
55 #include <compat/svr4_32/svr4_32_ucontext.h>
56 #include <compat/svr4_32/svr4_32_syscallargs.h>
57 #include <compat/svr4_32/svr4_32_util.h>
58
59 static inline int svr4_to_native_rl __P((int));
60
61 static inline int
62 svr4_to_native_rl(rl)
63 int rl;
64 {
65 switch (rl) {
66 case SVR4_RLIMIT_CPU:
67 return RLIMIT_CPU;
68 case SVR4_RLIMIT_FSIZE:
69 return RLIMIT_FSIZE;
70 case SVR4_RLIMIT_DATA:
71 return RLIMIT_DATA;
72 case SVR4_RLIMIT_STACK:
73 return RLIMIT_STACK;
74 case SVR4_RLIMIT_CORE:
75 return RLIMIT_CORE;
76 case SVR4_RLIMIT_NOFILE:
77 return RLIMIT_NOFILE;
78 case SVR4_RLIMIT_VMEM:
79 return RLIMIT_RSS;
80 default:
81 return -1;
82 }
83 }
84
85 /*
86 * Check if the resource limit fits within the BSD range and it is not
87 * one of the magic SVR4 limit values
88 */
89 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
90 ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
91 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
92 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
93
94 #define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
95 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
96 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
97 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
98
99 int
100 svr4_32_sys_getrlimit(l, v, retval)
101 struct lwp *l;
102 void *v;
103 register_t *retval;
104 {
105 struct svr4_32_sys_getrlimit_args *uap = v;
106 struct proc *p = l->l_proc;
107 int rl = svr4_to_native_rl(SCARG(uap, which));
108 struct rlimit blim;
109 struct svr4_rlimit slim;
110
111 if (rl == -1)
112 return EINVAL;
113
114 blim = p->p_rlimit[rl];
115
116 /*
117 * Our infinity, is their maxfiles.
118 */
119 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
120 blim.rlim_max = maxfiles;
121
122 /*
123 * If the limit can be be represented, it is returned.
124 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
125 * else return RLIM_SAVED_CUR
126 */
127 if (blim.rlim_max == RLIM_INFINITY)
128 slim.rlim_max = SVR4_RLIM_INFINITY;
129 else if (OKLIMIT(blim.rlim_max))
130 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
131 else
132 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
133
134 if (blim.rlim_cur == RLIM_INFINITY)
135 slim.rlim_cur = SVR4_RLIM_INFINITY;
136 else if (OKLIMIT(blim.rlim_cur))
137 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
138 else if (blim.rlim_max == blim.rlim_cur)
139 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
140 else
141 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
142
143 switch (rl) {
144 case RLIMIT_DATA:
145 if (blim.rlim_cur > MAXDSIZ32)
146 blim.rlim_cur = MAXDSIZ32;
147 if (blim.rlim_max > MAXDSIZ32)
148 blim.rlim_max = MAXDSIZ32;
149 break;
150
151 case RLIMIT_STACK:
152 if (blim.rlim_cur > MAXSSIZ32)
153 blim.rlim_cur = MAXSSIZ32;
154 if (blim.rlim_max > MAXSSIZ32)
155 blim.rlim_max = MAXSSIZ32;
156 default:
157 break;
158 }
159
160 return copyout(&slim, (caddr_t)(u_long)SCARG(uap, rlp), sizeof(slim));
161 }
162
163
164 int
165 svr4_32_sys_setrlimit(l, v, retval)
166 struct lwp *l;
167 void *v;
168 register_t *retval;
169 {
170 struct svr4_32_sys_setrlimit_args *uap = v;
171 struct proc *p = l->l_proc;
172 int rl = svr4_to_native_rl(SCARG(uap, which));
173 struct rlimit blim, *limp;
174 struct svr4_rlimit slim;
175 int error;
176
177 if (rl == -1)
178 return EINVAL;
179
180 limp = &p->p_rlimit[rl];
181
182 if ((error = copyin((caddr_t)(u_long)SCARG(uap, rlp), &slim,
183 sizeof(slim))) != 0)
184 return error;
185
186 /*
187 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
188 * unlimited.
189 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
190 * new limit to the corresponding saved hard limit, and if
191 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
192 * corresponding saved soft limit.
193 *
194 */
195 if (slim.rlim_max == SVR4_RLIM_INFINITY)
196 blim.rlim_max = RLIM_INFINITY;
197 else if (OKLIMIT(slim.rlim_max))
198 blim.rlim_max = (rlim_t) slim.rlim_max;
199 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
200 blim.rlim_max = limp->rlim_max;
201 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
202 blim.rlim_max = limp->rlim_cur;
203
204 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
205 blim.rlim_cur = RLIM_INFINITY;
206 else if (OKLIMIT(slim.rlim_cur))
207 blim.rlim_cur = (rlim_t) slim.rlim_cur;
208 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
209 blim.rlim_cur = limp->rlim_max;
210 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
211 blim.rlim_cur = limp->rlim_cur;
212
213 return dosetrlimit(l, p, rl, &blim);
214 }
215
216
217 int
218 svr4_32_sys_getrlimit64(l, v, retval)
219 struct lwp *l;
220 void *v;
221 register_t *retval;
222 {
223 struct svr4_32_sys_getrlimit64_args *uap = v;
224 struct proc *p = l->l_proc;
225 int rl = svr4_to_native_rl(SCARG(uap, which));
226 struct rlimit blim;
227 struct svr4_rlimit64 slim;
228
229 if (rl == -1)
230 return EINVAL;
231
232 blim = p->p_rlimit[rl];
233
234 /*
235 * Our infinity, is their maxfiles.
236 */
237 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
238 blim.rlim_max = maxfiles;
239
240 /*
241 * If the limit can be be represented, it is returned.
242 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
243 * else return SVR4_RLIM_SAVED_CUR
244 */
245 if (blim.rlim_max == RLIM_INFINITY)
246 slim.rlim_max = SVR4_RLIM64_INFINITY;
247 else if (OKLIMIT64(blim.rlim_max))
248 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
249 else
250 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
251
252 if (blim.rlim_cur == RLIM_INFINITY)
253 slim.rlim_cur = SVR4_RLIM64_INFINITY;
254 else if (OKLIMIT64(blim.rlim_cur))
255 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
256 else if (blim.rlim_max == blim.rlim_cur)
257 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
258 else
259 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
260
261 return copyout(&slim, (caddr_t)(u_long)SCARG(uap, rlp),
262 sizeof(slim));
263 }
264
265
266 int
267 svr4_32_sys_setrlimit64(l, v, retval)
268 struct lwp *l;
269 void *v;
270 register_t *retval;
271 {
272 struct svr4_32_sys_setrlimit64_args *uap = v;
273 struct proc *p = l->l_proc;
274 int rl = svr4_to_native_rl(SCARG(uap, which));
275 struct rlimit blim, *limp;
276 struct svr4_rlimit64 slim;
277 int error;
278
279 if (rl == -1)
280 return EINVAL;
281
282 limp = &p->p_rlimit[rl];
283
284 if ((error = copyin((caddr_t)(u_long)SCARG(uap, rlp),
285 &slim, sizeof(slim))) != 0)
286 return error;
287
288 /*
289 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
290 * unlimited.
291 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
292 * new limit to the corresponding saved hard limit, and if
293 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
294 * corresponding saved soft limit.
295 *
296 */
297 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
298 blim.rlim_max = RLIM_INFINITY;
299 else if (OKLIMIT64(slim.rlim_max))
300 blim.rlim_max = (rlim_t) slim.rlim_max;
301 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
302 blim.rlim_max = limp->rlim_max;
303 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
304 blim.rlim_max = limp->rlim_cur;
305
306 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
307 blim.rlim_cur = RLIM_INFINITY;
308 else if (OKLIMIT64(slim.rlim_cur))
309 blim.rlim_cur = (rlim_t) slim.rlim_cur;
310 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
311 blim.rlim_cur = limp->rlim_max;
312 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
313 blim.rlim_cur = limp->rlim_cur;
314
315 return dosetrlimit(l, p, rl, &blim);
316 }
Cache object: a3841855d8008e3c1d2f0c5891bbc1e2
|