2 * Copyright (c) 2000-2018 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 */
145 * Limits on the number of open files per process, and the number
146 * of child processes per process.
148 * Note: would be in kern/subr_param.c in FreeBSD.
150 __private_extern__
int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
152 SYSCTL_INT(_kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
153 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
155 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
156 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
158 /* Args and fn for proc_iteration callback used in setpriority */
159 struct puser_nice_args
{
166 static int puser_donice_callback(proc_t p
, void * arg
);
169 /* Args and fn for proc_iteration callback used in setpriority */
170 struct ppgrp_nice_args
{
176 static int ppgrp_donice_callback(proc_t p
, void * arg
);
179 * Resource controls and accounting.
182 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, int32_t *retval
)
185 int low
= PRIO_MAX
+ 1;
186 kauth_cred_t my_cred
;
190 /* would also test (uap->who < 0), but id_t is unsigned */
191 if (uap
->who
> 0x7fffffff) {
195 switch (uap
->which
) {
201 p
= proc_find(uap
->who
);
211 struct pgrp
*pg
= PGRP_NULL
;
214 /* returns the pgrp to ref */
215 pg
= proc_pgrp(curp
);
216 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
219 /* No need for iteration as it is a simple scan */
221 PGMEMBERS_FOREACH(pg
, p
) {
222 if (p
->p_nice
< low
) {
233 uap
->who
= kauth_cred_getuid(kauth_cred_get());
238 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
239 my_cred
= kauth_cred_proc_ref(p
);
240 if (kauth_cred_getuid(my_cred
) == uap
->who
&&
244 kauth_cred_unref(&my_cred
);
251 case PRIO_DARWIN_THREAD
:
252 /* we currently only support the current thread */
257 low
= proc_get_thread_policy(current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_DARWIN_BG
);
261 case PRIO_DARWIN_PROCESS
:
265 p
= proc_find(uap
->who
);
266 if (p
== PROC_NULL
) {
272 error
= get_background_proc(curp
, p
, &low
);
282 case PRIO_DARWIN_ROLE
:
286 p
= proc_find(uap
->who
);
287 if (p
== PROC_NULL
) {
293 error
= proc_get_darwin_role(curp
, p
, &low
);
306 if (low
== PRIO_MAX
+ 1) {
313 /* call back function used for proc iteration in PRIO_USER */
315 puser_donice_callback(proc_t p
, void * arg
)
318 struct puser_nice_args
* pun
= (struct puser_nice_args
*)arg
;
319 kauth_cred_t my_cred
;
321 my_cred
= kauth_cred_proc_ref(p
);
322 if (kauth_cred_getuid(my_cred
) == pun
->who
) {
323 error
= donice(pun
->curp
, p
, pun
->prio
);
324 if (pun
->errorp
!= NULL
) {
325 *pun
->errorp
= error
;
327 if (pun
->foundp
!= NULL
) {
329 *pun
->foundp
= n
+ 1;
332 kauth_cred_unref(&my_cred
);
334 return PROC_RETURNED
;
337 /* call back function used for proc iteration in PRIO_PGRP */
339 ppgrp_donice_callback(proc_t p
, void * arg
)
342 struct ppgrp_nice_args
* pun
= (struct ppgrp_nice_args
*)arg
;
345 error
= donice(pun
->curp
, p
, pun
->prio
);
346 if (pun
->errorp
!= NULL
) {
347 *pun
->errorp
= error
;
349 if (pun
->foundp
!= NULL
) {
351 *pun
->foundp
= n
+ 1;
354 return PROC_RETURNED
;
366 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, int32_t *retval
)
369 int found
= 0, error
= 0;
372 AUDIT_ARG(cmd
, uap
->which
);
373 AUDIT_ARG(owner
, uap
->who
, 0);
374 AUDIT_ARG(value32
, uap
->prio
);
376 /* would also test (uap->who < 0), but id_t is unsigned */
377 if (uap
->who
> 0x7fffffff) {
381 switch (uap
->which
) {
386 p
= proc_find(uap
->who
);
392 error
= donice(curp
, p
, uap
->prio
);
400 struct pgrp
*pg
= PGRP_NULL
;
401 struct ppgrp_nice_args ppgrp
;
404 pg
= proc_pgrp(curp
);
405 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
410 ppgrp
.prio
= uap
->prio
;
411 ppgrp
.foundp
= &found
;
412 ppgrp
.errorp
= &error
;
414 /* PGRP_DROPREF drops the reference on process group */
415 pgrp_iterate(pg
, PGRP_DROPREF
, ppgrp_donice_callback
, (void *)&ppgrp
, NULL
, NULL
);
421 struct puser_nice_args punice
;
424 uap
->who
= kauth_cred_getuid(kauth_cred_get());
428 punice
.prio
= uap
->prio
;
429 punice
.who
= uap
->who
;
430 punice
.foundp
= &found
;
432 punice
.errorp
= &error
;
433 proc_iterate(PROC_ALLPROCLIST
, puser_donice_callback
, (void *)&punice
, NULL
, NULL
);
438 case PRIO_DARWIN_THREAD
: {
439 /* we currently only support the current thread */
444 error
= do_background_thread(current_thread(), uap
->prio
);
449 case PRIO_DARWIN_PROCESS
: {
453 p
= proc_find(uap
->who
);
460 error
= do_background_proc(curp
, p
, uap
->prio
);
469 case PRIO_DARWIN_GPU
: {
474 p
= proc_find(uap
->who
);
475 if (p
== PROC_NULL
) {
479 error
= set_gpudeny_proc(curp
, p
, uap
->prio
);
486 case PRIO_DARWIN_ROLE
: {
490 p
= proc_find(uap
->who
);
491 if (p
== PROC_NULL
) {
497 error
= proc_set_darwin_role(curp
, p
, uap
->prio
);
512 if (error
== EIDRM
) {
524 * mac_check_proc_sched:???
527 donice(struct proc
*curp
, struct proc
*chgp
, int n
)
531 kauth_cred_t my_cred
;
533 ucred
= kauth_cred_proc_ref(curp
);
534 my_cred
= kauth_cred_proc_ref(chgp
);
536 if (suser(ucred
, NULL
) && kauth_cred_getruid(ucred
) &&
537 kauth_cred_getuid(ucred
) != kauth_cred_getuid(my_cred
) &&
538 kauth_cred_getruid(ucred
) != kauth_cred_getuid(my_cred
)) {
548 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
)) {
553 error
= mac_proc_check_sched(curp
, chgp
);
561 (void)resetpriority(chgp
);
563 kauth_cred_unref(&ucred
);
564 kauth_cred_unref(&my_cred
);
569 set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
573 kauth_cred_t target_cred
;
575 ucred
= kauth_cred_get();
576 target_cred
= kauth_cred_proc_ref(targetp
);
578 /* TODO: Entitlement instead of uid check */
580 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
581 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
582 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
587 if (curp
== targetp
) {
593 error
= mac_proc_check_sched(curp
, targetp
);
600 case PRIO_DARWIN_GPU_DENY
:
601 task_set_gpu_denied(proc_task(targetp
), TRUE
);
603 case PRIO_DARWIN_GPU_ALLOW
:
604 task_set_gpu_denied(proc_task(targetp
), FALSE
);
612 kauth_cred_unref(&target_cred
);
617 proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
)
622 kauth_cred_t ucred
, target_cred
;
624 ucred
= kauth_cred_get();
625 target_cred
= kauth_cred_proc_ref(targetp
);
627 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
628 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
629 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
630 if (priv_check_cred(ucred
, PRIV_SETPRIORITY_DARWIN_ROLE
, 0) != 0) {
636 if (curp
!= targetp
) {
638 if ((error
= mac_proc_check_sched(curp
, targetp
))) {
644 proc_get_darwinbgstate(proc_task(targetp
), &flagsp
);
645 if ((flagsp
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
652 if ((error
= proc_darwin_role_to_task_role(priority
, &role
))) {
656 proc_set_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
,
657 TASK_POLICY_ROLE
, role
);
660 kauth_cred_unref(&target_cred
);
665 proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
)
670 kauth_cred_t ucred
, target_cred
;
672 ucred
= kauth_cred_get();
673 target_cred
= kauth_cred_proc_ref(targetp
);
675 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
676 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
677 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
682 if (curp
!= targetp
) {
684 if ((error
= mac_proc_check_sched(curp
, targetp
))) {
690 role
= proc_get_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
692 *priority
= proc_task_role_to_darwin_role(role
);
695 kauth_cred_unref(&target_cred
);
701 get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
)
705 kauth_cred_t ucred
, target_cred
;
707 ucred
= kauth_cred_get();
708 target_cred
= kauth_cred_proc_ref(targetp
);
710 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
711 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
712 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
717 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
719 *priority
= proc_get_task_policy(current_task(), external
, TASK_POLICY_DARWIN_BG
);
722 kauth_cred_unref(&target_cred
);
727 do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
734 kauth_cred_t target_cred
;
738 ucred
= kauth_cred_get();
739 target_cred
= kauth_cred_proc_ref(targetp
);
741 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
742 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
743 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
749 error
= mac_proc_check_sched(curp
, targetp
);
755 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
759 enable
= TASK_POLICY_ENABLE
;
761 case PRIO_DARWIN_NONUI
:
762 /* ignored for compatibility */
765 /* TODO: EINVAL if priority != 0 */
766 enable
= TASK_POLICY_DISABLE
;
770 proc_set_task_policy(proc_task(targetp
), external
, TASK_POLICY_DARWIN_BG
, enable
);
773 kauth_cred_unref(&target_cred
);
778 do_background_socket(struct proc
*p
, thread_t thread
)
781 struct filedesc
*fdp
;
784 int background
= false;
786 int update_necp
= false;
791 if (thread
!= THREAD_NULL
) {
792 background
= proc_get_effective_thread_policy(thread
, TASK_POLICY_ALL_SOCKETS_BG
);
794 background
= proc_get_effective_task_policy(proc_task(p
), TASK_POLICY_ALL_SOCKETS_BG
);
799 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
800 * the sockets with the background flag. There's nothing
801 * to do here for the PRIO_DARWIN_THREAD case.
803 if (thread
== THREAD_NULL
) {
806 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
807 fp
= fdp
->fd_ofiles
[i
];
808 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0) {
811 if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_SOCKET
) {
812 struct socket
*sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
813 socket_set_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
814 sockp
->so_background_thread
= NULL
;
817 else if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_NETPOLICY
) {
818 if (necp_set_client_as_background(p
, fp
, background
)) {
826 /* disable networking IO throttle.
827 * NOTE - It is a known limitation of the current design that we
828 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
829 * sockets created by other threads within this process.
832 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
833 struct socket
*sockp
;
835 fp
= fdp
->fd_ofiles
[i
];
836 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0) {
839 if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_SOCKET
) {
840 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
841 /* skip if only clearing this thread's sockets */
842 if ((thread
) && (sockp
->so_background_thread
!= thread
)) {
845 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
846 sockp
->so_background_thread
= NULL
;
849 else if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_NETPOLICY
) {
850 if (necp_set_client_as_background(p
, fp
, background
)) {
862 necp_update_all_clients();
866 #pragma unused(p, thread)
872 * do_background_thread
874 * Requires: thread reference
877 * EPERM Tried to background while in vfork
878 * XXX - todo - does this need a MACF hook?
881 do_background_thread(thread_t thread
, int priority
)
884 int enable
, external
;
887 ut
= get_bsdthread_info(thread
);
889 /* Backgrounding is unsupported for threads in vfork */
890 if ((ut
->uu_flag
& UT_VFORK
) != 0) {
894 /* Backgrounding is unsupported for workq threads */
895 if (thread_is_static_param(thread
)) {
899 /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
900 if (thread_has_qos_policy(thread
)) {
901 thread_remove_qos_policy(thread
);
905 /* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
906 enable
= (priority
== PRIO_DARWIN_BG
) ? TASK_POLICY_ENABLE
: TASK_POLICY_DISABLE
;
907 external
= (current_thread() == thread
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
909 proc_set_thread_policy(thread
, external
, TASK_POLICY_DARWIN_BG
, enable
);
922 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
927 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
928 sizeof(struct rlimit
)))) {
932 return dosetrlimit(p
, uap
->which
, &alim
);
938 * ENOMEM Cannot copy limit structure
941 * Notes: EINVAL is returned both for invalid arguments, and in the
942 * case that the current usage (e.g. RLIMIT_STACK) is already
943 * in excess of the requested limit.
946 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
)
948 struct rlimit
*alimp
;
951 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
953 /* Mask out POSIX flag, saved above */
954 which
&= ~_RLIMIT_POSIX_FLAG
;
956 if (which
>= RLIM_NLIMITS
) {
960 alimp
= &p
->p_rlimit
[which
];
961 if (limp
->rlim_cur
> limp
->rlim_max
) {
965 if (limp
->rlim_cur
> alimp
->rlim_max
||
966 limp
->rlim_max
> alimp
->rlim_max
) {
967 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
974 if ((error
= proc_limitreplace(p
)) != 0) {
975 proc_limitunblock(p
);
979 alimp
= &p
->p_rlimit
[which
];
983 if (limp
->rlim_cur
== RLIM_INFINITY
) {
984 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
985 timerclear(&p
->p_rlim_cpu
);
987 task_absolutetime_info_data_t tinfo
;
988 mach_msg_type_number_t count
;
989 struct timeval ttv
, tv
;
991 clock_usec_t tv_usec
;
993 count
= TASK_ABSOLUTETIME_INFO_COUNT
;
994 task_info(p
->task
, TASK_ABSOLUTETIME_INFO
,
995 (task_info_t
)&tinfo
, &count
);
996 absolutetime_to_microtime(tinfo
.total_user
+ tinfo
.total_system
,
999 ttv
.tv_usec
= tv_usec
;
1001 tv
.tv_sec
= (limp
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: limp
->rlim_cur
);
1003 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
1006 if (timercmp(&p
->p_rlim_cpu
, &tv
, >)) {
1007 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
1009 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
1011 timerclear(&p
->p_rlim_cpu
);
1013 psignal(p
, SIGXCPU
);
1019 if (limp
->rlim_cur
> maxdmap
) {
1020 limp
->rlim_cur
= maxdmap
;
1022 if (limp
->rlim_max
> maxdmap
) {
1023 limp
->rlim_max
= maxdmap
;
1028 if (p
->p_lflag
& P_LCUSTOM_STACK
) {
1029 /* Process has a custom stack set - rlimit cannot be used to change it */
1034 /* Disallow illegal stack size instead of clipping */
1035 if (limp
->rlim_cur
> maxsmap
||
1036 limp
->rlim_max
> maxsmap
) {
1042 * 4797860 - workaround poorly written installers by
1043 * doing previous implementation (< 10.5) when caller
1044 * is non-POSIX conforming.
1046 if (limp
->rlim_cur
> maxsmap
) {
1047 limp
->rlim_cur
= maxsmap
;
1049 if (limp
->rlim_max
> maxsmap
) {
1050 limp
->rlim_max
= maxsmap
;
1056 * Stack is allocated to the max at exec time with only
1057 * "rlim_cur" bytes accessible. If stack limit is going
1058 * up make more accessible, if going down make inaccessible.
1060 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
1065 size
= round_page_64(limp
->rlim_cur
);
1066 size
-= round_page_64(alimp
->rlim_cur
);
1068 addr
= p
->user_stack
- round_page_64(limp
->rlim_cur
);
1069 kr
= mach_vm_protect(current_map(),
1071 FALSE
, VM_PROT_DEFAULT
);
1072 if (kr
!= KERN_SUCCESS
) {
1076 } else if (limp
->rlim_cur
< alimp
->rlim_cur
) {
1084 * First check if new stack limit would agree
1085 * with current stack usage.
1086 * Get the current thread's stack pointer...
1088 cur_sp
= thread_adjuserstack(current_thread(),
1090 if (cur_sp
<= p
->user_stack
&&
1091 cur_sp
> (p
->user_stack
-
1092 round_page_64(alimp
->rlim_cur
))) {
1093 /* stack pointer is in main stack */
1094 if (cur_sp
<= (p
->user_stack
-
1095 round_page_64(limp
->rlim_cur
))) {
1097 * New limit would cause
1098 * current usage to be invalid:
1105 /* not on the main stack: reject */
1110 size
= round_page_64(alimp
->rlim_cur
);
1111 size
-= round_page_64(limp
->rlim_cur
);
1113 addr
= p
->user_stack
- round_page_64(alimp
->rlim_cur
);
1115 kr
= mach_vm_protect(current_map(),
1117 FALSE
, VM_PROT_NONE
);
1118 if (kr
!= KERN_SUCCESS
) {
1129 * Only root can set the maxfiles limits, as it is
1130 * systemwide resource. If we are expecting POSIX behavior,
1131 * instead of clamping the value, return EINVAL. We do this
1132 * because historically, people have been able to attempt to
1133 * set RLIM_INFINITY to get "whatever the maximum is".
1135 if (kauth_cred_issuser(kauth_cred_get())) {
1136 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1137 limp
->rlim_cur
> (rlim_t
)maxfiles
) {
1142 limp
->rlim_cur
= maxfiles
;
1144 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1145 limp
->rlim_max
> (rlim_t
)maxfiles
) {
1146 limp
->rlim_max
= maxfiles
;
1149 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1150 limp
->rlim_cur
> (rlim_t
)maxfilesperproc
) {
1155 limp
->rlim_cur
= maxfilesperproc
;
1157 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1158 limp
->rlim_max
> (rlim_t
)maxfilesperproc
) {
1159 limp
->rlim_max
= maxfilesperproc
;
1166 * Only root can set to the maxproc limits, as it is
1167 * systemwide resource; all others are limited to
1168 * maxprocperuid (presumably less than maxproc).
1170 if (kauth_cred_issuser(kauth_cred_get())) {
1171 if (limp
->rlim_cur
> (rlim_t
)maxproc
) {
1172 limp
->rlim_cur
= maxproc
;
1174 if (limp
->rlim_max
> (rlim_t
)maxproc
) {
1175 limp
->rlim_max
= maxproc
;
1178 if (limp
->rlim_cur
> (rlim_t
)maxprocperuid
) {
1179 limp
->rlim_cur
= maxprocperuid
;
1181 if (limp
->rlim_max
> (rlim_t
)maxprocperuid
) {
1182 limp
->rlim_max
= maxprocperuid
;
1187 case RLIMIT_MEMLOCK
:
1189 * Tell the Mach VM layer about the new limit value.
1192 vm_map_set_user_wire_limit(current_map(), limp
->rlim_cur
);
1200 proc_limitunblock(p
);
1206 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
1208 struct rlimit lim
= {};
1211 * Take out flag now in case we need to use it to trigger variant
1214 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
1216 if (uap
->which
>= RLIM_NLIMITS
) {
1219 proc_limitget(p
, uap
->which
, &lim
);
1220 return copyout((caddr_t
)&lim
,
1221 uap
->rlp
, sizeof(struct rlimit
));
1225 * Transform the running time and tick information in proc p into user,
1226 * system, and interrupt time usage.
1228 /* No lock on proc is held for this.. */
1230 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
1242 mach_task_basic_info_data_t tinfo
;
1243 task_thread_times_info_data_t ttimesinfo
;
1244 task_events_info_data_t teventsinfo
;
1245 mach_msg_type_number_t task_info_count
, task_ttimes_count
;
1246 mach_msg_type_number_t task_events_count
;
1247 struct timeval ut
, st
;
1249 task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
1250 task_info(task
, MACH_TASK_BASIC_INFO
,
1251 (task_info_t
)&tinfo
, &task_info_count
);
1252 ut
.tv_sec
= tinfo
.user_time
.seconds
;
1253 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
1254 st
.tv_sec
= tinfo
.system_time
.seconds
;
1255 st
.tv_usec
= tinfo
.system_time
.microseconds
;
1256 timeradd(&ut
, up
, up
);
1257 timeradd(&st
, sp
, sp
);
1259 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1260 task_info(task
, TASK_THREAD_TIMES_INFO
,
1261 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
1263 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1264 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1265 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1266 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1267 timeradd(&ut
, up
, up
);
1268 timeradd(&st
, sp
, sp
);
1270 task_events_count
= TASK_EVENTS_INFO_COUNT
;
1271 task_info(task
, TASK_EVENTS_INFO
,
1272 (task_info_t
)&teventsinfo
, &task_events_count
);
1275 * No need to lock "p": this does not need to be
1276 * completely consistent, right ?
1278 p
->p_stats
->p_ru
.ru_minflt
= (teventsinfo
.faults
-
1279 teventsinfo
.pageins
);
1280 p
->p_stats
->p_ru
.ru_majflt
= teventsinfo
.pageins
;
1281 p
->p_stats
->p_ru
.ru_nivcsw
= (teventsinfo
.csw
-
1282 p
->p_stats
->p_ru
.ru_nvcsw
);
1283 if (p
->p_stats
->p_ru
.ru_nivcsw
< 0) {
1284 p
->p_stats
->p_ru
.ru_nivcsw
= 0;
1287 p
->p_stats
->p_ru
.ru_maxrss
= tinfo
.resident_size_max
;
1291 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
1292 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
1296 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1298 struct rusage
*rup
, rubuf
;
1299 struct user64_rusage rubuf64
= {};
1300 struct user32_rusage rubuf32
= {};
1301 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1302 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1303 struct timeval utime
;
1304 struct timeval stime
;
1309 calcru(p
, &utime
, &stime
, NULL
);
1311 rup
= &p
->p_stats
->p_ru
;
1312 rup
->ru_utime
= utime
;
1313 rup
->ru_stime
= stime
;
1320 case RUSAGE_CHILDREN
:
1322 rup
= &p
->p_stats
->p_cru
;
1330 if (IS_64BIT_PROCESS(p
)) {
1331 retsize
= sizeof(rubuf64
);
1332 retbuf
= (caddr_t
)&rubuf64
;
1333 munge_user64_rusage(&rubuf
, &rubuf64
);
1335 retsize
= sizeof(rubuf32
);
1336 retbuf
= (caddr_t
)&rubuf32
;
1337 munge_user32_rusage(&rubuf
, &rubuf32
);
1340 return copyout(retbuf
, uap
->rusage
, retsize
);
1344 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
1349 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
1350 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
1351 if (ru
->ru_maxrss
< ru2
->ru_maxrss
) {
1352 ru
->ru_maxrss
= ru2
->ru_maxrss
;
1354 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
1355 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--) {
1361 * Add the rusage stats of child in parent.
1363 * It adds rusage statistics of child process and statistics of all its
1364 * children to its parent.
1366 * Note: proc lock of parent should be held while calling this function.
1369 update_rusage_info_child(struct rusage_info_child
*ri
, rusage_info_current
*ri_current
)
1371 ri
->ri_child_user_time
+= (ri_current
->ri_user_time
+
1372 ri_current
->ri_child_user_time
);
1373 ri
->ri_child_system_time
+= (ri_current
->ri_system_time
+
1374 ri_current
->ri_child_system_time
);
1375 ri
->ri_child_pkg_idle_wkups
+= (ri_current
->ri_pkg_idle_wkups
+
1376 ri_current
->ri_child_pkg_idle_wkups
);
1377 ri
->ri_child_interrupt_wkups
+= (ri_current
->ri_interrupt_wkups
+
1378 ri_current
->ri_child_interrupt_wkups
);
1379 ri
->ri_child_pageins
+= (ri_current
->ri_pageins
+
1380 ri_current
->ri_child_pageins
);
1381 ri
->ri_child_elapsed_abstime
+= ((ri_current
->ri_proc_exit_abstime
-
1382 ri_current
->ri_proc_start_abstime
) + ri_current
->ri_child_elapsed_abstime
);
1386 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1389 limp
->rlim_cur
= p
->p_rlimit
[which
].rlim_cur
;
1390 limp
->rlim_max
= p
->p_rlimit
[which
].rlim_max
;
1396 proc_limitdrop(proc_t p
, int exiting
)
1398 struct plimit
* freelim
= NULL
;
1399 struct plimit
* freeoldlim
= NULL
;
1403 if (--p
->p_limit
->pl_refcnt
== 0) {
1404 freelim
= p
->p_limit
;
1407 if ((exiting
!= 0) && (p
->p_olimit
!= NULL
) && (--p
->p_olimit
->pl_refcnt
== 0)) {
1408 freeoldlim
= p
->p_olimit
;
1413 if (freelim
!= NULL
) {
1414 FREE_ZONE(freelim
, sizeof *p
->p_limit
, M_PLIMIT
);
1416 if (freeoldlim
!= NULL
) {
1417 FREE_ZONE(freeoldlim
, sizeof *p
->p_olimit
, M_PLIMIT
);
1423 proc_limitfork(proc_t parent
, proc_t child
)
1426 child
->p_limit
= parent
->p_limit
;
1427 child
->p_limit
->pl_refcnt
++;
1428 child
->p_olimit
= NULL
;
1433 proc_limitblock(proc_t p
)
1436 while (p
->p_lflag
& P_LLIMCHANGE
) {
1437 p
->p_lflag
|= P_LLIMWAIT
;
1438 msleep(&p
->p_olimit
, &p
->p_mlock
, 0, "proc_limitblock", NULL
);
1440 p
->p_lflag
|= P_LLIMCHANGE
;
1446 proc_limitunblock(proc_t p
)
1449 p
->p_lflag
&= ~P_LLIMCHANGE
;
1450 if (p
->p_lflag
& P_LLIMWAIT
) {
1451 p
->p_lflag
&= ~P_LLIMWAIT
;
1452 wakeup(&p
->p_olimit
);
1457 /* This is called behind serialization provided by proc_limitblock/unlbock */
1459 proc_limitreplace(proc_t p
)
1461 struct plimit
*copy
;
1466 if (p
->p_limit
->pl_refcnt
== 1) {
1473 MALLOC_ZONE(copy
, struct plimit
*,
1474 sizeof(struct plimit
), M_PLIMIT
, M_WAITOK
);
1480 bcopy(p
->p_limit
->pl_rlimit
, copy
->pl_rlimit
,
1481 sizeof(struct rlimit
) * RLIM_NLIMITS
);
1482 copy
->pl_refcnt
= 1;
1483 /* hang on to reference to old till process exits */
1484 p
->p_olimit
= p
->p_limit
;
1492 iopolicysys_disk(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1494 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1496 iopolicysys_vfs_atime_updates(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1498 iopolicysys_vfs_materialize_dataless_files(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1500 iopolicysys_vfs_statfs_no_data_volume(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1505 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1507 * Parameters: cmd Policy command
1508 * arg Pointer to policy arguments
1510 * Returns: 0 Success
1511 * EINVAL Invalid command or invalid policy arguments
1515 iopolicysys(struct proc
*p
, struct iopolicysys_args
*uap
, int32_t *retval
)
1518 struct _iopol_param_t iop_param
;
1520 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0) {
1524 switch (iop_param
.iop_iotype
) {
1525 case IOPOL_TYPE_DISK
:
1526 error
= iopolicysys_disk(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1527 if (error
== EIDRM
) {
1535 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY
:
1536 error
= iopolicysys_vfs_hfs_case_sensitivity(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1541 case IOPOL_TYPE_VFS_ATIME_UPDATES
:
1542 error
= iopolicysys_vfs_atime_updates(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1547 case IOPOL_TYPE_VFS_MATERIALIZE_DATALESS_FILES
:
1548 error
= iopolicysys_vfs_materialize_dataless_files(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1553 case IOPOL_TYPE_VFS_STATFS_NO_DATA_VOLUME
:
1554 error
= iopolicysys_vfs_statfs_no_data_volume(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1563 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1564 if (uap
->cmd
== IOPOL_CMD_GET
) {
1565 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1576 iopolicysys_disk(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1582 /* Validate scope */
1584 case IOPOL_SCOPE_PROCESS
:
1585 thread
= THREAD_NULL
;
1586 policy_flavor
= TASK_POLICY_IOPOL
;
1589 case IOPOL_SCOPE_THREAD
:
1590 thread
= current_thread();
1591 policy_flavor
= TASK_POLICY_IOPOL
;
1593 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1594 if (cmd
== IOPOL_CMD_SET
&& thread_has_qos_policy(thread
)) {
1600 case IOPOL_THROTTLE
:
1601 case IOPOL_IMPORTANT
:
1602 case IOPOL_STANDARD
:
1603 if (!thread_is_static_param(thread
)) {
1604 thread_remove_qos_policy(thread
);
1606 * This is not an error case, this is to return a marker to user-space that
1607 * we stripped the thread of its QoS class.
1612 /* otherwise, fall through to the error case. */
1620 case IOPOL_SCOPE_DARWIN_BG
:
1622 /* Embedded doesn't want this as BG is always IOPOL_THROTTLE */
1625 #else /* CONFIG_EMBEDDED */
1626 thread
= THREAD_NULL
;
1627 policy_flavor
= TASK_POLICY_DARWIN_BG_IOPOL
;
1629 #endif /* CONFIG_EMBEDDED */
1636 /* Validate policy */
1637 if (cmd
== IOPOL_CMD_SET
) {
1640 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1641 /* the current default BG throttle level is UTILITY */
1642 policy
= IOPOL_UTILITY
;
1644 policy
= IOPOL_IMPORTANT
;
1649 case IOPOL_THROTTLE
:
1650 /* These levels are OK */
1652 case IOPOL_IMPORTANT
:
1654 case IOPOL_STANDARD
:
1657 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1658 /* These levels are invalid for BG */
1662 /* OK for other scopes */
1671 /* Perform command */
1674 if (thread
!= THREAD_NULL
) {
1675 proc_set_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1677 proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1681 if (thread
!= THREAD_NULL
) {
1682 policy
= proc_get_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
);
1684 policy
= proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
);
1686 iop_param
->iop_policy
= policy
;
1689 error
= EINVAL
; /* unknown command */
1698 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1702 /* Validate scope */
1704 case IOPOL_SCOPE_PROCESS
:
1705 /* Only process OK */
1712 /* Validate policy */
1713 if (cmd
== IOPOL_CMD_SET
) {
1715 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1717 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1718 /* These policies are OK */
1726 /* Perform command */
1729 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1730 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1731 boolean_t entitled
= FALSE
;
1732 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1740 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1741 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
), &p
->p_vfs_iopolicy
);
1743 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1744 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
, &p
->p_vfs_iopolicy
);
1753 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
)
1754 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1755 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
;
1758 error
= EINVAL
; /* unknown command */
1767 get_thread_atime_policy(struct uthread
*ut
)
1769 return (ut
->uu_flag
& UT_ATIME_UPDATE
) ? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1773 set_thread_atime_policy(struct uthread
*ut
, int policy
)
1775 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1776 ut
->uu_flag
|= UT_ATIME_UPDATE
;
1778 ut
->uu_flag
&= ~UT_ATIME_UPDATE
;
1783 set_task_atime_policy(struct proc
*p
, int policy
)
1785 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1786 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
, &p
->p_vfs_iopolicy
);
1788 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
), &p
->p_vfs_iopolicy
);
1793 get_task_atime_policy(struct proc
*p
)
1795 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_ATIME_UPDATES
) ? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1799 iopolicysys_vfs_atime_updates(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1804 /* Validate scope */
1806 case IOPOL_SCOPE_THREAD
:
1807 thread
= current_thread();
1809 case IOPOL_SCOPE_PROCESS
:
1810 thread
= THREAD_NULL
;
1817 /* Validate policy */
1818 if (cmd
== IOPOL_CMD_SET
) {
1820 case IOPOL_ATIME_UPDATES_DEFAULT
:
1821 case IOPOL_ATIME_UPDATES_OFF
:
1829 /* Perform command */
1832 if (thread
!= THREAD_NULL
) {
1833 set_thread_atime_policy(get_bsdthread_info(thread
), policy
);
1835 set_task_atime_policy(p
, policy
);
1839 if (thread
!= THREAD_NULL
) {
1840 policy
= get_thread_atime_policy(get_bsdthread_info(thread
));
1842 policy
= get_task_atime_policy(p
);
1844 iop_param
->iop_policy
= policy
;
1847 error
= EINVAL
; /* unknown command */
1856 get_thread_materialize_policy(struct uthread
*ut
)
1858 if (ut
->uu_flag
& UT_NSPACE_NODATALESSFAULTS
) {
1859 return IOPOL_MATERIALIZE_DATALESS_FILES_OFF
;
1860 } else if (ut
->uu_flag
& UT_NSPACE_FORCEDATALESSFAULTS
) {
1861 return IOPOL_MATERIALIZE_DATALESS_FILES_ON
;
1863 /* Default thread behavior is "inherit process behavior". */
1864 return IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT
;
1868 set_thread_materialize_policy(struct uthread
*ut
, int policy
)
1870 if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_OFF
) {
1871 ut
->uu_flag
&= ~UT_NSPACE_FORCEDATALESSFAULTS
;
1872 ut
->uu_flag
|= UT_NSPACE_NODATALESSFAULTS
;
1873 } else if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_ON
) {
1874 ut
->uu_flag
&= ~UT_NSPACE_NODATALESSFAULTS
;
1875 ut
->uu_flag
|= UT_NSPACE_FORCEDATALESSFAULTS
;
1877 ut
->uu_flag
&= ~(UT_NSPACE_NODATALESSFAULTS
| UT_NSPACE_FORCEDATALESSFAULTS
);
1882 set_proc_materialize_policy(struct proc
*p
, int policy
)
1884 if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT
) {
1886 * Caller has specified "use the default policy".
1887 * The default policy is to NOT materialize dataless
1890 policy
= IOPOL_MATERIALIZE_DATALESS_FILES_OFF
;
1892 if (policy
== IOPOL_MATERIALIZE_DATALESS_FILES_ON
) {
1893 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
, &p
->p_vfs_iopolicy
);
1895 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
), &p
->p_vfs_iopolicy
);
1900 get_proc_materialize_policy(struct proc
*p
)
1902 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
) ? IOPOL_MATERIALIZE_DATALESS_FILES_ON
: IOPOL_MATERIALIZE_DATALESS_FILES_OFF
;
1906 iopolicysys_vfs_materialize_dataless_files(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1911 /* Validate scope */
1913 case IOPOL_SCOPE_THREAD
:
1914 thread
= current_thread();
1916 case IOPOL_SCOPE_PROCESS
:
1917 thread
= THREAD_NULL
;
1924 /* Validate policy */
1925 if (cmd
== IOPOL_CMD_SET
) {
1927 case IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT
:
1928 case IOPOL_MATERIALIZE_DATALESS_FILES_OFF
:
1929 case IOPOL_MATERIALIZE_DATALESS_FILES_ON
:
1937 /* Perform command */
1940 if (thread
!= THREAD_NULL
) {
1941 set_thread_materialize_policy(get_bsdthread_info(thread
), policy
);
1943 set_proc_materialize_policy(p
, policy
);
1947 if (thread
!= THREAD_NULL
) {
1948 policy
= get_thread_materialize_policy(get_bsdthread_info(thread
));
1950 policy
= get_proc_materialize_policy(p
);
1952 iop_param
->iop_policy
= policy
;
1955 error
= EINVAL
; /* unknown command */
1964 iopolicysys_vfs_statfs_no_data_volume(struct proc
*p __unused
, int cmd
,
1965 int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1969 /* Validate scope */
1971 case IOPOL_SCOPE_PROCESS
:
1972 /* Only process OK */
1979 /* Validate policy */
1980 if (cmd
== IOPOL_CMD_SET
) {
1982 case IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT
:
1984 case IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME
:
1985 /* These policies are OK */
1993 /* Perform command */
1996 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1997 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1998 boolean_t entitled
= FALSE
;
1999 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
2007 case IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT
:
2008 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
), &p
->p_vfs_iopolicy
);
2010 case IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME
:
2011 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
, &p
->p_vfs_iopolicy
);
2020 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
)
2021 ? IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME
2022 : IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT
;
2025 error
= EINVAL
; /* unknown command */
2033 /* BSD call back function for task_policy networking changes */
2035 proc_apply_task_networkbg(void * bsd_info
, thread_t thread
)
2037 assert(bsd_info
!= PROC_NULL
);
2039 pid_t pid
= proc_pid((proc_t
)bsd_info
);
2041 proc_t p
= proc_find(pid
);
2043 if (p
!= PROC_NULL
) {
2044 assert(p
== (proc_t
)bsd_info
);
2046 do_background_socket(p
, thread
);
2052 gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
)
2054 struct rusage_info_child
*ri_child
;
2056 assert(p
->p_stats
!= NULL
);
2057 memset(ru
, 0, sizeof(*ru
));
2059 case RUSAGE_INFO_V4
:
2060 ru
->ri_logical_writes
= get_task_logical_writes(p
->task
, FALSE
);
2061 ru
->ri_lifetime_max_phys_footprint
= get_task_phys_footprint_lifetime_max(p
->task
);
2062 #if CONFIG_LEDGER_INTERVAL_MAX
2063 ru
->ri_interval_max_phys_footprint
= get_task_phys_footprint_interval_max(p
->task
, FALSE
);
2065 fill_task_monotonic_rusage(p
->task
, ru
);
2068 case RUSAGE_INFO_V3
:
2069 fill_task_qos_rusage(p
->task
, ru
);
2070 fill_task_billed_usage(p
->task
, ru
);
2073 case RUSAGE_INFO_V2
:
2074 fill_task_io_rusage(p
->task
, ru
);
2077 case RUSAGE_INFO_V1
:
2079 * p->p_stats->ri_child statistics are protected under proc lock.
2083 ri_child
= &(p
->p_stats
->ri_child
);
2084 ru
->ri_child_user_time
= ri_child
->ri_child_user_time
;
2085 ru
->ri_child_system_time
= ri_child
->ri_child_system_time
;
2086 ru
->ri_child_pkg_idle_wkups
= ri_child
->ri_child_pkg_idle_wkups
;
2087 ru
->ri_child_interrupt_wkups
= ri_child
->ri_child_interrupt_wkups
;
2088 ru
->ri_child_pageins
= ri_child
->ri_child_pageins
;
2089 ru
->ri_child_elapsed_abstime
= ri_child
->ri_child_elapsed_abstime
;
2094 case RUSAGE_INFO_V0
:
2095 proc_getexecutableuuid(p
, (unsigned char *)&ru
->ri_uuid
, sizeof(ru
->ri_uuid
));
2096 fill_task_rusage(p
->task
, ru
);
2097 ru
->ri_proc_start_abstime
= p
->p_stats
->ps_start
;
2102 proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
)
2104 rusage_info_current ri_current
= {};
2110 case RUSAGE_INFO_V0
:
2111 size
= sizeof(struct rusage_info_v0
);
2114 case RUSAGE_INFO_V1
:
2115 size
= sizeof(struct rusage_info_v1
);
2118 case RUSAGE_INFO_V2
:
2119 size
= sizeof(struct rusage_info_v2
);
2122 case RUSAGE_INFO_V3
:
2123 size
= sizeof(struct rusage_info_v3
);
2126 case RUSAGE_INFO_V4
:
2127 size
= sizeof(struct rusage_info_v4
);
2139 * If task is still alive, collect info from the live task itself.
2140 * Otherwise, look to the cached info in the zombie proc.
2142 if (p
->p_ru
== NULL
) {
2143 gather_rusage_info(p
, &ri_current
, flavor
);
2144 ri_current
.ri_proc_exit_abstime
= 0;
2145 error
= copyout(&ri_current
, buffer
, size
);
2147 ri_current
= p
->p_ru
->ri
;
2148 error
= copyout(&p
->p_ru
->ri
, buffer
, size
);
2155 mach_to_bsd_rv(int mach_rv
)
2163 case KERN_INVALID_ARGUMENT
:
2167 panic("unknown error %#x", mach_rv
);
2174 * Resource limit controls
2176 * uap->flavor available flavors:
2178 * RLIMIT_WAKEUPS_MONITOR
2179 * RLIMIT_CPU_USAGE_MONITOR
2180 * RLIMIT_THREAD_CPULIMITS
2181 * RLIMIT_FOOTPRINT_INTERVAL
2184 proc_rlimit_control(__unused
struct proc
*p
, struct proc_rlimit_control_args
*uap
, __unused
int32_t *retval
)
2188 struct proc_rlimit_control_wakeupmon wakeupmon_args
;
2189 uint32_t cpumon_flags
;
2190 uint32_t cpulimits_flags
;
2191 kauth_cred_t my_cred
, target_cred
;
2192 #if CONFIG_LEDGER_INTERVAL_MAX
2193 uint32_t footprint_interval_flags
;
2194 uint64_t interval_max_footprint
;
2195 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
2197 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
2198 if (uap
->pid
== -1) {
2199 targetp
= proc_self();
2201 targetp
= proc_find(uap
->pid
);
2204 /* proc_self() can return NULL for an exiting process */
2205 if (targetp
== PROC_NULL
) {
2209 my_cred
= kauth_cred_get();
2210 target_cred
= kauth_cred_proc_ref(targetp
);
2212 if (!kauth_cred_issuser(my_cred
) && kauth_cred_getruid(my_cred
) &&
2213 kauth_cred_getuid(my_cred
) != kauth_cred_getuid(target_cred
) &&
2214 kauth_cred_getruid(my_cred
) != kauth_cred_getuid(target_cred
)) {
2216 kauth_cred_unref(&target_cred
);
2220 switch (uap
->flavor
) {
2221 case RLIMIT_WAKEUPS_MONITOR
:
2222 if ((error
= copyin(uap
->arg
, &wakeupmon_args
, sizeof(wakeupmon_args
))) != 0) {
2225 if ((error
= mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp
->task
, &wakeupmon_args
.wm_flags
,
2226 &wakeupmon_args
.wm_rate
))) != 0) {
2229 error
= copyout(&wakeupmon_args
, uap
->arg
, sizeof(wakeupmon_args
));
2231 case RLIMIT_CPU_USAGE_MONITOR
:
2232 cpumon_flags
= uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
2233 error
= mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp
->task
, &cpumon_flags
));
2235 case RLIMIT_THREAD_CPULIMITS
:
2236 cpulimits_flags
= (uint32_t)uap
->arg
; // only need a limited set of bits, pass in void * argument
2238 if (uap
->pid
!= -1) {
2243 uint8_t percent
= 0;
2244 uint32_t ms_refill
= 0;
2247 percent
= (uint8_t)(cpulimits_flags
& 0xffU
); /* low 8 bits for percent */
2248 ms_refill
= (cpulimits_flags
>> 8) & 0xffffff; /* next 24 bits represent ms refill value */
2249 if (percent
>= 100) {
2254 ns_refill
= ((uint64_t)ms_refill
) * NSEC_PER_MSEC
;
2256 error
= mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK
, percent
, ns_refill
));
2259 #if CONFIG_LEDGER_INTERVAL_MAX
2260 case RLIMIT_FOOTPRINT_INTERVAL
:
2261 footprint_interval_flags
= uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
2263 * There is currently only one option for this flavor.
2265 if ((footprint_interval_flags
& FOOTPRINT_INTERVAL_RESET
) == 0) {
2269 interval_max_footprint
= get_task_phys_footprint_interval_max(targetp
->task
, TRUE
);
2271 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
2278 kauth_cred_unref(&target_cred
);
2281 * Return value from this function becomes errno to userland caller.
2287 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
2290 thread_selfusage(struct proc
*p __unused
, struct thread_selfusage_args
*uap __unused
, uint64_t *retval
)
2294 runtime
= thread_get_runtime_self();
2302 thread_selfcounts(__unused
struct proc
*p
, __unused
struct thread_selfcounts_args
*uap
, __unused
int *ret_out
)
2306 #endif /* !MONOTONIC */