kern_resource.c revision 1.44 1 /* $NetBSD: kern_resource.c,v 1.44 1998/02/10 14:09:37 mrg 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 "opt_uvm.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/resourcevar.h>
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52
53 #include <sys/mount.h>
54 #include <sys/syscallargs.h>
55
56 #include <vm/vm.h>
57
58 #if defined(UVM)
59 #include <uvm/uvm_extern.h>
60 #endif
61
62 void limfree __P((struct plimit *));
63 /*
64 * Resource controls and accounting.
65 */
66
67 int
68 sys_getpriority(curp, v, retval)
69 struct proc *curp;
70 void *v;
71 register_t *retval;
72 {
73 register struct sys_getpriority_args /* {
74 syscallarg(int) which;
75 syscallarg(int) who;
76 } */ *uap = v;
77 register struct proc *p;
78 register int low = NZERO + PRIO_MAX + 1;
79
80 switch (SCARG(uap, which)) {
81
82 case PRIO_PROCESS:
83 if (SCARG(uap, who) == 0)
84 p = curp;
85 else
86 p = pfind(SCARG(uap, who));
87 if (p == 0)
88 break;
89 low = p->p_nice;
90 break;
91
92 case PRIO_PGRP: {
93 register struct pgrp *pg;
94
95 if (SCARG(uap, who) == 0)
96 pg = curp->p_pgrp;
97 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
98 break;
99 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
100 if (p->p_nice < low)
101 low = p->p_nice;
102 }
103 break;
104 }
105
106 case PRIO_USER:
107 if (SCARG(uap, who) == 0)
108 SCARG(uap, who) = curp->p_ucred->cr_uid;
109 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
110 if (p->p_ucred->cr_uid == SCARG(uap, who) &&
111 p->p_nice < low)
112 low = p->p_nice;
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 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
171 if (p->p_ucred->cr_uid == SCARG(uap, who)) {
172 error = donice(curp, p, SCARG(uap, prio));
173 found++;
174 }
175 break;
176
177 default:
178 return (EINVAL);
179 }
180 if (found == 0)
181 return (ESRCH);
182 return (error);
183 }
184
185 int
186 donice(curp, chgp, n)
187 register struct proc *curp, *chgp;
188 register int n;
189 {
190 register struct pcred *pcred = curp->p_cred;
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 (void)resetpriority(chgp);
205 return (0);
206 }
207
208 /* ARGSUSED */
209 int
210 sys_setrlimit(p, v, retval)
211 struct proc *p;
212 void *v;
213 register_t *retval;
214 {
215 register struct sys_setrlimit_args /* {
216 syscallarg(int) which;
217 syscallarg(const struct rlimit *) rlp;
218 } */ *uap = v;
219 int which = SCARG(uap, which);
220 struct rlimit alim;
221 int error;
222
223 error = copyin(SCARG(uap, rlp), &alim, sizeof (struct rlimit));
224 if (error)
225 return (error);
226 return (dosetrlimit(p, which, &alim));
227 }
228
229 int
230 dosetrlimit(p, which, limp)
231 struct proc *p;
232 int which;
233 struct rlimit *limp;
234 {
235 register struct rlimit *alimp;
236 extern unsigned maxdmap, maxsmap;
237 int error;
238
239 if ((u_int)which >= RLIM_NLIMITS)
240 return (EINVAL);
241
242 if (limp->rlim_cur < 0 || limp->rlim_max < 0)
243 return (EINVAL);
244
245 alimp = &p->p_rlimit[which];
246 if (limp->rlim_cur > alimp->rlim_max ||
247 limp->rlim_max > alimp->rlim_max)
248 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
249 return (error);
250 if (limp->rlim_cur > limp->rlim_max)
251 limp->rlim_cur = limp->rlim_max;
252 if (p->p_limit->p_refcnt > 1 &&
253 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
254 p->p_limit->p_refcnt--;
255 p->p_limit = limcopy(p->p_limit);
256 alimp = &p->p_rlimit[which];
257 }
258
259 switch (which) {
260
261 case RLIMIT_DATA:
262 if (limp->rlim_cur > maxdmap)
263 limp->rlim_cur = maxdmap;
264 if (limp->rlim_max > maxdmap)
265 limp->rlim_max = maxdmap;
266 break;
267
268 case RLIMIT_STACK:
269 if (limp->rlim_cur > maxsmap)
270 limp->rlim_cur = maxsmap;
271 if (limp->rlim_max > maxsmap)
272 limp->rlim_max = maxsmap;
273
274 /*
275 * Stack is allocated to the max at exec time with
276 * only "rlim_cur" bytes accessible (In other words,
277 * allocates stack dividing two contiguous regions at
278 * "rlim_cur" bytes boundary).
279 *
280 * Since allocation is done in terms of page, roundup
281 * "rlim_cur" (otherwise, contiguous regions
282 * overlap). If stack limit is going up make more
283 * accessible, if going down make inaccessible.
284 */
285 limp->rlim_cur = round_page(limp->rlim_cur);
286 if (limp->rlim_cur != alimp->rlim_cur) {
287 vm_offset_t addr;
288 vm_size_t size;
289 vm_prot_t prot;
290
291 if (limp->rlim_cur > alimp->rlim_cur) {
292 prot = VM_PROT_ALL;
293 size = limp->rlim_cur - alimp->rlim_cur;
294 addr = USRSTACK - limp->rlim_cur;
295 } else {
296 prot = VM_PROT_NONE;
297 size = alimp->rlim_cur - limp->rlim_cur;
298 addr = USRSTACK - alimp->rlim_cur;
299 }
300 #if defined(UVM)
301 (void) uvm_map_protect(&p->p_vmspace->vm_map,
302 addr, addr+size, prot, FALSE);
303 #else
304 (void) vm_map_protect(&p->p_vmspace->vm_map,
305 addr, addr+size, prot, FALSE);
306 #endif
307 }
308 break;
309
310 case RLIMIT_NOFILE:
311 if (limp->rlim_cur > maxfiles)
312 limp->rlim_cur = maxfiles;
313 if (limp->rlim_max > maxfiles)
314 limp->rlim_max = maxfiles;
315 break;
316
317 case RLIMIT_NPROC:
318 if (limp->rlim_cur > maxproc)
319 limp->rlim_cur = maxproc;
320 if (limp->rlim_max > maxproc)
321 limp->rlim_max = maxproc;
322 break;
323 }
324 *alimp = *limp;
325 return (0);
326 }
327
328 /* ARGSUSED */
329 int
330 sys_getrlimit(p, v, retval)
331 struct proc *p;
332 void *v;
333 register_t *retval;
334 {
335 register struct sys_getrlimit_args /* {
336 syscallarg(int) which;
337 syscallarg(struct rlimit *) rlp;
338 } */ *uap = v;
339 int which = SCARG(uap, which);
340
341 if ((u_int)which >= RLIM_NLIMITS)
342 return (EINVAL);
343 return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
344 sizeof (struct rlimit)));
345 }
346
347 /*
348 * Transform the running time and tick information in proc p into user,
349 * system, and interrupt time usage.
350 */
351 void
352 calcru(p, up, sp, ip)
353 register struct proc *p;
354 register struct timeval *up;
355 register struct timeval *sp;
356 register struct timeval *ip;
357 {
358 register u_quad_t u, st, ut, it, tot;
359 register long sec, usec;
360 register int s;
361 struct timeval tv;
362
363 s = splstatclock();
364 st = p->p_sticks;
365 ut = p->p_uticks;
366 it = p->p_iticks;
367 splx(s);
368
369 tot = st + ut + it;
370 if (tot == 0) {
371 up->tv_sec = up->tv_usec = 0;
372 sp->tv_sec = sp->tv_usec = 0;
373 if (ip != NULL)
374 ip->tv_sec = ip->tv_usec = 0;
375 return;
376 }
377
378 sec = p->p_rtime.tv_sec;
379 usec = p->p_rtime.tv_usec;
380 if (p == curproc) {
381 /*
382 * Adjust for the current time slice. This is actually fairly
383 * important since the error here is on the order of a time
384 * quantum, which is much greater than the sampling error.
385 */
386 microtime(&tv);
387 sec += tv.tv_sec - runtime.tv_sec;
388 usec += tv.tv_usec - runtime.tv_usec;
389 }
390 u = (u_quad_t) sec * 1000000 + usec;
391 st = (u * st) / tot;
392 sp->tv_sec = st / 1000000;
393 sp->tv_usec = st % 1000000;
394 ut = (u * ut) / tot;
395 up->tv_sec = ut / 1000000;
396 up->tv_usec = ut % 1000000;
397 if (ip != NULL) {
398 it = (u * it) / tot;
399 ip->tv_sec = it / 1000000;
400 ip->tv_usec = it % 1000000;
401 }
402 }
403
404 /* ARGSUSED */
405 int
406 sys_getrusage(p, v, retval)
407 register struct proc *p;
408 void *v;
409 register_t *retval;
410 {
411 register struct sys_getrusage_args /* {
412 syscallarg(int) who;
413 syscallarg(struct rusage *) rusage;
414 } */ *uap = v;
415 register struct rusage *rup;
416
417 switch (SCARG(uap, who)) {
418
419 case RUSAGE_SELF:
420 rup = &p->p_stats->p_ru;
421 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
422 break;
423
424 case RUSAGE_CHILDREN:
425 rup = &p->p_stats->p_cru;
426 break;
427
428 default:
429 return (EINVAL);
430 }
431 return (copyout(rup, SCARG(uap, rusage), sizeof (struct rusage)));
432 }
433
434 void
435 ruadd(ru, ru2)
436 register struct rusage *ru, *ru2;
437 {
438 register long *ip, *ip2;
439 register int i;
440
441 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
442 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
443 if (ru->ru_maxrss < ru2->ru_maxrss)
444 ru->ru_maxrss = ru2->ru_maxrss;
445 ip = &ru->ru_first; ip2 = &ru2->ru_first;
446 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
447 *ip++ += *ip2++;
448 }
449
450 /*
451 * Make a copy of the plimit structure.
452 * We share these structures copy-on-write after fork,
453 * and copy when a limit is changed.
454 */
455 struct plimit *
456 limcopy(lim)
457 struct plimit *lim;
458 {
459 register struct plimit *newlim;
460
461 MALLOC(newlim, struct plimit *, sizeof(struct plimit),
462 M_SUBPROC, M_WAITOK);
463 bcopy(lim->pl_rlimit, newlim->pl_rlimit,
464 sizeof(struct rlimit) * RLIM_NLIMITS);
465 newlim->p_lflags = 0;
466 newlim->p_refcnt = 1;
467 return (newlim);
468 }
469
470 void
471 limfree(lim)
472 struct plimit *lim;
473 {
474
475 if (--lim->p_refcnt > 0)
476 return;
477 FREE(lim, M_SUBPROC);
478 }
479