1 /* $NetBSD: svr4_resource.c,v 1.16.10.1 2009/04/01 00:25:22 snj 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 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: svr4_resource.c,v 1.16.10.1 2009/04/01 00:25:22 snj Exp $");
34
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>
41
42 #include <compat/svr4/svr4_types.h>
43 #include <compat/svr4/svr4_resource.h>
44 #include <compat/svr4/svr4_signal.h>
45 #include <compat/svr4/svr4_lwp.h>
46 #include <compat/svr4/svr4_ucontext.h>
47 #include <compat/svr4/svr4_syscallargs.h>
48 #include <compat/svr4/svr4_util.h>
49
50 static inline int svr4_to_native_rl(int);
51
52 static inline int
53 svr4_to_native_rl(int rl)
54 {
55 switch (rl) {
56 case SVR4_RLIMIT_CPU:
57 return RLIMIT_CPU;
58 case SVR4_RLIMIT_FSIZE:
59 return RLIMIT_FSIZE;
60 case SVR4_RLIMIT_DATA:
61 return RLIMIT_DATA;
62 case SVR4_RLIMIT_STACK:
63 return RLIMIT_STACK;
64 case SVR4_RLIMIT_CORE:
65 return RLIMIT_CORE;
66 case SVR4_RLIMIT_NOFILE:
67 return RLIMIT_NOFILE;
68 case SVR4_RLIMIT_VMEM:
69 return RLIMIT_AS;
70 default:
71 return -1;
72 }
73 }
74
75 /*
76 * Check if the resource limit fits within the BSD range and it is not
77 * one of the magic SVR4 limit values
78 */
79 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
80 ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
81 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
82 ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
83
84 #define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
85 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
86 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
87 ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
88
89 int
90 svr4_sys_getrlimit(struct lwp *l, const struct svr4_sys_getrlimit_args *uap, register_t *retval)
91 {
92 int rl = svr4_to_native_rl(SCARG(uap, which));
93 struct proc *p = l->l_proc;
94 struct rlimit blim;
95 struct svr4_rlimit slim;
96
97 if (rl == -1)
98 return EINVAL;
99
100 blim = p->p_rlimit[rl];
101
102 /*
103 * Our infinity, is their maxfiles.
104 */
105 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
106 blim.rlim_max = maxfiles;
107
108 /*
109 * If the limit can be be represented, it is returned.
110 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
111 * else return RLIM_SAVED_CUR
112 */
113 if (blim.rlim_max == RLIM_INFINITY)
114 slim.rlim_max = SVR4_RLIM_INFINITY;
115 else if (OKLIMIT(blim.rlim_max))
116 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
117 else
118 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
119
120 if (blim.rlim_cur == RLIM_INFINITY)
121 slim.rlim_cur = SVR4_RLIM_INFINITY;
122 else if (OKLIMIT(blim.rlim_cur))
123 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
124 else if (blim.rlim_max == blim.rlim_cur)
125 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
126 else
127 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
128
129 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
130 }
131
132
133 int
134 svr4_sys_setrlimit(struct lwp *l, const struct svr4_sys_setrlimit_args *uap, register_t *retval)
135 {
136 int rl = svr4_to_native_rl(SCARG(uap, which));
137 struct rlimit blim, *limp;
138 struct svr4_rlimit slim;
139 int error;
140
141 if (rl == -1)
142 return EINVAL;
143
144 limp = &l->l_proc->p_rlimit[rl];
145
146 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
147 return error;
148
149 /*
150 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
151 * unlimited.
152 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
153 * new limit to the corresponding saved hard limit, and if
154 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
155 * corresponding saved soft limit.
156 *
157 */
158 if (slim.rlim_max == SVR4_RLIM_INFINITY)
159 blim.rlim_max = RLIM_INFINITY;
160 else if (OKLIMIT(slim.rlim_max))
161 blim.rlim_max = (rlim_t) slim.rlim_max;
162 else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
163 blim.rlim_max = limp->rlim_max;
164 else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
165 blim.rlim_max = limp->rlim_cur;
166
167 if (slim.rlim_cur == SVR4_RLIM_INFINITY)
168 blim.rlim_cur = RLIM_INFINITY;
169 else if (OKLIMIT(slim.rlim_cur))
170 blim.rlim_cur = (rlim_t) slim.rlim_cur;
171 else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
172 blim.rlim_cur = limp->rlim_max;
173 else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
174 blim.rlim_cur = limp->rlim_cur;
175
176 return dosetrlimit(l, l->l_proc, rl, &blim);
177 }
178
179
180 int
181 svr4_sys_getrlimit64(struct lwp *l, const struct svr4_sys_getrlimit64_args *uap, register_t *retval)
182 {
183 int rl = svr4_to_native_rl(SCARG(uap, which));
184 struct proc *p = l->l_proc;
185 struct rlimit blim;
186 struct svr4_rlimit64 slim;
187
188 if (rl == -1)
189 return EINVAL;
190
191 blim = p->p_rlimit[rl];
192
193 /*
194 * Our infinity, is their maxfiles.
195 */
196 if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
197 blim.rlim_max = maxfiles;
198
199 /*
200 * If the limit can be be represented, it is returned.
201 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
202 * else return SVR4_RLIM_SAVED_CUR
203 */
204 if (blim.rlim_max == RLIM_INFINITY)
205 slim.rlim_max = SVR4_RLIM64_INFINITY;
206 else if (OKLIMIT64(blim.rlim_max))
207 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
208 else
209 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
210
211 if (blim.rlim_cur == RLIM_INFINITY)
212 slim.rlim_cur = SVR4_RLIM64_INFINITY;
213 else if (OKLIMIT64(blim.rlim_cur))
214 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
215 else if (blim.rlim_max == blim.rlim_cur)
216 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
217 else
218 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
219
220 return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
221 }
222
223
224 int
225 svr4_sys_setrlimit64(struct lwp *l, const struct svr4_sys_setrlimit64_args *uap, register_t *retval)
226 {
227 int rl = svr4_to_native_rl(SCARG(uap, which));
228 struct rlimit blim, *limp;
229 struct svr4_rlimit64 slim;
230 int error;
231
232 if (rl == -1)
233 return EINVAL;
234
235 limp = &l->l_proc->p_rlimit[rl];
236
237 if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
238 return error;
239
240 /*
241 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
242 * unlimited.
243 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
244 * new limit to the corresponding saved hard limit, and if
245 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
246 * corresponding saved soft limit.
247 *
248 */
249 if (slim.rlim_max == SVR4_RLIM64_INFINITY)
250 blim.rlim_max = RLIM_INFINITY;
251 else if (OKLIMIT64(slim.rlim_max))
252 blim.rlim_max = (rlim_t) slim.rlim_max;
253 else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
254 blim.rlim_max = limp->rlim_max;
255 else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
256 blim.rlim_max = limp->rlim_cur;
257
258 if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
259 blim.rlim_cur = RLIM_INFINITY;
260 else if (OKLIMIT64(slim.rlim_cur))
261 blim.rlim_cur = (rlim_t) slim.rlim_cur;
262 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
263 blim.rlim_cur = limp->rlim_max;
264 else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
265 blim.rlim_cur = limp->rlim_cur;
266
267 return dosetrlimit(l, l->l_proc, rl, &blim);
268 }
Cache object: ef912933de50f928697188bab686dddb
|