2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
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.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
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>
75 #include <sys/mount_internal.h>
76 #include <sys/sysproto.h>
78 #include <bsm/audit_kernel.h>
80 #include <machine/vmparam.h>
82 #include <mach/mach_types.h>
83 #include <mach/time_value.h>
84 #include <mach/task_info.h>
85 #include <mach/vm_map.h>
87 #include <vm/vm_map.h>
89 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
90 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
92 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
93 rlim_t maxsmap
= MAXSSIZ
; /* XXX */
96 * Limits on the number of open files per process, and the number
97 * of child processes per process.
99 * Note: would be in kern/subr_param.c in FreeBSD.
101 int maxprocperuid
= CHILD_MAX
; /* max # of procs per user */
102 int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
104 SYSCTL_INT( _kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
,
105 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
107 SYSCTL_INT( _kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
,
108 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
112 * Resource controls and accounting.
115 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, register_t
*retval
)
117 register struct proc
*p
;
118 register int low
= PRIO_MAX
+ 1;
123 switch (uap
->which
) {
136 register struct pgrp
*pg
;
140 else if ((pg
= pgfind(uap
->who
)) == NULL
)
142 for (p
= pg
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
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
&&
161 if (low
== PRIO_MAX
+ 1)
169 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, __unused register_t
*retval
)
171 register struct proc
*p
;
172 int found
= 0, error
= 0;
174 AUDIT_ARG(cmd
, uap
->which
);
175 AUDIT_ARG(owner
, uap
->who
, 0);
176 AUDIT_ARG(value
, uap
->prio
);
181 switch (uap
->which
) {
190 error
= donice(curp
, p
, uap
->prio
);
195 register struct pgrp
*pg
;
199 else if ((pg
= pgfind(uap
->who
)) == NULL
)
201 for (p
= pg
->pg_members
.lh_first
; p
!= 0;
202 p
= p
->p_pglist
.le_next
) {
203 error
= donice(curp
, p
, uap
->prio
);
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
);
228 donice(curp
, chgp
, n
)
229 register struct proc
*curp
, *chgp
;
232 kauth_cred_t ucred
= curp
->p_ucred
;
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
))
242 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
))
245 (void)resetpriority(chgp
);
252 setrlimit(struct proc
*p
, register struct setrlimit_args
*uap
, __unused register_t
*retval
)
257 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
258 sizeof (struct rlimit
))))
260 return (dosetrlimit(p
, uap
->which
, &alim
));
264 dosetrlimit(p
, which
, limp
)
269 register struct rlimit
*alimp
;
272 if (which
>= RLIM_NLIMITS
)
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
)))
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
];
291 if (limp
->rlim_cur
> maxdmap
)
292 limp
->rlim_cur
= maxdmap
;
293 if (limp
->rlim_max
> maxdmap
)
294 limp
->rlim_max
= maxdmap
;
298 if (limp
->rlim_cur
> maxsmap
)
299 limp
->rlim_cur
= maxsmap
;
300 if (limp
->rlim_max
> maxsmap
)
301 limp
->rlim_max
= maxsmap
;
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.
307 if (limp
->rlim_cur
!= alimp
->rlim_cur
) {
311 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
313 size
= round_page_64(limp
->rlim_cur
);
314 size
-= round_page_64(alimp
->rlim_cur
);
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
;
322 #endif /* STACK_GROWTH_UP */
323 if (mach_vm_allocate(current_map(),
325 VM_FLAGS_FIXED
) != KERN_SUCCESS
)
335 * Only root can set the maxfiles limits, as it is systemwide resource
338 if (limp
->rlim_cur
> maxfiles
)
339 limp
->rlim_cur
= maxfiles
;
340 if (limp
->rlim_max
> maxfiles
)
341 limp
->rlim_max
= maxfiles
;
344 if (limp
->rlim_cur
> maxfilesperproc
)
345 limp
->rlim_cur
= maxfilesperproc
;
346 if (limp
->rlim_max
> maxfilesperproc
)
347 limp
->rlim_max
= maxfilesperproc
;
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).
358 if (limp
->rlim_cur
> maxproc
)
359 limp
->rlim_cur
= maxproc
;
360 if (limp
->rlim_max
> maxproc
)
361 limp
->rlim_max
= maxproc
;
364 if (limp
->rlim_cur
> maxprocperuid
)
365 limp
->rlim_cur
= maxprocperuid
;
366 if (limp
->rlim_max
> maxprocperuid
)
367 limp
->rlim_max
= maxprocperuid
;
378 getrlimit(struct proc
*p
, register struct getrlimit_args
*uap
, __unused register_t
*retval
)
380 if (uap
->which
>= RLIM_NLIMITS
)
382 return (copyout((caddr_t
)&p
->p_rlimit
[uap
->which
],
383 uap
->rlp
, sizeof (struct rlimit
)));
387 * Transform the running time and tick information in proc p into user,
388 * system, and interrupt time usage.
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
;
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
;
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
);
421 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
422 task_info(task
, TASK_THREAD_TIMES_INFO
,
423 &ttimesinfo
, &task_ttimes_stuff
);
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
);
434 __private_extern__
void munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
);
438 getrusage(register struct proc
*p
, register struct getrusage_args
*uap
, __unused register_t
*retval
)
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 */
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
453 case RUSAGE_CHILDREN
:
454 rup
= &p
->p_stats
->p_cru
;
461 if (IS_64BIT_PROCESS(p
)) {
462 retsize
= sizeof(rubuf64
);
463 retbuf
= (caddr_t
)&rubuf64
;
464 munge_rusage(&rubuf
, &rubuf64
);
466 return (copyout(retbuf
, uap
->rusage
, retsize
));
471 register struct rusage
*ru
, *ru2
;
473 register long *ip
, *ip2
;
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
--)
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.
494 register struct plimit
*copy
;
496 MALLOC_ZONE(copy
, struct plimit
*,
497 sizeof(struct plimit
), M_SUBPROC
, M_WAITOK
);
500 bcopy(lim
->pl_rlimit
, copy
->pl_rlimit
,
501 sizeof(struct rlimit
) * RLIM_NLIMITS
);