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
);
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
;
787 if (thread
!= THREAD_NULL
) {
788 background
= proc_get_effective_thread_policy(thread
, TASK_POLICY_ALL_SOCKETS_BG
);
790 background
= proc_get_effective_task_policy(proc_task(p
), TASK_POLICY_ALL_SOCKETS_BG
);
795 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
796 * the sockets with the background flag. There's nothing
797 * to do here for the PRIO_DARWIN_THREAD case.
799 if (thread
== THREAD_NULL
) {
802 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
803 fp
= fdp
->fd_ofiles
[i
];
804 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0) {
807 if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_SOCKET
) {
808 struct socket
*sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
809 socket_set_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
810 sockp
->so_background_thread
= NULL
;
813 else if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_NETPOLICY
) {
814 necp_set_client_as_background(p
, fp
, background
);
820 /* disable networking IO throttle.
821 * NOTE - It is a known limitation of the current design that we
822 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
823 * sockets created by other threads within this process.
826 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
827 struct socket
*sockp
;
829 fp
= fdp
->fd_ofiles
[i
];
830 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0) {
833 if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_SOCKET
) {
834 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
835 /* skip if only clearing this thread's sockets */
836 if ((thread
) && (sockp
->so_background_thread
!= thread
)) {
839 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
840 sockp
->so_background_thread
= NULL
;
843 else if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_NETPOLICY
) {
844 necp_set_client_as_background(p
, fp
, background
);
852 #pragma unused(p, thread)
858 * do_background_thread
860 * Requires: thread reference
863 * EPERM Tried to background while in vfork
864 * XXX - todo - does this need a MACF hook?
867 do_background_thread(thread_t thread
, int priority
)
870 int enable
, external
;
873 ut
= get_bsdthread_info(thread
);
875 /* Backgrounding is unsupported for threads in vfork */
876 if ((ut
->uu_flag
& UT_VFORK
) != 0) {
880 /* Backgrounding is unsupported for workq threads */
881 if (thread_is_static_param(thread
)) {
885 /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
886 if (thread_has_qos_policy(thread
)) {
887 thread_remove_qos_policy(thread
);
891 /* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
892 enable
= (priority
== PRIO_DARWIN_BG
) ? TASK_POLICY_ENABLE
: TASK_POLICY_DISABLE
;
893 external
= (current_thread() == thread
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
895 proc_set_thread_policy(thread
, external
, TASK_POLICY_DARWIN_BG
, enable
);
908 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
913 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
914 sizeof(struct rlimit
)))) {
918 return dosetrlimit(p
, uap
->which
, &alim
);
924 * ENOMEM Cannot copy limit structure
927 * Notes: EINVAL is returned both for invalid arguments, and in the
928 * case that the current usage (e.g. RLIMIT_STACK) is already
929 * in excess of the requested limit.
932 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
)
934 struct rlimit
*alimp
;
937 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
939 /* Mask out POSIX flag, saved above */
940 which
&= ~_RLIMIT_POSIX_FLAG
;
942 if (which
>= RLIM_NLIMITS
) {
946 alimp
= &p
->p_rlimit
[which
];
947 if (limp
->rlim_cur
> limp
->rlim_max
) {
951 if (limp
->rlim_cur
> alimp
->rlim_max
||
952 limp
->rlim_max
> alimp
->rlim_max
) {
953 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
960 if ((error
= proc_limitreplace(p
)) != 0) {
961 proc_limitunblock(p
);
965 alimp
= &p
->p_rlimit
[which
];
969 if (limp
->rlim_cur
== RLIM_INFINITY
) {
970 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
971 timerclear(&p
->p_rlim_cpu
);
973 task_absolutetime_info_data_t tinfo
;
974 mach_msg_type_number_t count
;
975 struct timeval ttv
, tv
;
977 clock_usec_t tv_usec
;
979 count
= TASK_ABSOLUTETIME_INFO_COUNT
;
980 task_info(p
->task
, TASK_ABSOLUTETIME_INFO
,
981 (task_info_t
)&tinfo
, &count
);
982 absolutetime_to_microtime(tinfo
.total_user
+ tinfo
.total_system
,
985 ttv
.tv_usec
= tv_usec
;
987 tv
.tv_sec
= (limp
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: limp
->rlim_cur
);
989 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
992 if (timercmp(&p
->p_rlim_cpu
, &tv
, >)) {
993 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
995 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
997 timerclear(&p
->p_rlim_cpu
);
1005 if (limp
->rlim_cur
> maxdmap
) {
1006 limp
->rlim_cur
= maxdmap
;
1008 if (limp
->rlim_max
> maxdmap
) {
1009 limp
->rlim_max
= maxdmap
;
1014 if (p
->p_lflag
& P_LCUSTOM_STACK
) {
1015 /* Process has a custom stack set - rlimit cannot be used to change it */
1020 /* Disallow illegal stack size instead of clipping */
1021 if (limp
->rlim_cur
> maxsmap
||
1022 limp
->rlim_max
> maxsmap
) {
1028 * 4797860 - workaround poorly written installers by
1029 * doing previous implementation (< 10.5) when caller
1030 * is non-POSIX conforming.
1032 if (limp
->rlim_cur
> maxsmap
) {
1033 limp
->rlim_cur
= maxsmap
;
1035 if (limp
->rlim_max
> maxsmap
) {
1036 limp
->rlim_max
= maxsmap
;
1042 * Stack is allocated to the max at exec time with only
1043 * "rlim_cur" bytes accessible. If stack limit is going
1044 * up make more accessible, if going down make inaccessible.
1046 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
1051 size
= round_page_64(limp
->rlim_cur
);
1052 size
-= round_page_64(alimp
->rlim_cur
);
1054 addr
= p
->user_stack
- round_page_64(limp
->rlim_cur
);
1055 kr
= mach_vm_protect(current_map(),
1057 FALSE
, VM_PROT_DEFAULT
);
1058 if (kr
!= KERN_SUCCESS
) {
1062 } else if (limp
->rlim_cur
< alimp
->rlim_cur
) {
1070 * First check if new stack limit would agree
1071 * with current stack usage.
1072 * Get the current thread's stack pointer...
1074 cur_sp
= thread_adjuserstack(current_thread(),
1076 if (cur_sp
<= p
->user_stack
&&
1077 cur_sp
> (p
->user_stack
-
1078 round_page_64(alimp
->rlim_cur
))) {
1079 /* stack pointer is in main stack */
1080 if (cur_sp
<= (p
->user_stack
-
1081 round_page_64(limp
->rlim_cur
))) {
1083 * New limit would cause
1084 * current usage to be invalid:
1091 /* not on the main stack: reject */
1096 size
= round_page_64(alimp
->rlim_cur
);
1097 size
-= round_page_64(limp
->rlim_cur
);
1099 addr
= p
->user_stack
- round_page_64(alimp
->rlim_cur
);
1101 kr
= mach_vm_protect(current_map(),
1103 FALSE
, VM_PROT_NONE
);
1104 if (kr
!= KERN_SUCCESS
) {
1115 * Only root can set the maxfiles limits, as it is
1116 * systemwide resource. If we are expecting POSIX behavior,
1117 * instead of clamping the value, return EINVAL. We do this
1118 * because historically, people have been able to attempt to
1119 * set RLIM_INFINITY to get "whatever the maximum is".
1121 if (kauth_cred_issuser(kauth_cred_get())) {
1122 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1123 limp
->rlim_cur
> (rlim_t
)maxfiles
) {
1128 limp
->rlim_cur
= maxfiles
;
1130 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1131 limp
->rlim_max
> (rlim_t
)maxfiles
) {
1132 limp
->rlim_max
= maxfiles
;
1135 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1136 limp
->rlim_cur
> (rlim_t
)maxfilesperproc
) {
1141 limp
->rlim_cur
= maxfilesperproc
;
1143 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1144 limp
->rlim_max
> (rlim_t
)maxfilesperproc
) {
1145 limp
->rlim_max
= maxfilesperproc
;
1152 * Only root can set to the maxproc limits, as it is
1153 * systemwide resource; all others are limited to
1154 * maxprocperuid (presumably less than maxproc).
1156 if (kauth_cred_issuser(kauth_cred_get())) {
1157 if (limp
->rlim_cur
> (rlim_t
)maxproc
) {
1158 limp
->rlim_cur
= maxproc
;
1160 if (limp
->rlim_max
> (rlim_t
)maxproc
) {
1161 limp
->rlim_max
= maxproc
;
1164 if (limp
->rlim_cur
> (rlim_t
)maxprocperuid
) {
1165 limp
->rlim_cur
= maxprocperuid
;
1167 if (limp
->rlim_max
> (rlim_t
)maxprocperuid
) {
1168 limp
->rlim_max
= maxprocperuid
;
1173 case RLIMIT_MEMLOCK
:
1175 * Tell the Mach VM layer about the new limit value.
1178 vm_map_set_user_wire_limit(current_map(), limp
->rlim_cur
);
1186 proc_limitunblock(p
);
1192 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
1194 struct rlimit lim
= {};
1197 * Take out flag now in case we need to use it to trigger variant
1200 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
1202 if (uap
->which
>= RLIM_NLIMITS
) {
1205 proc_limitget(p
, uap
->which
, &lim
);
1206 return copyout((caddr_t
)&lim
,
1207 uap
->rlp
, sizeof(struct rlimit
));
1211 * Transform the running time and tick information in proc p into user,
1212 * system, and interrupt time usage.
1214 /* No lock on proc is held for this.. */
1216 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
1228 mach_task_basic_info_data_t tinfo
;
1229 task_thread_times_info_data_t ttimesinfo
;
1230 task_events_info_data_t teventsinfo
;
1231 mach_msg_type_number_t task_info_count
, task_ttimes_count
;
1232 mach_msg_type_number_t task_events_count
;
1233 struct timeval ut
, st
;
1235 task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
1236 task_info(task
, MACH_TASK_BASIC_INFO
,
1237 (task_info_t
)&tinfo
, &task_info_count
);
1238 ut
.tv_sec
= tinfo
.user_time
.seconds
;
1239 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
1240 st
.tv_sec
= tinfo
.system_time
.seconds
;
1241 st
.tv_usec
= tinfo
.system_time
.microseconds
;
1242 timeradd(&ut
, up
, up
);
1243 timeradd(&st
, sp
, sp
);
1245 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1246 task_info(task
, TASK_THREAD_TIMES_INFO
,
1247 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
1249 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1250 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1251 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1252 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1253 timeradd(&ut
, up
, up
);
1254 timeradd(&st
, sp
, sp
);
1256 task_events_count
= TASK_EVENTS_INFO_COUNT
;
1257 task_info(task
, TASK_EVENTS_INFO
,
1258 (task_info_t
)&teventsinfo
, &task_events_count
);
1261 * No need to lock "p": this does not need to be
1262 * completely consistent, right ?
1264 p
->p_stats
->p_ru
.ru_minflt
= (teventsinfo
.faults
-
1265 teventsinfo
.pageins
);
1266 p
->p_stats
->p_ru
.ru_majflt
= teventsinfo
.pageins
;
1267 p
->p_stats
->p_ru
.ru_nivcsw
= (teventsinfo
.csw
-
1268 p
->p_stats
->p_ru
.ru_nvcsw
);
1269 if (p
->p_stats
->p_ru
.ru_nivcsw
< 0) {
1270 p
->p_stats
->p_ru
.ru_nivcsw
= 0;
1273 p
->p_stats
->p_ru
.ru_maxrss
= tinfo
.resident_size_max
;
1277 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
1278 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
1282 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1284 struct rusage
*rup
, rubuf
;
1285 struct user64_rusage rubuf64
= {};
1286 struct user32_rusage rubuf32
= {};
1287 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1288 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1289 struct timeval utime
;
1290 struct timeval stime
;
1295 calcru(p
, &utime
, &stime
, NULL
);
1297 rup
= &p
->p_stats
->p_ru
;
1298 rup
->ru_utime
= utime
;
1299 rup
->ru_stime
= stime
;
1306 case RUSAGE_CHILDREN
:
1308 rup
= &p
->p_stats
->p_cru
;
1316 if (IS_64BIT_PROCESS(p
)) {
1317 retsize
= sizeof(rubuf64
);
1318 retbuf
= (caddr_t
)&rubuf64
;
1319 munge_user64_rusage(&rubuf
, &rubuf64
);
1321 retsize
= sizeof(rubuf32
);
1322 retbuf
= (caddr_t
)&rubuf32
;
1323 munge_user32_rusage(&rubuf
, &rubuf32
);
1326 return copyout(retbuf
, uap
->rusage
, retsize
);
1330 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
1335 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
1336 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
1337 if (ru
->ru_maxrss
< ru2
->ru_maxrss
) {
1338 ru
->ru_maxrss
= ru2
->ru_maxrss
;
1340 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
1341 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--) {
1347 * Add the rusage stats of child in parent.
1349 * It adds rusage statistics of child process and statistics of all its
1350 * children to its parent.
1352 * Note: proc lock of parent should be held while calling this function.
1355 update_rusage_info_child(struct rusage_info_child
*ri
, rusage_info_current
*ri_current
)
1357 ri
->ri_child_user_time
+= (ri_current
->ri_user_time
+
1358 ri_current
->ri_child_user_time
);
1359 ri
->ri_child_system_time
+= (ri_current
->ri_system_time
+
1360 ri_current
->ri_child_system_time
);
1361 ri
->ri_child_pkg_idle_wkups
+= (ri_current
->ri_pkg_idle_wkups
+
1362 ri_current
->ri_child_pkg_idle_wkups
);
1363 ri
->ri_child_interrupt_wkups
+= (ri_current
->ri_interrupt_wkups
+
1364 ri_current
->ri_child_interrupt_wkups
);
1365 ri
->ri_child_pageins
+= (ri_current
->ri_pageins
+
1366 ri_current
->ri_child_pageins
);
1367 ri
->ri_child_elapsed_abstime
+= ((ri_current
->ri_proc_exit_abstime
-
1368 ri_current
->ri_proc_start_abstime
) + ri_current
->ri_child_elapsed_abstime
);
1372 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1375 limp
->rlim_cur
= p
->p_rlimit
[which
].rlim_cur
;
1376 limp
->rlim_max
= p
->p_rlimit
[which
].rlim_max
;
1382 proc_limitdrop(proc_t p
, int exiting
)
1384 struct plimit
* freelim
= NULL
;
1385 struct plimit
* freeoldlim
= NULL
;
1389 if (--p
->p_limit
->pl_refcnt
== 0) {
1390 freelim
= p
->p_limit
;
1393 if ((exiting
!= 0) && (p
->p_olimit
!= NULL
) && (--p
->p_olimit
->pl_refcnt
== 0)) {
1394 freeoldlim
= p
->p_olimit
;
1399 if (freelim
!= NULL
) {
1400 FREE_ZONE(freelim
, sizeof *p
->p_limit
, M_PLIMIT
);
1402 if (freeoldlim
!= NULL
) {
1403 FREE_ZONE(freeoldlim
, sizeof *p
->p_olimit
, M_PLIMIT
);
1409 proc_limitfork(proc_t parent
, proc_t child
)
1412 child
->p_limit
= parent
->p_limit
;
1413 child
->p_limit
->pl_refcnt
++;
1414 child
->p_olimit
= NULL
;
1419 proc_limitblock(proc_t p
)
1422 while (p
->p_lflag
& P_LLIMCHANGE
) {
1423 p
->p_lflag
|= P_LLIMWAIT
;
1424 msleep(&p
->p_olimit
, &p
->p_mlock
, 0, "proc_limitblock", NULL
);
1426 p
->p_lflag
|= P_LLIMCHANGE
;
1432 proc_limitunblock(proc_t p
)
1435 p
->p_lflag
&= ~P_LLIMCHANGE
;
1436 if (p
->p_lflag
& P_LLIMWAIT
) {
1437 p
->p_lflag
&= ~P_LLIMWAIT
;
1438 wakeup(&p
->p_olimit
);
1443 /* This is called behind serialization provided by proc_limitblock/unlbock */
1445 proc_limitreplace(proc_t p
)
1447 struct plimit
*copy
;
1452 if (p
->p_limit
->pl_refcnt
== 1) {
1459 MALLOC_ZONE(copy
, struct plimit
*,
1460 sizeof(struct plimit
), M_PLIMIT
, M_WAITOK
);
1466 bcopy(p
->p_limit
->pl_rlimit
, copy
->pl_rlimit
,
1467 sizeof(struct rlimit
) * RLIM_NLIMITS
);
1468 copy
->pl_refcnt
= 1;
1469 /* hang on to reference to old till process exits */
1470 p
->p_olimit
= p
->p_limit
;
1478 iopolicysys_disk(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1480 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1482 iopolicysys_vfs_atime_updates(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1487 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1489 * Parameters: cmd Policy command
1490 * arg Pointer to policy arguments
1492 * Returns: 0 Success
1493 * EINVAL Invalid command or invalid policy arguments
1497 iopolicysys(struct proc
*p
, struct iopolicysys_args
*uap
, int32_t *retval
)
1500 struct _iopol_param_t iop_param
;
1502 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0) {
1506 switch (iop_param
.iop_iotype
) {
1507 case IOPOL_TYPE_DISK
:
1508 error
= iopolicysys_disk(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1509 if (error
== EIDRM
) {
1517 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY
:
1518 error
= iopolicysys_vfs_hfs_case_sensitivity(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1523 case IOPOL_TYPE_VFS_ATIME_UPDATES
:
1524 error
= iopolicysys_vfs_atime_updates(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1534 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1535 if (uap
->cmd
== IOPOL_CMD_GET
) {
1536 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1547 iopolicysys_disk(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1553 /* Validate scope */
1555 case IOPOL_SCOPE_PROCESS
:
1556 thread
= THREAD_NULL
;
1557 policy_flavor
= TASK_POLICY_IOPOL
;
1560 case IOPOL_SCOPE_THREAD
:
1561 thread
= current_thread();
1562 policy_flavor
= TASK_POLICY_IOPOL
;
1564 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1565 if (cmd
== IOPOL_CMD_SET
&& thread_has_qos_policy(thread
)) {
1571 case IOPOL_THROTTLE
:
1572 case IOPOL_IMPORTANT
:
1573 case IOPOL_STANDARD
:
1574 if (!thread_is_static_param(thread
)) {
1575 thread_remove_qos_policy(thread
);
1577 * This is not an error case, this is to return a marker to user-space that
1578 * we stripped the thread of its QoS class.
1583 /* otherwise, fall through to the error case. */
1591 case IOPOL_SCOPE_DARWIN_BG
:
1593 /* Embedded doesn't want this as BG is always IOPOL_THROTTLE */
1596 #else /* CONFIG_EMBEDDED */
1597 thread
= THREAD_NULL
;
1598 policy_flavor
= TASK_POLICY_DARWIN_BG_IOPOL
;
1600 #endif /* CONFIG_EMBEDDED */
1607 /* Validate policy */
1608 if (cmd
== IOPOL_CMD_SET
) {
1611 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1612 /* the current default BG throttle level is UTILITY */
1613 policy
= IOPOL_UTILITY
;
1615 policy
= IOPOL_IMPORTANT
;
1620 case IOPOL_THROTTLE
:
1621 /* These levels are OK */
1623 case IOPOL_IMPORTANT
:
1625 case IOPOL_STANDARD
:
1628 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1629 /* These levels are invalid for BG */
1633 /* OK for other scopes */
1642 /* Perform command */
1645 if (thread
!= THREAD_NULL
) {
1646 proc_set_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1648 proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1652 if (thread
!= THREAD_NULL
) {
1653 policy
= proc_get_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
);
1655 policy
= proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
);
1657 iop_param
->iop_policy
= policy
;
1660 error
= EINVAL
; /* unknown command */
1669 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1673 /* Validate scope */
1675 case IOPOL_SCOPE_PROCESS
:
1676 /* Only process OK */
1683 /* Validate policy */
1684 if (cmd
== IOPOL_CMD_SET
) {
1686 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1688 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1689 /* These policies are OK */
1697 /* Perform command */
1700 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1701 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1702 boolean_t entitled
= FALSE
;
1703 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1711 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1712 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
), &p
->p_vfs_iopolicy
);
1714 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1715 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
, &p
->p_vfs_iopolicy
);
1724 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
)
1725 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1726 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
;
1729 error
= EINVAL
; /* unknown command */
1738 get_thread_atime_policy(struct uthread
*ut
)
1740 return (ut
->uu_flag
& UT_ATIME_UPDATE
) ? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1744 set_thread_atime_policy(struct uthread
*ut
, int policy
)
1746 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1747 ut
->uu_flag
|= UT_ATIME_UPDATE
;
1749 ut
->uu_flag
&= ~UT_ATIME_UPDATE
;
1754 set_task_atime_policy(struct proc
*p
, int policy
)
1756 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1757 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
, &p
->p_vfs_iopolicy
);
1759 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
), &p
->p_vfs_iopolicy
);
1764 get_task_atime_policy(struct proc
*p
)
1766 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_ATIME_UPDATES
) ? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1770 iopolicysys_vfs_atime_updates(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1775 /* Validate scope */
1777 case IOPOL_SCOPE_THREAD
:
1778 thread
= current_thread();
1780 case IOPOL_SCOPE_PROCESS
:
1781 thread
= THREAD_NULL
;
1788 /* Validate policy */
1789 if (cmd
== IOPOL_CMD_SET
) {
1791 case IOPOL_ATIME_UPDATES_DEFAULT
:
1792 case IOPOL_ATIME_UPDATES_OFF
:
1800 /* Perform command */
1803 if (thread
!= THREAD_NULL
) {
1804 set_thread_atime_policy(get_bsdthread_info(thread
), policy
);
1806 set_task_atime_policy(p
, policy
);
1810 if (thread
!= THREAD_NULL
) {
1811 policy
= get_thread_atime_policy(get_bsdthread_info(thread
));
1813 policy
= get_task_atime_policy(p
);
1815 iop_param
->iop_policy
= policy
;
1818 error
= EINVAL
; /* unknown command */
1826 /* BSD call back function for task_policy networking changes */
1828 proc_apply_task_networkbg(void * bsd_info
, thread_t thread
)
1830 assert(bsd_info
!= PROC_NULL
);
1832 pid_t pid
= proc_pid((proc_t
)bsd_info
);
1834 proc_t p
= proc_find(pid
);
1836 if (p
!= PROC_NULL
) {
1837 assert(p
== (proc_t
)bsd_info
);
1839 do_background_socket(p
, thread
);
1845 gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
)
1847 struct rusage_info_child
*ri_child
;
1849 assert(p
->p_stats
!= NULL
);
1850 memset(ru
, 0, sizeof(*ru
));
1852 case RUSAGE_INFO_V4
:
1853 ru
->ri_logical_writes
= get_task_logical_writes(p
->task
);
1854 ru
->ri_lifetime_max_phys_footprint
= get_task_phys_footprint_lifetime_max(p
->task
);
1855 #if CONFIG_LEDGER_INTERVAL_MAX
1856 ru
->ri_interval_max_phys_footprint
= get_task_phys_footprint_interval_max(p
->task
, FALSE
);
1858 fill_task_monotonic_rusage(p
->task
, ru
);
1861 case RUSAGE_INFO_V3
:
1862 fill_task_qos_rusage(p
->task
, ru
);
1863 fill_task_billed_usage(p
->task
, ru
);
1866 case RUSAGE_INFO_V2
:
1867 fill_task_io_rusage(p
->task
, ru
);
1870 case RUSAGE_INFO_V1
:
1872 * p->p_stats->ri_child statistics are protected under proc lock.
1876 ri_child
= &(p
->p_stats
->ri_child
);
1877 ru
->ri_child_user_time
= ri_child
->ri_child_user_time
;
1878 ru
->ri_child_system_time
= ri_child
->ri_child_system_time
;
1879 ru
->ri_child_pkg_idle_wkups
= ri_child
->ri_child_pkg_idle_wkups
;
1880 ru
->ri_child_interrupt_wkups
= ri_child
->ri_child_interrupt_wkups
;
1881 ru
->ri_child_pageins
= ri_child
->ri_child_pageins
;
1882 ru
->ri_child_elapsed_abstime
= ri_child
->ri_child_elapsed_abstime
;
1887 case RUSAGE_INFO_V0
:
1888 proc_getexecutableuuid(p
, (unsigned char *)&ru
->ri_uuid
, sizeof(ru
->ri_uuid
));
1889 fill_task_rusage(p
->task
, ru
);
1890 ru
->ri_proc_start_abstime
= p
->p_stats
->ps_start
;
1895 proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
)
1897 rusage_info_current ri_current
= {};
1903 case RUSAGE_INFO_V0
:
1904 size
= sizeof(struct rusage_info_v0
);
1907 case RUSAGE_INFO_V1
:
1908 size
= sizeof(struct rusage_info_v1
);
1911 case RUSAGE_INFO_V2
:
1912 size
= sizeof(struct rusage_info_v2
);
1915 case RUSAGE_INFO_V3
:
1916 size
= sizeof(struct rusage_info_v3
);
1919 case RUSAGE_INFO_V4
:
1920 size
= sizeof(struct rusage_info_v4
);
1932 * If task is still alive, collect info from the live task itself.
1933 * Otherwise, look to the cached info in the zombie proc.
1935 if (p
->p_ru
== NULL
) {
1936 gather_rusage_info(p
, &ri_current
, flavor
);
1937 ri_current
.ri_proc_exit_abstime
= 0;
1938 error
= copyout(&ri_current
, buffer
, size
);
1940 ri_current
= p
->p_ru
->ri
;
1941 error
= copyout(&p
->p_ru
->ri
, buffer
, size
);
1948 mach_to_bsd_rv(int mach_rv
)
1956 case KERN_INVALID_ARGUMENT
:
1960 panic("unknown error %#x", mach_rv
);
1967 * Resource limit controls
1969 * uap->flavor available flavors:
1971 * RLIMIT_WAKEUPS_MONITOR
1972 * RLIMIT_CPU_USAGE_MONITOR
1973 * RLIMIT_THREAD_CPULIMITS
1974 * RLIMIT_FOOTPRINT_INTERVAL
1977 proc_rlimit_control(__unused
struct proc
*p
, struct proc_rlimit_control_args
*uap
, __unused
int32_t *retval
)
1981 struct proc_rlimit_control_wakeupmon wakeupmon_args
;
1982 uint32_t cpumon_flags
;
1983 uint32_t cpulimits_flags
;
1984 kauth_cred_t my_cred
, target_cred
;
1985 #if CONFIG_LEDGER_INTERVAL_MAX
1986 uint32_t footprint_interval_flags
;
1987 uint64_t interval_max_footprint
;
1988 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
1990 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
1991 if (uap
->pid
== -1) {
1992 targetp
= proc_self();
1994 targetp
= proc_find(uap
->pid
);
1997 /* proc_self() can return NULL for an exiting process */
1998 if (targetp
== PROC_NULL
) {
2002 my_cred
= kauth_cred_get();
2003 target_cred
= kauth_cred_proc_ref(targetp
);
2005 if (!kauth_cred_issuser(my_cred
) && kauth_cred_getruid(my_cred
) &&
2006 kauth_cred_getuid(my_cred
) != kauth_cred_getuid(target_cred
) &&
2007 kauth_cred_getruid(my_cred
) != kauth_cred_getuid(target_cred
)) {
2009 kauth_cred_unref(&target_cred
);
2013 switch (uap
->flavor
) {
2014 case RLIMIT_WAKEUPS_MONITOR
:
2015 if ((error
= copyin(uap
->arg
, &wakeupmon_args
, sizeof(wakeupmon_args
))) != 0) {
2018 if ((error
= mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp
->task
, &wakeupmon_args
.wm_flags
,
2019 &wakeupmon_args
.wm_rate
))) != 0) {
2022 error
= copyout(&wakeupmon_args
, uap
->arg
, sizeof(wakeupmon_args
));
2024 case RLIMIT_CPU_USAGE_MONITOR
:
2025 cpumon_flags
= uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
2026 error
= mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp
->task
, &cpumon_flags
));
2028 case RLIMIT_THREAD_CPULIMITS
:
2029 cpulimits_flags
= (uint32_t)uap
->arg
; // only need a limited set of bits, pass in void * argument
2031 if (uap
->pid
!= -1) {
2036 uint8_t percent
= 0;
2037 uint32_t ms_refill
= 0;
2040 percent
= (uint8_t)(cpulimits_flags
& 0xffU
); /* low 8 bits for percent */
2041 ms_refill
= (cpulimits_flags
>> 8) & 0xffffff; /* next 24 bits represent ms refill value */
2042 if (percent
>= 100) {
2047 ns_refill
= ((uint64_t)ms_refill
) * NSEC_PER_MSEC
;
2049 error
= mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK
, percent
, ns_refill
));
2052 #if CONFIG_LEDGER_INTERVAL_MAX
2053 case RLIMIT_FOOTPRINT_INTERVAL
:
2054 footprint_interval_flags
= uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
2056 * There is currently only one option for this flavor.
2058 if ((footprint_interval_flags
& FOOTPRINT_INTERVAL_RESET
) == 0) {
2062 interval_max_footprint
= get_task_phys_footprint_interval_max(targetp
->task
, TRUE
);
2064 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
2071 kauth_cred_unref(&target_cred
);
2074 * Return value from this function becomes errno to userland caller.
2080 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
2083 thread_selfusage(struct proc
*p __unused
, struct thread_selfusage_args
*uap __unused
, uint64_t *retval
)
2087 runtime
= thread_get_runtime_self();
2095 thread_selfcounts(__unused
struct proc
*p
, __unused
struct thread_selfcounts_args
*uap
, __unused
int *ret_out
)
2099 #endif /* !MONOTONIC */