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