1 /* $NetBSD: svr4_resource.c,v 1.12 2006/11/16 01:32:44 christos 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_resource.c,v 1.12 2006/11/16 01:32:44 christos 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/svr4_types.h>
50 #include <compat/svr4/svr4_resource.h>
51 #include <compat/svr4/svr4_signal.h>
52 #include <compat/svr4/svr4_lwp.h>
53 #include <compat/svr4/svr4_ucontext.h>
54 #include <compat/svr4/svr4_syscallargs.h>
55 #include <compat/svr4/svr4_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_sys_getrlimit(struct lwp *l, void *v, register_t *retval)
99 {
100 struct svr4_sys_getrlimit_args *uap = v;
101 int rl = svr4_to_native_rl(SCARG(uap, which));
102 struct proc *p = l->l_proc;
103 struct rlimit blim;
104 struct svr4_rlimit slim;
105
106 if (rl == -1)
107 return EINVAL;
108
109 blim = p->p_rlimit[rl];
110
111 /*
112 * Our infinity, is their maxfiles.
113 */
114 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
115 blim.rlim_max = maxfiles;
116
117 /*
118 * If the limit can be be represented, it is returned.
119 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
120 * else return RLIM_SAVED_CUR
121 */
122 if (blim.rlim_max == RLIM_INFINITY)
123 slim.rlim_max = SVR4_RLIM_INFINITY;
124 else if (OKLIMIT(blim.rlim_max))
125 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
126 else
127 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
128
129 if (blim.rlim_cur == RLIM_INFINITY)
130 slim.rlim_cur = SVR4_RLIM_INFINITY;
131 else if (OKLIMIT(blim.rlim_cur))
132 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
133 else if (blim.rlim_max == blim.rlim_cur)
134 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
135 else
136 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
137
138 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
139 }
140
141
142 int
143 svr4_sys_setrlimit(struct lwp *l, void *v, register_t *retval)
144 {
145 struct svr4_sys_setrlimit_args *uap = v;
146 int rl = svr4_to_native_rl(SCARG(uap, which));
147 struct rlimit blim, *limp;
148 struct svr4_rlimit slim;
149 int error;
150
151 if (rl == -1)
152 return EINVAL;
153
154 limp = &l->l_proc->p_rlimit[rl];
155
156 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
157 return error;
158
159 /*
160 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
161 * unlimited.
162 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
163 * new limit to the corresponding saved hard limit, and if
164 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
165 * corresponding saved soft limit.
166 *
167 */
168 if (slim.rlim_max == SVR4_RLIM_INFINITY)
169 blim.rlim_max = RLIM_INFINITY;
170 else if (OKLIMIT(slim.rlim_max))
171 blim.rlim_max = (rlim_t) slim.rlim_max;
172 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
173 blim.rlim_max = limp->rlim_max;
174 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
175 blim.rlim_max = limp->rlim_cur;
176
177 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
178 blim.rlim_cur = RLIM_INFINITY;
179 else if (OKLIMIT(slim.rlim_cur))
180 blim.rlim_cur = (rlim_t) slim.rlim_cur;
181 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
182 blim.rlim_cur = limp->rlim_max;
183 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
184 blim.rlim_cur = limp->rlim_cur;
185
186 return dosetrlimit(l, l->l_proc, rl, &blim);
187 }
188
189
190 int
191 svr4_sys_getrlimit64(struct lwp *l, void *v, register_t *retval)
192 {
193 struct svr4_sys_getrlimit64_args *uap = v;
194 int rl = svr4_to_native_rl(SCARG(uap, which));
195 struct proc *p = l->l_proc;
196 struct rlimit blim;
197 struct svr4_rlimit64 slim;
198
199 if (rl == -1)
200 return EINVAL;
201
202 blim = p->p_rlimit[rl];
203
204 /*
205 * Our infinity, is their maxfiles.
206 */
207 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
208 blim.rlim_max = maxfiles;
209
210 /*
211 * If the limit can be be represented, it is returned.
212 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
213 * else return SVR4_RLIM_SAVED_CUR
214 */
215 if (blim.rlim_max == RLIM_INFINITY)
216 slim.rlim_max = SVR4_RLIM64_INFINITY;
217 else if (OKLIMIT64(blim.rlim_max))
218 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
219 else
220 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
221
222 if (blim.rlim_cur == RLIM_INFINITY)
223 slim.rlim_cur = SVR4_RLIM64_INFINITY;
224 else if (OKLIMIT64(blim.rlim_cur))
225 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
226 else if (blim.rlim_max == blim.rlim_cur)
227 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
228 else
229 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
230
231 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
232 }
233
234
235 int
236 svr4_sys_setrlimit64(struct lwp *l, void *v, register_t *retval)
237 {
238 struct svr4_sys_setrlimit64_args *uap = v;
239 int rl = svr4_to_native_rl(SCARG(uap, which));
240 struct rlimit blim, *limp;
241 struct svr4_rlimit64 slim;
242 int error;
243
244 if (rl == -1)
245 return EINVAL;
246
247 limp = &l->l_proc->p_rlimit[rl];
248
249 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
250 return error;
251
252 /*
253 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
254 * unlimited.
255 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
256 * new limit to the corresponding saved hard limit, and if
257 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
258 * corresponding saved soft limit.
259 *
260 */
261 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
262 blim.rlim_max = RLIM_INFINITY;
263 else if (OKLIMIT64(slim.rlim_max))
264 blim.rlim_max = (rlim_t) slim.rlim_max;
265 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
266 blim.rlim_max = limp->rlim_max;
267 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
268 blim.rlim_max = limp->rlim_cur;
269
270 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
271 blim.rlim_cur = RLIM_INFINITY;
272 else if (OKLIMIT64(slim.rlim_cur))
273 blim.rlim_cur = (rlim_t) slim.rlim_cur;
274 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
275 blim.rlim_cur = limp->rlim_max;
276 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
277 blim.rlim_cur = limp->rlim_cur;
278
279 return dosetrlimit(l, l->l_proc, rl, &blim);
280 }
Cache object: 63b413e19a15460a792c09ba2db55f5c
|