2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/sysctl.h>
74 #include <sys/kernel.h>
75 #include <sys/file_internal.h>
76 #include <sys/resourcevar.h>
77 #include <sys/malloc.h>
78 #include <sys/proc_internal.h>
79 #include <sys/kauth.h>
80 #include <machine/spl.h>
82 #include <sys/mount_internal.h>
83 #include <sys/sysproto.h>
85 #include <bsm/audit_kernel.h>
87 #include <machine/vmparam.h>
89 #include <mach/mach_types.h>
90 #include <mach/time_value.h>
91 #include <mach/task_info.h>
92 #include <mach/vm_map.h>
94 #include <vm/vm_map.h>
96 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
97 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
99 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
100 rlim_t maxsmap
= MAXSSIZ
; /* XXX */
103 * Limits on the number of open files per process, and the number
104 * of child processes per process.
106 * Note: would be in kern/subr_param.c in FreeBSD.
108 extern int maxprocperuid
; /* max # of procs per user */
109 int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
111 SYSCTL_INT( _kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
,
112 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
114 SYSCTL_INT( _kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
,
115 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
119 * Resource controls and accounting.
122 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, register_t
*retval
)
124 register struct proc
*p
;
125 register int low
= PRIO_MAX
+ 1;
130 switch (uap
->which
) {
143 register struct pgrp
*pg
;
147 else if ((pg
= pgfind(uap
->who
)) == NULL
)
149 for (p
= pg
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
158 uap
->who
= kauth_cred_getuid(kauth_cred_get());
159 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
160 if (kauth_cred_getuid(p
->p_ucred
) == uap
->who
&&
168 if (low
== PRIO_MAX
+ 1)
176 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, __unused register_t
*retval
)
178 register struct proc
*p
;
179 int found
= 0, error
= 0;
181 AUDIT_ARG(cmd
, uap
->which
);
182 AUDIT_ARG(owner
, uap
->who
, 0);
183 AUDIT_ARG(value
, uap
->prio
);
188 switch (uap
->which
) {
197 error
= donice(curp
, p
, uap
->prio
);
202 register struct pgrp
*pg
;
206 else if ((pg
= pgfind(uap
->who
)) == NULL
)
208 for (p
= pg
->pg_members
.lh_first
; p
!= 0;
209 p
= p
->p_pglist
.le_next
) {
210 error
= donice(curp
, p
, uap
->prio
);
218 uap
->who
= kauth_cred_getuid(kauth_cred_get());
219 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
220 if (kauth_cred_getuid(p
->p_ucred
) == uap
->who
) {
221 error
= donice(curp
, p
, uap
->prio
);
235 donice(curp
, chgp
, n
)
236 register struct proc
*curp
, *chgp
;
239 kauth_cred_t ucred
= curp
->p_ucred
;
241 if (suser(ucred
, NULL
) && ucred
->cr_ruid
&&
242 kauth_cred_getuid(ucred
) != kauth_cred_getuid(chgp
->p_ucred
) &&
243 ucred
->cr_ruid
!= kauth_cred_getuid(chgp
->p_ucred
))
249 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
))
252 (void)resetpriority(chgp
);
259 setrlimit(struct proc
*p
, register struct setrlimit_args
*uap
, __unused register_t
*retval
)
264 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
265 sizeof (struct rlimit
))))
267 return (dosetrlimit(p
, uap
->which
, &alim
));
271 dosetrlimit(p
, which
, limp
)
276 register struct rlimit
*alimp
;
279 if (which
>= RLIM_NLIMITS
)
281 alimp
= &p
->p_rlimit
[which
];
282 if (limp
->rlim_cur
> alimp
->rlim_max
||
283 limp
->rlim_max
> alimp
->rlim_max
)
284 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
286 if (limp
->rlim_cur
> limp
->rlim_max
)
287 limp
->rlim_cur
= limp
->rlim_max
;
288 if (p
->p_limit
->p_refcnt
> 1 &&
289 (p
->p_limit
->p_lflags
& PL_SHAREMOD
) == 0) {
290 p
->p_limit
->p_refcnt
--;
291 p
->p_limit
= limcopy(p
->p_limit
);
292 alimp
= &p
->p_rlimit
[which
];
298 if (limp
->rlim_cur
> maxdmap
)
299 limp
->rlim_cur
= maxdmap
;
300 if (limp
->rlim_max
> maxdmap
)
301 limp
->rlim_max
= maxdmap
;
305 if (limp
->rlim_cur
> maxsmap
)
306 limp
->rlim_cur
= maxsmap
;
307 if (limp
->rlim_max
> maxsmap
)
308 limp
->rlim_max
= maxsmap
;
310 * Stack is allocated to the max at exec time with only
311 * "rlim_cur" bytes accessible. If stack limit is going
312 * up make more accessible, if going down make inaccessible.
314 if (limp
->rlim_cur
!= alimp
->rlim_cur
) {
318 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
320 size
= round_page_64(limp
->rlim_cur
);
321 size
-= round_page_64(alimp
->rlim_cur
);
324 /* go to top of current stack */
325 addr
= p
->user_stack
+ alimp
->rlim_cur
;
326 #else STACK_GROWTH_UP
327 addr
= p
->user_stack
- alimp
->rlim_cur
;
329 #endif /* STACK_GROWTH_UP */
330 if (mach_vm_allocate(current_map(),
332 VM_FLAGS_FIXED
) != KERN_SUCCESS
)
342 * Only root can set the maxfiles limits, as it is systemwide resource
345 if (limp
->rlim_cur
> maxfiles
)
346 limp
->rlim_cur
= maxfiles
;
347 if (limp
->rlim_max
> maxfiles
)
348 limp
->rlim_max
= maxfiles
;
351 if (limp
->rlim_cur
> maxfilesperproc
)
352 limp
->rlim_cur
= maxfilesperproc
;
353 if (limp
->rlim_max
> maxfilesperproc
)
354 limp
->rlim_max
= maxfilesperproc
;
360 * Only root can set to the maxproc limits, as it is
361 * systemwide resource; all others are limited to
362 * maxprocperuid (presumably less than maxproc).
365 if (limp
->rlim_cur
> maxproc
)
366 limp
->rlim_cur
= maxproc
;
367 if (limp
->rlim_max
> maxproc
)
368 limp
->rlim_max
= maxproc
;
371 if (limp
->rlim_cur
> maxprocperuid
)
372 limp
->rlim_cur
= maxprocperuid
;
373 if (limp
->rlim_max
> maxprocperuid
)
374 limp
->rlim_max
= maxprocperuid
;
385 getrlimit(struct proc
*p
, register struct getrlimit_args
*uap
, __unused register_t
*retval
)
387 if (uap
->which
>= RLIM_NLIMITS
)
389 return (copyout((caddr_t
)&p
->p_rlimit
[uap
->which
],
390 uap
->rlp
, sizeof (struct rlimit
)));
394 * Transform the running time and tick information in proc p into user,
395 * system, and interrupt time usage.
398 calcru(p
, up
, sp
, ip
)
399 register struct proc
*p
;
400 register struct timeval
*up
;
401 register struct timeval
*sp
;
402 register struct timeval
*ip
;
413 task_basic_info_data_t tinfo
;
414 task_thread_times_info_data_t ttimesinfo
;
415 int task_info_stuff
, task_ttimes_stuff
;
416 struct timeval ut
,st
;
418 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
419 task_info(task
, TASK_BASIC_INFO
,
420 &tinfo
, &task_info_stuff
);
421 ut
.tv_sec
= tinfo
.user_time
.seconds
;
422 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
423 st
.tv_sec
= tinfo
.system_time
.seconds
;
424 st
.tv_usec
= tinfo
.system_time
.microseconds
;
425 timeradd(&ut
, up
, up
);
426 timeradd(&st
, sp
, sp
);
428 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
429 task_info(task
, TASK_THREAD_TIMES_INFO
,
430 &ttimesinfo
, &task_ttimes_stuff
);
432 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
433 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
434 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
435 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
436 timeradd(&ut
, up
, up
);
437 timeradd(&st
, sp
, sp
);
441 __private_extern__
void munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
);
445 getrusage(register struct proc
*p
, register struct getrusage_args
*uap
, __unused register_t
*retval
)
447 struct rusage
*rup
, rubuf
;
448 struct user_rusage rubuf64
;
449 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
450 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
454 rup
= &p
->p_stats
->p_ru
;
455 calcru(p
, &rup
->ru_utime
, &rup
->ru_stime
, NULL
);
456 // LP64todo: proc struct should have 64 bit version of struct
460 case RUSAGE_CHILDREN
:
461 rup
= &p
->p_stats
->p_cru
;
468 if (IS_64BIT_PROCESS(p
)) {
469 retsize
= sizeof(rubuf64
);
470 retbuf
= (caddr_t
)&rubuf64
;
471 munge_rusage(&rubuf
, &rubuf64
);
473 return (copyout(retbuf
, uap
->rusage
, retsize
));
478 register struct rusage
*ru
, *ru2
;
480 register long *ip
, *ip2
;
483 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
484 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
485 if (ru
->ru_maxrss
< ru2
->ru_maxrss
)
486 ru
->ru_maxrss
= ru2
->ru_maxrss
;
487 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
488 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--)
493 * Make a copy of the plimit structure.
494 * We share these structures copy-on-write after fork,
495 * and copy when a limit is changed.
501 register struct plimit
*copy
;
503 MALLOC_ZONE(copy
, struct plimit
*,
504 sizeof(struct plimit
), M_SUBPROC
, M_WAITOK
);
507 bcopy(lim
->pl_rlimit
, copy
->pl_rlimit
,
508 sizeof(struct rlimit
) * RLIM_NLIMITS
);