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