1 /* $NetBSD: svr4_32_resource.c,v 1.16 2009/01/11 13:14:15 nakayama Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
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);
55 svr4_to_native_rl(int rl
)
60 case SVR4_RLIMIT_FSIZE
:
62 case SVR4_RLIMIT_DATA
:
64 case SVR4_RLIMIT_STACK
:
66 case SVR4_RLIMIT_CORE
:
68 case SVR4_RLIMIT_NOFILE
:
70 case SVR4_RLIMIT_VMEM
:
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)
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
));
97 struct svr4_rlimit slim
;
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
;
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
;
129 slim
.rlim_cur
= SVR4_RLIM_SAVED_CUR
;
133 if (blim
.rlim_cur
> MAXDSIZ32
)
134 blim
.rlim_cur
= MAXDSIZ32
;
135 if (blim
.rlim_max
> MAXDSIZ32
)
136 blim
.rlim_max
= MAXDSIZ32
;
140 if (blim
.rlim_cur
> MAXSSIZ32
)
141 blim
.rlim_cur
= MAXSSIZ32
;
142 if (blim
.rlim_max
> MAXSSIZ32
)
143 blim
.rlim_max
= MAXSSIZ32
;
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
;
164 limp
= &p
->p_rlimit
[rl
];
166 if ((error
= copyin(SCARG_P32(uap
, rlp
), &slim
,
171 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
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
));
207 struct svr4_rlimit64 slim
;
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
;
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
;
239 slim
.rlim_cur
= SVR4_RLIM64_SAVED_CUR
;
241 return copyout(&slim
, SCARG_P32(uap
, rlp
),
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
;
258 limp
= &p
->p_rlimit
[rl
];
260 if ((error
= copyin(SCARG_P32(uap
, rlp
),
261 &slim
, sizeof(slim
))) != 0)
265 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
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
);