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