]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_resource.c
1bbc7b59b9f507cc106808061cbb79a3d4af5bea
[apple/xnu.git] / bsd / kern / kern_resource.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
24 /*-
25 * Copyright (c) 1982, 1986, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
62 */
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/sysctl.h>
67 #include <sys/kernel.h>
68 #include <sys/file_internal.h>
69 #include <sys/resourcevar.h>
70 #include <sys/malloc.h>
71 #include <sys/proc_internal.h>
72 #include <sys/kauth.h>
73 #include <machine/spl.h>
74
75 #include <sys/mount_internal.h>
76 #include <sys/sysproto.h>
77
78 #include <bsm/audit_kernel.h>
79
80 #include <machine/vmparam.h>
81
82 #include <mach/mach_types.h>
83 #include <mach/time_value.h>
84 #include <mach/task_info.h>
85 #include <mach/vm_map.h>
86
87 #include <vm/vm_map.h>
88
89 int donice(struct proc *curp, struct proc *chgp, int n);
90 int dosetrlimit(struct proc *p, u_int which, struct rlimit *limp);
91
92 rlim_t maxdmap = MAXDSIZ; /* XXX */
93 rlim_t maxsmap = MAXSSIZ; /* XXX */
94
95 /*
96 * Limits on the number of open files per process, and the number
97 * of child processes per process.
98 *
99 * Note: would be in kern/subr_param.c in FreeBSD.
100 */
101 int maxprocperuid = CHILD_MAX; /* max # of procs per user */
102 int maxfilesperproc = OPEN_MAX; /* per-proc open files limit */
103
104 SYSCTL_INT( _kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW,
105 &maxprocperuid, 0, "Maximum processes allowed per userid" );
106
107 SYSCTL_INT( _kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
108 &maxfilesperproc, 0, "Maximum files allowed open per process" );
109
110
111 /*
112 * Resource controls and accounting.
113 */
114 int
115 getpriority(struct proc *curp, struct getpriority_args *uap, register_t *retval)
116 {
117 register struct proc *p;
118 register int low = PRIO_MAX + 1;
119
120 if (uap->who < 0)
121 return (EINVAL);
122
123 switch (uap->which) {
124
125 case PRIO_PROCESS:
126 if (uap->who == 0)
127 p = curp;
128 else
129 p = pfind(uap->who);
130 if (p == 0)
131 break;
132 low = p->p_nice;
133 break;
134
135 case PRIO_PGRP: {
136 register struct pgrp *pg;
137
138 if (uap->who == 0)
139 pg = curp->p_pgrp;
140 else if ((pg = pgfind(uap->who)) == NULL)
141 break;
142 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
143 if (p->p_nice < low)
144 low = p->p_nice;
145 }
146 break;
147 }
148
149 case PRIO_USER:
150 if (uap->who == 0)
151 uap->who = kauth_cred_getuid(kauth_cred_get());
152 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
153 if (kauth_cred_getuid(p->p_ucred) == uap->who &&
154 p->p_nice < low)
155 low = p->p_nice;
156 break;
157
158 default:
159 return (EINVAL);
160 }
161 if (low == PRIO_MAX + 1)
162 return (ESRCH);
163 *retval = low;
164 return (0);
165 }
166
167 /* ARGSUSED */
168 int
169 setpriority(struct proc *curp, struct setpriority_args *uap, __unused register_t *retval)
170 {
171 register struct proc *p;
172 int found = 0, error = 0;
173
174 AUDIT_ARG(cmd, uap->which);
175 AUDIT_ARG(owner, uap->who, 0);
176 AUDIT_ARG(value, uap->prio);
177
178 if (uap->who < 0)
179 return (EINVAL);
180
181 switch (uap->which) {
182
183 case PRIO_PROCESS:
184 if (uap->who == 0)
185 p = curp;
186 else
187 p = pfind(uap->who);
188 if (p == 0)
189 break;
190 error = donice(curp, p, uap->prio);
191 found++;
192 break;
193
194 case PRIO_PGRP: {
195 register struct pgrp *pg;
196
197 if (uap->who == 0)
198 pg = curp->p_pgrp;
199 else if ((pg = pgfind(uap->who)) == NULL)
200 break;
201 for (p = pg->pg_members.lh_first; p != 0;
202 p = p->p_pglist.le_next) {
203 error = donice(curp, p, uap->prio);
204 found++;
205 }
206 break;
207 }
208
209 case PRIO_USER:
210 if (uap->who == 0)
211 uap->who = kauth_cred_getuid(kauth_cred_get());
212 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
213 if (kauth_cred_getuid(p->p_ucred) == uap->who) {
214 error = donice(curp, p, uap->prio);
215 found++;
216 }
217 break;
218
219 default:
220 return (EINVAL);
221 }
222 if (found == 0)
223 return (ESRCH);
224 return (error);
225 }
226
227 int
228 donice(curp, chgp, n)
229 register struct proc *curp, *chgp;
230 register int n;
231 {
232 kauth_cred_t ucred = curp->p_ucred;
233
234 if (suser(ucred, NULL) && ucred->cr_ruid &&
235 kauth_cred_getuid(ucred) != kauth_cred_getuid(chgp->p_ucred) &&
236 ucred->cr_ruid != kauth_cred_getuid(chgp->p_ucred))
237 return (EPERM);
238 if (n > PRIO_MAX)
239 n = PRIO_MAX;
240 if (n < PRIO_MIN)
241 n = PRIO_MIN;
242 if (n < chgp->p_nice && suser(ucred, &curp->p_acflag))
243 return (EACCES);
244 chgp->p_nice = n;
245 (void)resetpriority(chgp);
246 return (0);
247 }
248
249
250 /* ARGSUSED */
251 int
252 setrlimit(struct proc *p, register struct setrlimit_args *uap, __unused register_t *retval)
253 {
254 struct rlimit alim;
255 int error;
256
257 if ((error = copyin(uap->rlp, (caddr_t)&alim,
258 sizeof (struct rlimit))))
259 return (error);
260 return (dosetrlimit(p, uap->which, &alim));
261 }
262
263 int
264 dosetrlimit(p, which, limp)
265 struct proc *p;
266 u_int which;
267 struct rlimit *limp;
268 {
269 register struct rlimit *alimp;
270 int error;
271
272 if (which >= RLIM_NLIMITS)
273 return (EINVAL);
274 alimp = &p->p_rlimit[which];
275 if (limp->rlim_cur > alimp->rlim_max ||
276 limp->rlim_max > alimp->rlim_max)
277 if ((error = suser(kauth_cred_get(), &p->p_acflag)))
278 return (error);
279 if (limp->rlim_cur > limp->rlim_max)
280 limp->rlim_cur = limp->rlim_max;
281 if (p->p_limit->p_refcnt > 1 &&
282 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
283 p->p_limit->p_refcnt--;
284 p->p_limit = limcopy(p->p_limit);
285 alimp = &p->p_rlimit[which];
286 }
287
288 switch (which) {
289
290 case RLIMIT_DATA:
291 if (limp->rlim_cur > maxdmap)
292 limp->rlim_cur = maxdmap;
293 if (limp->rlim_max > maxdmap)
294 limp->rlim_max = maxdmap;
295 break;
296
297 case RLIMIT_STACK:
298 if (limp->rlim_cur > maxsmap)
299 limp->rlim_cur = maxsmap;
300 if (limp->rlim_max > maxsmap)
301 limp->rlim_max = maxsmap;
302 /*
303 * Stack is allocated to the max at exec time with only
304 * "rlim_cur" bytes accessible. If stack limit is going
305 * up make more accessible, if going down make inaccessible.
306 */
307 if (limp->rlim_cur != alimp->rlim_cur) {
308 user_addr_t addr;
309 user_size_t size;
310
311 if (limp->rlim_cur > alimp->rlim_cur) {
312 /* grow stack */
313 size = round_page_64(limp->rlim_cur);
314 size -= round_page_64(alimp->rlim_cur);
315
316 #if STACK_GROWTH_UP
317 /* go to top of current stack */
318 addr = p->user_stack + alimp->rlim_cur;
319 #else STACK_GROWTH_UP
320 addr = p->user_stack - alimp->rlim_cur;
321 addr -= size;
322 #endif /* STACK_GROWTH_UP */
323 if (mach_vm_allocate(current_map(),
324 &addr, size,
325 VM_FLAGS_FIXED) != KERN_SUCCESS)
326 return(EINVAL);
327 } else {
328 /* shrink stack */
329 }
330 }
331 break;
332
333 case RLIMIT_NOFILE:
334 /*
335 * Only root can set the maxfiles limits, as it is systemwide resource
336 */
337 if ( is_suser() ) {
338 if (limp->rlim_cur > maxfiles)
339 limp->rlim_cur = maxfiles;
340 if (limp->rlim_max > maxfiles)
341 limp->rlim_max = maxfiles;
342 }
343 else {
344 if (limp->rlim_cur > maxfilesperproc)
345 limp->rlim_cur = maxfilesperproc;
346 if (limp->rlim_max > maxfilesperproc)
347 limp->rlim_max = maxfilesperproc;
348 }
349 break;
350
351 case RLIMIT_NPROC:
352 /*
353 * Only root can set to the maxproc limits, as it is
354 * systemwide resource; all others are limited to
355 * maxprocperuid (presumably less than maxproc).
356 */
357 if ( is_suser() ) {
358 if (limp->rlim_cur > maxproc)
359 limp->rlim_cur = maxproc;
360 if (limp->rlim_max > maxproc)
361 limp->rlim_max = maxproc;
362 }
363 else {
364 if (limp->rlim_cur > maxprocperuid)
365 limp->rlim_cur = maxprocperuid;
366 if (limp->rlim_max > maxprocperuid)
367 limp->rlim_max = maxprocperuid;
368 }
369 break;
370
371 } /* switch... */
372 *alimp = *limp;
373 return (0);
374 }
375
376 /* ARGSUSED */
377 int
378 getrlimit(struct proc *p, register struct getrlimit_args *uap, __unused register_t *retval)
379 {
380 if (uap->which >= RLIM_NLIMITS)
381 return (EINVAL);
382 return (copyout((caddr_t)&p->p_rlimit[uap->which],
383 uap->rlp, sizeof (struct rlimit)));
384 }
385
386 /*
387 * Transform the running time and tick information in proc p into user,
388 * system, and interrupt time usage.
389 */
390 void
391 calcru(p, up, sp, ip)
392 register struct proc *p;
393 register struct timeval *up;
394 register struct timeval *sp;
395 register struct timeval *ip;
396 {
397 task_t task;
398
399 timerclear(up);
400 timerclear(sp);
401 if (ip != NULL)
402 timerclear(ip);
403
404 task = p->task;
405 if (task) {
406 task_basic_info_data_t tinfo;
407 task_thread_times_info_data_t ttimesinfo;
408 int task_info_stuff, task_ttimes_stuff;
409 struct timeval ut,st;
410
411 task_info_stuff = TASK_BASIC_INFO_COUNT;
412 task_info(task, TASK_BASIC_INFO,
413 &tinfo, &task_info_stuff);
414 ut.tv_sec = tinfo.user_time.seconds;
415 ut.tv_usec = tinfo.user_time.microseconds;
416 st.tv_sec = tinfo.system_time.seconds;
417 st.tv_usec = tinfo.system_time.microseconds;
418 timeradd(&ut, up, up);
419 timeradd(&st, sp, sp);
420
421 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
422 task_info(task, TASK_THREAD_TIMES_INFO,
423 &ttimesinfo, &task_ttimes_stuff);
424
425 ut.tv_sec = ttimesinfo.user_time.seconds;
426 ut.tv_usec = ttimesinfo.user_time.microseconds;
427 st.tv_sec = ttimesinfo.system_time.seconds;
428 st.tv_usec = ttimesinfo.system_time.microseconds;
429 timeradd(&ut, up, up);
430 timeradd(&st, sp, sp);
431 }
432 }
433
434 __private_extern__ void munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p);
435
436 /* ARGSUSED */
437 int
438 getrusage(register struct proc *p, register struct getrusage_args *uap, __unused register_t *retval)
439 {
440 struct rusage *rup, rubuf;
441 struct user_rusage rubuf64;
442 size_t retsize = sizeof(rubuf); /* default: 32 bits */
443 caddr_t retbuf = (caddr_t)&rubuf; /* default: 32 bits */
444
445 switch (uap->who) {
446 case RUSAGE_SELF:
447 rup = &p->p_stats->p_ru;
448 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
449 // LP64todo: proc struct should have 64 bit version of struct
450 rubuf = *rup;
451 break;
452
453 case RUSAGE_CHILDREN:
454 rup = &p->p_stats->p_cru;
455 rubuf = *rup;
456 break;
457
458 default:
459 return (EINVAL);
460 }
461 if (IS_64BIT_PROCESS(p)) {
462 retsize = sizeof(rubuf64);
463 retbuf = (caddr_t)&rubuf64;
464 munge_rusage(&rubuf, &rubuf64);
465 }
466 return (copyout(retbuf, uap->rusage, retsize));
467 }
468
469 void
470 ruadd(ru, ru2)
471 register struct rusage *ru, *ru2;
472 {
473 register long *ip, *ip2;
474 register int i;
475
476 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
477 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
478 if (ru->ru_maxrss < ru2->ru_maxrss)
479 ru->ru_maxrss = ru2->ru_maxrss;
480 ip = &ru->ru_first; ip2 = &ru2->ru_first;
481 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
482 *ip++ += *ip2++;
483 }
484
485 /*
486 * Make a copy of the plimit structure.
487 * We share these structures copy-on-write after fork,
488 * and copy when a limit is changed.
489 */
490 struct plimit *
491 limcopy(lim)
492 struct plimit *lim;
493 {
494 register struct plimit *copy;
495
496 MALLOC_ZONE(copy, struct plimit *,
497 sizeof(struct plimit), M_SUBPROC, M_WAITOK);
498 if (copy == NULL)
499 panic("limcopy");
500 bcopy(lim->pl_rlimit, copy->pl_rlimit,
501 sizeof(struct rlimit) * RLIM_NLIMITS);
502 copy->p_lflags = 0;
503 copy->p_refcnt = 1;
504 return (copy);
505 }