2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
79 #include <sys/file_internal.h>
80 #include <sys/resourcevar.h>
81 #include <sys/malloc.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <machine/spl.h>
86 #include <sys/mount_internal.h>
87 #include <sys/sysproto.h>
89 #include <security/audit/audit.h>
91 #include <machine/vmparam.h>
93 #include <mach/mach_types.h>
94 #include <mach/time_value.h>
95 #include <mach/task.h>
96 #include <mach/task_info.h>
97 #include <mach/vm_map.h>
98 #include <mach/mach_vm.h>
99 #include <mach/thread_act.h> /* for thread_policy_set( ) */
100 #include <kern/lock.h>
101 #include <kern/thread.h>
103 #include <kern/task.h>
104 #include <kern/clock.h> /* for absolutetime_to_microtime() */
105 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
106 #include <sys/socketvar.h> /* for struct socket */
108 #include <vm/vm_map.h>
110 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
111 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
112 int uthread_get_background_state(uthread_t
);
113 static void do_background_socket(struct proc
*p
, thread_t thread
, int priority
);
114 static int do_background_thread(struct proc
*curp
, thread_t thread
, int priority
);
115 static int do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
116 void proc_apply_task_networkbg_internal(proc_t
);
118 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
119 rlim_t maxsmap
= MAXSSIZ
- PAGE_SIZE
; /* XXX */
122 * Limits on the number of open files per process, and the number
123 * of child processes per process.
125 * Note: would be in kern/subr_param.c in FreeBSD.
127 __private_extern__
int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
129 SYSCTL_INT(_kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
130 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
132 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
133 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
135 /* Args and fn for proc_iteration callback used in setpriority */
136 struct puser_nice_args
{
143 static int puser_donice_callback(proc_t p
, void * arg
);
146 /* Args and fn for proc_iteration callback used in setpriority */
147 struct ppgrp_nice_args
{
153 static int ppgrp_donice_callback(proc_t p
, void * arg
);
156 * Resource controls and accounting.
159 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, int32_t *retval
)
162 int low
= PRIO_MAX
+ 1;
163 kauth_cred_t my_cred
;
165 /* would also test (uap->who < 0), but id_t is unsigned */
166 if (uap
->who
> 0x7fffffff)
169 switch (uap
->which
) {
176 p
= proc_find(uap
->who
);
186 struct pgrp
*pg
= PGRP_NULL
;
189 /* returns the pgrp to ref */
190 pg
= proc_pgrp(curp
);
191 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
194 /* No need for iteration as it is a simple scan */
196 for (p
= pg
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
207 uap
->who
= kauth_cred_getuid(kauth_cred_get());
211 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
212 my_cred
= kauth_cred_proc_ref(p
);
213 if (kauth_cred_getuid(my_cred
) == uap
->who
&&
216 kauth_cred_unref(&my_cred
);
223 case PRIO_DARWIN_THREAD
: {
227 /* we currently only support the current thread */
232 thread
= current_thread();
233 ut
= get_bsdthread_info(thread
);
236 if ( (ut
->uu_flag
& UT_BACKGROUND_TRAFFIC_MGT
) != 0 ) {
245 if (low
== PRIO_MAX
+ 1)
251 /* call back function used for proc iteration in PRIO_USER */
253 puser_donice_callback(proc_t p
, void * arg
)
256 struct puser_nice_args
* pun
= (struct puser_nice_args
*)arg
;
257 kauth_cred_t my_cred
;
259 my_cred
= kauth_cred_proc_ref(p
);
260 if (kauth_cred_getuid(my_cred
) == pun
->who
) {
261 error
= donice(pun
->curp
, p
, pun
->prio
);
262 if (pun
->errorp
!= NULL
)
263 *pun
->errorp
= error
;
264 if (pun
->foundp
!= NULL
) {
269 kauth_cred_unref(&my_cred
);
271 return(PROC_RETURNED
);
274 /* call back function used for proc iteration in PRIO_PGRP */
276 ppgrp_donice_callback(proc_t p
, void * arg
)
279 struct ppgrp_nice_args
* pun
= (struct ppgrp_nice_args
*)arg
;
282 error
= donice(pun
->curp
, p
, pun
->prio
);
283 if (pun
->errorp
!= NULL
)
284 *pun
->errorp
= error
;
285 if (pun
->foundp
!= NULL
) {
290 return(PROC_RETURNED
);
302 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, __unused
int32_t *retval
)
305 int found
= 0, error
= 0;
308 AUDIT_ARG(cmd
, uap
->which
);
309 AUDIT_ARG(owner
, uap
->who
, 0);
310 AUDIT_ARG(value32
, uap
->prio
);
312 /* would also test (uap->who < 0), but id_t is unsigned */
313 if (uap
->who
> 0x7fffffff)
316 switch (uap
->which
) {
322 p
= proc_find(uap
->who
);
327 error
= donice(curp
, p
, uap
->prio
);
334 struct pgrp
*pg
= PGRP_NULL
;
335 struct ppgrp_nice_args ppgrp
;
338 pg
= proc_pgrp(curp
);
339 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
)
343 ppgrp
.prio
= uap
->prio
;
344 ppgrp
.foundp
= &found
;
345 ppgrp
.errorp
= &error
;
347 /* PGRP_DROPREF drops the reference on process group */
348 pgrp_iterate(pg
, PGRP_DROPREF
, ppgrp_donice_callback
, (void *)&ppgrp
, NULL
, NULL
);
354 struct puser_nice_args punice
;
357 uap
->who
= kauth_cred_getuid(kauth_cred_get());
360 punice
.prio
= uap
->prio
;
361 punice
.who
= uap
->who
;
362 punice
.foundp
= &found
;
364 punice
.errorp
= &error
;
365 proc_iterate(PROC_ALLPROCLIST
, puser_donice_callback
, (void *)&punice
, NULL
, NULL
);
370 case PRIO_DARWIN_THREAD
: {
371 /* we currently only support the current thread */
375 error
= do_background_thread(curp
, current_thread(), uap
->prio
);
377 (void) do_background_socket(curp
, current_thread(), uap
->prio
);
383 case PRIO_DARWIN_PROCESS
: {
387 p
= proc_find(uap
->who
);
393 error
= do_background_proc(curp
, p
, uap
->prio
);
395 (void) do_background_socket(p
, NULL
, uap
->prio
);
417 * mac_check_proc_sched:???
420 donice(struct proc
*curp
, struct proc
*chgp
, int n
)
424 kauth_cred_t my_cred
;
426 ucred
= kauth_cred_proc_ref(curp
);
427 my_cred
= kauth_cred_proc_ref(chgp
);
429 if (suser(ucred
, NULL
) && kauth_cred_getruid(ucred
) &&
430 kauth_cred_getuid(ucred
) != kauth_cred_getuid(my_cred
) &&
431 kauth_cred_getruid(ucred
) != kauth_cred_getuid(my_cred
)) {
439 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
)) {
444 error
= mac_proc_check_sched(curp
, chgp
);
451 (void)resetpriority(chgp
);
453 kauth_cred_unref(&ucred
);
454 kauth_cred_unref(&my_cred
);
459 do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
463 kauth_cred_t target_cred
;
465 task_category_policy_data_t info
;
468 ucred
= kauth_cred_get();
469 target_cred
= kauth_cred_proc_ref(targetp
);
471 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
472 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
473 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
))
480 error
= mac_proc_check_sched(curp
, targetp
);
486 if (priority
== PRIO_DARWIN_NONUI
)
487 error
= proc_apply_task_gpuacc(targetp
->task
, TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_NOACCESS
);
489 error
= proc_set1_bgtaskpolicy(targetp
->task
, priority
);
492 #else /* !CONFIG_EMBEDDED */
494 /* set the max scheduling priority on the task */
495 if (priority
== PRIO_DARWIN_BG
) {
496 info
.role
= TASK_THROTTLE_APPLICATION
;
498 else if (priority
== PRIO_DARWIN_NONUI
) {
499 info
.role
= TASK_NONUI_APPLICATION
;
502 info
.role
= TASK_DEFAULT_APPLICATION
;
505 error
= task_policy_set(targetp
->task
,
506 TASK_CATEGORY_POLICY
,
507 (task_policy_t
) &info
,
508 TASK_CATEGORY_POLICY_COUNT
);
515 /* mark proc structure as backgrounded */
516 if (priority
== PRIO_DARWIN_BG
) {
517 targetp
->p_lflag
|= P_LBACKGROUND
;
519 targetp
->p_lflag
&= ~P_LBACKGROUND
;
522 /* set or reset the disk I/O priority */
523 targetp
->p_iopol_disk
= (priority
== PRIO_DARWIN_BG
?
524 IOPOL_THROTTLE
: IOPOL_DEFAULT
);
526 proc_unlock(targetp
);
527 #endif /* !CONFIG_EMBEDDED */
530 kauth_cred_unref(&target_cred
);
535 do_background_socket(struct proc
*p
, thread_t thread
, int priority
)
537 struct filedesc
*fdp
;
541 if (priority
== PRIO_DARWIN_BG
) {
543 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
544 * the sockets with the background flag. There's nothing
545 * to do here for the PRIO_DARWIN_THREAD case.
547 if (thread
== NULL
) {
551 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
552 struct socket
*sockp
;
554 fp
= fdp
->fd_ofiles
[i
];
555 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
556 fp
->f_fglob
->fg_type
!= DTYPE_SOCKET
) {
559 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
560 socket_set_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
561 sockp
->so_background_thread
= NULL
;
568 /* disable networking IO throttle.
569 * NOTE - It is a known limitation of the current design that we
570 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
571 * sockets created by other threads within this process.
575 for ( i
= 0; i
< fdp
->fd_nfiles
; i
++ ) {
576 struct socket
*sockp
;
578 fp
= fdp
->fd_ofiles
[ i
];
579 if ( fp
== NULL
|| (fdp
->fd_ofileflags
[ i
] & UF_RESERVED
) != 0 ||
580 fp
->f_fglob
->fg_type
!= DTYPE_SOCKET
) {
583 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
584 /* skip if only clearing this thread's sockets */
585 if ((thread
) && (sockp
->so_background_thread
!= thread
)) {
588 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
589 sockp
->so_background_thread
= NULL
;
597 * do_background_thread
599 * XXX - todo - does this need a MACF hook?
601 * NOTE: To maintain binary compatibility with PRIO_DARWIN_THREAD with respect
602 * to network traffic management, UT_BACKGROUND_TRAFFIC_MGT is set/cleared
603 * along with UT_BACKGROUND flag, as the latter alone no longer implies
604 * any form of traffic regulation (it simply means that the thread is
605 * background.) With PRIO_DARWIN_PROCESS, any form of network traffic
606 * management must be explicitly requested via whatever means appropriate,
607 * and only TRAFFIC_MGT_SO_BACKGROUND is set via do_background_socket().
610 do_background_thread(struct proc
*curp __unused
, thread_t thread
, int priority
)
615 #else /* !CONFIG_EMBEDDED */
616 thread_precedence_policy_data_t policy
;
617 #endif /* !CONFIG_EMBEDDED */
619 ut
= get_bsdthread_info(thread
);
621 /* Backgrounding is unsupported for threads in vfork */
622 if ( (ut
->uu_flag
& UT_VFORK
) != 0) {
627 error
= proc_set1_bgthreadpolicy(curp
->task
, thread_tid(thread
), priority
);
629 #else /* !CONFIG_EMBEDDED */
630 if ( (priority
& PRIO_DARWIN_BG
) == 0 ) {
631 /* turn off backgrounding of thread */
632 if ( (ut
->uu_flag
& UT_BACKGROUND
) == 0 ) {
638 * Clear background bit in thread and disable disk IO
639 * throttle as well as network traffic management.
640 * The corresponding socket flags for sockets created by
641 * this thread will be cleared in do_background_socket().
643 ut
->uu_flag
&= ~(UT_BACKGROUND
| UT_BACKGROUND_TRAFFIC_MGT
);
644 ut
->uu_iopol_disk
= IOPOL_NORMAL
;
646 /* reset thread priority (we did not save previous value) */
647 policy
.importance
= 0;
648 thread_policy_set( thread
, THREAD_PRECEDENCE_POLICY
,
649 (thread_policy_t
)&policy
,
650 THREAD_PRECEDENCE_POLICY_COUNT
);
654 /* background this thread */
655 if ( (ut
->uu_flag
& UT_BACKGROUND
) != 0 ) {
656 /* already backgrounded */
661 * Tag thread as background and throttle disk IO, as well
662 * as regulate network traffics. Future sockets created
663 * by this thread will have their corresponding socket
664 * flags set at socket create time.
666 ut
->uu_flag
|= (UT_BACKGROUND
| UT_BACKGROUND_TRAFFIC_MGT
);
667 ut
->uu_iopol_disk
= IOPOL_THROTTLE
;
669 policy
.importance
= INT_MIN
;
670 thread_policy_set( thread
, THREAD_PRECEDENCE_POLICY
,
671 (thread_policy_t
)&policy
,
672 THREAD_PRECEDENCE_POLICY_COUNT
);
674 /* throttle networking IO happens in socket( ) syscall.
675 * If UT_{BACKGROUND,BACKGROUND_TRAFFIC_MGT} is set in the current
676 * thread then TRAFFIC_MGT_SO_{BACKGROUND,BG_REGULATE} is set.
677 * Existing sockets are taken care of by do_background_socket().
679 #endif /* !CONFIG_EMBEDDED */
684 int mach_do_background_thread(thread_t thread
, int prio
);
687 mach_do_background_thread(thread_t thread
, int prio
)
690 struct proc
*curp
= NULL
;
691 struct proc
*targetp
= NULL
;
694 targetp
= get_bsdtask_info(get_threadtask(thread
));
696 return KERN_INVALID_ARGUMENT
;
700 if (curp
== PROC_NULL
) {
704 ucred
= kauth_cred_proc_ref(curp
);
706 if (suser(ucred
, NULL
) && curp
!= targetp
) {
707 error
= KERN_PROTECTION_FAILURE
;
711 error
= do_background_thread(curp
, thread
, prio
);
713 (void) do_background_socket(curp
, thread
, prio
);
715 if (error
== EPERM
) {
716 error
= KERN_PROTECTION_FAILURE
;
718 error
= KERN_FAILURE
;
724 kauth_cred_unref(&ucred
);
727 #endif /* CONFIG_EMBEDDED */
731 * If the thread or its proc has been put into the background
732 * with setpriority(PRIO_DARWIN_{THREAD,PROCESS}, *, PRIO_DARWIN_BG),
733 * report that status.
735 * Returns: PRIO_DARWIN_BG if background
739 uthread_get_background_state(uthread_t uth
)
741 proc_t p
= uth
->uu_proc
;
742 if (p
&& (p
->p_lflag
& P_LBACKGROUND
))
743 return PRIO_DARWIN_BG
;
745 if (uth
->uu_flag
& UT_BACKGROUND
)
746 return PRIO_DARWIN_BG
;
750 #endif /* CONFIG_EMBEDDED */
759 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
764 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
765 sizeof (struct rlimit
))))
768 return (dosetrlimit(p
, uap
->which
, &alim
));
774 * ENOMEM Cannot copy limit structure
777 * Notes: EINVAL is returned both for invalid arguments, and in the
778 * case that the current usage (e.g. RLIMIT_STACK) is already
779 * in excess of the requested limit.
782 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
)
784 struct rlimit
*alimp
;
787 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
789 /* Mask out POSIX flag, saved above */
790 which
&= ~_RLIMIT_POSIX_FLAG
;
792 if (which
>= RLIM_NLIMITS
)
795 alimp
= &p
->p_rlimit
[which
];
796 if (limp
->rlim_cur
> limp
->rlim_max
)
799 if (limp
->rlim_cur
> alimp
->rlim_max
||
800 limp
->rlim_max
> alimp
->rlim_max
)
801 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
807 if ((error
= proc_limitreplace(p
)) != 0) {
808 proc_limitunblock(p
);
812 alimp
= &p
->p_rlimit
[which
];
817 if (limp
->rlim_cur
== RLIM_INFINITY
) {
818 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
819 timerclear(&p
->p_rlim_cpu
);
822 task_absolutetime_info_data_t tinfo
;
823 mach_msg_type_number_t count
;
824 struct timeval ttv
, tv
;
826 clock_usec_t tv_usec
;
828 count
= TASK_ABSOLUTETIME_INFO_COUNT
;
829 task_info(p
->task
, TASK_ABSOLUTETIME_INFO
,
830 (task_info_t
)&tinfo
, &count
);
831 absolutetime_to_microtime(tinfo
.total_user
+ tinfo
.total_system
,
834 ttv
.tv_usec
= tv_usec
;
836 tv
.tv_sec
= (limp
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: limp
->rlim_cur
);
838 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
841 if (timercmp(&p
->p_rlim_cpu
, &tv
, >))
842 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
844 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
846 timerclear(&p
->p_rlim_cpu
);
854 if (limp
->rlim_cur
> maxdmap
)
855 limp
->rlim_cur
= maxdmap
;
856 if (limp
->rlim_max
> maxdmap
)
857 limp
->rlim_max
= maxdmap
;
861 /* Disallow illegal stack size instead of clipping */
862 if (limp
->rlim_cur
> maxsmap
||
863 limp
->rlim_max
> maxsmap
) {
870 * 4797860 - workaround poorly written installers by
871 * doing previous implementation (< 10.5) when caller
872 * is non-POSIX conforming.
874 if (limp
->rlim_cur
> maxsmap
)
875 limp
->rlim_cur
= maxsmap
;
876 if (limp
->rlim_max
> maxsmap
)
877 limp
->rlim_max
= maxsmap
;
882 * Stack is allocated to the max at exec time with only
883 * "rlim_cur" bytes accessible. If stack limit is going
884 * up make more accessible, if going down make inaccessible.
886 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
891 size
= round_page_64(limp
->rlim_cur
);
892 size
-= round_page_64(alimp
->rlim_cur
);
895 /* go to top of current stack */
896 addr
= p
->user_stack
+ round_page_64(alimp
->rlim_cur
);
897 #else /* STACK_GROWTH_UP */
898 addr
= p
->user_stack
- round_page_64(limp
->rlim_cur
);
899 #endif /* STACK_GROWTH_UP */
900 kr
= mach_vm_protect(current_map(),
902 FALSE
, VM_PROT_DEFAULT
);
903 if (kr
!= KERN_SUCCESS
) {
907 } else if (limp
->rlim_cur
< alimp
->rlim_cur
) {
915 * First check if new stack limit would agree
916 * with current stack usage.
917 * Get the current thread's stack pointer...
919 cur_sp
= thread_adjuserstack(current_thread(),
922 if (cur_sp
>= p
->user_stack
&&
923 cur_sp
< (p
->user_stack
+
924 round_page_64(alimp
->rlim_cur
))) {
925 /* current stack pointer is in main stack */
926 if (cur_sp
>= (p
->user_stack
+
927 round_page_64(limp
->rlim_cur
))) {
929 * New limit would cause
930 * current usage to be invalid:
937 /* not on the main stack: reject */
942 #else /* STACK_GROWTH_UP */
943 if (cur_sp
<= p
->user_stack
&&
944 cur_sp
> (p
->user_stack
-
945 round_page_64(alimp
->rlim_cur
))) {
946 /* stack pointer is in main stack */
947 if (cur_sp
<= (p
->user_stack
-
948 round_page_64(limp
->rlim_cur
))) {
950 * New limit would cause
951 * current usage to be invalid:
958 /* not on the main stack: reject */
962 #endif /* STACK_GROWTH_UP */
964 size
= round_page_64(alimp
->rlim_cur
);
965 size
-= round_page_64(limp
->rlim_cur
);
968 addr
= p
->user_stack
+ round_page_64(limp
->rlim_cur
);
969 #else /* STACK_GROWTH_UP */
970 addr
= p
->user_stack
- round_page_64(alimp
->rlim_cur
);
971 #endif /* STACK_GROWTH_UP */
973 kr
= mach_vm_protect(current_map(),
975 FALSE
, VM_PROT_NONE
);
976 if (kr
!= KERN_SUCCESS
) {
987 * Only root can set the maxfiles limits, as it is
988 * systemwide resource. If we are expecting POSIX behavior,
989 * instead of clamping the value, return EINVAL. We do this
990 * because historically, people have been able to attempt to
991 * set RLIM_INFINITY to get "whatever the maximum is".
994 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
995 limp
->rlim_cur
> (rlim_t
)maxfiles
) {
1000 limp
->rlim_cur
= maxfiles
;
1002 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1003 limp
->rlim_max
> (rlim_t
)maxfiles
)
1004 limp
->rlim_max
= maxfiles
;
1007 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1008 limp
->rlim_cur
> (rlim_t
)maxfilesperproc
) {
1013 limp
->rlim_cur
= maxfilesperproc
;
1015 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1016 limp
->rlim_max
> (rlim_t
)maxfilesperproc
)
1017 limp
->rlim_max
= maxfilesperproc
;
1023 * Only root can set to the maxproc limits, as it is
1024 * systemwide resource; all others are limited to
1025 * maxprocperuid (presumably less than maxproc).
1028 if (limp
->rlim_cur
> (rlim_t
)maxproc
)
1029 limp
->rlim_cur
= maxproc
;
1030 if (limp
->rlim_max
> (rlim_t
)maxproc
)
1031 limp
->rlim_max
= maxproc
;
1034 if (limp
->rlim_cur
> (rlim_t
)maxprocperuid
)
1035 limp
->rlim_cur
= maxprocperuid
;
1036 if (limp
->rlim_max
> (rlim_t
)maxprocperuid
)
1037 limp
->rlim_max
= maxprocperuid
;
1041 case RLIMIT_MEMLOCK
:
1043 * Tell the Mach VM layer about the new limit value.
1046 vm_map_set_user_wire_limit(current_map(), limp
->rlim_cur
);
1055 proc_limitunblock(p
);
1061 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
1066 * Take out flag now in case we need to use it to trigger variant
1069 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
1071 if (uap
->which
>= RLIM_NLIMITS
)
1073 proc_limitget(p
, uap
->which
, &lim
);
1074 return (copyout((caddr_t
)&lim
,
1075 uap
->rlp
, sizeof (struct rlimit
)));
1079 * Transform the running time and tick information in proc p into user,
1080 * system, and interrupt time usage.
1082 /* No lock on proc is held for this.. */
1084 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
1095 task_basic_info_32_data_t tinfo
;
1096 task_thread_times_info_data_t ttimesinfo
;
1097 task_events_info_data_t teventsinfo
;
1098 mach_msg_type_number_t task_info_count
, task_ttimes_count
;
1099 mach_msg_type_number_t task_events_count
;
1100 struct timeval ut
,st
;
1102 task_info_count
= TASK_BASIC_INFO_32_COUNT
;
1103 task_info(task
, TASK_BASIC2_INFO_32
,
1104 (task_info_t
)&tinfo
, &task_info_count
);
1105 ut
.tv_sec
= tinfo
.user_time
.seconds
;
1106 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
1107 st
.tv_sec
= tinfo
.system_time
.seconds
;
1108 st
.tv_usec
= tinfo
.system_time
.microseconds
;
1109 timeradd(&ut
, up
, up
);
1110 timeradd(&st
, sp
, sp
);
1112 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1113 task_info(task
, TASK_THREAD_TIMES_INFO
,
1114 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
1116 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1117 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1118 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1119 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1120 timeradd(&ut
, up
, up
);
1121 timeradd(&st
, sp
, sp
);
1123 task_events_count
= TASK_EVENTS_INFO_COUNT
;
1124 task_info(task
, TASK_EVENTS_INFO
,
1125 (task_info_t
)&teventsinfo
, &task_events_count
);
1128 * No need to lock "p": this does not need to be
1129 * completely consistent, right ?
1131 p
->p_stats
->p_ru
.ru_minflt
= (teventsinfo
.faults
-
1132 teventsinfo
.pageins
);
1133 p
->p_stats
->p_ru
.ru_majflt
= teventsinfo
.pageins
;
1134 p
->p_stats
->p_ru
.ru_nivcsw
= (teventsinfo
.csw
-
1135 p
->p_stats
->p_ru
.ru_nvcsw
);
1136 if (p
->p_stats
->p_ru
.ru_nivcsw
< 0)
1137 p
->p_stats
->p_ru
.ru_nivcsw
= 0;
1139 p
->p_stats
->p_ru
.ru_maxrss
= tinfo
.resident_size
;
1143 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
1144 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
1148 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1150 struct rusage
*rup
, rubuf
;
1151 struct user64_rusage rubuf64
;
1152 struct user32_rusage rubuf32
;
1153 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1154 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1155 struct timeval utime
;
1156 struct timeval stime
;
1161 calcru(p
, &utime
, &stime
, NULL
);
1163 rup
= &p
->p_stats
->p_ru
;
1164 rup
->ru_utime
= utime
;
1165 rup
->ru_stime
= stime
;
1172 case RUSAGE_CHILDREN
:
1174 rup
= &p
->p_stats
->p_cru
;
1182 if (IS_64BIT_PROCESS(p
)) {
1183 retsize
= sizeof(rubuf64
);
1184 retbuf
= (caddr_t
)&rubuf64
;
1185 munge_user64_rusage(&rubuf
, &rubuf64
);
1187 retsize
= sizeof(rubuf32
);
1188 retbuf
= (caddr_t
)&rubuf32
;
1189 munge_user32_rusage(&rubuf
, &rubuf32
);
1192 return (copyout(retbuf
, uap
->rusage
, retsize
));
1196 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
1201 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
1202 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
1203 if (ru
->ru_maxrss
< ru2
->ru_maxrss
)
1204 ru
->ru_maxrss
= ru2
->ru_maxrss
;
1205 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
1206 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--)
1211 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1214 limp
->rlim_cur
= p
->p_rlimit
[which
].rlim_cur
;
1215 limp
->rlim_max
= p
->p_rlimit
[which
].rlim_max
;
1221 proc_limitdrop(proc_t p
, int exiting
)
1223 struct plimit
* freelim
= NULL
;
1224 struct plimit
* freeoldlim
= NULL
;
1228 if (--p
->p_limit
->pl_refcnt
== 0) {
1229 freelim
= p
->p_limit
;
1232 if ((exiting
!= 0) && (p
->p_olimit
!= NULL
) && (--p
->p_olimit
->pl_refcnt
== 0)) {
1233 freeoldlim
= p
->p_olimit
;
1238 if (freelim
!= NULL
)
1239 FREE_ZONE(freelim
, sizeof *p
->p_limit
, M_PLIMIT
);
1240 if (freeoldlim
!= NULL
)
1241 FREE_ZONE(freeoldlim
, sizeof *p
->p_olimit
, M_PLIMIT
);
1246 proc_limitfork(proc_t parent
, proc_t child
)
1249 child
->p_limit
= parent
->p_limit
;
1250 child
->p_limit
->pl_refcnt
++;
1251 child
->p_olimit
= NULL
;
1256 proc_limitblock(proc_t p
)
1259 while (p
->p_lflag
& P_LLIMCHANGE
) {
1260 p
->p_lflag
|= P_LLIMWAIT
;
1261 msleep(&p
->p_olimit
, &p
->p_mlock
, 0, "proc_limitblock", NULL
);
1263 p
->p_lflag
|= P_LLIMCHANGE
;
1270 proc_limitunblock(proc_t p
)
1273 p
->p_lflag
&= ~P_LLIMCHANGE
;
1274 if (p
->p_lflag
& P_LLIMWAIT
) {
1275 p
->p_lflag
&= ~P_LLIMWAIT
;
1276 wakeup(&p
->p_olimit
);
1281 /* This is called behind serialization provided by proc_limitblock/unlbock */
1283 proc_limitreplace(proc_t p
)
1285 struct plimit
*copy
;
1290 if (p
->p_limit
->pl_refcnt
== 1) {
1297 MALLOC_ZONE(copy
, struct plimit
*,
1298 sizeof(struct plimit
), M_PLIMIT
, M_WAITOK
);
1304 bcopy(p
->p_limit
->pl_rlimit
, copy
->pl_rlimit
,
1305 sizeof(struct rlimit
) * RLIM_NLIMITS
);
1306 copy
->pl_refcnt
= 1;
1307 /* hang on to reference to old till process exits */
1308 p
->p_olimit
= p
->p_limit
;
1319 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1321 * Parameters: cmd Policy command
1322 * arg Pointer to policy arguments
1324 * Returns: 0 Success
1325 * EINVAL Invalid command or invalid policy arguments
1329 iopolicysys(__unused
struct proc
*p
, __unused
struct iopolicysys_args
*uap
, __unused
int32_t *retval
)
1332 struct _iopol_param_t iop_param
;
1333 #if !CONFIG_EMBEDDED
1334 int processwide
= 0;
1335 #else /* !CONFIG_EMBEDDED */
1336 thread_t thread
= THREAD_NULL
;
1337 struct uthread
*ut
= NULL
;
1339 #endif /* !CONFIG_EMBEDDED */
1341 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0)
1344 if (iop_param
.iop_iotype
!= IOPOL_TYPE_DISK
) {
1349 #if !CONFIG_EMBEDDED
1350 switch (iop_param
.iop_scope
) {
1351 case IOPOL_SCOPE_PROCESS
:
1354 case IOPOL_SCOPE_THREAD
:
1364 switch (iop_param
.iop_policy
) {
1367 case IOPOL_THROTTLE
:
1369 if(processwide
!= 0)
1370 proc_apply_task_diskacc(current_task(), iop_param
.iop_policy
);
1372 proc_apply_thread_selfdiskacc(iop_param
.iop_policy
);
1382 if(processwide
!= 0)
1383 iop_param
.iop_policy
= proc_get_task_disacc(current_task());
1385 iop_param
.iop_policy
= proc_get_thread_selfdiskacc();
1387 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1391 error
= EINVAL
; // unknown command
1395 #else /* !CONFIG_EMBEDDED */
1396 switch (iop_param
.iop_scope
) {
1397 case IOPOL_SCOPE_PROCESS
:
1398 policy
= &p
->p_iopol_disk
;
1400 case IOPOL_SCOPE_THREAD
:
1401 thread
= current_thread();
1402 ut
= get_bsdthread_info(thread
);
1403 policy
= &ut
->uu_iopol_disk
;
1412 switch (iop_param
.iop_policy
) {
1415 case IOPOL_THROTTLE
:
1418 *policy
= iop_param
.iop_policy
;
1430 case IOPOL_THROTTLE
:
1432 iop_param
.iop_policy
= *policy
;
1434 default: // in-kernel
1435 // this should never happen
1436 printf("%s: unknown I/O policy %d\n", __func__
, *policy
);
1437 // restore to default value
1438 *policy
= IOPOL_DEFAULT
;
1439 iop_param
.iop_policy
= *policy
;
1442 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1445 error
= EINVAL
; // unknown command
1449 #endif /* !CONFIG_EMBEDDED */
1456 boolean_t
thread_is_io_throttled(void);
1459 thread_is_io_throttled(void)
1462 #if !CONFIG_EMBEDDED
1464 return(proc_get_task_selfdiskacc() == IOPOL_THROTTLE
);
1466 #else /* !CONFIG_EMBEDDED */
1470 ut
= get_bsdthread_info(current_thread());
1473 policy
= current_proc()->p_iopol_disk
;
1475 if (ut
->uu_iopol_disk
!= IOPOL_DEFAULT
)
1476 policy
= ut
->uu_iopol_disk
;
1478 if (policy
== IOPOL_THROTTLE
)
1482 #endif /* !CONFIG_EMBEDDED */
1486 proc_apply_task_networkbg(void * bsd_info
)
1488 proc_t p
= PROC_NULL
;
1489 proc_t curp
= (proc_t
)bsd_info
;
1494 if (p
!= PROC_NULL
) {
1495 do_background_socket(p
, NULL
, PRIO_DARWIN_BG
);
1501 proc_restore_task_networkbg(void * bsd_info
)
1503 proc_t p
= PROC_NULL
;
1504 proc_t curp
= (proc_t
)bsd_info
;
1509 if (p
!= PROC_NULL
) {
1510 do_background_socket(p
, NULL
, 0);
1517 proc_set_task_networkbg(void * bsdinfo
, int setbg
)
1520 proc_apply_task_networkbg(bsdinfo
);
1522 proc_restore_task_networkbg(bsdinfo
);
1526 proc_apply_task_networkbg_internal(proc_t p
)
1528 if (p
!= PROC_NULL
) {
1529 do_background_socket(p
, NULL
, PRIO_DARWIN_BG
);