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