2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
79 #include <sys/file_internal.h>
80 #include <sys/resourcevar.h>
81 #include <sys/malloc.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <machine/spl.h>
86 #include <sys/mount_internal.h>
87 #include <sys/sysproto.h>
89 #include <security/audit/audit.h>
91 #include <machine/vmparam.h>
93 #include <mach/mach_types.h>
94 #include <mach/time_value.h>
95 #include <mach/task.h>
96 #include <mach/task_info.h>
97 #include <mach/vm_map.h>
98 #include <mach/mach_vm.h>
99 #include <mach/thread_act.h> /* for thread_policy_set( ) */
100 #include <kern/thread.h>
102 #include <kern/task.h>
103 #include <kern/clock.h> /* for absolutetime_to_microtime() */
104 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
105 #include <sys/socketvar.h> /* for struct socket */
107 #include <vm/vm_map.h>
109 #include <kern/assert.h>
110 #include <sys/resource.h>
111 #include <IOKit/IOBSD.h>
113 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
114 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
115 int uthread_get_background_state(uthread_t
);
116 static void do_background_socket(struct proc
*p
, thread_t thread
);
117 static int do_background_thread(struct proc
*curp
, thread_t thread
, int priority
);
118 static int do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
119 static int set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
120 static int proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
);
121 static int proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
);
122 static int get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
);
123 void proc_apply_task_networkbg_internal(proc_t
, thread_t
);
124 void proc_restore_task_networkbg_internal(proc_t
, thread_t
);
125 int proc_pid_rusage(int pid
, int flavor
, user_addr_t buf
, int32_t *retval
);
126 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
127 int fill_task_rusage(task_t task
, rusage_info_current
*ri
);
128 void fill_task_billed_usage(task_t task
, rusage_info_current
*ri
);
129 int fill_task_io_rusage(task_t task
, rusage_info_current
*ri
);
130 int fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
);
131 static void rusage_info_conversion(rusage_info_t ri_info
, rusage_info_current
*ri_current
, int flavor
);
133 int proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
);
135 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
136 rlim_t maxsmap
= MAXSSIZ
- PAGE_MAX_SIZE
; /* XXX */
139 * Limits on the number of open files per process, and the number
140 * of child processes per process.
142 * Note: would be in kern/subr_param.c in FreeBSD.
144 __private_extern__
int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
146 SYSCTL_INT(_kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
147 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
149 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
150 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
152 /* Args and fn for proc_iteration callback used in setpriority */
153 struct puser_nice_args
{
160 static int puser_donice_callback(proc_t p
, void * arg
);
163 /* Args and fn for proc_iteration callback used in setpriority */
164 struct ppgrp_nice_args
{
170 static int ppgrp_donice_callback(proc_t p
, void * arg
);
173 * Resource controls and accounting.
176 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, int32_t *retval
)
179 int low
= PRIO_MAX
+ 1;
180 kauth_cred_t my_cred
;
184 /* would also test (uap->who < 0), but id_t is unsigned */
185 if (uap
->who
> 0x7fffffff)
188 switch (uap
->which
) {
195 p
= proc_find(uap
->who
);
205 struct pgrp
*pg
= PGRP_NULL
;
208 /* returns the pgrp to ref */
209 pg
= proc_pgrp(curp
);
210 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
213 /* No need for iteration as it is a simple scan */
215 for (p
= pg
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
226 uap
->who
= kauth_cred_getuid(kauth_cred_get());
230 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
231 my_cred
= kauth_cred_proc_ref(p
);
232 if (kauth_cred_getuid(my_cred
) == uap
->who
&&
235 kauth_cred_unref(&my_cred
);
242 case PRIO_DARWIN_THREAD
:
243 /* we currently only support the current thread */
247 low
= proc_get_task_policy(current_task(), current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_DARWIN_BG
);
251 case PRIO_DARWIN_PROCESS
:
255 p
= proc_find(uap
->who
);
261 error
= get_background_proc(curp
, p
, &low
);
269 case PRIO_DARWIN_ROLE
:
273 p
= proc_find(uap
->who
);
279 error
= proc_get_darwin_role(curp
, p
, &low
);
290 if (low
== PRIO_MAX
+ 1)
296 /* call back function used for proc iteration in PRIO_USER */
298 puser_donice_callback(proc_t p
, void * arg
)
301 struct puser_nice_args
* pun
= (struct puser_nice_args
*)arg
;
302 kauth_cred_t my_cred
;
304 my_cred
= kauth_cred_proc_ref(p
);
305 if (kauth_cred_getuid(my_cred
) == pun
->who
) {
306 error
= donice(pun
->curp
, p
, pun
->prio
);
307 if (pun
->errorp
!= NULL
)
308 *pun
->errorp
= error
;
309 if (pun
->foundp
!= NULL
) {
314 kauth_cred_unref(&my_cred
);
316 return(PROC_RETURNED
);
319 /* call back function used for proc iteration in PRIO_PGRP */
321 ppgrp_donice_callback(proc_t p
, void * arg
)
324 struct ppgrp_nice_args
* pun
= (struct ppgrp_nice_args
*)arg
;
327 error
= donice(pun
->curp
, p
, pun
->prio
);
328 if (pun
->errorp
!= NULL
)
329 *pun
->errorp
= error
;
330 if (pun
->foundp
!= NULL
) {
335 return(PROC_RETURNED
);
347 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, int32_t *retval
)
350 int found
= 0, error
= 0;
353 AUDIT_ARG(cmd
, uap
->which
);
354 AUDIT_ARG(owner
, uap
->who
, 0);
355 AUDIT_ARG(value32
, uap
->prio
);
357 /* would also test (uap->who < 0), but id_t is unsigned */
358 if (uap
->who
> 0x7fffffff)
361 switch (uap
->which
) {
367 p
= proc_find(uap
->who
);
372 error
= donice(curp
, p
, uap
->prio
);
379 struct pgrp
*pg
= PGRP_NULL
;
380 struct ppgrp_nice_args ppgrp
;
383 pg
= proc_pgrp(curp
);
384 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
)
388 ppgrp
.prio
= uap
->prio
;
389 ppgrp
.foundp
= &found
;
390 ppgrp
.errorp
= &error
;
392 /* PGRP_DROPREF drops the reference on process group */
393 pgrp_iterate(pg
, PGRP_DROPREF
, ppgrp_donice_callback
, (void *)&ppgrp
, NULL
, NULL
);
399 struct puser_nice_args punice
;
402 uap
->who
= kauth_cred_getuid(kauth_cred_get());
405 punice
.prio
= uap
->prio
;
406 punice
.who
= uap
->who
;
407 punice
.foundp
= &found
;
409 punice
.errorp
= &error
;
410 proc_iterate(PROC_ALLPROCLIST
, puser_donice_callback
, (void *)&punice
, NULL
, NULL
);
415 case PRIO_DARWIN_THREAD
: {
416 /* we currently only support the current thread */
420 error
= do_background_thread(curp
, current_thread(), uap
->prio
);
425 case PRIO_DARWIN_PROCESS
: {
429 p
= proc_find(uap
->who
);
435 error
= do_background_proc(curp
, p
, uap
->prio
);
443 case PRIO_DARWIN_GPU
: {
447 p
= proc_find(uap
->who
);
451 error
= set_gpudeny_proc(curp
, p
, uap
->prio
);
458 case PRIO_DARWIN_ROLE
: {
462 p
= proc_find(uap
->who
);
468 error
= proc_set_darwin_role(curp
, p
, uap
->prio
);
481 if (error
== EIDRM
) {
493 * mac_check_proc_sched:???
496 donice(struct proc
*curp
, struct proc
*chgp
, int n
)
500 kauth_cred_t my_cred
;
502 ucred
= kauth_cred_proc_ref(curp
);
503 my_cred
= kauth_cred_proc_ref(chgp
);
505 if (suser(ucred
, NULL
) && kauth_cred_getruid(ucred
) &&
506 kauth_cred_getuid(ucred
) != kauth_cred_getuid(my_cred
) &&
507 kauth_cred_getruid(ucred
) != kauth_cred_getuid(my_cred
)) {
515 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
)) {
520 error
= mac_proc_check_sched(curp
, chgp
);
527 (void)resetpriority(chgp
);
529 kauth_cred_unref(&ucred
);
530 kauth_cred_unref(&my_cred
);
535 set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
539 kauth_cred_t target_cred
;
541 ucred
= kauth_cred_get();
542 target_cred
= kauth_cred_proc_ref(targetp
);
544 /* TODO: Entitlement instead of uid check */
546 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
547 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
548 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
553 if (curp
== targetp
) {
559 error
= mac_proc_check_sched(curp
, targetp
);
565 case PRIO_DARWIN_GPU_DENY
:
566 task_set_gpu_denied(proc_task(targetp
), TRUE
);
568 case PRIO_DARWIN_GPU_ALLOW
:
569 task_set_gpu_denied(proc_task(targetp
), FALSE
);
577 kauth_cred_unref(&target_cred
);
583 proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
)
588 kauth_cred_t ucred
, target_cred
;
590 ucred
= kauth_cred_get();
591 target_cred
= kauth_cred_proc_ref(targetp
);
593 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
594 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
595 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
600 if (curp
!= targetp
) {
602 if ((error
= mac_proc_check_sched(curp
, targetp
)))
607 proc_get_darwinbgstate(proc_task(targetp
), &flagsp
);
608 if ((flagsp
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
615 if ((error
= proc_darwin_role_to_task_role(priority
, &role
)))
618 proc_set_task_policy(proc_task(targetp
), THREAD_NULL
,
619 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
, role
);
622 kauth_cred_unref(&target_cred
);
627 proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
)
632 kauth_cred_t ucred
, target_cred
;
634 ucred
= kauth_cred_get();
635 target_cred
= kauth_cred_proc_ref(targetp
);
637 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
638 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
639 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
644 if (curp
!= targetp
) {
646 if ((error
= mac_proc_check_sched(curp
, targetp
)))
651 role
= proc_get_task_policy(proc_task(targetp
), THREAD_NULL
,
652 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
654 *priority
= proc_task_role_to_darwin_role(role
);
657 kauth_cred_unref(&target_cred
);
663 get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
)
667 kauth_cred_t ucred
, target_cred
;
669 ucred
= kauth_cred_get();
670 target_cred
= kauth_cred_proc_ref(targetp
);
672 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
673 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
674 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
679 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
681 *priority
= proc_get_task_policy(current_task(), THREAD_NULL
, external
, TASK_POLICY_DARWIN_BG
);
684 kauth_cred_unref(&target_cred
);
689 do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
696 kauth_cred_t target_cred
;
700 ucred
= kauth_cred_get();
701 target_cred
= kauth_cred_proc_ref(targetp
);
703 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
704 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
705 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
))
712 error
= mac_proc_check_sched(curp
, targetp
);
717 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
721 enable
= TASK_POLICY_ENABLE
;
723 case PRIO_DARWIN_NONUI
:
724 /* ignored for compatibility */
727 /* TODO: EINVAL if priority != 0 */
728 enable
= TASK_POLICY_DISABLE
;
732 proc_set_task_policy(proc_task(targetp
), THREAD_NULL
, external
, TASK_POLICY_DARWIN_BG
, enable
);
735 kauth_cred_unref(&target_cred
);
740 do_background_socket(struct proc
*p
, thread_t thread
)
743 struct filedesc
*fdp
;
749 if (thread
!= THREAD_NULL
)
750 background
= proc_get_effective_thread_policy(thread
, TASK_POLICY_ALL_SOCKETS_BG
);
752 background
= proc_get_effective_task_policy(proc_task(p
), TASK_POLICY_ALL_SOCKETS_BG
);
756 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
757 * the sockets with the background flag. There's nothing
758 * to do here for the PRIO_DARWIN_THREAD case.
760 if (thread
== THREAD_NULL
) {
763 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
764 struct socket
*sockp
;
766 fp
= fdp
->fd_ofiles
[i
];
767 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
768 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
) {
771 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
772 socket_set_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
773 sockp
->so_background_thread
= NULL
;
777 /* disable networking IO throttle.
778 * NOTE - It is a known limitation of the current design that we
779 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
780 * sockets created by other threads within this process.
783 for ( i
= 0; i
< fdp
->fd_nfiles
; i
++ ) {
784 struct socket
*sockp
;
786 fp
= fdp
->fd_ofiles
[ i
];
787 if ( fp
== NULL
|| (fdp
->fd_ofileflags
[ i
] & UF_RESERVED
) != 0 ||
788 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
) {
791 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
792 /* skip if only clearing this thread's sockets */
793 if ((thread
) && (sockp
->so_background_thread
!= thread
)) {
796 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
797 sockp
->so_background_thread
= NULL
;
803 #pragma unused(p, thread)
809 * do_background_thread
811 * EPERM Tried to background while in vfork
812 * XXX - todo - does this need a MACF hook?
815 do_background_thread(struct proc
*curp
, thread_t thread
, int priority
)
818 int enable
, external
;
821 ut
= get_bsdthread_info(thread
);
823 /* Backgrounding is unsupported for threads in vfork */
824 if ((ut
->uu_flag
& UT_VFORK
) != 0)
827 if (thread_is_static_param(thread
)) {
831 /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
832 if (thread_has_qos_policy(thread
)) {
833 thread_remove_qos_policy(thread
);
837 /* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
838 enable
= (priority
== PRIO_DARWIN_BG
) ? TASK_POLICY_ENABLE
: TASK_POLICY_DISABLE
;
839 external
= (current_thread() == thread
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
841 proc_set_task_policy_thread(curp
->task
, thread_tid(thread
), external
,
842 TASK_POLICY_DARWIN_BG
, enable
);
855 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
860 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
861 sizeof (struct rlimit
))))
864 return (dosetrlimit(p
, uap
->which
, &alim
));
870 * ENOMEM Cannot copy limit structure
873 * Notes: EINVAL is returned both for invalid arguments, and in the
874 * case that the current usage (e.g. RLIMIT_STACK) is already
875 * in excess of the requested limit.
878 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
)
880 struct rlimit
*alimp
;
883 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
885 /* Mask out POSIX flag, saved above */
886 which
&= ~_RLIMIT_POSIX_FLAG
;
888 if (which
>= RLIM_NLIMITS
)
891 alimp
= &p
->p_rlimit
[which
];
892 if (limp
->rlim_cur
> limp
->rlim_max
)
895 if (limp
->rlim_cur
> alimp
->rlim_max
||
896 limp
->rlim_max
> alimp
->rlim_max
)
897 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
903 if ((error
= proc_limitreplace(p
)) != 0) {
904 proc_limitunblock(p
);
908 alimp
= &p
->p_rlimit
[which
];
913 if (limp
->rlim_cur
== RLIM_INFINITY
) {
914 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
915 timerclear(&p
->p_rlim_cpu
);
918 task_absolutetime_info_data_t tinfo
;
919 mach_msg_type_number_t count
;
920 struct timeval ttv
, tv
;
922 clock_usec_t tv_usec
;
924 count
= TASK_ABSOLUTETIME_INFO_COUNT
;
925 task_info(p
->task
, TASK_ABSOLUTETIME_INFO
,
926 (task_info_t
)&tinfo
, &count
);
927 absolutetime_to_microtime(tinfo
.total_user
+ tinfo
.total_system
,
930 ttv
.tv_usec
= tv_usec
;
932 tv
.tv_sec
= (limp
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: limp
->rlim_cur
);
934 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
937 if (timercmp(&p
->p_rlim_cpu
, &tv
, >))
938 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
940 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
942 timerclear(&p
->p_rlim_cpu
);
950 if (limp
->rlim_cur
> maxdmap
)
951 limp
->rlim_cur
= maxdmap
;
952 if (limp
->rlim_max
> maxdmap
)
953 limp
->rlim_max
= maxdmap
;
957 /* Disallow illegal stack size instead of clipping */
958 if (limp
->rlim_cur
> maxsmap
||
959 limp
->rlim_max
> maxsmap
) {
966 * 4797860 - workaround poorly written installers by
967 * doing previous implementation (< 10.5) when caller
968 * is non-POSIX conforming.
970 if (limp
->rlim_cur
> maxsmap
)
971 limp
->rlim_cur
= maxsmap
;
972 if (limp
->rlim_max
> maxsmap
)
973 limp
->rlim_max
= maxsmap
;
978 * Stack is allocated to the max at exec time with only
979 * "rlim_cur" bytes accessible. If stack limit is going
980 * up make more accessible, if going down make inaccessible.
982 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
987 size
= round_page_64(limp
->rlim_cur
);
988 size
-= round_page_64(alimp
->rlim_cur
);
990 addr
= p
->user_stack
- round_page_64(limp
->rlim_cur
);
991 kr
= mach_vm_protect(current_map(),
993 FALSE
, VM_PROT_DEFAULT
);
994 if (kr
!= KERN_SUCCESS
) {
998 } else if (limp
->rlim_cur
< alimp
->rlim_cur
) {
1006 * First check if new stack limit would agree
1007 * with current stack usage.
1008 * Get the current thread's stack pointer...
1010 cur_sp
= thread_adjuserstack(current_thread(),
1012 if (cur_sp
<= p
->user_stack
&&
1013 cur_sp
> (p
->user_stack
-
1014 round_page_64(alimp
->rlim_cur
))) {
1015 /* stack pointer is in main stack */
1016 if (cur_sp
<= (p
->user_stack
-
1017 round_page_64(limp
->rlim_cur
))) {
1019 * New limit would cause
1020 * current usage to be invalid:
1027 /* not on the main stack: reject */
1032 size
= round_page_64(alimp
->rlim_cur
);
1033 size
-= round_page_64(limp
->rlim_cur
);
1035 addr
= p
->user_stack
- round_page_64(alimp
->rlim_cur
);
1037 kr
= mach_vm_protect(current_map(),
1039 FALSE
, VM_PROT_NONE
);
1040 if (kr
!= KERN_SUCCESS
) {
1051 * Only root can set the maxfiles limits, as it is
1052 * systemwide resource. If we are expecting POSIX behavior,
1053 * instead of clamping the value, return EINVAL. We do this
1054 * because historically, people have been able to attempt to
1055 * set RLIM_INFINITY to get "whatever the maximum is".
1057 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1058 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1059 limp
->rlim_cur
> (rlim_t
)maxfiles
) {
1064 limp
->rlim_cur
= maxfiles
;
1066 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1067 limp
->rlim_max
> (rlim_t
)maxfiles
)
1068 limp
->rlim_max
= maxfiles
;
1071 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1072 limp
->rlim_cur
> (rlim_t
)maxfilesperproc
) {
1077 limp
->rlim_cur
= maxfilesperproc
;
1079 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1080 limp
->rlim_max
> (rlim_t
)maxfilesperproc
)
1081 limp
->rlim_max
= maxfilesperproc
;
1087 * Only root can set to the maxproc limits, as it is
1088 * systemwide resource; all others are limited to
1089 * maxprocperuid (presumably less than maxproc).
1091 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1092 if (limp
->rlim_cur
> (rlim_t
)maxproc
)
1093 limp
->rlim_cur
= maxproc
;
1094 if (limp
->rlim_max
> (rlim_t
)maxproc
)
1095 limp
->rlim_max
= maxproc
;
1098 if (limp
->rlim_cur
> (rlim_t
)maxprocperuid
)
1099 limp
->rlim_cur
= maxprocperuid
;
1100 if (limp
->rlim_max
> (rlim_t
)maxprocperuid
)
1101 limp
->rlim_max
= maxprocperuid
;
1105 case RLIMIT_MEMLOCK
:
1107 * Tell the Mach VM layer about the new limit value.
1110 vm_map_set_user_wire_limit(current_map(), limp
->rlim_cur
);
1119 proc_limitunblock(p
);
1125 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
1130 * Take out flag now in case we need to use it to trigger variant
1133 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
1135 if (uap
->which
>= RLIM_NLIMITS
)
1137 proc_limitget(p
, uap
->which
, &lim
);
1138 return (copyout((caddr_t
)&lim
,
1139 uap
->rlp
, sizeof (struct rlimit
)));
1143 * Transform the running time and tick information in proc p into user,
1144 * system, and interrupt time usage.
1146 /* No lock on proc is held for this.. */
1148 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
1159 mach_task_basic_info_data_t tinfo
;
1160 task_thread_times_info_data_t ttimesinfo
;
1161 task_events_info_data_t teventsinfo
;
1162 mach_msg_type_number_t task_info_count
, task_ttimes_count
;
1163 mach_msg_type_number_t task_events_count
;
1164 struct timeval ut
,st
;
1166 task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
1167 task_info(task
, MACH_TASK_BASIC_INFO
,
1168 (task_info_t
)&tinfo
, &task_info_count
);
1169 ut
.tv_sec
= tinfo
.user_time
.seconds
;
1170 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
1171 st
.tv_sec
= tinfo
.system_time
.seconds
;
1172 st
.tv_usec
= tinfo
.system_time
.microseconds
;
1173 timeradd(&ut
, up
, up
);
1174 timeradd(&st
, sp
, sp
);
1176 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1177 task_info(task
, TASK_THREAD_TIMES_INFO
,
1178 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
1180 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1181 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1182 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1183 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1184 timeradd(&ut
, up
, up
);
1185 timeradd(&st
, sp
, sp
);
1187 task_events_count
= TASK_EVENTS_INFO_COUNT
;
1188 task_info(task
, TASK_EVENTS_INFO
,
1189 (task_info_t
)&teventsinfo
, &task_events_count
);
1192 * No need to lock "p": this does not need to be
1193 * completely consistent, right ?
1195 p
->p_stats
->p_ru
.ru_minflt
= (teventsinfo
.faults
-
1196 teventsinfo
.pageins
);
1197 p
->p_stats
->p_ru
.ru_majflt
= teventsinfo
.pageins
;
1198 p
->p_stats
->p_ru
.ru_nivcsw
= (teventsinfo
.csw
-
1199 p
->p_stats
->p_ru
.ru_nvcsw
);
1200 if (p
->p_stats
->p_ru
.ru_nivcsw
< 0)
1201 p
->p_stats
->p_ru
.ru_nivcsw
= 0;
1203 p
->p_stats
->p_ru
.ru_maxrss
= tinfo
.resident_size_max
;
1207 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
1208 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
1212 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1214 struct rusage
*rup
, rubuf
;
1215 struct user64_rusage rubuf64
;
1216 struct user32_rusage rubuf32
;
1217 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1218 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1219 struct timeval utime
;
1220 struct timeval stime
;
1225 calcru(p
, &utime
, &stime
, NULL
);
1227 rup
= &p
->p_stats
->p_ru
;
1228 rup
->ru_utime
= utime
;
1229 rup
->ru_stime
= stime
;
1236 case RUSAGE_CHILDREN
:
1238 rup
= &p
->p_stats
->p_cru
;
1246 if (IS_64BIT_PROCESS(p
)) {
1247 retsize
= sizeof(rubuf64
);
1248 retbuf
= (caddr_t
)&rubuf64
;
1249 munge_user64_rusage(&rubuf
, &rubuf64
);
1251 retsize
= sizeof(rubuf32
);
1252 retbuf
= (caddr_t
)&rubuf32
;
1253 munge_user32_rusage(&rubuf
, &rubuf32
);
1256 return (copyout(retbuf
, uap
->rusage
, retsize
));
1260 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
1265 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
1266 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
1267 if (ru
->ru_maxrss
< ru2
->ru_maxrss
)
1268 ru
->ru_maxrss
= ru2
->ru_maxrss
;
1269 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
1270 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--)
1275 * Add the rusage stats of child in parent.
1277 * It adds rusage statistics of child process and statistics of all its
1278 * children to its parent.
1280 * Note: proc lock of parent should be held while calling this function.
1283 update_rusage_info_child(struct rusage_info_child
*ri
, rusage_info_current
*ri_current
)
1285 ri
->ri_child_user_time
+= (ri_current
->ri_user_time
+
1286 ri_current
->ri_child_user_time
);
1287 ri
->ri_child_system_time
+= (ri_current
->ri_system_time
+
1288 ri_current
->ri_child_system_time
);
1289 ri
->ri_child_pkg_idle_wkups
+= (ri_current
->ri_pkg_idle_wkups
+
1290 ri_current
->ri_child_pkg_idle_wkups
);
1291 ri
->ri_child_interrupt_wkups
+= (ri_current
->ri_interrupt_wkups
+
1292 ri_current
->ri_child_interrupt_wkups
);
1293 ri
->ri_child_pageins
+= (ri_current
->ri_pageins
+
1294 ri_current
->ri_child_pageins
);
1295 ri
->ri_child_elapsed_abstime
+= ((ri_current
->ri_proc_exit_abstime
-
1296 ri_current
->ri_proc_start_abstime
) + ri_current
->ri_child_elapsed_abstime
);
1300 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1303 limp
->rlim_cur
= p
->p_rlimit
[which
].rlim_cur
;
1304 limp
->rlim_max
= p
->p_rlimit
[which
].rlim_max
;
1310 proc_limitdrop(proc_t p
, int exiting
)
1312 struct plimit
* freelim
= NULL
;
1313 struct plimit
* freeoldlim
= NULL
;
1317 if (--p
->p_limit
->pl_refcnt
== 0) {
1318 freelim
= p
->p_limit
;
1321 if ((exiting
!= 0) && (p
->p_olimit
!= NULL
) && (--p
->p_olimit
->pl_refcnt
== 0)) {
1322 freeoldlim
= p
->p_olimit
;
1327 if (freelim
!= NULL
)
1328 FREE_ZONE(freelim
, sizeof *p
->p_limit
, M_PLIMIT
);
1329 if (freeoldlim
!= NULL
)
1330 FREE_ZONE(freeoldlim
, sizeof *p
->p_olimit
, M_PLIMIT
);
1335 proc_limitfork(proc_t parent
, proc_t child
)
1338 child
->p_limit
= parent
->p_limit
;
1339 child
->p_limit
->pl_refcnt
++;
1340 child
->p_olimit
= NULL
;
1345 proc_limitblock(proc_t p
)
1348 while (p
->p_lflag
& P_LLIMCHANGE
) {
1349 p
->p_lflag
|= P_LLIMWAIT
;
1350 msleep(&p
->p_olimit
, &p
->p_mlock
, 0, "proc_limitblock", NULL
);
1352 p
->p_lflag
|= P_LLIMCHANGE
;
1359 proc_limitunblock(proc_t p
)
1362 p
->p_lflag
&= ~P_LLIMCHANGE
;
1363 if (p
->p_lflag
& P_LLIMWAIT
) {
1364 p
->p_lflag
&= ~P_LLIMWAIT
;
1365 wakeup(&p
->p_olimit
);
1370 /* This is called behind serialization provided by proc_limitblock/unlbock */
1372 proc_limitreplace(proc_t p
)
1374 struct plimit
*copy
;
1379 if (p
->p_limit
->pl_refcnt
== 1) {
1386 MALLOC_ZONE(copy
, struct plimit
*,
1387 sizeof(struct plimit
), M_PLIMIT
, M_WAITOK
);
1393 bcopy(p
->p_limit
->pl_rlimit
, copy
->pl_rlimit
,
1394 sizeof(struct rlimit
) * RLIM_NLIMITS
);
1395 copy
->pl_refcnt
= 1;
1396 /* hang on to reference to old till process exits */
1397 p
->p_olimit
= p
->p_limit
;
1407 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1409 * Parameters: cmd Policy command
1410 * arg Pointer to policy arguments
1412 * Returns: 0 Success
1413 * EINVAL Invalid command or invalid policy arguments
1418 iopolicysys_disk(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1420 iopolicysys_vfs(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1423 iopolicysys(struct proc
*p
, struct iopolicysys_args
*uap
, int32_t *retval
)
1426 struct _iopol_param_t iop_param
;
1428 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0)
1431 switch (iop_param
.iop_iotype
) {
1432 case IOPOL_TYPE_DISK
:
1433 error
= iopolicysys_disk(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1434 if (error
== EIDRM
) {
1441 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY
:
1442 error
= iopolicysys_vfs(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1451 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1452 if (uap
->cmd
== IOPOL_CMD_GET
) {
1453 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1463 iopolicysys_disk(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1469 /* Validate scope */
1471 case IOPOL_SCOPE_PROCESS
:
1472 thread
= THREAD_NULL
;
1473 policy_flavor
= TASK_POLICY_IOPOL
;
1476 case IOPOL_SCOPE_THREAD
:
1477 thread
= current_thread();
1478 policy_flavor
= TASK_POLICY_IOPOL
;
1480 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1481 if (cmd
== IOPOL_CMD_SET
&& thread_has_qos_policy(thread
)) {
1487 case IOPOL_THROTTLE
:
1488 case IOPOL_IMPORTANT
:
1489 case IOPOL_STANDARD
:
1490 if (!thread_is_static_param(thread
)) {
1491 thread_remove_qos_policy(thread
);
1493 * This is not an error case, this is to return a marker to user-space that
1494 * we stripped the thread of its QoS class.
1499 /* otherwise, fall through to the error case. */
1507 case IOPOL_SCOPE_DARWIN_BG
:
1508 thread
= THREAD_NULL
;
1509 policy_flavor
= TASK_POLICY_DARWIN_BG_IOPOL
;
1517 /* Validate policy */
1518 if (cmd
== IOPOL_CMD_SET
) {
1521 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1522 /* the current default BG throttle level is UTILITY */
1523 policy
= IOPOL_UTILITY
;
1525 policy
= IOPOL_IMPORTANT
;
1530 case IOPOL_THROTTLE
:
1531 /* These levels are OK */
1533 case IOPOL_IMPORTANT
:
1535 case IOPOL_STANDARD
:
1538 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1539 /* These levels are invalid for BG */
1543 /* OK for other scopes */
1552 /* Perform command */
1555 proc_set_task_policy(current_task(), thread
,
1556 TASK_POLICY_INTERNAL
, policy_flavor
,
1560 policy
= proc_get_task_policy(current_task(), thread
,
1561 TASK_POLICY_INTERNAL
, policy_flavor
);
1563 iop_param
->iop_policy
= policy
;
1566 error
= EINVAL
; /* unknown command */
1575 iopolicysys_vfs(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1579 /* Validate scope */
1581 case IOPOL_SCOPE_PROCESS
:
1582 /* Only process OK */
1589 /* Validate policy */
1590 if (cmd
== IOPOL_CMD_SET
) {
1592 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1594 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1595 /* These policies are OK */
1603 /* Perform command */
1606 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1607 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1608 boolean_t entitled
= FALSE
;
1609 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1617 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1618 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
), &p
->p_vfs_iopolicy
);
1620 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1621 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
, &p
->p_vfs_iopolicy
);
1630 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
)
1631 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1632 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
;
1635 error
= EINVAL
; /* unknown command */
1643 /* BSD call back function for task_policy */
1644 void proc_apply_task_networkbg(void * bsd_info
, thread_t thread
);
1647 proc_apply_task_networkbg(void * bsd_info
, thread_t thread
)
1649 assert(bsd_info
!= PROC_NULL
);
1651 pid_t pid
= proc_pid((proc_t
)bsd_info
);
1653 proc_t p
= proc_find(pid
);
1655 if (p
!= PROC_NULL
) {
1656 assert(p
== (proc_t
)bsd_info
);
1658 do_background_socket(p
, thread
);
1664 gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
)
1666 struct rusage_info_child
*ri_child
;
1668 assert(p
->p_stats
!= NULL
);
1671 case RUSAGE_INFO_V3
:
1672 fill_task_qos_rusage(p
->task
, ru
);
1673 fill_task_billed_usage(p
->task
, ru
);
1676 case RUSAGE_INFO_V2
:
1677 fill_task_io_rusage(p
->task
, ru
);
1680 case RUSAGE_INFO_V1
:
1682 * p->p_stats->ri_child statistics are protected under proc lock.
1686 ri_child
= &(p
->p_stats
->ri_child
);
1687 ru
->ri_child_user_time
= ri_child
->ri_child_user_time
;
1688 ru
->ri_child_system_time
= ri_child
->ri_child_system_time
;
1689 ru
->ri_child_pkg_idle_wkups
= ri_child
->ri_child_pkg_idle_wkups
;
1690 ru
->ri_child_interrupt_wkups
= ri_child
->ri_child_interrupt_wkups
;
1691 ru
->ri_child_pageins
= ri_child
->ri_child_pageins
;
1692 ru
->ri_child_elapsed_abstime
= ri_child
->ri_child_elapsed_abstime
;
1697 case RUSAGE_INFO_V0
:
1698 proc_getexecutableuuid(p
, (unsigned char *)&ru
->ri_uuid
, sizeof (ru
->ri_uuid
));
1699 fill_task_rusage(p
->task
, ru
);
1700 ru
->ri_proc_start_abstime
= p
->p_stats
->ps_start
;
1705 rusage_info_conversion(rusage_info_t ri_info
, rusage_info_current
*ri_current
, int flavor
)
1707 struct rusage_info_v0
*ri_v0
;
1708 struct rusage_info_v1
*ri_v1
;
1709 struct rusage_info_v2
*ri_v2
;
1713 case RUSAGE_INFO_V2
:
1714 ri_v2
= (struct rusage_info_v2
*)ri_info
;
1715 ri_v2
->ri_diskio_bytesread
= ri_current
->ri_diskio_bytesread
;
1716 ri_v2
->ri_diskio_byteswritten
= ri_current
->ri_diskio_byteswritten
;
1719 case RUSAGE_INFO_V1
:
1720 ri_v1
= (struct rusage_info_v1
*)ri_info
;
1721 ri_v1
->ri_child_user_time
= ri_current
->ri_child_user_time
;
1722 ri_v1
->ri_child_system_time
= ri_current
->ri_child_system_time
;
1723 ri_v1
->ri_child_pkg_idle_wkups
= ri_current
->ri_child_pkg_idle_wkups
;
1724 ri_v1
->ri_child_interrupt_wkups
= ri_current
->ri_child_interrupt_wkups
;
1725 ri_v1
->ri_child_pageins
= ri_current
->ri_child_pageins
;
1726 ri_v1
->ri_child_elapsed_abstime
= ri_current
->ri_child_elapsed_abstime
;
1729 case RUSAGE_INFO_V0
:
1730 ri_v0
= (struct rusage_info_v0
*)ri_info
;
1731 memcpy(&ri_v0
->ri_uuid
[0], &ri_current
->ri_uuid
[0], sizeof(ri_v0
->ri_uuid
));
1732 ri_v0
->ri_user_time
= ri_current
->ri_user_time
;
1733 ri_v0
->ri_system_time
= ri_current
->ri_system_time
;
1734 ri_v0
->ri_pkg_idle_wkups
= ri_current
->ri_pkg_idle_wkups
;
1735 ri_v0
->ri_interrupt_wkups
= ri_current
->ri_interrupt_wkups
;
1736 ri_v0
->ri_pageins
= ri_current
->ri_pageins
;
1737 ri_v0
->ri_wired_size
= ri_current
->ri_wired_size
;
1738 ri_v0
->ri_resident_size
= ri_current
->ri_resident_size
;
1739 ri_v0
->ri_phys_footprint
= ri_current
->ri_phys_footprint
;
1740 ri_v0
->ri_proc_start_abstime
= ri_current
->ri_proc_start_abstime
;
1741 ri_v0
->ri_proc_exit_abstime
= ri_current
->ri_proc_exit_abstime
;
1752 proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
)
1754 struct rusage_info_v0 ri_v0
;
1755 struct rusage_info_v1 ri_v1
;
1756 struct rusage_info_v2 ri_v2
;
1757 struct rusage_info_v3 ri_v3
;
1759 rusage_info_current ri_current
;
1764 case RUSAGE_INFO_V0
:
1766 * If task is still alive, collect info from the live task itself.
1767 * Otherwise, look to the cached info in the zombie proc.
1769 if (p
->p_ru
== NULL
) {
1770 gather_rusage_info(p
, &ri_current
, flavor
);
1771 ri_current
.ri_proc_exit_abstime
= 0;
1772 rusage_info_conversion(&ri_v0
, &ri_current
, flavor
);
1774 rusage_info_conversion(&ri_v0
, &p
->p_ru
->ri
, flavor
);
1776 error
= copyout(&ri_v0
, buffer
, sizeof (ri_v0
));
1779 case RUSAGE_INFO_V1
:
1781 * If task is still alive, collect info from the live task itself.
1782 * Otherwise, look to the cached info in the zombie proc.
1784 if (p
->p_ru
== NULL
) {
1785 gather_rusage_info(p
, &ri_current
, flavor
);
1786 ri_current
.ri_proc_exit_abstime
= 0;
1787 rusage_info_conversion(&ri_v1
, &ri_current
, flavor
);
1789 rusage_info_conversion(&ri_v1
, &p
->p_ru
->ri
, flavor
);
1791 error
= copyout(&ri_v1
, buffer
, sizeof (ri_v1
));
1794 case RUSAGE_INFO_V2
:
1796 * If task is still alive, collect info from the live task itself.
1797 * Otherwise, look to the cached info in the zombie proc.
1799 if (p
->p_ru
== NULL
) {
1800 gather_rusage_info(p
, &ri_current
, flavor
);
1801 ri_current
.ri_proc_exit_abstime
= 0;
1802 rusage_info_conversion(&ri_v2
, &ri_current
, flavor
);
1804 rusage_info_conversion(&ri_v2
, &p
->p_ru
->ri
, flavor
);
1806 error
= copyout(&ri_v2
, buffer
, sizeof (ri_v2
));
1809 case RUSAGE_INFO_V3
:
1811 * If task is still alive, collect info from the live task itself.
1812 * Otherwise, look to the cached info in the zombie proc.
1814 if (p
->p_ru
== NULL
) {
1815 gather_rusage_info(p
, &ri_v3
, flavor
);
1816 ri_v3
.ri_proc_exit_abstime
= 0;
1818 ri_v3
= p
->p_ru
->ri
;
1820 error
= copyout(&ri_v3
, buffer
, sizeof (ri_v3
));
1832 mach_to_bsd_rv(int mach_rv
)
1840 case KERN_INVALID_ARGUMENT
:
1844 panic("unknown error %#x", mach_rv
);
1851 * Resource limit controls
1853 * uap->flavor available flavors:
1855 * RLIMIT_WAKEUPS_MONITOR
1858 proc_rlimit_control(__unused
struct proc
*p
, struct proc_rlimit_control_args
*uap
, __unused
int32_t *retval
)
1862 struct proc_rlimit_control_wakeupmon wakeupmon_args
;
1863 uint32_t cpumon_flags
;
1864 uint32_t cpulimits_flags
;
1865 kauth_cred_t my_cred
, target_cred
;
1867 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
1868 if (uap
->pid
== -1) {
1869 targetp
= proc_self();
1871 targetp
= proc_find(uap
->pid
);
1874 /* proc_self() can return NULL for an exiting process */
1875 if (targetp
== PROC_NULL
) {
1879 my_cred
= kauth_cred_get();
1880 target_cred
= kauth_cred_proc_ref(targetp
);
1882 if (!kauth_cred_issuser(my_cred
) && kauth_cred_getruid(my_cred
) &&
1883 kauth_cred_getuid(my_cred
) != kauth_cred_getuid(target_cred
) &&
1884 kauth_cred_getruid(my_cred
) != kauth_cred_getuid(target_cred
)) {
1886 kauth_cred_unref(&target_cred
);
1890 switch (uap
->flavor
) {
1891 case RLIMIT_WAKEUPS_MONITOR
:
1892 if ((error
= copyin(uap
->arg
, &wakeupmon_args
, sizeof (wakeupmon_args
))) != 0) {
1895 if ((error
= mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp
->task
, &wakeupmon_args
.wm_flags
,
1896 &wakeupmon_args
.wm_rate
))) != 0) {
1899 error
= copyout(&wakeupmon_args
, uap
->arg
, sizeof (wakeupmon_args
));
1901 case RLIMIT_CPU_USAGE_MONITOR
:
1902 cpumon_flags
= uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
1903 error
= mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp
->task
, &cpumon_flags
));
1905 case RLIMIT_THREAD_CPULIMITS
:
1906 cpulimits_flags
= (uint32_t)uap
->arg
; // only need a limited set of bits, pass in void * argument
1908 if (uap
->pid
!= -1) {
1913 uint8_t percent
= 0;
1914 uint32_t ms_refill
= 0;
1917 percent
= (uint8_t)(cpulimits_flags
& 0xffU
); /* low 8 bits for percent */
1918 ms_refill
= (cpulimits_flags
>> 8) & 0xffffff; /* next 24 bits represent ms refill value */
1919 if (percent
>= 100) {
1924 ns_refill
= ((uint64_t)ms_refill
) * NSEC_PER_MSEC
;
1926 error
= mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK
, percent
, ns_refill
));
1934 kauth_cred_unref(&target_cred
);
1937 * Return value from this function becomes errno to userland caller.
1943 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
1945 int thread_selfusage(struct proc
*p __unused
, struct thread_selfusage_args
*uap __unused
, uint64_t *retval
)
1949 runtime
= thread_get_runtime_self();