2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 * Portions of this software have been derived from software contributed
31 * to the FreeBSD Project by Mark Newton.
33 * Copyright (c) 1999 Mark Newton
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * Derived from: $NetBSD: svr4_resource.c,v 1.3 1998/12/13 18:00:52 christos Exp $
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include <sys/param.h>
65 #include <sys/systm.h>
68 #include <sys/mutex.h>
70 #include <sys/resource.h>
71 #include <sys/resourcevar.h>
72 #include <sys/syscallsubr.h>
74 #include <compat/svr4/svr4.h>
75 #include <compat/svr4/svr4_types.h>
76 #include <compat/svr4/svr4_resource.h>
77 #include <compat/svr4/svr4_signal.h>
78 #include <compat/svr4/svr4_proto.h>
79 #include <compat/svr4/svr4_util.h>
81 static __inline int svr4_to_native_rl(int);
90 case SVR4_RLIMIT_FSIZE:
92 case SVR4_RLIMIT_DATA:
94 case SVR4_RLIMIT_STACK:
96 case SVR4_RLIMIT_CORE:
98 case SVR4_RLIMIT_NOFILE:
100 case SVR4_RLIMIT_VMEM:
108 * Check if the resource limit fits within the BSD range and it is not
109 * one of the magic SVR4 limit values
111 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
112 ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
113 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
114 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
116 #define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
117 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
118 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
119 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
122 svr4_sys_getrlimit(td, uap)
124 struct svr4_sys_getrlimit_args *uap;
126 int rl = svr4_to_native_rl(uap->which);
128 struct svr4_rlimit slim;
133 PROC_LOCK(td->td_proc);
134 lim_rlimit(td->td_proc, rl, &blim);
135 PROC_UNLOCK(td->td_proc);
138 * Our infinity, is their maxfiles.
140 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
141 blim.rlim_max = maxfiles;
144 * If the limit can be be represented, it is returned.
145 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
146 * else return RLIM_SAVED_CUR
148 if (blim.rlim_max == RLIM_INFINITY)
149 slim.rlim_max = SVR4_RLIM_INFINITY;
150 else if (OKLIMIT(blim.rlim_max))
151 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
153 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
155 if (blim.rlim_cur == RLIM_INFINITY)
156 slim.rlim_cur = SVR4_RLIM_INFINITY;
157 else if (OKLIMIT(blim.rlim_cur))
158 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
159 else if (blim.rlim_max == blim.rlim_cur)
160 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
162 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
164 return copyout(&slim, uap->rlp, sizeof(*uap->rlp));
169 svr4_sys_setrlimit(td, uap)
171 struct svr4_sys_setrlimit_args *uap;
173 int rl = svr4_to_native_rl(uap->which);
174 struct rlimit blim, curlim;
175 struct svr4_rlimit slim;
181 if ((error = copyin(uap->rlp, &slim, sizeof(slim))) != 0)
184 PROC_LOCK(td->td_proc);
185 lim_rlimit(td->td_proc, rl, &curlim);
186 PROC_UNLOCK(td->td_proc);
189 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
191 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
192 * new limit to the corresponding saved hard limit, and if
193 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
194 * corresponding saved soft limit.
197 if (slim.rlim_max == SVR4_RLIM_INFINITY)
198 blim.rlim_max = RLIM_INFINITY;
199 else if (OKLIMIT(slim.rlim_max))
200 blim.rlim_max = (rlim_t) slim.rlim_max;
201 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
202 blim.rlim_max = curlim.rlim_max;
203 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
204 blim.rlim_max = curlim.rlim_cur;
206 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
207 blim.rlim_cur = RLIM_INFINITY;
208 else if (OKLIMIT(slim.rlim_cur))
209 blim.rlim_cur = (rlim_t) slim.rlim_cur;
210 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
211 blim.rlim_cur = curlim.rlim_max;
212 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
213 blim.rlim_cur = curlim.rlim_cur;
215 return (kern_setrlimit(td, rl, &blim));
220 svr4_sys_getrlimit64(td, uap)
222 struct svr4_sys_getrlimit64_args *uap;
224 int rl = svr4_to_native_rl(uap->which);
226 struct svr4_rlimit64 slim;
231 PROC_LOCK(td->td_proc);
232 lim_rlimit(td->td_proc, rl, &blim);
233 PROC_UNLOCK(td->td_proc);
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, uap->rlp, sizeof(*uap->rlp));
267 svr4_sys_setrlimit64(td, uap)
269 struct svr4_sys_setrlimit64_args *uap;
271 int rl = svr4_to_native_rl(uap->which);
272 struct rlimit blim, curlim;
273 struct svr4_rlimit64 slim;
279 if ((error = copyin(uap->rlp, &slim, sizeof(slim))) != 0)
282 PROC_LOCK(td->td_proc);
283 lim_rlimit(td->td_proc, rl, &curlim);
284 PROC_UNLOCK(td->td_proc);
287 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
289 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
290 * new limit to the corresponding saved hard limit, and if
291 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
292 * corresponding saved soft limit.
295 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
296 blim.rlim_max = RLIM_INFINITY;
297 else if (OKLIMIT64(slim.rlim_max))
298 blim.rlim_max = (rlim_t) slim.rlim_max;
299 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
300 blim.rlim_max = curlim.rlim_max;
301 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
302 blim.rlim_max = curlim.rlim_cur;
304 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
305 blim.rlim_cur = RLIM_INFINITY;
306 else if (OKLIMIT64(slim.rlim_cur))
307 blim.rlim_cur = (rlim_t) slim.rlim_cur;
308 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
309 blim.rlim_cur = curlim.rlim_max;
310 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
311 blim.rlim_cur = curlim.rlim_cur;
313 return (kern_setrlimit(td, rl, &blim));