2 * Copyright (c) 2000-2020 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 <sys/mount_internal.h>
85 #include <sys/sysproto.h>
87 #include <security/audit/audit.h>
89 #include <machine/vmparam.h>
91 #include <mach/mach_types.h>
92 #include <mach/time_value.h>
93 #include <mach/task.h>
94 #include <mach/task_info.h>
95 #include <mach/vm_map.h>
96 #include <mach/mach_vm.h>
97 #include <mach/thread_act.h> /* for thread_policy_set( ) */
98 #include <kern/thread.h>
99 #include <kern/policy_internal.h>
101 #include <kern/task.h>
102 #include <kern/clock.h> /* for absolutetime_to_microtime() */
103 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
104 #include <sys/socketvar.h> /* for struct socket */
106 #include <net/necp.h>
109 #include <vm/vm_map.h>
111 #include <kern/assert.h>
112 #include <sys/resource.h>
113 #include <sys/priv.h>
114 #include <IOKit/IOBSD.h>
117 #include <security/mac_framework.h>
120 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
121 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
122 int uthread_get_background_state(uthread_t
);
123 static void do_background_socket(struct proc
*p
, thread_t thread
);
124 static int do_background_thread(thread_t thread
, int priority
);
125 static int do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
126 static int set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
127 static int proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
);
128 static int proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
);
129 static int get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
);
130 int proc_pid_rusage(int pid
, int flavor
, user_addr_t buf
, int32_t *retval
);
131 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
132 int fill_task_rusage(task_t task
, rusage_info_current
*ri
);
133 void fill_task_billed_usage(task_t task
, rusage_info_current
*ri
);
134 int fill_task_io_rusage(task_t task
, rusage_info_current
*ri
);
135 int fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
);
136 uint64_t get_task_logical_writes(task_t task
, boolean_t external
);
137 void fill_task_monotonic_rusage(task_t task
, rusage_info_current
*ri
);
139 int proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
);
141 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
142 rlim_t maxsmap
= MAXSSIZ
- PAGE_MAX_SIZE
; /* XXX */
144 /* For plimit reference count */
145 os_refgrp_decl(, rlimit_refgrp
, "plimit_refcnt", NULL
);
147 ZONE_DECLARE(plimit_zone
, "plimit", sizeof(struct plimit
), ZC_NOENCRYPT
);
150 * Limits on the number of open files per process, and the number
151 * of child processes per process.
153 * Note: would be in kern/subr_param.c in FreeBSD.
155 __private_extern__
int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
157 SYSCTL_INT(_kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
158 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
160 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
161 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
163 /* Args and fn for proc_iteration callback used in setpriority */
164 struct puser_nice_args
{
171 static int puser_donice_callback(proc_t p
, void * arg
);
174 /* Args and fn for proc_iteration callback used in setpriority */
175 struct ppgrp_nice_args
{
181 static int ppgrp_donice_callback(proc_t p
, void * arg
);
184 * Resource controls and accounting.
187 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, int32_t *retval
)
190 int low
= PRIO_MAX
+ 1;
191 kauth_cred_t my_cred
;
195 /* would also test (uap->who < 0), but id_t is unsigned */
196 if (uap
->who
> 0x7fffffff) {
200 switch (uap
->which
) {
206 p
= proc_find(uap
->who
);
216 struct pgrp
*pg
= PGRP_NULL
;
219 /* returns the pgrp to ref */
220 pg
= proc_pgrp(curp
);
221 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
224 /* No need for iteration as it is a simple scan */
226 PGMEMBERS_FOREACH(pg
, p
) {
227 if (p
->p_nice
< low
) {
238 uap
->who
= kauth_cred_getuid(kauth_cred_get());
243 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
244 my_cred
= kauth_cred_proc_ref(p
);
245 if (kauth_cred_getuid(my_cred
) == uap
->who
&&
249 kauth_cred_unref(&my_cred
);
256 case PRIO_DARWIN_THREAD
:
257 /* we currently only support the current thread */
262 low
= proc_get_thread_policy(current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_DARWIN_BG
);
266 case PRIO_DARWIN_PROCESS
:
270 p
= proc_find(uap
->who
);
271 if (p
== PROC_NULL
) {
277 error
= get_background_proc(curp
, p
, &low
);
287 case PRIO_DARWIN_ROLE
:
291 p
= proc_find(uap
->who
);
292 if (p
== PROC_NULL
) {
298 error
= proc_get_darwin_role(curp
, p
, &low
);
311 if (low
== PRIO_MAX
+ 1) {
318 /* call back function used for proc iteration in PRIO_USER */
320 puser_donice_callback(proc_t p
, void * arg
)
323 struct puser_nice_args
* pun
= (struct puser_nice_args
*)arg
;
324 kauth_cred_t my_cred
;
326 my_cred
= kauth_cred_proc_ref(p
);
327 if (kauth_cred_getuid(my_cred
) == pun
->who
) {
328 error
= donice(pun
->curp
, p
, pun
->prio
);
329 if (pun
->errorp
!= NULL
) {
330 *pun
->errorp
= error
;
332 if (pun
->foundp
!= NULL
) {
334 *pun
->foundp
= n
+ 1;
337 kauth_cred_unref(&my_cred
);
339 return PROC_RETURNED
;
342 /* call back function used for proc iteration in PRIO_PGRP */
344 ppgrp_donice_callback(proc_t p
, void * arg
)
347 struct ppgrp_nice_args
* pun
= (struct ppgrp_nice_args
*)arg
;
350 error
= donice(pun
->curp
, p
, pun
->prio
);
351 if (pun
->errorp
!= NULL
) {
352 *pun
->errorp
= error
;
354 if (pun
->foundp
!= NULL
) {
356 *pun
->foundp
= n
+ 1;
359 return PROC_RETURNED
;
371 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, int32_t *retval
)
374 int found
= 0, error
= 0;
377 AUDIT_ARG(cmd
, uap
->which
);
378 AUDIT_ARG(owner
, uap
->who
, 0);
379 AUDIT_ARG(value32
, uap
->prio
);
381 /* would also test (uap->who < 0), but id_t is unsigned */
382 if (uap
->who
> 0x7fffffff) {
386 switch (uap
->which
) {
391 p
= proc_find(uap
->who
);
397 error
= donice(curp
, p
, uap
->prio
);
405 struct pgrp
*pg
= PGRP_NULL
;
406 struct ppgrp_nice_args ppgrp
;
409 pg
= proc_pgrp(curp
);
410 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
415 ppgrp
.prio
= uap
->prio
;
416 ppgrp
.foundp
= &found
;
417 ppgrp
.errorp
= &error
;
419 /* PGRP_DROPREF drops the reference on process group */
420 pgrp_iterate(pg
, PGRP_DROPREF
, ppgrp_donice_callback
, (void *)&ppgrp
, NULL
, NULL
);
426 struct puser_nice_args punice
;
429 uap
->who
= kauth_cred_getuid(kauth_cred_get());
433 punice
.prio
= uap
->prio
;
434 punice
.who
= uap
->who
;
435 punice
.foundp
= &found
;
437 punice
.errorp
= &error
;
438 proc_iterate(PROC_ALLPROCLIST
, puser_donice_callback
, (void *)&punice
, NULL
, NULL
);
443 case PRIO_DARWIN_THREAD
: {
444 /* we currently only support the current thread */
449 error
= do_background_thread(current_thread(), uap
->prio
);
454 case PRIO_DARWIN_PROCESS
: {
458 p
= proc_find(uap
->who
);
465 error
= do_background_proc(curp
, p
, uap
->prio
);
474 case PRIO_DARWIN_GPU
: {
479 p
= proc_find(uap
->who
);
480 if (p
== PROC_NULL
) {
484 error
= set_gpudeny_proc(curp
, p
, uap
->prio
);
491 case PRIO_DARWIN_ROLE
: {
495 p
= proc_find(uap
->who
);
496 if (p
== PROC_NULL
) {
502 error
= proc_set_darwin_role(curp
, p
, uap
->prio
);
517 if (error
== EIDRM
) {
529 * mac_check_proc_sched:???
532 donice(struct proc
*curp
, struct proc
*chgp
, int n
)
536 kauth_cred_t my_cred
;
538 ucred
= kauth_cred_proc_ref(curp
);
539 my_cred
= kauth_cred_proc_ref(chgp
);
541 if (suser(ucred
, NULL
) && kauth_cred_getruid(ucred
) &&
542 kauth_cred_getuid(ucred
) != kauth_cred_getuid(my_cred
) &&
543 kauth_cred_getruid(ucred
) != kauth_cred_getuid(my_cred
)) {
553 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
)) {
558 error
= mac_proc_check_sched(curp
, chgp
);
564 chgp
->p_nice
= (char)n
;
566 (void)resetpriority(chgp
);
568 kauth_cred_unref(&ucred
);
569 kauth_cred_unref(&my_cred
);
574 set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
578 kauth_cred_t target_cred
;
580 ucred
= kauth_cred_get();
581 target_cred
= kauth_cred_proc_ref(targetp
);
583 /* TODO: Entitlement instead of uid check */
585 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
586 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
587 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
592 if (curp
== targetp
) {
598 error
= mac_proc_check_sched(curp
, targetp
);
605 case PRIO_DARWIN_GPU_DENY
:
606 task_set_gpu_denied(proc_task(targetp
), TRUE
);
608 case PRIO_DARWIN_GPU_ALLOW
:
609 task_set_gpu_denied(proc_task(targetp
), FALSE
);
617 kauth_cred_unref(&target_cred
);
622 proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
)
627 kauth_cred_t ucred
, target_cred
;
629 ucred
= kauth_cred_get();
630 target_cred
= kauth_cred_proc_ref(targetp
);
632 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
633 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
634 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
635 if (priv_check_cred(ucred
, PRIV_SETPRIORITY_DARWIN_ROLE
, 0) != 0) {
641 if (curp
!= targetp
) {
643 if ((error
= mac_proc_check_sched(curp
, targetp
))) {
649 proc_get_darwinbgstate(proc_task(targetp
), &flagsp
);
650 if ((flagsp
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
655 task_role_t role
= TASK_UNSPECIFIED
;
657 if ((error
= proc_darwin_role_to_task_role(priority
, &role
))) {
661 proc_set_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
,
662 TASK_POLICY_ROLE
, role
);
665 kauth_cred_unref(&target_cred
);
670 proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
)
675 kauth_cred_t ucred
, target_cred
;
677 ucred
= kauth_cred_get();
678 target_cred
= kauth_cred_proc_ref(targetp
);
680 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
681 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
682 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
687 if (curp
!= targetp
) {
689 if ((error
= mac_proc_check_sched(curp
, targetp
))) {
695 role
= proc_get_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
697 *priority
= proc_task_role_to_darwin_role(role
);
700 kauth_cred_unref(&target_cred
);
706 get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
)
710 kauth_cred_t ucred
, target_cred
;
712 ucred
= kauth_cred_get();
713 target_cred
= kauth_cred_proc_ref(targetp
);
715 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
716 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
717 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
722 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
724 *priority
= proc_get_task_policy(current_task(), external
, TASK_POLICY_DARWIN_BG
);
727 kauth_cred_unref(&target_cred
);
732 do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
739 kauth_cred_t target_cred
;
743 ucred
= kauth_cred_get();
744 target_cred
= kauth_cred_proc_ref(targetp
);
746 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
747 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
748 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
754 error
= mac_proc_check_sched(curp
, targetp
);
760 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
764 enable
= TASK_POLICY_ENABLE
;
766 case PRIO_DARWIN_NONUI
:
767 /* ignored for compatibility */
770 /* TODO: EINVAL if priority != 0 */
771 enable
= TASK_POLICY_DISABLE
;
775 proc_set_task_policy(proc_task(targetp
), external
, TASK_POLICY_DARWIN_BG
, enable
);
778 kauth_cred_unref(&target_cred
);
783 do_background_socket(struct proc
*p
, thread_t thread
)
787 int background
= false;
789 int update_necp
= false;
794 if (thread
!= THREAD_NULL
) {
795 background
= proc_get_effective_thread_policy(thread
, TASK_POLICY_ALL_SOCKETS_BG
);
797 background
= proc_get_effective_task_policy(proc_task(p
), TASK_POLICY_ALL_SOCKETS_BG
);
802 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
803 * the sockets with the background flag. There's nothing
804 * to do here for the PRIO_DARWIN_THREAD case.
806 if (thread
== THREAD_NULL
) {
808 if (FILEGLOB_DTYPE(fp
->fp_glob
) == DTYPE_SOCKET
) {
809 struct socket
*sockp
= (struct socket
*)fp
->fp_glob
->fg_data
;
810 socket_set_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
811 sockp
->so_background_thread
= NULL
;
814 else if (FILEGLOB_DTYPE(fp
->fp_glob
) == DTYPE_NETPOLICY
) {
815 if (necp_set_client_as_background(p
, fp
, background
)) {
823 /* disable networking IO throttle.
824 * NOTE - It is a known limitation of the current design that we
825 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
826 * sockets created by other threads within this process.
829 struct socket
*sockp
;
831 if (FILEGLOB_DTYPE(fp
->fp_glob
) == DTYPE_SOCKET
) {
832 sockp
= (struct socket
*)fp
->fp_glob
->fg_data
;
833 /* skip if only clearing this thread's sockets */
834 if ((thread
) && (sockp
->so_background_thread
!= thread
)) {
837 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
838 sockp
->so_background_thread
= NULL
;
841 else if (FILEGLOB_DTYPE(fp
->fp_glob
) == DTYPE_NETPOLICY
) {
842 if (necp_set_client_as_background(p
, fp
, background
)) {
854 necp_update_all_clients();
858 #pragma unused(p, thread)
864 * do_background_thread
866 * Requires: thread reference
869 * EPERM Tried to background while in vfork
870 * XXX - todo - does this need a MACF hook?
873 do_background_thread(thread_t thread
, int priority
)
876 int enable
, external
;
879 ut
= get_bsdthread_info(thread
);
881 /* Backgrounding is unsupported for threads in vfork */
882 if ((ut
->uu_flag
& UT_VFORK
) != 0) {
886 /* Backgrounding is unsupported for workq threads */
887 if (thread_is_static_param(thread
)) {
891 /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
892 if (thread_has_qos_policy(thread
)) {
893 thread_remove_qos_policy(thread
);
897 /* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
898 enable
= (priority
== PRIO_DARWIN_BG
) ? TASK_POLICY_ENABLE
: TASK_POLICY_DISABLE
;
899 external
= (current_thread() == thread
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
901 proc_set_thread_policy(thread
, external
, TASK_POLICY_DARWIN_BG
, enable
);
914 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
919 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
920 sizeof(struct rlimit
)))) {
924 return dosetrlimit(p
, uap
->which
, &alim
);
932 * Notes: EINVAL is returned both for invalid arguments, and in the
933 * case that the current usage (e.g. RLIMIT_STACK) is already
934 * in excess of the requested limit.
937 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*newrlim
)
942 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
944 /* Mask out POSIX flag, saved above */
945 which
&= ~_RLIMIT_POSIX_FLAG
;
947 /* Unknown resource */
948 if (which
>= RLIM_NLIMITS
) {
953 * Take a snapshot of the current rlimit values and read this throughout
954 * this routine. This minimizes the critical sections and allow other
955 * processes in the system to access the plimit while we are in the
956 * middle of this setrlimit call.
959 rlim
= p
->p_limit
->pl_rlimit
[which
];
963 /* Sanity check: new soft limit cannot exceed new hard limit */
964 if (newrlim
->rlim_cur
> newrlim
->rlim_max
) {
968 * Sanity check: only super-user may raise the hard limit.
969 * newrlim->rlim_cur > rlim.rlim_max implies that the call is increasing the hard limit as well.
971 else if (newrlim
->rlim_cur
> rlim
.rlim_max
|| newrlim
->rlim_max
> rlim
.rlim_max
) {
972 /* suser() returns 0 if the calling thread is super user. */
973 error
= suser(kauth_cred_get(), &p
->p_acflag
);
977 /* Invalid setrlimit request: EINVAL or EPERM */
981 /* Only one thread is able to change the current process's rlimit values */
986 /* We have the reader lock of the process's plimit so it's safe to read the rlimit values */
989 if (newrlim
->rlim_cur
== RLIM_INFINITY
) {
990 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
991 timerclear(&p
->p_rlim_cpu
);
993 task_absolutetime_info_data_t tinfo
;
994 mach_msg_type_number_t count
;
995 struct timeval ttv
, tv
;
997 clock_usec_t tv_usec
;
999 count
= TASK_ABSOLUTETIME_INFO_COUNT
;
1000 task_info(p
->task
, TASK_ABSOLUTETIME_INFO
, (task_info_t
)&tinfo
, &count
);
1001 absolutetime_to_microtime(tinfo
.total_user
+ tinfo
.total_system
, &tv_sec
, &tv_usec
);
1002 ttv
.tv_sec
= tv_sec
;
1003 ttv
.tv_usec
= tv_usec
;
1005 tv
.tv_sec
= (newrlim
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: (__darwin_time_t
)newrlim
->rlim_cur
);
1007 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
1010 if (timercmp(&p
->p_rlim_cpu
, &tv
, >)) {
1011 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
1013 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
1015 timerclear(&p
->p_rlim_cpu
);
1017 psignal(p
, SIGXCPU
);
1023 if (newrlim
->rlim_cur
> maxdmap
) {
1024 newrlim
->rlim_cur
= maxdmap
;
1026 if (newrlim
->rlim_max
> maxdmap
) {
1027 newrlim
->rlim_max
= maxdmap
;
1032 if (p
->p_lflag
& P_LCUSTOM_STACK
) {
1033 /* Process has a custom stack set - rlimit cannot be used to change it */
1038 /* Disallow illegal stack size instead of clipping */
1039 if (newrlim
->rlim_cur
> maxsmap
||
1040 newrlim
->rlim_max
> maxsmap
) {
1046 * 4797860 - workaround poorly written installers by
1047 * doing previous implementation (< 10.5) when caller
1048 * is non-POSIX conforming.
1050 if (newrlim
->rlim_cur
> maxsmap
) {
1051 newrlim
->rlim_cur
= maxsmap
;
1053 if (newrlim
->rlim_max
> maxsmap
) {
1054 newrlim
->rlim_max
= maxsmap
;
1060 * Stack is allocated to the max at exec time with only
1061 * "rlim_cur" bytes accessible. If stack limit is going
1062 * up make more accessible, if going down make inaccessible.
1064 if (newrlim
->rlim_cur
> rlim
.rlim_cur
) {
1065 mach_vm_offset_t addr
;
1066 mach_vm_size_t size
;
1069 size
= round_page_64(newrlim
->rlim_cur
);
1070 size
-= round_page_64(rlim
.rlim_cur
);
1072 addr
= (mach_vm_offset_t
)(p
->user_stack
- round_page_64(newrlim
->rlim_cur
));
1073 kr
= mach_vm_protect(current_map(), addr
, size
, FALSE
, VM_PROT_DEFAULT
);
1074 if (kr
!= KERN_SUCCESS
) {
1078 } else if (newrlim
->rlim_cur
< rlim
.rlim_cur
) {
1079 mach_vm_offset_t addr
;
1080 mach_vm_size_t size
;
1086 * First check if new stack limit would agree
1087 * with current stack usage.
1088 * Get the current thread's stack pointer...
1090 cur_sp
= thread_adjuserstack(current_thread(), 0);
1091 if (cur_sp
<= p
->user_stack
&&
1092 cur_sp
> (p
->user_stack
- round_page_64(rlim
.rlim_cur
))) {
1093 /* stack pointer is in main stack */
1094 if (cur_sp
<= (p
->user_stack
- round_page_64(newrlim
->rlim_cur
))) {
1096 * New limit would cause current usage to be invalid:
1103 /* not on the main stack: reject */
1108 size
= round_page_64(rlim
.rlim_cur
);
1109 size
-= round_page_64(rlim
.rlim_cur
);
1111 addr
= (mach_vm_offset_t
)(p
->user_stack
- round_page_64(rlim
.rlim_cur
));
1113 kr
= mach_vm_protect(current_map(), addr
, size
, FALSE
, VM_PROT_NONE
);
1114 if (kr
!= KERN_SUCCESS
) {
1125 * Nothing to be done here as we already performed the sanity checks before entering the switch code block.
1126 * The real NOFILE limits enforced by the kernel is capped at MIN(RLIMIT_NOFILE, maxfilesperproc)
1132 * Only root can set to the maxproc limits, as it is
1133 * systemwide resource; all others are limited to
1134 * maxprocperuid (presumably less than maxproc).
1136 if (kauth_cred_issuser(kauth_cred_get())) {
1137 if (newrlim
->rlim_cur
> (rlim_t
)maxproc
) {
1138 newrlim
->rlim_cur
= maxproc
;
1140 if (newrlim
->rlim_max
> (rlim_t
)maxproc
) {
1141 newrlim
->rlim_max
= maxproc
;
1144 if (newrlim
->rlim_cur
> (rlim_t
)maxprocperuid
) {
1145 newrlim
->rlim_cur
= maxprocperuid
;
1147 if (newrlim
->rlim_max
> (rlim_t
)maxprocperuid
) {
1148 newrlim
->rlim_max
= maxprocperuid
;
1153 case RLIMIT_MEMLOCK
:
1155 * Tell the Mach VM layer about the new limit value.
1157 newrlim
->rlim_cur
= (vm_size_t
)newrlim
->rlim_cur
;
1158 vm_map_set_user_wire_limit(current_map(), (vm_size_t
)newrlim
->rlim_cur
);
1162 /* Everything checks out and we are now ready to update the rlimit */
1169 * COW the current plimit if it's shared, otherwise update it in place.
1170 * Finally unblock other threads wishing to change plimit.
1173 proc_limitupdate(p
, newrlim
, (uint8_t)which
);
1174 proc_limitunblock(p
);
1178 * This setrlimit has failed, just leave the plimit as is and unblock other
1179 * threads wishing to change plimit.
1182 proc_limitunblock(p
);
1191 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
1193 struct rlimit lim
= {};
1196 * Take out flag now in case we need to use it to trigger variant
1199 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
1201 if (uap
->which
>= RLIM_NLIMITS
) {
1204 proc_limitget(p
, uap
->which
, &lim
);
1205 return copyout((caddr_t
)&lim
,
1206 uap
->rlp
, sizeof(struct rlimit
));
1210 * Transform the running time and tick information in proc p into user,
1211 * system, and interrupt time usage.
1213 /* No lock on proc is held for this.. */
1215 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
1227 mach_task_basic_info_data_t tinfo
;
1228 task_thread_times_info_data_t ttimesinfo
;
1229 task_events_info_data_t teventsinfo
;
1230 mach_msg_type_number_t task_info_count
, task_ttimes_count
;
1231 mach_msg_type_number_t task_events_count
;
1232 struct timeval ut
, st
;
1234 task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
1235 task_info(task
, MACH_TASK_BASIC_INFO
,
1236 (task_info_t
)&tinfo
, &task_info_count
);
1237 ut
.tv_sec
= tinfo
.user_time
.seconds
;
1238 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
1239 st
.tv_sec
= tinfo
.system_time
.seconds
;
1240 st
.tv_usec
= tinfo
.system_time
.microseconds
;
1241 timeradd(&ut
, up
, up
);
1242 timeradd(&st
, sp
, sp
);
1244 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1245 task_info(task
, TASK_THREAD_TIMES_INFO
,
1246 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
1248 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1249 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1250 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1251 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1252 timeradd(&ut
, up
, up
);
1253 timeradd(&st
, sp
, sp
);
1255 task_events_count
= TASK_EVENTS_INFO_COUNT
;
1256 task_info(task
, TASK_EVENTS_INFO
,
1257 (task_info_t
)&teventsinfo
, &task_events_count
);
1260 * No need to lock "p": this does not need to be
1261 * completely consistent, right ?
1263 p
->p_stats
->p_ru
.ru_minflt
= (teventsinfo
.faults
-
1264 teventsinfo
.pageins
);
1265 p
->p_stats
->p_ru
.ru_majflt
= teventsinfo
.pageins
;
1266 p
->p_stats
->p_ru
.ru_nivcsw
= (teventsinfo
.csw
-
1267 p
->p_stats
->p_ru
.ru_nvcsw
);
1268 if (p
->p_stats
->p_ru
.ru_nivcsw
< 0) {
1269 p
->p_stats
->p_ru
.ru_nivcsw
= 0;
1272 p
->p_stats
->p_ru
.ru_maxrss
= (long)tinfo
.resident_size_max
;
1276 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
1277 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
1281 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1283 struct rusage
*rup
, rubuf
;
1284 struct user64_rusage rubuf64
= {};
1285 struct user32_rusage rubuf32
= {};
1286 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1287 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1288 struct timeval utime
;
1289 struct timeval stime
;
1294 calcru(p
, &utime
, &stime
, NULL
);
1296 rup
= &p
->p_stats
->p_ru
;
1297 rup
->ru_utime
= utime
;
1298 rup
->ru_stime
= stime
;
1305 case RUSAGE_CHILDREN
:
1307 rup
= &p
->p_stats
->p_cru
;
1315 if (IS_64BIT_PROCESS(p
)) {
1316 retsize
= sizeof(rubuf64
);
1317 retbuf
= (caddr_t
)&rubuf64
;
1318 munge_user64_rusage(&rubuf
, &rubuf64
);
1320 retsize
= sizeof(rubuf32
);
1321 retbuf
= (caddr_t
)&rubuf32
;
1322 munge_user32_rusage(&rubuf
, &rubuf32
);
1325 return copyout(retbuf
, uap
->rusage
, retsize
);
1329 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
1334 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
1335 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
1336 if (ru
->ru_maxrss
< ru2
->ru_maxrss
) {
1337 ru
->ru_maxrss
= ru2
->ru_maxrss
;
1339 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
1340 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--) {
1346 * Add the rusage stats of child in parent.
1348 * It adds rusage statistics of child process and statistics of all its
1349 * children to its parent.
1351 * Note: proc lock of parent should be held while calling this function.
1354 update_rusage_info_child(struct rusage_info_child
*ri
, rusage_info_current
*ri_current
)
1356 ri
->ri_child_user_time
+= (ri_current
->ri_user_time
+
1357 ri_current
->ri_child_user_time
);
1358 ri
->ri_child_system_time
+= (ri_current
->ri_system_time
+
1359 ri_current
->ri_child_system_time
);
1360 ri
->ri_child_pkg_idle_wkups
+= (ri_current
->ri_pkg_idle_wkups
+
1361 ri_current
->ri_child_pkg_idle_wkups
);
1362 ri
->ri_child_interrupt_wkups
+= (ri_current
->ri_interrupt_wkups
+
1363 ri_current
->ri_child_interrupt_wkups
);
1364 ri
->ri_child_pageins
+= (ri_current
->ri_pageins
+
1365 ri_current
->ri_child_pageins
);
1366 ri
->ri_child_elapsed_abstime
+= ((ri_current
->ri_proc_exit_abstime
-
1367 ri_current
->ri_proc_start_abstime
) + ri_current
->ri_child_elapsed_abstime
);
1371 * Reading soft limit from specified resource.
1374 proc_limitgetcur(proc_t p
, int which
, boolean_t to_lock_proc
)
1379 assert(which
< RLIM_NLIMITS
);
1382 * Serialize access to the process's plimit pointer for concurrent threads.
1385 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
1389 rlim_cur
= p
->p_limit
->pl_rlimit
[which
].rlim_cur
;
1399 * Writing soft limit to specified resource. This is an internal function
1400 * used only by proc_exit and vfork_exit_internal to update RLIMIT_FSIZE in
1401 * place without invoking setrlimit.
1404 proc_limitsetcur_internal(proc_t p
, int which
, rlim_t value
)
1409 assertf(which
== RLIMIT_FSIZE
, "%s only supports RLIMIT_FSIZE\n", __FUNCTION__
);
1414 /* Only one thread is able to change rlimit values at a time */
1417 /* Prepare an rlimit for proc_limitupdate */
1418 rlim
= p
->p_limit
->pl_rlimit
[which
];
1419 rlim
.rlim_cur
= value
;
1422 * proc_limitupdate will COW the current plimit and update specified the soft limit
1423 * if the plimit is shared, otherwise it will update the soft limit in place.
1425 proc_limitupdate(p
, &rlim
, (uint8_t)which
);
1427 /* Unblock other threads wishing to change plimit */
1428 proc_limitunblock(p
);
1434 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1438 assert(which
< RLIM_NLIMITS
);
1440 /* Protect writes to the process's plimit pointer issued by concurrent threads */
1443 limp
->rlim_cur
= p
->p_limit
->pl_rlimit
[which
].rlim_cur
;
1444 limp
->rlim_max
= p
->p_limit
->pl_rlimit
[which
].rlim_max
;
1450 proc_limitfork(proc_t parent
, proc_t child
)
1452 assert(parent
&& child
);
1456 /* Child proc inherits parent's plimit */
1457 child
->p_limit
= parent
->p_limit
;
1459 /* Increment refcnt of the shared plimit */
1460 os_ref_retain(&parent
->p_limit
->pl_refcnt
);
1462 proc_unlock(parent
);
1466 proc_limitdrop(proc_t p
)
1468 struct plimit
*free_plim
= NULL
;
1469 os_ref_count_t refcnt
;
1473 /* Drop the plimit reference before exiting the system */
1474 refcnt
= os_ref_release(&p
->p_limit
->pl_refcnt
);
1476 free_plim
= p
->p_limit
;
1482 /* We are the last user of this plimit, free it now. */
1483 if (free_plim
!= NULL
) {
1484 zfree(plimit_zone
, free_plim
);
1489 * proc_limitblock/unblock are used to serialize access to plimit
1490 * from concurrent threads within the same process.
1491 * Callers must be holding the proc lock to enter, return with
1492 * the proc lock locked
1495 proc_limitblock(proc_t p
)
1497 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_OWNED
);
1499 while (p
->p_lflag
& P_LLIMCHANGE
) {
1500 p
->p_lflag
|= P_LLIMWAIT
;
1501 msleep(&p
->p_limit
, &p
->p_mlock
, 0, "proc_limitblock", NULL
);
1503 p
->p_lflag
|= P_LLIMCHANGE
;
1507 * Callers must be holding the proc lock to enter, return with
1508 * the proc lock locked
1511 proc_limitunblock(proc_t p
)
1513 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_OWNED
);
1515 p
->p_lflag
&= ~P_LLIMCHANGE
;
1516 if (p
->p_lflag
& P_LLIMWAIT
) {
1517 p
->p_lflag
&= ~P_LLIMWAIT
;
1518 wakeup(&p
->p_limit
);
1523 * Change the rlimit values of process "p" to "rlim" for resource "which".
1525 * If the current plimit is shared by multiple processes (refcnt > 1):
1526 * this routine replaces the process's original plimit with a new plimit,
1527 * update the requeted rlimit values, and free the original plimit if this
1528 * process is the last user.
1530 * If the current plimit is used only by the calling process (refcnt == 1):
1531 * this routine updates the new rlimit values in place.
1533 * Note: caller must be holding the proc lock before entering this routine.
1534 * This routine allocates and frees kernel memory without holding the proc lock
1535 * to minimize contention, and returns with the proc lock held.
1538 proc_limitupdate(proc_t p
, struct rlimit
*rlim
, uint8_t which
)
1540 struct plimit
*copy_plim
;
1541 struct plimit
*free_plim
;
1542 os_ref_count_t refcnt
;
1544 assert(p
&& p
->p_limit
);
1546 assert(which
< RLIM_NLIMITS
);
1547 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_OWNED
);
1550 * If we are the only user of this plimit, don't bother allocating a plimit
1551 * before making changes. Just modify the rlimit values in place.
1553 refcnt
= os_ref_get_count(&p
->p_limit
->pl_refcnt
);
1555 p
->p_limit
->pl_rlimit
[which
] = *rlim
;
1560 * Allocating a new plimit for this process to apply the requested rlimit values.
1561 * Not holding the lock on the original plimit gives other processes in the system
1562 * a chance to access the plimit while we wait for memory below.
1564 * The default zalloc should always succeed when WAIT flag.
1567 copy_plim
= zalloc(plimit_zone
);
1569 /* Copy the current p_limit */
1571 bcopy(p
->p_limit
->pl_rlimit
, copy_plim
->pl_rlimit
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
1574 * Drop our reference to the old plimit. Other processes sharing the old plimit could
1575 * have exited the system when we wait for memory for the new plimit above, thus, we
1576 * need to check the refcnt again and free the old plimit if this process is the last
1577 * user. Also since we are holding the proc lock here, it's impossible for another threads
1578 * to dereference the plimit, so it's safe to free the old plimit memory.
1581 refcnt
= os_ref_release(&p
->p_limit
->pl_refcnt
);
1583 free_plim
= p
->p_limit
;
1585 /* Initialize the newly allocated plimit */
1586 os_ref_init_count(©_plim
->pl_refcnt
, &rlimit_refgrp
, 1);
1588 /* Apply new rlimit values */
1589 copy_plim
->pl_rlimit
[which
] = *rlim
;
1591 /* All set, update the process's plimit pointer to the new plimit. */
1592 p
->p_limit
= copy_plim
;
1595 if (free_plim
!= NULL
) {
1596 zfree(plimit_zone
, free_plim
);
1599 /* Return with proc->p_mlock locked */
1604 iopolicysys_disk(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1606 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1608 iopolicysys_vfs_atime_updates(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1610 iopolicysys_vfs_materialize_dataless_files(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1612 iopolicysys_vfs_statfs_no_data_volume(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1614 iopolicysys_vfs_trigger_resolve(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1616 iopolicysys_vfs_ignore_content_protection(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1618 iopolicysys_vfs_ignore_node_permissions(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*ipo_param
);
1620 iopolicysys_vfs_skip_mtime_update(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1625 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1627 * Parameters: cmd Policy command
1628 * arg Pointer to policy arguments
1630 * Returns: 0 Success
1631 * EINVAL Invalid command or invalid policy arguments
1635 iopolicysys(struct proc
*p
, struct iopolicysys_args
*uap
, int32_t *retval
)
1638 struct _iopol_param_t iop_param
;
1640 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0) {
1644 switch (iop_param
.iop_iotype
) {
1645 case IOPOL_TYPE_DISK
:
1646 error
= iopolicysys_disk(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1647 if (error
== EIDRM
) {
1655 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY
:
1656 error
= iopolicysys_vfs_hfs_case_sensitivity(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1661 case IOPOL_TYPE_VFS_ATIME_UPDATES
:
1662 error
= iopolicysys_vfs_atime_updates(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1667 case IOPOL_TYPE_VFS_MATERIALIZE_DATALESS_FILES
:
1668 error
= iopolicysys_vfs_materialize_dataless_files(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1673 case IOPOL_TYPE_VFS_STATFS_NO_DATA_VOLUME
:
1674 error
= iopolicysys_vfs_statfs_no_data_volume(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1679 case IOPOL_TYPE_VFS_TRIGGER_RESOLVE
:
1680 error
= iopolicysys_vfs_trigger_resolve(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1685 case IOPOL_TYPE_VFS_IGNORE_CONTENT_PROTECTION
:
1686 error
= iopolicysys_vfs_ignore_content_protection(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1691 case IOPOL_TYPE_VFS_IGNORE_PERMISSIONS
:
1692 error
= iopolicysys_vfs_ignore_node_permissions(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1697 case IOPOL_TYPE_VFS_SKIP_MTIME_UPDATE
:
1698 error
= iopolicysys_vfs_skip_mtime_update(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1708 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1709 if (uap
->cmd
== IOPOL_CMD_GET
) {
1710 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1721 iopolicysys_disk(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1727 /* Validate scope */
1729 case IOPOL_SCOPE_PROCESS
:
1730 thread
= THREAD_NULL
;
1731 policy_flavor
= TASK_POLICY_IOPOL
;
1734 case IOPOL_SCOPE_THREAD
:
1735 thread
= current_thread();
1736 policy_flavor
= TASK_POLICY_IOPOL
;
1738 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1739 if (cmd
== IOPOL_CMD_SET
&& thread_has_qos_policy(thread
)) {
1745 case IOPOL_THROTTLE
:
1746 case IOPOL_IMPORTANT
:
1747 case IOPOL_STANDARD
:
1748 if (!thread_is_static_param(thread
)) {
1749 thread_remove_qos_policy(thread
);
1751 * This is not an error case, this is to return a marker to user-space that
1752 * we stripped the thread of its QoS class.
1765 case IOPOL_SCOPE_DARWIN_BG
:
1766 #if !defined(XNU_TARGET_OS_OSX)
1767 /* We don't want this on platforms outside of macOS as BG is always IOPOL_THROTTLE */
1770 #else /* !defined(XNU_TARGET_OS_OSX) */
1771 thread
= THREAD_NULL
;
1772 policy_flavor
= TASK_POLICY_DARWIN_BG_IOPOL
;
1774 #endif /* !defined(XNU_TARGET_OS_OSX) */
1781 /* Validate policy */
1782 if (cmd
== IOPOL_CMD_SET
) {
1785 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1786 /* the current default BG throttle level is UTILITY */
1787 policy
= IOPOL_UTILITY
;
1789 policy
= IOPOL_IMPORTANT
;
1794 case IOPOL_THROTTLE
:
1795 /* These levels are OK */
1797 case IOPOL_IMPORTANT
:
1799 case IOPOL_STANDARD
:
1802 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1803 /* These levels are invalid for BG */
1807 /* OK for other scopes */
1816 /* Perform command */
1819 if (thread
!= THREAD_NULL
) {
1820 proc_set_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1822 proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1826 if (thread
!= THREAD_NULL
) {
1827 policy
= proc_get_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
);
1829 policy
= proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
);
1831 iop_param
->iop_policy
= policy
;
1834 error
= EINVAL
; /* unknown command */
1843 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1847 /* Validate scope */
1849 case IOPOL_SCOPE_PROCESS
:
1850 /* Only process OK */
1857 /* Validate policy */
1858 if (cmd
== IOPOL_CMD_SET
) {
1860 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1862 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1863 /* These policies are OK */
1871 /* Perform command */
1874 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1875 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1876 boolean_t entitled
= FALSE
;
1877 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1885 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1886 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
), &p
->p_vfs_iopolicy
);
1888 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1889 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
, &p
->p_vfs_iopolicy
);
1898 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
)
1899 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1900 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
;
1903 error
= EINVAL
; /* unknown command */
1912 get_thread_atime_policy(struct uthread
*ut
)
1914 return (ut
->uu_flag
& UT_ATIME_UPDATE
) ? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1918 set_thread_atime_policy(struct uthread
*ut
, int policy
)
1920 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1921 ut
->uu_flag
|= UT_ATIME_UPDATE
;
1923 ut
->uu_flag
&= ~UT_ATIME_UPDATE
;
1928 set_task_atime_policy(struct proc
*p
, int policy
)
1930 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1931 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
, &p
->p_vfs_iopolicy
);
1933 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
), &p
->p_vfs_iopolicy
);
1938 get_task_atime_policy(struct proc
*p
)
1940 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_ATIME_UPDATES
) ? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1944 iopolicysys_vfs_atime_updates(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1949 /* Validate scope */
1951 case IOPOL_SCOPE_THREAD
:
1952 thread
= current_thread();
1954 case IOPOL_SCOPE_PROCESS
:
1955 thread
= THREAD_NULL
;
1962 /* Validate policy */
1963 if (cmd
== IOPOL_CMD_SET
) {
1965 case IOPOL_ATIME_UPDATES_DEFAULT
:
1966 case IOPOL_ATIME_UPDATES_OFF
:
1974 /* Perform command */
1977 if (thread
!= THREAD_NULL
) {
1978 set_thread_atime_policy(get_bsdthread_info(thread
), policy
);
1980 set_task_atime_policy(p
, policy
);
1984 if (thread
!= THREAD_NULL
) {
1985 policy
= get_thread_atime_policy(get_bsdthread_info(thread
));
1987 policy
= get_task_atime_policy(p
);
1989 iop_param
->iop_policy
= policy
;
1992 error
= EINVAL
; /* unknown command */
2001 get_thread_materialize_policy(struct uthread
*ut
)
2003 if (ut
->uu_flag
& UT_NSPACE_NODATALESSFAULTS
) {
2004 return IOPOL_MATERIALIZE_DATALESS_FILES_OFF
;
2005 } else if (ut
->uu_flag
& UT_NSPACE_FORCEDATALESSFAULTS
) {
2006 return IOPOL_MATERIALIZE_DATALESS_FILES_ON
;
2008 /* Default thread behavior is "inherit process behavior". */
2009 return IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT
;
2013 set_thread_materialize_policy(struct uthread
*ut
, int policy
)
2015 if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_OFF
) {
2016 ut
->uu_flag
&= ~UT_NSPACE_FORCEDATALESSFAULTS
;
2017 ut
->uu_flag
|= UT_NSPACE_NODATALESSFAULTS
;
2018 } else if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_ON
) {
2019 ut
->uu_flag
&= ~UT_NSPACE_NODATALESSFAULTS
;
2020 ut
->uu_flag
|= UT_NSPACE_FORCEDATALESSFAULTS
;
2022 ut
->uu_flag
&= ~(UT_NSPACE_NODATALESSFAULTS
| UT_NSPACE_FORCEDATALESSFAULTS
);
2027 set_proc_materialize_policy(struct proc
*p
, int policy
)
2029 if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT
) {
2031 * Caller has specified "use the default policy".
2032 * The default policy is to NOT materialize dataless
2035 policy
= IOPOL_MATERIALIZE_DATALESS_FILES_OFF
;
2037 if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_ON
) {
2038 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
, &p
->p_vfs_iopolicy
);
2040 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
), &p
->p_vfs_iopolicy
);
2045 get_proc_materialize_policy(struct proc
*p
)
2047 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
) ? IOPOL_MATERIALIZE_DATALESS_FILES_ON
: IOPOL_MATERIALIZE_DATALESS_FILES_OFF
;
2051 iopolicysys_vfs_materialize_dataless_files(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
2056 /* Validate scope */
2058 case IOPOL_SCOPE_THREAD
:
2059 thread
= current_thread();
2061 case IOPOL_SCOPE_PROCESS
:
2062 thread
= THREAD_NULL
;
2069 /* Validate policy */
2070 if (cmd
== IOPOL_CMD_SET
) {
2072 case IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT
:
2073 case IOPOL_MATERIALIZE_DATALESS_FILES_OFF
:
2074 case IOPOL_MATERIALIZE_DATALESS_FILES_ON
:
2082 /* Perform command */
2085 if (thread
!= THREAD_NULL
) {
2086 set_thread_materialize_policy(get_bsdthread_info(thread
), policy
);
2088 set_proc_materialize_policy(p
, policy
);
2092 if (thread
!= THREAD_NULL
) {
2093 policy
= get_thread_materialize_policy(get_bsdthread_info(thread
));
2095 policy
= get_proc_materialize_policy(p
);
2097 iop_param
->iop_policy
= policy
;
2100 error
= EINVAL
; /* unknown command */
2109 iopolicysys_vfs_statfs_no_data_volume(struct proc
*p __unused
, int cmd
,
2110 int scope
, int policy
, struct _iopol_param_t
*iop_param
)
2114 /* Validate scope */
2116 case IOPOL_SCOPE_PROCESS
:
2117 /* Only process OK */
2124 /* Validate policy */
2125 if (cmd
== IOPOL_CMD_SET
) {
2127 case IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT
:
2129 case IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME
:
2130 /* These policies are OK */
2138 /* Perform command */
2141 if (0 == kauth_cred_issuser(kauth_cred_get())) {
2142 /* If it's a non-root process, it needs to have the entitlement to set the policy */
2143 boolean_t entitled
= FALSE
;
2144 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
2152 case IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT
:
2153 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
), &p
->p_vfs_iopolicy
);
2155 case IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME
:
2156 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
, &p
->p_vfs_iopolicy
);
2165 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
)
2166 ? IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME
2167 : IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT
;
2170 error
= EINVAL
; /* unknown command */
2179 iopolicysys_vfs_trigger_resolve(struct proc
*p __unused
, int cmd
,
2180 int scope
, int policy
, struct _iopol_param_t
*iop_param
)
2184 /* Validate scope */
2186 case IOPOL_SCOPE_PROCESS
:
2187 /* Only process OK */
2194 /* Validate policy */
2195 if (cmd
== IOPOL_CMD_SET
) {
2197 case IOPOL_VFS_TRIGGER_RESOLVE_DEFAULT
:
2199 case IOPOL_VFS_TRIGGER_RESOLVE_OFF
:
2200 /* These policies are OK */
2208 /* Perform command */
2212 case IOPOL_VFS_TRIGGER_RESOLVE_DEFAULT
:
2213 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_TRIGGER_RESOLVE_DISABLE
), &p
->p_vfs_iopolicy
);
2215 case IOPOL_VFS_TRIGGER_RESOLVE_OFF
:
2216 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_TRIGGER_RESOLVE_DISABLE
, &p
->p_vfs_iopolicy
);
2225 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_TRIGGER_RESOLVE_DISABLE
)
2226 ? IOPOL_VFS_TRIGGER_RESOLVE_OFF
2227 : IOPOL_VFS_TRIGGER_RESOLVE_DEFAULT
;
2230 error
= EINVAL
; /* unknown command */
2239 iopolicysys_vfs_ignore_content_protection(struct proc
*p
, int cmd
, int scope
,
2240 int policy
, struct _iopol_param_t
*iop_param
)
2244 /* Validate scope */
2246 case IOPOL_SCOPE_PROCESS
:
2247 /* Only process OK */
2254 /* Validate policy */
2255 if (cmd
== IOPOL_CMD_SET
) {
2257 case IOPOL_VFS_CONTENT_PROTECTION_DEFAULT
:
2259 case IOPOL_VFS_CONTENT_PROTECTION_IGNORE
:
2260 /* These policies are OK */
2268 /* Perform command */
2271 if (0 == kauth_cred_issuser(kauth_cred_get())) {
2272 /* If it's a non-root process, it needs to have the entitlement to set the policy */
2273 boolean_t entitled
= FALSE
;
2274 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
2282 case IOPOL_VFS_CONTENT_PROTECTION_DEFAULT
:
2283 os_atomic_andnot(&p
->p_vfs_iopolicy
, P_VFS_IOPOLICY_IGNORE_CONTENT_PROTECTION
, relaxed
);
2285 case IOPOL_VFS_CONTENT_PROTECTION_IGNORE
:
2286 os_atomic_or(&p
->p_vfs_iopolicy
, P_VFS_IOPOLICY_IGNORE_CONTENT_PROTECTION
, relaxed
);
2295 iop_param
->iop_policy
= (os_atomic_load(&p
->p_vfs_iopolicy
, relaxed
) & P_VFS_IOPOLICY_IGNORE_CONTENT_PROTECTION
)
2296 ? IOPOL_VFS_CONTENT_PROTECTION_IGNORE
2297 : IOPOL_VFS_CONTENT_PROTECTION_DEFAULT
;
2300 error
= EINVAL
; /* unknown command */
2308 #define AUTHORIZED_ACCESS_ENTITLEMENT \
2309 "com.apple.private.vfs.authorized-access"
2311 iopolicysys_vfs_ignore_node_permissions(struct proc
*p
, int cmd
, int scope
,
2312 int policy
, __unused
struct _iopol_param_t
*iop_param
)
2317 case IOPOL_SCOPE_PROCESS
:
2325 policy
= os_atomic_load(&p
->p_vfs_iopolicy
, relaxed
) & P_VFS_IOPOLICY_IGNORE_NODE_PERMISSIONS
?
2326 IOPOL_VFS_IGNORE_PERMISSIONS_ON
: IOPOL_VFS_IGNORE_PERMISSIONS_OFF
;
2327 iop_param
->iop_policy
= policy
;
2330 /* SET is handled after the switch */
2336 if (!IOTaskHasEntitlement(current_task(), AUTHORIZED_ACCESS_ENTITLEMENT
)) {
2342 case IOPOL_VFS_IGNORE_PERMISSIONS_OFF
:
2343 os_atomic_andnot(&p
->p_vfs_iopolicy
, P_VFS_IOPOLICY_IGNORE_NODE_PERMISSIONS
, relaxed
);
2345 case IOPOL_VFS_IGNORE_PERMISSIONS_ON
:
2346 os_atomic_or(&p
->p_vfs_iopolicy
, P_VFS_IOPOLICY_IGNORE_NODE_PERMISSIONS
, relaxed
);
2358 #define SKIP_MTIME_UPDATE_ENTITLEMENT \
2359 "com.apple.private.vfs.skip-mtime-updates"
2361 iopolicysys_vfs_skip_mtime_update(struct proc
*p
, int cmd
, int scope
,
2362 int policy
, __unused
struct _iopol_param_t
*iop_param
)
2367 case IOPOL_SCOPE_PROCESS
:
2375 policy
= os_atomic_load(&p
->p_vfs_iopolicy
, relaxed
) & P_VFS_IOPOLICY_SKIP_MTIME_UPDATE
?
2376 IOPOL_VFS_SKIP_MTIME_UPDATE_ON
: IOPOL_VFS_SKIP_MTIME_UPDATE_OFF
;
2377 iop_param
->iop_policy
= policy
;
2385 if (!IOTaskHasEntitlement(current_task(), SKIP_MTIME_UPDATE_ENTITLEMENT
)) {
2391 case IOPOL_VFS_SKIP_MTIME_UPDATE_OFF
:
2392 os_atomic_andnot(&p
->p_vfs_iopolicy
, P_VFS_IOPOLICY_SKIP_MTIME_UPDATE
, relaxed
);
2394 case IOPOL_VFS_SKIP_MTIME_UPDATE_ON
:
2395 os_atomic_or(&p
->p_vfs_iopolicy
, P_VFS_IOPOLICY_SKIP_MTIME_UPDATE
, relaxed
);
2406 /* BSD call back function for task_policy networking changes */
2408 proc_apply_task_networkbg(void * bsd_info
, thread_t thread
)
2410 assert(bsd_info
!= PROC_NULL
);
2412 pid_t pid
= proc_pid((proc_t
)bsd_info
);
2414 proc_t p
= proc_find(pid
);
2416 if (p
!= PROC_NULL
) {
2417 assert(p
== (proc_t
)bsd_info
);
2419 do_background_socket(p
, thread
);
2425 gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
)
2427 struct rusage_info_child
*ri_child
;
2429 assert(p
->p_stats
!= NULL
);
2430 memset(ru
, 0, sizeof(*ru
));
2432 case RUSAGE_INFO_V5
:
2433 #if !XNU_TARGET_OS_OSX && __has_feature(ptrauth_calls)
2434 if (vm_shared_region_is_reslide(p
->task
)) {
2435 ru
->ri_flags
|= RU_PROC_RUNS_RESLIDE
;
2437 #endif /* !XNU_TARGET_OS_OSX && __has_feature(ptrauth_calls) */
2439 case RUSAGE_INFO_V4
:
2440 ru
->ri_logical_writes
= get_task_logical_writes(p
->task
, FALSE
);
2441 ru
->ri_lifetime_max_phys_footprint
= get_task_phys_footprint_lifetime_max(p
->task
);
2442 #if CONFIG_LEDGER_INTERVAL_MAX
2443 ru
->ri_interval_max_phys_footprint
= get_task_phys_footprint_interval_max(p
->task
, FALSE
);
2445 fill_task_monotonic_rusage(p
->task
, ru
);
2448 case RUSAGE_INFO_V3
:
2449 fill_task_qos_rusage(p
->task
, ru
);
2450 fill_task_billed_usage(p
->task
, ru
);
2453 case RUSAGE_INFO_V2
:
2454 fill_task_io_rusage(p
->task
, ru
);
2457 case RUSAGE_INFO_V1
:
2459 * p->p_stats->ri_child statistics are protected under proc lock.
2463 ri_child
= &(p
->p_stats
->ri_child
);
2464 ru
->ri_child_user_time
= ri_child
->ri_child_user_time
;
2465 ru
->ri_child_system_time
= ri_child
->ri_child_system_time
;
2466 ru
->ri_child_pkg_idle_wkups
= ri_child
->ri_child_pkg_idle_wkups
;
2467 ru
->ri_child_interrupt_wkups
= ri_child
->ri_child_interrupt_wkups
;
2468 ru
->ri_child_pageins
= ri_child
->ri_child_pageins
;
2469 ru
->ri_child_elapsed_abstime
= ri_child
->ri_child_elapsed_abstime
;
2474 case RUSAGE_INFO_V0
:
2475 proc_getexecutableuuid(p
, (unsigned char *)&ru
->ri_uuid
, sizeof(ru
->ri_uuid
));
2476 fill_task_rusage(p
->task
, ru
);
2477 ru
->ri_proc_start_abstime
= p
->p_stats
->ps_start
;
2482 proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
)
2484 rusage_info_current ri_current
= {};
2490 case RUSAGE_INFO_V0
:
2491 size
= sizeof(struct rusage_info_v0
);
2494 case RUSAGE_INFO_V1
:
2495 size
= sizeof(struct rusage_info_v1
);
2498 case RUSAGE_INFO_V2
:
2499 size
= sizeof(struct rusage_info_v2
);
2502 case RUSAGE_INFO_V3
:
2503 size
= sizeof(struct rusage_info_v3
);
2506 case RUSAGE_INFO_V4
:
2507 size
= sizeof(struct rusage_info_v4
);
2510 case RUSAGE_INFO_V5
:
2511 size
= sizeof(struct rusage_info_v5
);
2522 * If task is still alive, collect info from the live task itself.
2523 * Otherwise, look to the cached info in the zombie proc.
2525 if (p
->p_ru
== NULL
) {
2526 gather_rusage_info(p
, &ri_current
, flavor
);
2527 ri_current
.ri_proc_exit_abstime
= 0;
2528 error
= copyout(&ri_current
, buffer
, size
);
2530 ri_current
= p
->p_ru
->ri
;
2531 error
= copyout(&p
->p_ru
->ri
, buffer
, size
);
2538 mach_to_bsd_rv(int mach_rv
)
2546 case KERN_INVALID_ARGUMENT
:
2550 panic("unknown error %#x", mach_rv
);
2557 * Resource limit controls
2559 * uap->flavor available flavors:
2561 * RLIMIT_WAKEUPS_MONITOR
2562 * RLIMIT_CPU_USAGE_MONITOR
2563 * RLIMIT_THREAD_CPULIMITS
2564 * RLIMIT_FOOTPRINT_INTERVAL
2567 proc_rlimit_control(__unused
struct proc
*p
, struct proc_rlimit_control_args
*uap
, __unused
int32_t *retval
)
2571 struct proc_rlimit_control_wakeupmon wakeupmon_args
;
2572 uint32_t cpumon_flags
;
2573 uint32_t cpulimits_flags
;
2574 kauth_cred_t my_cred
, target_cred
;
2575 #if CONFIG_LEDGER_INTERVAL_MAX
2576 uint32_t footprint_interval_flags
;
2577 uint64_t interval_max_footprint
;
2578 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
2580 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
2581 if (uap
->pid
== -1) {
2582 targetp
= proc_self();
2584 targetp
= proc_find(uap
->pid
);
2587 /* proc_self() can return NULL for an exiting process */
2588 if (targetp
== PROC_NULL
) {
2592 my_cred
= kauth_cred_get();
2593 target_cred
= kauth_cred_proc_ref(targetp
);
2595 if (!kauth_cred_issuser(my_cred
) && kauth_cred_getruid(my_cred
) &&
2596 kauth_cred_getuid(my_cred
) != kauth_cred_getuid(target_cred
) &&
2597 kauth_cred_getruid(my_cred
) != kauth_cred_getuid(target_cred
)) {
2599 kauth_cred_unref(&target_cred
);
2603 switch (uap
->flavor
) {
2604 case RLIMIT_WAKEUPS_MONITOR
:
2605 if ((error
= copyin(uap
->arg
, &wakeupmon_args
, sizeof(wakeupmon_args
))) != 0) {
2608 if ((error
= mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp
->task
, &wakeupmon_args
.wm_flags
,
2609 &wakeupmon_args
.wm_rate
))) != 0) {
2612 error
= copyout(&wakeupmon_args
, uap
->arg
, sizeof(wakeupmon_args
));
2614 case RLIMIT_CPU_USAGE_MONITOR
:
2615 cpumon_flags
= (uint32_t)uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
2616 error
= mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp
->task
, &cpumon_flags
));
2618 case RLIMIT_THREAD_CPULIMITS
:
2619 cpulimits_flags
= (uint32_t)uap
->arg
; // only need a limited set of bits, pass in void * argument
2621 if (uap
->pid
!= -1) {
2626 uint8_t percent
= 0;
2627 uint32_t ms_refill
= 0;
2630 percent
= (uint8_t)(cpulimits_flags
& 0xffU
); /* low 8 bits for percent */
2631 ms_refill
= (cpulimits_flags
>> 8) & 0xffffff; /* next 24 bits represent ms refill value */
2632 if (percent
>= 100) {
2637 ns_refill
= ((uint64_t)ms_refill
) * NSEC_PER_MSEC
;
2639 error
= mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK
, percent
, ns_refill
));
2642 #if CONFIG_LEDGER_INTERVAL_MAX
2643 case RLIMIT_FOOTPRINT_INTERVAL
:
2644 footprint_interval_flags
= (uint32_t)uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
2646 * There is currently only one option for this flavor.
2648 if ((footprint_interval_flags
& FOOTPRINT_INTERVAL_RESET
) == 0) {
2652 interval_max_footprint
= get_task_phys_footprint_interval_max(targetp
->task
, TRUE
);
2654 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
2661 kauth_cred_unref(&target_cred
);
2664 * Return value from this function becomes errno to userland caller.
2670 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
2673 thread_selfusage(struct proc
*p __unused
, struct thread_selfusage_args
*uap __unused
, uint64_t *retval
)
2677 runtime
= thread_get_runtime_self();
2685 thread_selfcounts(__unused
struct proc
*p
, __unused
struct thread_selfcounts_args
*uap
, __unused
int *ret_out
)
2689 #endif /* !MONOTONIC */