Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / emulation / svr4 / svr4_resource.c
1 /*      Derived from:
2  *      $NetBSD: svr4_resource.c,v 1.3 1998/12/13 18:00:52 christos Exp $
3  */
4
5 /*-
6  * Original copyright:
7  *
8  * Copyright (c) 1998 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Christos Zoulas.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *        This product includes software developed by the NetBSD
25  *        Foundation, Inc. and its contributors.
26  * 4. Neither the name of The NetBSD Foundation nor the names of its
27  *    contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  * 
42  * $FreeBSD: src/sys/svr4/svr4_resource.c,v 1.4.2.1 2002/09/02 21:22:54 dillon Exp $
43  * $DragonFly: src/sys/emulation/svr4/Attic/svr4_resource.c,v 1.2 2003/06/17 04:28:57 dillon Exp $
44  */
45
46 /*
47  * Portions of this software have been derived from software contributed
48  * to the FreeBSD Project by Mark Newton.
49  *
50  * Copyright (c) 1999 Mark Newton
51  * All rights reserved.
52  * 
53  * Redistribution and use in source and binary forms, with or without
54  * modification, are permitted provided that the following conditions
55  * are met:
56  * 1. Redistributions of source code must retain the above copyright
57  *    notice, this list of conditions and the following disclaimer.
58  * 2. Redistributions in binary form must reproduce the above copyright
59  *    notice, this list of conditions and the following disclaimer in the
60  *    documentation and/or other materials provided with the distribution.
61  * 3. The name of the author may not be used to endorse or promote products
62  *    derived from this software without specific prior written permission
63  *
64  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
65  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
66  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
67  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
68  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
69  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
73  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74  */
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79 #include <sys/file.h>
80 #include <sys/resource.h>
81 #include <sys/resourcevar.h>
82
83 #include <svr4/svr4.h>
84 #include <svr4/svr4_types.h>
85 #include <svr4/svr4_resource.h>
86 #include <svr4/svr4_signal.h>
87 #include <svr4/svr4_proto.h>
88 #include <svr4/svr4_util.h>
89
90 static __inline int svr4_to_native_rl __P((int));
91
92 static __inline int
93 svr4_to_native_rl(rl)
94         int rl;
95 {
96         switch (rl) {
97         case SVR4_RLIMIT_CPU:
98                 return RLIMIT_CPU;
99         case SVR4_RLIMIT_FSIZE:
100                 return RLIMIT_FSIZE;
101         case SVR4_RLIMIT_DATA:
102                 return RLIMIT_DATA;
103         case SVR4_RLIMIT_STACK:
104                 return RLIMIT_STACK;
105         case SVR4_RLIMIT_CORE:
106                 return RLIMIT_CORE;
107         case SVR4_RLIMIT_NOFILE:
108                 return RLIMIT_NOFILE;
109         case SVR4_RLIMIT_VMEM:
110                 return RLIMIT_VMEM;
111         default:
112                 return -1;
113         }
114 }
115
116 /*
117  * Check if the resource limit fits within the BSD range and it is not
118  * one of the magic SVR4 limit values
119  */
120 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
121         ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
122         ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
123         ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
124
125 #define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
126         ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
127         ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
128         ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
129
130 int
131 svr4_sys_getrlimit(p, uap)
132         register struct proc *p;
133         struct svr4_sys_getrlimit_args *uap;
134 {
135         int rl = svr4_to_native_rl(SCARG(uap, which));
136         struct rlimit blim;
137         struct svr4_rlimit slim;
138
139         if (rl == -1)
140                 return EINVAL;
141
142         blim = p->p_rlimit[rl];
143
144         /*
145          * Our infinity, is their maxfiles.
146          */
147         if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
148                 blim.rlim_max = maxfiles;
149
150         /*
151          * If the limit can be be represented, it is returned.
152          * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
153          * else return RLIM_SAVED_CUR
154          */
155         if (blim.rlim_max == RLIM_INFINITY)
156                 slim.rlim_max = SVR4_RLIM_INFINITY;
157         else if (OKLIMIT(blim.rlim_max))
158                 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
159         else
160                 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
161
162         if (blim.rlim_cur == RLIM_INFINITY)
163                 slim.rlim_cur = SVR4_RLIM_INFINITY;
164         else if (OKLIMIT(blim.rlim_cur))
165                 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
166         else if (blim.rlim_max == blim.rlim_cur)
167                 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
168         else
169                 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
170
171         return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
172 }
173
174
175 int
176 svr4_sys_setrlimit(p, uap)
177         register struct proc *p;
178         struct svr4_sys_setrlimit_args *uap;
179 {
180         int rl = svr4_to_native_rl(SCARG(uap, which));
181         struct rlimit blim, *limp;
182         struct svr4_rlimit slim;
183         int error;
184
185         if (rl == -1)
186                 return EINVAL;
187
188         limp = &p->p_rlimit[rl];
189
190         if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
191                 return error;
192
193         /*
194          * if the limit is SVR4_RLIM_INFINITY, then we set it to our
195          * unlimited.
196          * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
197          * new limit to the corresponding saved hard limit, and if
198          * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
199          * corresponding saved soft limit.
200          *
201          */
202         if (slim.rlim_max == SVR4_RLIM_INFINITY)
203                 blim.rlim_max = RLIM_INFINITY;
204         else if (OKLIMIT(slim.rlim_max))
205                 blim.rlim_max = (rlim_t) slim.rlim_max;
206         else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
207                 blim.rlim_max = limp->rlim_max;
208         else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
209                 blim.rlim_max = limp->rlim_cur;
210
211         if (slim.rlim_cur == SVR4_RLIM_INFINITY)
212                 blim.rlim_cur = RLIM_INFINITY;
213         else if (OKLIMIT(slim.rlim_cur))
214                 blim.rlim_cur = (rlim_t) slim.rlim_cur;
215         else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
216                 blim.rlim_cur = limp->rlim_max;
217         else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
218                 blim.rlim_cur = limp->rlim_cur;
219
220         return dosetrlimit(p, rl, &blim);
221 }
222
223
224 int
225 svr4_sys_getrlimit64(p, uap)
226         register struct proc *p;
227         struct svr4_sys_getrlimit64_args *uap;
228 {
229         int rl = svr4_to_native_rl(SCARG(uap, which));
230         struct rlimit blim;
231         struct svr4_rlimit64 slim;
232
233         if (rl == -1)
234                 return EINVAL;
235
236         blim = p->p_rlimit[rl];
237
238         /*
239          * Our infinity, is their maxfiles.
240          */
241         if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
242                 blim.rlim_max = maxfiles;
243
244         /*
245          * If the limit can be be represented, it is returned.
246          * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
247          * else return SVR4_RLIM_SAVED_CUR
248          */
249         if (blim.rlim_max == RLIM_INFINITY)
250                 slim.rlim_max = SVR4_RLIM64_INFINITY;
251         else if (OKLIMIT64(blim.rlim_max))
252                 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
253         else
254                 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
255
256         if (blim.rlim_cur == RLIM_INFINITY)
257                 slim.rlim_cur = SVR4_RLIM64_INFINITY;
258         else if (OKLIMIT64(blim.rlim_cur))
259                 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
260         else if (blim.rlim_max == blim.rlim_cur)
261                 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
262         else
263                 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
264
265         return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
266 }
267
268
269 int
270 svr4_sys_setrlimit64(p, uap)
271         register struct proc *p;
272         struct svr4_sys_setrlimit64_args *uap;
273 {
274         int rl = svr4_to_native_rl(SCARG(uap, which));
275         struct rlimit blim, *limp;
276         struct svr4_rlimit64 slim;
277         int error;
278
279         if (rl == -1)
280                 return EINVAL;
281
282         limp = &p->p_rlimit[rl];
283
284         if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
285                 return error;
286
287         /*
288          * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
289          * unlimited.
290          * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
291          * new limit to the corresponding saved hard limit, and if
292          * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
293          * corresponding saved soft limit.
294          *
295          */
296         if (slim.rlim_max == SVR4_RLIM64_INFINITY)
297                 blim.rlim_max = RLIM_INFINITY;
298         else if (OKLIMIT64(slim.rlim_max))
299                 blim.rlim_max = (rlim_t) slim.rlim_max;
300         else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
301                 blim.rlim_max = limp->rlim_max;
302         else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
303                 blim.rlim_max = limp->rlim_cur;
304
305         if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
306                 blim.rlim_cur = RLIM_INFINITY;
307         else if (OKLIMIT64(slim.rlim_cur))
308                 blim.rlim_cur = (rlim_t) slim.rlim_cur;
309         else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
310                 blim.rlim_cur = limp->rlim_max;
311         else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
312                 blim.rlim_cur = limp->rlim_cur;
313
314         return dosetrlimit(p, rl, &blim);
315 }