kern_resource.c revision 1.21 1 1.21 mycroft /* $NetBSD: kern_resource.c,v 1.21 1994/08/30 03:05:40 mycroft 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.17 cgd #include <sys/kernel.h>
45 1.19 cgd #include <sys/file.h>
46 1.17 cgd #include <sys/resourcevar.h>
47 1.17 cgd #include <sys/malloc.h>
48 1.17 cgd #include <sys/proc.h>
49 1.17 cgd
50 1.17 cgd #include <vm/vm.h>
51 1.17 cgd
52 1.17 cgd /*
53 1.17 cgd * Resource controls and accounting.
54 1.17 cgd */
55 1.17 cgd
56 1.17 cgd struct getpriority_args {
57 1.17 cgd int which;
58 1.17 cgd int who;
59 1.17 cgd };
60 1.17 cgd getpriority(curp, uap, retval)
61 1.17 cgd struct proc *curp;
62 1.17 cgd register struct getpriority_args *uap;
63 1.17 cgd int *retval;
64 1.17 cgd {
65 1.17 cgd register struct proc *p;
66 1.17 cgd register int low = PRIO_MAX + 1;
67 1.17 cgd
68 1.17 cgd switch (uap->which) {
69 1.17 cgd
70 1.17 cgd case PRIO_PROCESS:
71 1.17 cgd if (uap->who == 0)
72 1.17 cgd p = curp;
73 1.17 cgd else
74 1.17 cgd p = pfind(uap->who);
75 1.17 cgd if (p == 0)
76 1.17 cgd break;
77 1.17 cgd low = p->p_nice;
78 1.17 cgd break;
79 1.17 cgd
80 1.17 cgd case PRIO_PGRP: {
81 1.17 cgd register struct pgrp *pg;
82 1.17 cgd
83 1.17 cgd if (uap->who == 0)
84 1.17 cgd pg = curp->p_pgrp;
85 1.17 cgd else if ((pg = pgfind(uap->who)) == NULL)
86 1.17 cgd break;
87 1.21 mycroft for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
88 1.17 cgd if (p->p_nice < low)
89 1.17 cgd low = p->p_nice;
90 1.17 cgd }
91 1.17 cgd break;
92 1.17 cgd }
93 1.17 cgd
94 1.17 cgd case PRIO_USER:
95 1.17 cgd if (uap->who == 0)
96 1.17 cgd uap->who = curp->p_ucred->cr_uid;
97 1.21 mycroft for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
98 1.21 mycroft if (p->p_ucred->cr_uid == uap->who && p->p_nice < low)
99 1.17 cgd low = p->p_nice;
100 1.17 cgd break;
101 1.17 cgd
102 1.17 cgd default:
103 1.17 cgd return (EINVAL);
104 1.17 cgd }
105 1.17 cgd if (low == PRIO_MAX + 1)
106 1.17 cgd return (ESRCH);
107 1.17 cgd *retval = low;
108 1.17 cgd return (0);
109 1.17 cgd }
110 1.17 cgd
111 1.17 cgd struct setpriority_args {
112 1.17 cgd int which;
113 1.17 cgd int who;
114 1.17 cgd int prio;
115 1.17 cgd };
116 1.17 cgd /* ARGSUSED */
117 1.17 cgd setpriority(curp, uap, retval)
118 1.17 cgd struct proc *curp;
119 1.17 cgd register struct setpriority_args *uap;
120 1.17 cgd int *retval;
121 1.17 cgd {
122 1.17 cgd register struct proc *p;
123 1.17 cgd int found = 0, error = 0;
124 1.17 cgd
125 1.17 cgd switch (uap->which) {
126 1.17 cgd
127 1.17 cgd case PRIO_PROCESS:
128 1.17 cgd if (uap->who == 0)
129 1.17 cgd p = curp;
130 1.17 cgd else
131 1.17 cgd p = pfind(uap->who);
132 1.17 cgd if (p == 0)
133 1.17 cgd break;
134 1.17 cgd error = donice(curp, p, uap->prio);
135 1.17 cgd found++;
136 1.17 cgd break;
137 1.17 cgd
138 1.17 cgd case PRIO_PGRP: {
139 1.17 cgd register struct pgrp *pg;
140 1.17 cgd
141 1.17 cgd if (uap->who == 0)
142 1.17 cgd pg = curp->p_pgrp;
143 1.17 cgd else if ((pg = pgfind(uap->who)) == NULL)
144 1.17 cgd break;
145 1.21 mycroft for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
146 1.17 cgd error = donice(curp, p, uap->prio);
147 1.17 cgd found++;
148 1.17 cgd }
149 1.17 cgd break;
150 1.17 cgd }
151 1.17 cgd
152 1.17 cgd case PRIO_USER:
153 1.17 cgd if (uap->who == 0)
154 1.17 cgd uap->who = curp->p_ucred->cr_uid;
155 1.21 mycroft for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
156 1.17 cgd if (p->p_ucred->cr_uid == uap->who) {
157 1.17 cgd error = donice(curp, p, uap->prio);
158 1.17 cgd found++;
159 1.17 cgd }
160 1.17 cgd break;
161 1.17 cgd
162 1.17 cgd default:
163 1.17 cgd return (EINVAL);
164 1.17 cgd }
165 1.17 cgd if (found == 0)
166 1.17 cgd return (ESRCH);
167 1.17 cgd return (error);
168 1.17 cgd }
169 1.17 cgd
170 1.17 cgd donice(curp, chgp, n)
171 1.17 cgd register struct proc *curp, *chgp;
172 1.17 cgd register int n;
173 1.17 cgd {
174 1.17 cgd register struct pcred *pcred = curp->p_cred;
175 1.17 cgd
176 1.17 cgd if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
177 1.17 cgd pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
178 1.17 cgd pcred->p_ruid != chgp->p_ucred->cr_uid)
179 1.17 cgd return (EPERM);
180 1.17 cgd if (n > PRIO_MAX)
181 1.17 cgd n = PRIO_MAX;
182 1.17 cgd if (n < PRIO_MIN)
183 1.17 cgd n = PRIO_MIN;
184 1.17 cgd if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
185 1.17 cgd return (EACCES);
186 1.17 cgd chgp->p_nice = n;
187 1.19 cgd (void)resetpriority(chgp);
188 1.17 cgd return (0);
189 1.17 cgd }
190 1.17 cgd
191 1.17 cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
192 1.17 cgd struct osetrlimit_args {
193 1.17 cgd u_int which;
194 1.17 cgd struct orlimit *lim;
195 1.17 cgd };
196 1.17 cgd /* ARGSUSED */
197 1.17 cgd osetrlimit(p, uap, retval)
198 1.17 cgd struct proc *p;
199 1.17 cgd struct osetrlimit_args *uap;
200 1.17 cgd int *retval;
201 1.17 cgd {
202 1.17 cgd struct orlimit olim;
203 1.17 cgd struct rlimit lim;
204 1.17 cgd int error;
205 1.17 cgd
206 1.19 cgd if (error =
207 1.19 cgd copyin((caddr_t)uap->lim, (caddr_t)&olim, sizeof (struct orlimit)))
208 1.17 cgd return (error);
209 1.17 cgd lim.rlim_cur = olim.rlim_cur;
210 1.17 cgd lim.rlim_max = olim.rlim_max;
211 1.17 cgd return (dosetrlimit(p, uap->which, &lim));
212 1.17 cgd }
213 1.17 cgd
214 1.17 cgd struct ogetrlimit_args {
215 1.17 cgd u_int which;
216 1.17 cgd struct orlimit *rlp;
217 1.17 cgd };
218 1.17 cgd /* ARGSUSED */
219 1.17 cgd ogetrlimit(p, uap, retval)
220 1.17 cgd struct proc *p;
221 1.17 cgd register struct ogetrlimit_args *uap;
222 1.17 cgd int *retval;
223 1.17 cgd {
224 1.17 cgd struct orlimit olim;
225 1.17 cgd
226 1.17 cgd if (uap->which >= RLIM_NLIMITS)
227 1.17 cgd return (EINVAL);
228 1.19 cgd olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
229 1.17 cgd if (olim.rlim_cur == -1)
230 1.17 cgd olim.rlim_cur = 0x7fffffff;
231 1.17 cgd olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
232 1.17 cgd if (olim.rlim_max == -1)
233 1.17 cgd olim.rlim_max = 0x7fffffff;
234 1.17 cgd return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
235 1.17 cgd }
236 1.19 cgd #endif /* COMPAT_43 || COMPAT_SUNOS */
237 1.17 cgd
238 1.17 cgd struct setrlimit_args {
239 1.17 cgd u_int which;
240 1.17 cgd struct rlimit *lim;
241 1.17 cgd };
242 1.17 cgd /* ARGSUSED */
243 1.17 cgd setrlimit(p, uap, retval)
244 1.17 cgd struct proc *p;
245 1.17 cgd register struct setrlimit_args *uap;
246 1.17 cgd int *retval;
247 1.17 cgd {
248 1.19 cgd struct rlimit alim;
249 1.17 cgd int error;
250 1.17 cgd
251 1.19 cgd if (error =
252 1.19 cgd copyin((caddr_t)uap->lim, (caddr_t)&alim, sizeof (struct rlimit)))
253 1.17 cgd return (error);
254 1.19 cgd return (dosetrlimit(p, uap->which, &alim));
255 1.17 cgd }
256 1.17 cgd
257 1.17 cgd int
258 1.17 cgd dosetrlimit(p, which, limp)
259 1.17 cgd struct proc *p;
260 1.17 cgd u_int which;
261 1.17 cgd struct rlimit *limp;
262 1.17 cgd {
263 1.17 cgd register struct rlimit *alimp;
264 1.19 cgd extern unsigned maxdmap, maxsmap;
265 1.17 cgd int error;
266 1.17 cgd
267 1.17 cgd if (which >= RLIM_NLIMITS)
268 1.17 cgd return (EINVAL);
269 1.17 cgd alimp = &p->p_rlimit[which];
270 1.19 cgd if (limp->rlim_cur > alimp->rlim_max ||
271 1.17 cgd limp->rlim_max > alimp->rlim_max)
272 1.17 cgd if (error = suser(p->p_ucred, &p->p_acflag))
273 1.17 cgd return (error);
274 1.17 cgd if (limp->rlim_cur > limp->rlim_max)
275 1.17 cgd limp->rlim_cur = limp->rlim_max;
276 1.17 cgd if (p->p_limit->p_refcnt > 1 &&
277 1.17 cgd (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
278 1.17 cgd p->p_limit->p_refcnt--;
279 1.17 cgd p->p_limit = limcopy(p->p_limit);
280 1.17 cgd alimp = &p->p_rlimit[which];
281 1.17 cgd }
282 1.17 cgd
283 1.17 cgd switch (which) {
284 1.17 cgd
285 1.17 cgd case RLIMIT_DATA:
286 1.19 cgd if (limp->rlim_cur > maxdmap)
287 1.19 cgd limp->rlim_cur = maxdmap;
288 1.19 cgd if (limp->rlim_max > maxdmap)
289 1.19 cgd limp->rlim_max = maxdmap;
290 1.17 cgd break;
291 1.17 cgd
292 1.17 cgd case RLIMIT_STACK:
293 1.19 cgd if (limp->rlim_cur > maxsmap)
294 1.19 cgd limp->rlim_cur = maxsmap;
295 1.19 cgd if (limp->rlim_max > maxsmap)
296 1.19 cgd limp->rlim_max = maxsmap;
297 1.17 cgd /*
298 1.17 cgd * Stack is allocated to the max at exec time with only
299 1.17 cgd * "rlim_cur" bytes accessible. If stack limit is going
300 1.17 cgd * up make more accessible, if going down make inaccessible.
301 1.17 cgd */
302 1.17 cgd if (limp->rlim_cur != alimp->rlim_cur) {
303 1.17 cgd vm_offset_t addr;
304 1.17 cgd vm_size_t size;
305 1.17 cgd vm_prot_t prot;
306 1.17 cgd
307 1.17 cgd if (limp->rlim_cur > alimp->rlim_cur) {
308 1.17 cgd prot = VM_PROT_ALL;
309 1.17 cgd size = limp->rlim_cur - alimp->rlim_cur;
310 1.17 cgd addr = USRSTACK - limp->rlim_cur;
311 1.17 cgd } else {
312 1.17 cgd prot = VM_PROT_NONE;
313 1.17 cgd size = alimp->rlim_cur - limp->rlim_cur;
314 1.17 cgd addr = USRSTACK - alimp->rlim_cur;
315 1.17 cgd }
316 1.17 cgd addr = trunc_page(addr);
317 1.17 cgd size = round_page(size);
318 1.17 cgd (void) vm_map_protect(&p->p_vmspace->vm_map,
319 1.17 cgd addr, addr+size, prot, FALSE);
320 1.17 cgd }
321 1.17 cgd break;
322 1.19 cgd
323 1.19 cgd case RLIMIT_NOFILE:
324 1.19 cgd if (limp->rlim_cur > maxfiles)
325 1.19 cgd limp->rlim_cur = maxfiles;
326 1.19 cgd if (limp->rlim_max > maxfiles)
327 1.19 cgd limp->rlim_max = maxfiles;
328 1.19 cgd break;
329 1.19 cgd
330 1.19 cgd case RLIMIT_NPROC:
331 1.19 cgd if (limp->rlim_cur > maxproc)
332 1.19 cgd limp->rlim_cur = maxproc;
333 1.19 cgd if (limp->rlim_max > maxproc)
334 1.19 cgd limp->rlim_max = maxproc;
335 1.19 cgd break;
336 1.17 cgd }
337 1.17 cgd *alimp = *limp;
338 1.17 cgd return (0);
339 1.17 cgd }
340 1.17 cgd
341 1.17 cgd struct getrlimit_args {
342 1.17 cgd u_int which;
343 1.17 cgd struct rlimit *rlp;
344 1.17 cgd };
345 1.17 cgd /* ARGSUSED */
346 1.17 cgd getrlimit(p, uap, retval)
347 1.17 cgd struct proc *p;
348 1.17 cgd register struct getrlimit_args *uap;
349 1.17 cgd int *retval;
350 1.17 cgd {
351 1.17 cgd
352 1.17 cgd if (uap->which >= RLIM_NLIMITS)
353 1.17 cgd return (EINVAL);
354 1.17 cgd return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
355 1.17 cgd sizeof (struct rlimit)));
356 1.17 cgd }
357 1.17 cgd
358 1.17 cgd /*
359 1.17 cgd * Transform the running time and tick information in proc p into user,
360 1.17 cgd * system, and interrupt time usage.
361 1.17 cgd */
362 1.17 cgd calcru(p, up, sp, ip)
363 1.17 cgd register struct proc *p;
364 1.17 cgd register struct timeval *up;
365 1.17 cgd register struct timeval *sp;
366 1.17 cgd register struct timeval *ip;
367 1.17 cgd {
368 1.17 cgd register u_quad_t u, st, ut, it, tot;
369 1.17 cgd register u_long sec, usec;
370 1.17 cgd register int s;
371 1.17 cgd struct timeval tv;
372 1.17 cgd
373 1.17 cgd s = splstatclock();
374 1.17 cgd st = p->p_sticks;
375 1.17 cgd ut = p->p_uticks;
376 1.17 cgd it = p->p_iticks;
377 1.17 cgd splx(s);
378 1.17 cgd
379 1.17 cgd tot = st + ut + it;
380 1.17 cgd if (tot == 0) {
381 1.17 cgd up->tv_sec = up->tv_usec = 0;
382 1.17 cgd sp->tv_sec = sp->tv_usec = 0;
383 1.17 cgd if (ip != NULL)
384 1.17 cgd ip->tv_sec = ip->tv_usec = 0;
385 1.17 cgd return;
386 1.17 cgd }
387 1.17 cgd
388 1.17 cgd sec = p->p_rtime.tv_sec;
389 1.17 cgd usec = p->p_rtime.tv_usec;
390 1.17 cgd if (p == curproc) {
391 1.17 cgd /*
392 1.17 cgd * Adjust for the current time slice. This is actually fairly
393 1.17 cgd * important since the error here is on the order of a time
394 1.17 cgd * quantum, which is much greater than the sampling error.
395 1.17 cgd */
396 1.17 cgd microtime(&tv);
397 1.17 cgd sec += tv.tv_sec - runtime.tv_sec;
398 1.17 cgd usec += tv.tv_usec - runtime.tv_usec;
399 1.17 cgd }
400 1.17 cgd u = sec * 1000000 + usec;
401 1.17 cgd st = (u * st) / tot;
402 1.17 cgd sp->tv_sec = st / 1000000;
403 1.17 cgd sp->tv_usec = st % 1000000;
404 1.17 cgd ut = (u * ut) / tot;
405 1.17 cgd up->tv_sec = ut / 1000000;
406 1.17 cgd up->tv_usec = ut % 1000000;
407 1.17 cgd if (ip != NULL) {
408 1.17 cgd it = (u * it) / tot;
409 1.17 cgd ip->tv_sec = it / 1000000;
410 1.17 cgd ip->tv_usec = it % 1000000;
411 1.17 cgd }
412 1.17 cgd }
413 1.17 cgd
414 1.17 cgd struct getrusage_args {
415 1.17 cgd int who;
416 1.17 cgd struct rusage *rusage;
417 1.17 cgd };
418 1.17 cgd /* ARGSUSED */
419 1.17 cgd getrusage(p, uap, retval)
420 1.17 cgd register struct proc *p;
421 1.17 cgd register struct getrusage_args *uap;
422 1.17 cgd int *retval;
423 1.17 cgd {
424 1.17 cgd register struct rusage *rup;
425 1.17 cgd
426 1.17 cgd switch (uap->who) {
427 1.17 cgd
428 1.19 cgd case RUSAGE_SELF:
429 1.17 cgd rup = &p->p_stats->p_ru;
430 1.17 cgd calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
431 1.17 cgd break;
432 1.17 cgd
433 1.17 cgd case RUSAGE_CHILDREN:
434 1.17 cgd rup = &p->p_stats->p_cru;
435 1.17 cgd break;
436 1.17 cgd
437 1.17 cgd default:
438 1.17 cgd return (EINVAL);
439 1.17 cgd }
440 1.17 cgd return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
441 1.17 cgd sizeof (struct rusage)));
442 1.17 cgd }
443 1.17 cgd
444 1.17 cgd ruadd(ru, ru2)
445 1.17 cgd register struct rusage *ru, *ru2;
446 1.17 cgd {
447 1.17 cgd register long *ip, *ip2;
448 1.17 cgd register int i;
449 1.17 cgd
450 1.17 cgd timevaladd(&ru->ru_utime, &ru2->ru_utime);
451 1.17 cgd timevaladd(&ru->ru_stime, &ru2->ru_stime);
452 1.17 cgd if (ru->ru_maxrss < ru2->ru_maxrss)
453 1.17 cgd ru->ru_maxrss = ru2->ru_maxrss;
454 1.17 cgd ip = &ru->ru_first; ip2 = &ru2->ru_first;
455 1.17 cgd for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
456 1.17 cgd *ip++ += *ip2++;
457 1.17 cgd }
458 1.17 cgd
459 1.17 cgd /*
460 1.17 cgd * Make a copy of the plimit structure.
461 1.17 cgd * We share these structures copy-on-write after fork,
462 1.17 cgd * and copy when a limit is changed.
463 1.17 cgd */
464 1.17 cgd struct plimit *
465 1.17 cgd limcopy(lim)
466 1.17 cgd struct plimit *lim;
467 1.17 cgd {
468 1.17 cgd register struct plimit *copy;
469 1.17 cgd
470 1.17 cgd MALLOC(copy, struct plimit *, sizeof(struct plimit),
471 1.17 cgd M_SUBPROC, M_WAITOK);
472 1.17 cgd bcopy(lim->pl_rlimit, copy->pl_rlimit,
473 1.17 cgd sizeof(struct rlimit) * RLIM_NLIMITS);
474 1.17 cgd copy->p_lflags = 0;
475 1.17 cgd copy->p_refcnt = 1;
476 1.17 cgd return (copy);
477 1.17 cgd }
478