kern_resource.c revision 1.1.1.3 1 /*-
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
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 University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_resource.c 8.8 (Berkeley) 2/14/95
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/file.h>
45 #include <sys/resourcevar.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48
49 #include <sys/mount.h>
50 #include <sys/syscallargs.h>
51
52 #include <vm/vm.h>
53
54 int donice __P((struct proc *curp, struct proc *chgp, int n));
55 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
56
57 /*
58 * Resource controls and accounting.
59 */
60
61 int
62 getpriority(curp, uap, retval)
63 struct proc *curp;
64 register struct getpriority_args /* {
65 syscallarg(int) which;
66 syscallarg(int) who;
67 } */ *uap;
68 register_t *retval;
69 {
70 register struct proc *p;
71 register int low = PRIO_MAX + 1;
72
73 switch (SCARG(uap, which)) {
74
75 case PRIO_PROCESS:
76 if (SCARG(uap, who) == 0)
77 p = curp;
78 else
79 p = pfind(SCARG(uap, who));
80 if (p == 0)
81 break;
82 low = p->p_nice;
83 break;
84
85 case PRIO_PGRP: {
86 register struct pgrp *pg;
87
88 if (SCARG(uap, who) == 0)
89 pg = curp->p_pgrp;
90 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
91 break;
92 for (p = pg->pg_members.lh_first; p != 0;
93 p = p->p_pglist.le_next) {
94 if (p->p_nice < low)
95 low = p->p_nice;
96 }
97 break;
98 }
99
100 case PRIO_USER:
101 if (SCARG(uap, who) == 0)
102 SCARG(uap, who) = curp->p_ucred->cr_uid;
103 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
104 if (p->p_ucred->cr_uid == SCARG(uap, who) &&
105 p->p_nice < low)
106 low = p->p_nice;
107 break;
108
109 default:
110 return (EINVAL);
111 }
112 if (low == PRIO_MAX + 1)
113 return (ESRCH);
114 *retval = low;
115 return (0);
116 }
117
118 /* ARGSUSED */
119 int
120 setpriority(curp, uap, retval)
121 struct proc *curp;
122 register struct setpriority_args /* {
123 syscallarg(int) which;
124 syscallarg(int) who;
125 syscallarg(int) prio;
126 } */ *uap;
127 register_t *retval;
128 {
129 register struct proc *p;
130 int found = 0, error = 0;
131
132 switch (SCARG(uap, which)) {
133
134 case PRIO_PROCESS:
135 if (SCARG(uap, who) == 0)
136 p = curp;
137 else
138 p = pfind(SCARG(uap, who));
139 if (p == 0)
140 break;
141 error = donice(curp, p, SCARG(uap, prio));
142 found++;
143 break;
144
145 case PRIO_PGRP: {
146 register struct pgrp *pg;
147
148 if (SCARG(uap, who) == 0)
149 pg = curp->p_pgrp;
150 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
151 break;
152 for (p = pg->pg_members.lh_first; p != 0;
153 p = p->p_pglist.le_next) {
154 error = donice(curp, p, SCARG(uap, prio));
155 found++;
156 }
157 break;
158 }
159
160 case PRIO_USER:
161 if (SCARG(uap, who) == 0)
162 SCARG(uap, who) = curp->p_ucred->cr_uid;
163 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
164 if (p->p_ucred->cr_uid == SCARG(uap, who)) {
165 error = donice(curp, p, SCARG(uap, prio));
166 found++;
167 }
168 break;
169
170 default:
171 return (EINVAL);
172 }
173 if (found == 0)
174 return (ESRCH);
175 return (error);
176 }
177
178 int
179 donice(curp, chgp, n)
180 register struct proc *curp, *chgp;
181 register int n;
182 {
183 register struct pcred *pcred = curp->p_cred;
184
185 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
186 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
187 pcred->p_ruid != chgp->p_ucred->cr_uid)
188 return (EPERM);
189 if (n > PRIO_MAX)
190 n = PRIO_MAX;
191 if (n < PRIO_MIN)
192 n = PRIO_MIN;
193 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
194 return (EACCES);
195 chgp->p_nice = n;
196 (void)resetpriority(chgp);
197 return (0);
198 }
199
200 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
201 /* ARGSUSED */
202 int
203 compat_43_setrlimit(p, uap, retval)
204 struct proc *p;
205 struct compat_43_setrlimit_args /* {
206 syscallarg(u_int) which;
207 syscallarg(struct ogetrlimit *) rlp;
208 } */ *uap;
209 register_t *retval;
210 {
211 struct orlimit olim;
212 struct rlimit lim;
213 int error;
214
215 if (error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&olim,
216 sizeof (struct orlimit)))
217 return (error);
218 lim.rlim_cur = olim.rlim_cur;
219 lim.rlim_max = olim.rlim_max;
220 return (dosetrlimit(p, SCARG(uap, which), &lim));
221 }
222
223 /* ARGSUSED */
224 int
225 compat_43_getrlimit(p, uap, retval)
226 struct proc *p;
227 register struct compat_43_getrlimit_args /* {
228 syscallarg(u_int) which;
229 syscallarg(struct ogetrlimit *) rlp;
230 } */ *uap;
231 register_t *retval;
232 {
233 struct orlimit olim;
234
235 if (SCARG(uap, which) >= RLIM_NLIMITS)
236 return (EINVAL);
237 olim.rlim_cur = p->p_rlimit[SCARG(uap, which)].rlim_cur;
238 if (olim.rlim_cur == -1)
239 olim.rlim_cur = 0x7fffffff;
240 olim.rlim_max = p->p_rlimit[SCARG(uap, which)].rlim_max;
241 if (olim.rlim_max == -1)
242 olim.rlim_max = 0x7fffffff;
243 return (copyout((caddr_t)&olim, (caddr_t)SCARG(uap, rlp),
244 sizeof(olim)));
245 }
246 #endif /* COMPAT_43 || COMPAT_SUNOS */
247
248 /* ARGSUSED */
249 int
250 setrlimit(p, uap, retval)
251 struct proc *p;
252 register struct setrlimit_args /* {
253 syscallarg(u_int) which;
254 syscallarg(struct rlimit *) rlp;
255 } */ *uap;
256 register_t *retval;
257 {
258 struct rlimit alim;
259 int error;
260
261 if (error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&alim,
262 sizeof (struct rlimit)))
263 return (error);
264 return (dosetrlimit(p, SCARG(uap, which), &alim));
265 }
266
267 int
268 dosetrlimit(p, which, limp)
269 struct proc *p;
270 u_int which;
271 struct rlimit *limp;
272 {
273 register struct rlimit *alimp;
274 extern unsigned maxdmap;
275 int error;
276
277 if (which >= RLIM_NLIMITS)
278 return (EINVAL);
279 alimp = &p->p_rlimit[which];
280 if (limp->rlim_cur > alimp->rlim_max ||
281 limp->rlim_max > alimp->rlim_max)
282 if (error = suser(p->p_ucred, &p->p_acflag))
283 return (error);
284 if (limp->rlim_cur > limp->rlim_max)
285 limp->rlim_cur = limp->rlim_max;
286 if (p->p_limit->p_refcnt > 1 &&
287 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
288 p->p_limit->p_refcnt--;
289 p->p_limit = limcopy(p->p_limit);
290 alimp = &p->p_rlimit[which];
291 }
292
293 switch (which) {
294
295 case RLIMIT_DATA:
296 if (limp->rlim_cur > maxdmap)
297 limp->rlim_cur = maxdmap;
298 if (limp->rlim_max > maxdmap)
299 limp->rlim_max = maxdmap;
300 break;
301
302 case RLIMIT_STACK:
303 if (limp->rlim_cur > maxdmap)
304 limp->rlim_cur = maxdmap;
305 if (limp->rlim_max > maxdmap)
306 limp->rlim_max = maxdmap;
307 /*
308 * Stack is allocated to the max at exec time with only
309 * "rlim_cur" bytes accessible. If stack limit is going
310 * up make more accessible, if going down make inaccessible.
311 */
312 if (limp->rlim_cur != alimp->rlim_cur) {
313 vm_offset_t addr;
314 vm_size_t size;
315 vm_prot_t prot;
316
317 if (limp->rlim_cur > alimp->rlim_cur) {
318 prot = VM_PROT_ALL;
319 size = limp->rlim_cur - alimp->rlim_cur;
320 addr = USRSTACK - limp->rlim_cur;
321 } else {
322 prot = VM_PROT_NONE;
323 size = alimp->rlim_cur - limp->rlim_cur;
324 addr = USRSTACK - alimp->rlim_cur;
325 }
326 addr = trunc_page(addr);
327 size = round_page(size);
328 (void) vm_map_protect(&p->p_vmspace->vm_map,
329 addr, addr+size, prot, FALSE);
330 }
331 break;
332
333 case RLIMIT_NOFILE:
334 if (limp->rlim_cur > maxfiles)
335 limp->rlim_cur = maxfiles;
336 if (limp->rlim_max > maxfiles)
337 limp->rlim_max = maxfiles;
338 break;
339
340 case RLIMIT_NPROC:
341 if (limp->rlim_cur > maxproc)
342 limp->rlim_cur = maxproc;
343 if (limp->rlim_max > maxproc)
344 limp->rlim_max = maxproc;
345 break;
346 }
347 *alimp = *limp;
348 return (0);
349 }
350
351 /* ARGSUSED */
352 int
353 getrlimit(p, uap, retval)
354 struct proc *p;
355 register struct getrlimit_args /* {
356 syscallarg(u_int) which;
357 syscallarg(struct rlimit *) rlp;
358 } */ *uap;
359 register_t *retval;
360 {
361
362 if (SCARG(uap, which) >= RLIM_NLIMITS)
363 return (EINVAL);
364 return (copyout((caddr_t)&p->p_rlimit[SCARG(uap, which)],
365 (caddr_t)SCARG(uap, rlp), sizeof (struct rlimit)));
366 }
367
368 /*
369 * Transform the running time and tick information in proc p into user,
370 * system, and interrupt time usage.
371 */
372 void
373 calcru(p, up, sp, ip)
374 register struct proc *p;
375 register struct timeval *up;
376 register struct timeval *sp;
377 register struct timeval *ip;
378 {
379 register u_quad_t u, st, ut, it, tot;
380 register u_long sec, usec;
381 register int s;
382 struct timeval tv;
383
384 s = splstatclock();
385 st = p->p_sticks;
386 ut = p->p_uticks;
387 it = p->p_iticks;
388 splx(s);
389
390 tot = st + ut + it;
391 if (tot == 0) {
392 up->tv_sec = up->tv_usec = 0;
393 sp->tv_sec = sp->tv_usec = 0;
394 if (ip != NULL)
395 ip->tv_sec = ip->tv_usec = 0;
396 return;
397 }
398
399 sec = p->p_rtime.tv_sec;
400 usec = p->p_rtime.tv_usec;
401 if (p == curproc) {
402 /*
403 * Adjust for the current time slice. This is actually fairly
404 * important since the error here is on the order of a time
405 * quantum, which is much greater than the sampling error.
406 */
407 microtime(&tv);
408 sec += tv.tv_sec - runtime.tv_sec;
409 usec += tv.tv_usec - runtime.tv_usec;
410 }
411 u = sec * 1000000 + usec;
412 st = (u * st) / tot;
413 sp->tv_sec = st / 1000000;
414 sp->tv_usec = st % 1000000;
415 ut = (u * ut) / tot;
416 up->tv_sec = ut / 1000000;
417 up->tv_usec = ut % 1000000;
418 if (ip != NULL) {
419 it = (u * it) / tot;
420 ip->tv_sec = it / 1000000;
421 ip->tv_usec = it % 1000000;
422 }
423 }
424
425 /* ARGSUSED */
426 int
427 getrusage(p, uap, retval)
428 register struct proc *p;
429 register struct getrusage_args /* {
430 syscallarg(int) who;
431 syscallarg(struct rusage *) rusage;
432 } */ *uap;
433 register_t *retval;
434 {
435 register struct rusage *rup;
436
437 switch (SCARG(uap, who)) {
438
439 case RUSAGE_SELF:
440 rup = &p->p_stats->p_ru;
441 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
442 break;
443
444 case RUSAGE_CHILDREN:
445 rup = &p->p_stats->p_cru;
446 break;
447
448 default:
449 return (EINVAL);
450 }
451 return (copyout((caddr_t)rup, (caddr_t)SCARG(uap, rusage),
452 sizeof (struct rusage)));
453 }
454
455 void
456 ruadd(ru, ru2)
457 register struct rusage *ru, *ru2;
458 {
459 register long *ip, *ip2;
460 register int i;
461
462 timevaladd(&ru->ru_utime, &ru2->ru_utime);
463 timevaladd(&ru->ru_stime, &ru2->ru_stime);
464 if (ru->ru_maxrss < ru2->ru_maxrss)
465 ru->ru_maxrss = ru2->ru_maxrss;
466 ip = &ru->ru_first; ip2 = &ru2->ru_first;
467 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
468 *ip++ += *ip2++;
469 }
470
471 /*
472 * Make a copy of the plimit structure.
473 * We share these structures copy-on-write after fork,
474 * and copy when a limit is changed.
475 */
476 struct plimit *
477 limcopy(lim)
478 struct plimit *lim;
479 {
480 register struct plimit *copy;
481
482 MALLOC(copy, struct plimit *, sizeof(struct plimit),
483 M_SUBPROC, M_WAITOK);
484 bcopy(lim->pl_rlimit, copy->pl_rlimit,
485 sizeof(struct rlimit) * RLIM_NLIMITS);
486 copy->p_lflags = 0;
487 copy->p_refcnt = 1;
488 return (copy);
489 }
490