2 * $NetBSD: svr4_resource.c,v 1.3 1998/12/13 18:00:52 christos Exp $
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
42 * $FreeBSD: src/sys/svr4/svr4_resource.c,v 1.4.2.1 2002/09/02 21:22:54 dillon Exp $
43 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_resource.c,v 1.4 2003/08/07 21:17:19 dillon Exp $
47 * Portions of this software have been derived from software contributed
48 * to the FreeBSD Project by Mark Newton.
50 * Copyright (c) 1999 Mark Newton
51 * All rights reserved.
53 * Redistribution and use in source and binary forms, with or without
54 * modification, are permitted provided that the following conditions
56 * 1. Redistributions of source code must retain the above copyright
57 * notice, this list of conditions and the following disclaimer.
58 * 2. Redistributions in binary form must reproduce the above copyright
59 * notice, this list of conditions and the following disclaimer in the
60 * documentation and/or other materials provided with the distribution.
61 * 3. The name of the author may not be used to endorse or promote products
62 * derived from this software without specific prior written permission
64 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
65 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
66 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
67 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
68 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
69 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
73 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 #include <sys/param.h>
77 #include <sys/systm.h>
80 #include <sys/resource.h>
81 #include <sys/resourcevar.h>
84 #include "svr4_types.h"
85 #include "svr4_resource.h"
86 #include "svr4_signal.h"
87 #include "svr4_proto.h"
88 #include "svr4_util.h"
90 static __inline int svr4_to_native_rl __P((int));
99 case SVR4_RLIMIT_FSIZE:
101 case SVR4_RLIMIT_DATA:
103 case SVR4_RLIMIT_STACK:
105 case SVR4_RLIMIT_CORE:
107 case SVR4_RLIMIT_NOFILE:
108 return RLIMIT_NOFILE;
109 case SVR4_RLIMIT_VMEM:
117 * Check if the resource limit fits within the BSD range and it is not
118 * one of the magic SVR4 limit values
120 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
121 ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
122 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
123 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
125 #define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
126 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
127 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
128 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
131 svr4_sys_getrlimit(struct svr4_sys_getrlimit_args *uap)
133 struct proc *p = curproc;
134 int rl = svr4_to_native_rl(SCARG(uap, which));
136 struct svr4_rlimit slim;
141 blim = p->p_rlimit[rl];
144 * Our infinity, is their maxfiles.
146 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
147 blim.rlim_max = maxfiles;
150 * If the limit can be be represented, it is returned.
151 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
152 * else return RLIM_SAVED_CUR
154 if (blim.rlim_max == RLIM_INFINITY)
155 slim.rlim_max = SVR4_RLIM_INFINITY;
156 else if (OKLIMIT(blim.rlim_max))
157 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
159 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
161 if (blim.rlim_cur == RLIM_INFINITY)
162 slim.rlim_cur = SVR4_RLIM_INFINITY;
163 else if (OKLIMIT(blim.rlim_cur))
164 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
165 else if (blim.rlim_max == blim.rlim_cur)
166 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
168 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
170 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
175 svr4_sys_setrlimit(struct svr4_sys_setrlimit_args *uap)
177 struct proc *p = curproc;
178 int rl = svr4_to_native_rl(SCARG(uap, which));
179 struct rlimit blim, *limp;
180 struct svr4_rlimit slim;
186 limp = &p->p_rlimit[rl];
188 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
192 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
194 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
195 * new limit to the corresponding saved hard limit, and if
196 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
197 * corresponding saved soft limit.
200 if (slim.rlim_max == SVR4_RLIM_INFINITY)
201 blim.rlim_max = RLIM_INFINITY;
202 else if (OKLIMIT(slim.rlim_max))
203 blim.rlim_max = (rlim_t) slim.rlim_max;
204 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
205 blim.rlim_max = limp->rlim_max;
206 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
207 blim.rlim_max = limp->rlim_cur;
209 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
210 blim.rlim_cur = RLIM_INFINITY;
211 else if (OKLIMIT(slim.rlim_cur))
212 blim.rlim_cur = (rlim_t) slim.rlim_cur;
213 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
214 blim.rlim_cur = limp->rlim_max;
215 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
216 blim.rlim_cur = limp->rlim_cur;
218 return dosetrlimit(rl, &blim);
223 svr4_sys_getrlimit64(struct svr4_sys_getrlimit64_args *uap)
225 struct proc *p = curproc;
226 int rl = svr4_to_native_rl(SCARG(uap, which));
228 struct svr4_rlimit64 slim;
233 blim = p->p_rlimit[rl];
236 * Our infinity, is their maxfiles.
238 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
239 blim.rlim_max = maxfiles;
242 * If the limit can be be represented, it is returned.
243 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
244 * else return SVR4_RLIM_SAVED_CUR
246 if (blim.rlim_max == RLIM_INFINITY)
247 slim.rlim_max = SVR4_RLIM64_INFINITY;
248 else if (OKLIMIT64(blim.rlim_max))
249 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
251 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
253 if (blim.rlim_cur == RLIM_INFINITY)
254 slim.rlim_cur = SVR4_RLIM64_INFINITY;
255 else if (OKLIMIT64(blim.rlim_cur))
256 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
257 else if (blim.rlim_max == blim.rlim_cur)
258 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
260 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
262 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
267 svr4_sys_setrlimit64(struct svr4_sys_setrlimit64_args *uap)
269 struct proc *p = curproc;
270 int rl = svr4_to_native_rl(SCARG(uap, which));
271 struct rlimit blim, *limp;
272 struct svr4_rlimit64 slim;
278 limp = &p->p_rlimit[rl];
280 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
284 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
286 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
287 * new limit to the corresponding saved hard limit, and if
288 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
289 * corresponding saved soft limit.
292 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
293 blim.rlim_max = RLIM_INFINITY;
294 else if (OKLIMIT64(slim.rlim_max))
295 blim.rlim_max = (rlim_t) slim.rlim_max;
296 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
297 blim.rlim_max = limp->rlim_max;
298 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
299 blim.rlim_max = limp->rlim_cur;
301 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
302 blim.rlim_cur = RLIM_INFINITY;
303 else if (OKLIMIT64(slim.rlim_cur))
304 blim.rlim_cur = (rlim_t) slim.rlim_cur;
305 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
306 blim.rlim_cur = limp->rlim_max;
307 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
308 blim.rlim_cur = limp->rlim_cur;
310 return dosetrlimit(rl, &blim);