No empty .Rs/.Re
[netbsd-mini2440.git] / sys / compat / svr4_32 / svr4_32_resource.c
blob6e558daebd9e71c3c65d13886ed53ddcfb986f7f
1 /* $NetBSD: svr4_32_resource.c,v 1.16 2009/01/11 13:14:15 nakayama Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: svr4_32_resource.c,v 1.16 2009/01/11 13:14:15 nakayama Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/file.h>
39 #include <sys/resource.h>
40 #include <sys/resourcevar.h>
42 #include <machine/vmparam.h>
44 #include <compat/svr4_32/svr4_32_types.h>
45 #include <compat/svr4_32/svr4_32_resource.h>
46 #include <compat/svr4_32/svr4_32_signal.h>
47 #include <compat/svr4_32/svr4_32_lwp.h>
48 #include <compat/svr4_32/svr4_32_ucontext.h>
49 #include <compat/svr4_32/svr4_32_syscallargs.h>
50 #include <compat/svr4_32/svr4_32_util.h>
52 static inline int svr4_to_native_rl(int);
54 static inline int
55 svr4_to_native_rl(int rl)
57 switch (rl) {
58 case SVR4_RLIMIT_CPU:
59 return RLIMIT_CPU;
60 case SVR4_RLIMIT_FSIZE:
61 return RLIMIT_FSIZE;
62 case SVR4_RLIMIT_DATA:
63 return RLIMIT_DATA;
64 case SVR4_RLIMIT_STACK:
65 return RLIMIT_STACK;
66 case SVR4_RLIMIT_CORE:
67 return RLIMIT_CORE;
68 case SVR4_RLIMIT_NOFILE:
69 return RLIMIT_NOFILE;
70 case SVR4_RLIMIT_VMEM:
71 return RLIMIT_AS;
72 default:
73 return -1;
78 * Check if the resource limit fits within the BSD range and it is not
79 * one of the magic SVR4 limit values
81 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
82 ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
83 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
84 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
86 #define OKLIMIT64(l) (((rlim_t)(l)) < RLIM_INFINITY && \
87 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
88 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
89 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
91 int
92 svr4_32_sys_getrlimit(struct lwp *l, const struct svr4_32_sys_getrlimit_args *uap, register_t *retval)
94 struct proc *p = l->l_proc;
95 int rl = svr4_to_native_rl(SCARG(uap, which));
96 struct rlimit blim;
97 struct svr4_rlimit slim;
99 if (rl == -1)
100 return EINVAL;
102 blim = p->p_rlimit[rl];
105 * Our infinity, is their maxfiles.
107 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
108 blim.rlim_max = maxfiles;
111 * If the limit can be be represented, it is returned.
112 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
113 * else return RLIM_SAVED_CUR
115 if (blim.rlim_max == RLIM_INFINITY)
116 slim.rlim_max = SVR4_RLIM_INFINITY;
117 else if (OKLIMIT(blim.rlim_max))
118 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
119 else
120 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
122 if (blim.rlim_cur == RLIM_INFINITY)
123 slim.rlim_cur = SVR4_RLIM_INFINITY;
124 else if (OKLIMIT(blim.rlim_cur))
125 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
126 else if (blim.rlim_max == blim.rlim_cur)
127 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
128 else
129 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
131 switch (rl) {
132 case RLIMIT_DATA:
133 if (blim.rlim_cur > MAXDSIZ32)
134 blim.rlim_cur = MAXDSIZ32;
135 if (blim.rlim_max > MAXDSIZ32)
136 blim.rlim_max = MAXDSIZ32;
137 break;
139 case RLIMIT_STACK:
140 if (blim.rlim_cur > MAXSSIZ32)
141 blim.rlim_cur = MAXSSIZ32;
142 if (blim.rlim_max > MAXSSIZ32)
143 blim.rlim_max = MAXSSIZ32;
144 default:
145 break;
148 return copyout(&slim, SCARG_P32(uap, rlp), sizeof(slim));
153 svr4_32_sys_setrlimit(struct lwp *l, const struct svr4_32_sys_setrlimit_args *uap, register_t *retval)
155 struct proc *p = l->l_proc;
156 int rl = svr4_to_native_rl(SCARG(uap, which));
157 struct rlimit blim, *limp;
158 struct svr4_rlimit slim;
159 int error;
161 if (rl == -1)
162 return EINVAL;
164 limp = &p->p_rlimit[rl];
166 if ((error = copyin(SCARG_P32(uap, rlp), &slim,
167 sizeof(slim))) != 0)
168 return error;
171 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
172 * unlimited.
173 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
174 * new limit to the corresponding saved hard limit, and if
175 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
176 * corresponding saved soft limit.
179 if (slim.rlim_max == SVR4_RLIM_INFINITY)
180 blim.rlim_max = RLIM_INFINITY;
181 else if (OKLIMIT(slim.rlim_max))
182 blim.rlim_max = (rlim_t) slim.rlim_max;
183 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
184 blim.rlim_max = limp->rlim_max;
185 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
186 blim.rlim_max = limp->rlim_cur;
188 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
189 blim.rlim_cur = RLIM_INFINITY;
190 else if (OKLIMIT(slim.rlim_cur))
191 blim.rlim_cur = (rlim_t) slim.rlim_cur;
192 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
193 blim.rlim_cur = limp->rlim_max;
194 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
195 blim.rlim_cur = limp->rlim_cur;
197 return dosetrlimit(l, p, rl, &blim);
202 svr4_32_sys_getrlimit64(struct lwp *l, const struct svr4_32_sys_getrlimit64_args *uap, register_t *retval)
204 struct proc *p = l->l_proc;
205 int rl = svr4_to_native_rl(SCARG(uap, which));
206 struct rlimit blim;
207 struct svr4_rlimit64 slim;
209 if (rl == -1)
210 return EINVAL;
212 blim = p->p_rlimit[rl];
215 * Our infinity, is their maxfiles.
217 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
218 blim.rlim_max = maxfiles;
221 * If the limit can be be represented, it is returned.
222 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
223 * else return SVR4_RLIM_SAVED_CUR
225 if (blim.rlim_max == RLIM_INFINITY)
226 slim.rlim_max = SVR4_RLIM64_INFINITY;
227 else if (OKLIMIT64(blim.rlim_max))
228 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
229 else
230 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
232 if (blim.rlim_cur == RLIM_INFINITY)
233 slim.rlim_cur = SVR4_RLIM64_INFINITY;
234 else if (OKLIMIT64(blim.rlim_cur))
235 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
236 else if (blim.rlim_max == blim.rlim_cur)
237 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
238 else
239 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
241 return copyout(&slim, SCARG_P32(uap, rlp),
242 sizeof(slim));
247 svr4_32_sys_setrlimit64(struct lwp *l, const struct svr4_32_sys_setrlimit64_args *uap, register_t *retval)
249 struct proc *p = l->l_proc;
250 int rl = svr4_to_native_rl(SCARG(uap, which));
251 struct rlimit blim, *limp;
252 struct svr4_rlimit64 slim;
253 int error;
255 if (rl == -1)
256 return EINVAL;
258 limp = &p->p_rlimit[rl];
260 if ((error = copyin(SCARG_P32(uap, rlp),
261 &slim, sizeof(slim))) != 0)
262 return error;
265 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
266 * unlimited.
267 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
268 * new limit to the corresponding saved hard limit, and if
269 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
270 * corresponding saved soft limit.
273 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
274 blim.rlim_max = RLIM_INFINITY;
275 else if (OKLIMIT64(slim.rlim_max))
276 blim.rlim_max = (rlim_t) slim.rlim_max;
277 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
278 blim.rlim_max = limp->rlim_max;
279 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
280 blim.rlim_max = limp->rlim_cur;
282 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
283 blim.rlim_cur = RLIM_INFINITY;
284 else if (OKLIMIT64(slim.rlim_cur))
285 blim.rlim_cur = (rlim_t) slim.rlim_cur;
286 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
287 blim.rlim_cur = limp->rlim_max;
288 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
289 blim.rlim_cur = limp->rlim_cur;
291 return dosetrlimit(l, p, rl, &blim);