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