1 /* $NetBSD: svr4_32_resource.c,v 1.3 2003/01/19 16:47:15 thorpej 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.3 2003/01/19 16:47:15 thorpej 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 <compat/svr4_32/svr4_32_types.h>
50 #include <compat/svr4_32/svr4_32_resource.h>
51 #include <compat/svr4_32/svr4_32_signal.h>
52 #include <compat/svr4_32/svr4_32_lwp.h>
53 #include <compat/svr4_32/svr4_32_ucontext.h>
54 #include <compat/svr4_32/svr4_32_syscallargs.h>
55 #include <compat/svr4_32/svr4_32_util.h>
56
57 static __inline int svr4_to_native_rl __P((int));
58
59 static __inline int
60 svr4_to_native_rl(rl)
61 int rl;
62 {
63 switch (rl) {
64 case SVR4_RLIMIT_CPU:
65 return RLIMIT_CPU;
66 case SVR4_RLIMIT_FSIZE:
67 return RLIMIT_FSIZE;
68 case SVR4_RLIMIT_DATA:
69 return RLIMIT_DATA;
70 case SVR4_RLIMIT_STACK:
71 return RLIMIT_STACK;
72 case SVR4_RLIMIT_CORE:
73 return RLIMIT_CORE;
74 case SVR4_RLIMIT_NOFILE:
75 return RLIMIT_NOFILE;
76 case SVR4_RLIMIT_VMEM:
77 return RLIMIT_RSS;
78 default:
79 return -1;
80 }
81 }
82
83 /*
84 * Check if the resource limit fits within the BSD range and it is not
85 * one of the magic SVR4 limit values
86 */
87 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
88 ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
89 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
90 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
91
92 #define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
93 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
94 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
95 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
96
97 int
98 svr4_32_sys_getrlimit(l, v, retval)
99 struct lwp *l;
100 void *v;
101 register_t *retval;
102 {
103 struct svr4_32_sys_getrlimit_args *uap = v;
104 struct proc *p = l->l_proc;
105 int rl = svr4_to_native_rl(SCARG(uap, which));
106 struct rlimit blim;
107 struct svr4_rlimit slim;
108
109 if (rl == -1)
110 return EINVAL;
111
112 blim = p->p_rlimit[rl];
113
114 /*
115 * Our infinity, is their maxfiles.
116 */
117 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
118 blim.rlim_max = maxfiles;
119
120 /*
121 * If the limit can be be represented, it is returned.
122 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
123 * else return RLIM_SAVED_CUR
124 */
125 if (blim.rlim_max == RLIM_INFINITY)
126 slim.rlim_max = SVR4_RLIM_INFINITY;
127 else if (OKLIMIT(blim.rlim_max))
128 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
129 else
130 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
131
132 if (blim.rlim_cur == RLIM_INFINITY)
133 slim.rlim_cur = SVR4_RLIM_INFINITY;
134 else if (OKLIMIT(blim.rlim_cur))
135 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
136 else if (blim.rlim_max == blim.rlim_cur)
137 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
138 else
139 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
140
141 return copyout(&slim, (caddr_t)(u_long)SCARG(uap, rlp), sizeof(slim));
142 }
143
144
145 int
146 svr4_32_sys_setrlimit(l, v, retval)
147 struct lwp *l;
148 void *v;
149 register_t *retval;
150 {
151 struct svr4_32_sys_setrlimit_args *uap = v;
152 struct proc *p = l->l_proc;
153 int rl = svr4_to_native_rl(SCARG(uap, which));
154 struct rlimit blim, *limp;
155 struct svr4_rlimit slim;
156 int error;
157
158 if (rl == -1)
159 return EINVAL;
160
161 limp = &p->p_rlimit[rl];
162
163 if ((error = copyin((caddr_t)(u_long)SCARG(uap, rlp),
164 &slim, sizeof(slim))) != 0)
165 return error;
166
167 /*
168 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
169 * unlimited.
170 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
171 * new limit to the corresponding saved hard limit, and if
172 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
173 * corresponding saved soft limit.
174 *
175 */
176 if (slim.rlim_max == SVR4_RLIM_INFINITY)
177 blim.rlim_max = RLIM_INFINITY;
178 else if (OKLIMIT(slim.rlim_max))
179 blim.rlim_max = (rlim_t) slim.rlim_max;
180 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
181 blim.rlim_max = limp->rlim_max;
182 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
183 blim.rlim_max = limp->rlim_cur;
184
185 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
186 blim.rlim_cur = RLIM_INFINITY;
187 else if (OKLIMIT(slim.rlim_cur))
188 blim.rlim_cur = (rlim_t) slim.rlim_cur;
189 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
190 blim.rlim_cur = limp->rlim_max;
191 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
192 blim.rlim_cur = limp->rlim_cur;
193
194 return dosetrlimit(p, p->p_cred, rl, &blim);
195 }
196
197
198 int
199 svr4_32_sys_getrlimit64(l, v, retval)
200 struct lwp *l;
201 void *v;
202 register_t *retval;
203 {
204 struct svr4_32_sys_getrlimit64_args *uap = v;
205 struct proc *p = l->l_proc;
206 int rl = svr4_to_native_rl(SCARG(uap, which));
207 struct rlimit blim;
208 struct svr4_rlimit64 slim;
209
210 if (rl == -1)
211 return EINVAL;
212
213 blim = p->p_rlimit[rl];
214
215 /*
216 * Our infinity, is their maxfiles.
217 */
218 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
219 blim.rlim_max = maxfiles;
220
221 /*
222 * If the limit can be be represented, it is returned.
223 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
224 * else return SVR4_RLIM_SAVED_CUR
225 */
226 if (blim.rlim_max == RLIM_INFINITY)
227 slim.rlim_max = SVR4_RLIM64_INFINITY;
228 else if (OKLIMIT64(blim.rlim_max))
229 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
230 else
231 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
232
233 if (blim.rlim_cur == RLIM_INFINITY)
234 slim.rlim_cur = SVR4_RLIM64_INFINITY;
235 else if (OKLIMIT64(blim.rlim_cur))
236 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
237 else if (blim.rlim_max == blim.rlim_cur)
238 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
239 else
240 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
241
242 return copyout(&slim, (caddr_t)(u_long)SCARG(uap, rlp),
243 sizeof(slim));
244 }
245
246
247 int
248 svr4_32_sys_setrlimit64(l, v, retval)
249 struct lwp *l;
250 void *v;
251 register_t *retval;
252 {
253 struct svr4_32_sys_setrlimit64_args *uap = v;
254 struct proc *p = l->l_proc;
255 int rl = svr4_to_native_rl(SCARG(uap, which));
256 struct rlimit blim, *limp;
257 struct svr4_rlimit64 slim;
258 int error;
259
260 if (rl == -1)
261 return EINVAL;
262
263 limp = &p->p_rlimit[rl];
264
265 if ((error = copyin((caddr_t)(u_long)SCARG(uap, rlp),
266 &slim, sizeof(slim))) != 0)
267 return error;
268
269 /*
270 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
271 * unlimited.
272 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
273 * new limit to the corresponding saved hard limit, and if
274 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
275 * corresponding saved soft limit.
276 *
277 */
278 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
279 blim.rlim_max = RLIM_INFINITY;
280 else if (OKLIMIT64(slim.rlim_max))
281 blim.rlim_max = (rlim_t) slim.rlim_max;
282 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
283 blim.rlim_max = limp->rlim_max;
284 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
285 blim.rlim_max = limp->rlim_cur;
286
287 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
288 blim.rlim_cur = RLIM_INFINITY;
289 else if (OKLIMIT64(slim.rlim_cur))
290 blim.rlim_cur = (rlim_t) slim.rlim_cur;
291 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
292 blim.rlim_cur = limp->rlim_max;
293 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
294 blim.rlim_cur = limp->rlim_cur;
295
296 return dosetrlimit(p, p->p_cred, rl, &blim);
297 }
Cache object: b47c6a4231e8093b0560037270472f3d
|