2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
79 #include <sys/file_internal.h>
80 #include <sys/resourcevar.h>
81 #include <sys/malloc.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <machine/spl.h>
86 #include <sys/mount_internal.h>
87 #include <sys/sysproto.h>
89 #include <security/audit/audit.h>
91 #include <machine/vmparam.h>
93 #include <mach/mach_types.h>
94 #include <mach/time_value.h>
95 #include <mach/task.h>
96 #include <mach/task_info.h>
97 #include <mach/vm_map.h>
98 #include <mach/mach_vm.h>
99 #include <mach/thread_act.h> /* for thread_policy_set( ) */
100 #include <kern/lock.h>
101 #include <kern/thread.h>
103 #include <kern/task.h>
104 #include <kern/clock.h> /* for absolutetime_to_microtime() */
105 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
106 #include <sys/socketvar.h> /* for struct socket */
108 #include <vm/vm_map.h>
110 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
111 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
112 int uthread_get_background_state(uthread_t
);
113 static void do_background_socket(struct proc
*p
, thread_t thread
, int priority
);
114 static int do_background_thread(struct proc
*curp
, thread_t thread
, int priority
);
115 static int do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
116 void proc_apply_task_networkbg_internal(proc_t
, thread_t
);
117 void proc_restore_task_networkbg_internal(proc_t
, thread_t
);
119 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
120 rlim_t maxsmap
= MAXSSIZ
- PAGE_SIZE
; /* XXX */
123 * Limits on the number of open files per process, and the number
124 * of child processes per process.
126 * Note: would be in kern/subr_param.c in FreeBSD.
128 __private_extern__
int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
130 SYSCTL_INT(_kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
131 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
133 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
134 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
136 /* Args and fn for proc_iteration callback used in setpriority */
137 struct puser_nice_args
{
144 static int puser_donice_callback(proc_t p
, void * arg
);
147 /* Args and fn for proc_iteration callback used in setpriority */
148 struct ppgrp_nice_args
{
154 static int ppgrp_donice_callback(proc_t p
, void * arg
);
157 * Resource controls and accounting.
160 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, int32_t *retval
)
163 int low
= PRIO_MAX
+ 1;
164 kauth_cred_t my_cred
;
166 /* would also test (uap->who < 0), but id_t is unsigned */
167 if (uap
->who
> 0x7fffffff)
170 switch (uap
->which
) {
177 p
= proc_find(uap
->who
);
187 struct pgrp
*pg
= PGRP_NULL
;
190 /* returns the pgrp to ref */
191 pg
= proc_pgrp(curp
);
192 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
195 /* No need for iteration as it is a simple scan */
197 for (p
= pg
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
208 uap
->who
= kauth_cred_getuid(kauth_cred_get());
212 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
213 my_cred
= kauth_cred_proc_ref(p
);
214 if (kauth_cred_getuid(my_cred
) == uap
->who
&&
217 kauth_cred_unref(&my_cred
);
224 case PRIO_DARWIN_THREAD
: {
228 /* we currently only support the current thread */
233 thread
= current_thread();
234 ut
= get_bsdthread_info(thread
);
237 if ( (ut
->uu_flag
& UT_BACKGROUND_TRAFFIC_MGT
) != 0 ) {
246 if (low
== PRIO_MAX
+ 1)
252 /* call back function used for proc iteration in PRIO_USER */
254 puser_donice_callback(proc_t p
, void * arg
)
257 struct puser_nice_args
* pun
= (struct puser_nice_args
*)arg
;
258 kauth_cred_t my_cred
;
260 my_cred
= kauth_cred_proc_ref(p
);
261 if (kauth_cred_getuid(my_cred
) == pun
->who
) {
262 error
= donice(pun
->curp
, p
, pun
->prio
);
263 if (pun
->errorp
!= NULL
)
264 *pun
->errorp
= error
;
265 if (pun
->foundp
!= NULL
) {
270 kauth_cred_unref(&my_cred
);
272 return(PROC_RETURNED
);
275 /* call back function used for proc iteration in PRIO_PGRP */
277 ppgrp_donice_callback(proc_t p
, void * arg
)
280 struct ppgrp_nice_args
* pun
= (struct ppgrp_nice_args
*)arg
;
283 error
= donice(pun
->curp
, p
, pun
->prio
);
284 if (pun
->errorp
!= NULL
)
285 *pun
->errorp
= error
;
286 if (pun
->foundp
!= NULL
) {
291 return(PROC_RETURNED
);
303 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, __unused
int32_t *retval
)
306 int found
= 0, error
= 0;
309 AUDIT_ARG(cmd
, uap
->which
);
310 AUDIT_ARG(owner
, uap
->who
, 0);
311 AUDIT_ARG(value32
, uap
->prio
);
313 /* would also test (uap->who < 0), but id_t is unsigned */
314 if (uap
->who
> 0x7fffffff)
317 switch (uap
->which
) {
323 p
= proc_find(uap
->who
);
328 error
= donice(curp
, p
, uap
->prio
);
335 struct pgrp
*pg
= PGRP_NULL
;
336 struct ppgrp_nice_args ppgrp
;
339 pg
= proc_pgrp(curp
);
340 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
)
344 ppgrp
.prio
= uap
->prio
;
345 ppgrp
.foundp
= &found
;
346 ppgrp
.errorp
= &error
;
348 /* PGRP_DROPREF drops the reference on process group */
349 pgrp_iterate(pg
, PGRP_DROPREF
, ppgrp_donice_callback
, (void *)&ppgrp
, NULL
, NULL
);
355 struct puser_nice_args punice
;
358 uap
->who
= kauth_cred_getuid(kauth_cred_get());
361 punice
.prio
= uap
->prio
;
362 punice
.who
= uap
->who
;
363 punice
.foundp
= &found
;
365 punice
.errorp
= &error
;
366 proc_iterate(PROC_ALLPROCLIST
, puser_donice_callback
, (void *)&punice
, NULL
, NULL
);
371 case PRIO_DARWIN_THREAD
: {
372 /* process marked for termination no priority management */
373 if ((curp
->p_lflag
& P_LPTERMINATE
) != 0)
375 /* we currently only support the current thread */
379 error
= do_background_thread(curp
, current_thread(), uap
->prio
);
381 (void) do_background_socket(curp
, current_thread(), uap
->prio
);
387 case PRIO_DARWIN_PROCESS
: {
391 p
= proc_find(uap
->who
);
397 /* process marked for termination no priority management */
398 if ((p
->p_lflag
& P_LPTERMINATE
) != 0) {
401 error
= do_background_proc(curp
, p
, uap
->prio
);
403 (void) do_background_socket(p
, NULL
, uap
->prio
);
426 * mac_check_proc_sched:???
429 donice(struct proc
*curp
, struct proc
*chgp
, int n
)
433 kauth_cred_t my_cred
;
435 ucred
= kauth_cred_proc_ref(curp
);
436 my_cred
= kauth_cred_proc_ref(chgp
);
438 if (suser(ucred
, NULL
) && kauth_cred_getruid(ucred
) &&
439 kauth_cred_getuid(ucred
) != kauth_cred_getuid(my_cred
) &&
440 kauth_cred_getruid(ucred
) != kauth_cred_getuid(my_cred
)) {
448 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
)) {
453 error
= mac_proc_check_sched(curp
, chgp
);
460 (void)resetpriority(chgp
);
462 kauth_cred_unref(&ucred
);
463 kauth_cred_unref(&my_cred
);
468 do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
472 kauth_cred_t target_cred
;
474 ucred
= kauth_cred_get();
475 target_cred
= kauth_cred_proc_ref(targetp
);
477 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
478 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
479 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
))
486 error
= mac_proc_check_sched(curp
, targetp
);
491 if (priority
== PRIO_DARWIN_NONUI
)
492 error
= proc_apply_task_gpuacc(targetp
->task
, TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_NOACCESS
);
494 error
= proc_set_and_apply_bgtaskpolicy(targetp
->task
, priority
);
499 kauth_cred_unref(&target_cred
);
504 do_background_socket(struct proc
*p
, thread_t thread
, int priority
)
506 struct filedesc
*fdp
;
510 if (priority
== PRIO_DARWIN_BG
) {
512 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
513 * the sockets with the background flag. There's nothing
514 * to do here for the PRIO_DARWIN_THREAD case.
516 if (thread
== NULL
) {
520 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
521 struct socket
*sockp
;
523 fp
= fdp
->fd_ofiles
[i
];
524 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
525 fp
->f_fglob
->fg_type
!= DTYPE_SOCKET
) {
528 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
529 socket_set_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
530 sockp
->so_background_thread
= NULL
;
537 /* disable networking IO throttle.
538 * NOTE - It is a known limitation of the current design that we
539 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
540 * sockets created by other threads within this process.
544 for ( i
= 0; i
< fdp
->fd_nfiles
; i
++ ) {
545 struct socket
*sockp
;
547 fp
= fdp
->fd_ofiles
[ i
];
548 if ( fp
== NULL
|| (fdp
->fd_ofileflags
[ i
] & UF_RESERVED
) != 0 ||
549 fp
->f_fglob
->fg_type
!= DTYPE_SOCKET
) {
552 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
553 /* skip if only clearing this thread's sockets */
554 if ((thread
) && (sockp
->so_background_thread
!= thread
)) {
557 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
558 sockp
->so_background_thread
= NULL
;
566 * do_background_thread
568 * XXX - todo - does this need a MACF hook?
570 * NOTE: To maintain binary compatibility with PRIO_DARWIN_THREAD with respect
571 * to network traffic management, UT_BACKGROUND_TRAFFIC_MGT is set/cleared
572 * along with UT_BACKGROUND flag, as the latter alone no longer implies
573 * any form of traffic regulation (it simply means that the thread is
574 * background.) With PRIO_DARWIN_PROCESS, any form of network traffic
575 * management must be explicitly requested via whatever means appropriate,
576 * and only TRAFFIC_MGT_SO_BACKGROUND is set via do_background_socket().
579 do_background_thread(struct proc
*curp __unused
, thread_t thread
, int priority
)
584 ut
= get_bsdthread_info(thread
);
586 /* Backgrounding is unsupported for threads in vfork */
587 if ( (ut
->uu_flag
& UT_VFORK
) != 0) {
591 error
= proc_set_and_apply_bgthreadpolicy(curp
->task
, thread_tid(thread
), priority
);
597 int mach_do_background_thread(thread_t thread
, int prio
);
600 mach_do_background_thread(thread_t thread
, int prio
)
603 struct proc
*curp
= NULL
;
604 struct proc
*targetp
= NULL
;
607 targetp
= get_bsdtask_info(get_threadtask(thread
));
609 return KERN_INVALID_ARGUMENT
;
613 if (curp
== PROC_NULL
) {
617 ucred
= kauth_cred_proc_ref(curp
);
619 if (suser(ucred
, NULL
) && curp
!= targetp
) {
620 error
= KERN_PROTECTION_FAILURE
;
624 error
= do_background_thread(curp
, thread
, prio
);
626 (void) do_background_socket(curp
, thread
, prio
);
628 if (error
== EPERM
) {
629 error
= KERN_PROTECTION_FAILURE
;
631 error
= KERN_FAILURE
;
637 kauth_cred_unref(&ucred
);
640 #endif /* CONFIG_EMBEDDED */
649 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
654 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
655 sizeof (struct rlimit
))))
658 return (dosetrlimit(p
, uap
->which
, &alim
));
664 * ENOMEM Cannot copy limit structure
667 * Notes: EINVAL is returned both for invalid arguments, and in the
668 * case that the current usage (e.g. RLIMIT_STACK) is already
669 * in excess of the requested limit.
672 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
)
674 struct rlimit
*alimp
;
677 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
679 /* Mask out POSIX flag, saved above */
680 which
&= ~_RLIMIT_POSIX_FLAG
;
682 if (which
>= RLIM_NLIMITS
)
685 alimp
= &p
->p_rlimit
[which
];
686 if (limp
->rlim_cur
> limp
->rlim_max
)
689 if (limp
->rlim_cur
> alimp
->rlim_max
||
690 limp
->rlim_max
> alimp
->rlim_max
)
691 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
697 if ((error
= proc_limitreplace(p
)) != 0) {
698 proc_limitunblock(p
);
702 alimp
= &p
->p_rlimit
[which
];
707 if (limp
->rlim_cur
== RLIM_INFINITY
) {
708 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
709 timerclear(&p
->p_rlim_cpu
);
712 task_absolutetime_info_data_t tinfo
;
713 mach_msg_type_number_t count
;
714 struct timeval ttv
, tv
;
716 clock_usec_t tv_usec
;
718 count
= TASK_ABSOLUTETIME_INFO_COUNT
;
719 task_info(p
->task
, TASK_ABSOLUTETIME_INFO
,
720 (task_info_t
)&tinfo
, &count
);
721 absolutetime_to_microtime(tinfo
.total_user
+ tinfo
.total_system
,
724 ttv
.tv_usec
= tv_usec
;
726 tv
.tv_sec
= (limp
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: limp
->rlim_cur
);
728 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
731 if (timercmp(&p
->p_rlim_cpu
, &tv
, >))
732 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
734 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
736 timerclear(&p
->p_rlim_cpu
);
744 if (limp
->rlim_cur
> maxdmap
)
745 limp
->rlim_cur
= maxdmap
;
746 if (limp
->rlim_max
> maxdmap
)
747 limp
->rlim_max
= maxdmap
;
751 /* Disallow illegal stack size instead of clipping */
752 if (limp
->rlim_cur
> maxsmap
||
753 limp
->rlim_max
> maxsmap
) {
760 * 4797860 - workaround poorly written installers by
761 * doing previous implementation (< 10.5) when caller
762 * is non-POSIX conforming.
764 if (limp
->rlim_cur
> maxsmap
)
765 limp
->rlim_cur
= maxsmap
;
766 if (limp
->rlim_max
> maxsmap
)
767 limp
->rlim_max
= maxsmap
;
772 * Stack is allocated to the max at exec time with only
773 * "rlim_cur" bytes accessible. If stack limit is going
774 * up make more accessible, if going down make inaccessible.
776 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
781 size
= round_page_64(limp
->rlim_cur
);
782 size
-= round_page_64(alimp
->rlim_cur
);
784 addr
= p
->user_stack
- round_page_64(limp
->rlim_cur
);
785 kr
= mach_vm_protect(current_map(),
787 FALSE
, VM_PROT_DEFAULT
);
788 if (kr
!= KERN_SUCCESS
) {
792 } else if (limp
->rlim_cur
< alimp
->rlim_cur
) {
800 * First check if new stack limit would agree
801 * with current stack usage.
802 * Get the current thread's stack pointer...
804 cur_sp
= thread_adjuserstack(current_thread(),
806 if (cur_sp
<= p
->user_stack
&&
807 cur_sp
> (p
->user_stack
-
808 round_page_64(alimp
->rlim_cur
))) {
809 /* stack pointer is in main stack */
810 if (cur_sp
<= (p
->user_stack
-
811 round_page_64(limp
->rlim_cur
))) {
813 * New limit would cause
814 * current usage to be invalid:
821 /* not on the main stack: reject */
826 size
= round_page_64(alimp
->rlim_cur
);
827 size
-= round_page_64(limp
->rlim_cur
);
829 addr
= p
->user_stack
- round_page_64(alimp
->rlim_cur
);
831 kr
= mach_vm_protect(current_map(),
833 FALSE
, VM_PROT_NONE
);
834 if (kr
!= KERN_SUCCESS
) {
845 * Only root can set the maxfiles limits, as it is
846 * systemwide resource. If we are expecting POSIX behavior,
847 * instead of clamping the value, return EINVAL. We do this
848 * because historically, people have been able to attempt to
849 * set RLIM_INFINITY to get "whatever the maximum is".
852 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
853 limp
->rlim_cur
> (rlim_t
)maxfiles
) {
858 limp
->rlim_cur
= maxfiles
;
860 if (limp
->rlim_max
!= alimp
->rlim_max
&&
861 limp
->rlim_max
> (rlim_t
)maxfiles
)
862 limp
->rlim_max
= maxfiles
;
865 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
866 limp
->rlim_cur
> (rlim_t
)maxfilesperproc
) {
871 limp
->rlim_cur
= maxfilesperproc
;
873 if (limp
->rlim_max
!= alimp
->rlim_max
&&
874 limp
->rlim_max
> (rlim_t
)maxfilesperproc
)
875 limp
->rlim_max
= maxfilesperproc
;
881 * Only root can set to the maxproc limits, as it is
882 * systemwide resource; all others are limited to
883 * maxprocperuid (presumably less than maxproc).
886 if (limp
->rlim_cur
> (rlim_t
)maxproc
)
887 limp
->rlim_cur
= maxproc
;
888 if (limp
->rlim_max
> (rlim_t
)maxproc
)
889 limp
->rlim_max
= maxproc
;
892 if (limp
->rlim_cur
> (rlim_t
)maxprocperuid
)
893 limp
->rlim_cur
= maxprocperuid
;
894 if (limp
->rlim_max
> (rlim_t
)maxprocperuid
)
895 limp
->rlim_max
= maxprocperuid
;
901 * Tell the Mach VM layer about the new limit value.
904 vm_map_set_user_wire_limit(current_map(), limp
->rlim_cur
);
913 proc_limitunblock(p
);
919 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
924 * Take out flag now in case we need to use it to trigger variant
927 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
929 if (uap
->which
>= RLIM_NLIMITS
)
931 proc_limitget(p
, uap
->which
, &lim
);
932 return (copyout((caddr_t
)&lim
,
933 uap
->rlp
, sizeof (struct rlimit
)));
937 * Transform the running time and tick information in proc p into user,
938 * system, and interrupt time usage.
940 /* No lock on proc is held for this.. */
942 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
953 mach_task_basic_info_data_t tinfo
;
954 task_thread_times_info_data_t ttimesinfo
;
955 task_events_info_data_t teventsinfo
;
956 mach_msg_type_number_t task_info_count
, task_ttimes_count
;
957 mach_msg_type_number_t task_events_count
;
958 struct timeval ut
,st
;
960 task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
961 task_info(task
, MACH_TASK_BASIC_INFO
,
962 (task_info_t
)&tinfo
, &task_info_count
);
963 ut
.tv_sec
= tinfo
.user_time
.seconds
;
964 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
965 st
.tv_sec
= tinfo
.system_time
.seconds
;
966 st
.tv_usec
= tinfo
.system_time
.microseconds
;
967 timeradd(&ut
, up
, up
);
968 timeradd(&st
, sp
, sp
);
970 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
971 task_info(task
, TASK_THREAD_TIMES_INFO
,
972 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
974 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
975 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
976 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
977 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
978 timeradd(&ut
, up
, up
);
979 timeradd(&st
, sp
, sp
);
981 task_events_count
= TASK_EVENTS_INFO_COUNT
;
982 task_info(task
, TASK_EVENTS_INFO
,
983 (task_info_t
)&teventsinfo
, &task_events_count
);
986 * No need to lock "p": this does not need to be
987 * completely consistent, right ?
989 p
->p_stats
->p_ru
.ru_minflt
= (teventsinfo
.faults
-
990 teventsinfo
.pageins
);
991 p
->p_stats
->p_ru
.ru_majflt
= teventsinfo
.pageins
;
992 p
->p_stats
->p_ru
.ru_nivcsw
= (teventsinfo
.csw
-
993 p
->p_stats
->p_ru
.ru_nvcsw
);
994 if (p
->p_stats
->p_ru
.ru_nivcsw
< 0)
995 p
->p_stats
->p_ru
.ru_nivcsw
= 0;
997 p
->p_stats
->p_ru
.ru_maxrss
= tinfo
.resident_size_max
;
1001 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
1002 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
1006 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1008 struct rusage
*rup
, rubuf
;
1009 struct user64_rusage rubuf64
;
1010 struct user32_rusage rubuf32
;
1011 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1012 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1013 struct timeval utime
;
1014 struct timeval stime
;
1019 calcru(p
, &utime
, &stime
, NULL
);
1021 rup
= &p
->p_stats
->p_ru
;
1022 rup
->ru_utime
= utime
;
1023 rup
->ru_stime
= stime
;
1030 case RUSAGE_CHILDREN
:
1032 rup
= &p
->p_stats
->p_cru
;
1040 if (IS_64BIT_PROCESS(p
)) {
1041 retsize
= sizeof(rubuf64
);
1042 retbuf
= (caddr_t
)&rubuf64
;
1043 munge_user64_rusage(&rubuf
, &rubuf64
);
1045 retsize
= sizeof(rubuf32
);
1046 retbuf
= (caddr_t
)&rubuf32
;
1047 munge_user32_rusage(&rubuf
, &rubuf32
);
1050 return (copyout(retbuf
, uap
->rusage
, retsize
));
1054 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
1059 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
1060 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
1061 if (ru
->ru_maxrss
< ru2
->ru_maxrss
)
1062 ru
->ru_maxrss
= ru2
->ru_maxrss
;
1063 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
1064 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--)
1069 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1072 limp
->rlim_cur
= p
->p_rlimit
[which
].rlim_cur
;
1073 limp
->rlim_max
= p
->p_rlimit
[which
].rlim_max
;
1079 proc_limitdrop(proc_t p
, int exiting
)
1081 struct plimit
* freelim
= NULL
;
1082 struct plimit
* freeoldlim
= NULL
;
1086 if (--p
->p_limit
->pl_refcnt
== 0) {
1087 freelim
= p
->p_limit
;
1090 if ((exiting
!= 0) && (p
->p_olimit
!= NULL
) && (--p
->p_olimit
->pl_refcnt
== 0)) {
1091 freeoldlim
= p
->p_olimit
;
1096 if (freelim
!= NULL
)
1097 FREE_ZONE(freelim
, sizeof *p
->p_limit
, M_PLIMIT
);
1098 if (freeoldlim
!= NULL
)
1099 FREE_ZONE(freeoldlim
, sizeof *p
->p_olimit
, M_PLIMIT
);
1104 proc_limitfork(proc_t parent
, proc_t child
)
1107 child
->p_limit
= parent
->p_limit
;
1108 child
->p_limit
->pl_refcnt
++;
1109 child
->p_olimit
= NULL
;
1114 proc_limitblock(proc_t p
)
1117 while (p
->p_lflag
& P_LLIMCHANGE
) {
1118 p
->p_lflag
|= P_LLIMWAIT
;
1119 msleep(&p
->p_olimit
, &p
->p_mlock
, 0, "proc_limitblock", NULL
);
1121 p
->p_lflag
|= P_LLIMCHANGE
;
1128 proc_limitunblock(proc_t p
)
1131 p
->p_lflag
&= ~P_LLIMCHANGE
;
1132 if (p
->p_lflag
& P_LLIMWAIT
) {
1133 p
->p_lflag
&= ~P_LLIMWAIT
;
1134 wakeup(&p
->p_olimit
);
1139 /* This is called behind serialization provided by proc_limitblock/unlbock */
1141 proc_limitreplace(proc_t p
)
1143 struct plimit
*copy
;
1148 if (p
->p_limit
->pl_refcnt
== 1) {
1155 MALLOC_ZONE(copy
, struct plimit
*,
1156 sizeof(struct plimit
), M_PLIMIT
, M_WAITOK
);
1162 bcopy(p
->p_limit
->pl_rlimit
, copy
->pl_rlimit
,
1163 sizeof(struct rlimit
) * RLIM_NLIMITS
);
1164 copy
->pl_refcnt
= 1;
1165 /* hang on to reference to old till process exits */
1166 p
->p_olimit
= p
->p_limit
;
1177 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1179 * Parameters: cmd Policy command
1180 * arg Pointer to policy arguments
1182 * Returns: 0 Success
1183 * EINVAL Invalid command or invalid policy arguments
1187 iopolicysys(__unused
struct proc
*p
, __unused
struct iopolicysys_args
*uap
, __unused
int32_t *retval
)
1190 struct _iopol_param_t iop_param
;
1191 int processwide
= 0;
1193 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0)
1196 if (iop_param
.iop_iotype
!= IOPOL_TYPE_DISK
) {
1201 switch (iop_param
.iop_scope
) {
1202 case IOPOL_SCOPE_PROCESS
:
1205 case IOPOL_SCOPE_THREAD
:
1215 switch (iop_param
.iop_policy
) {
1218 case IOPOL_THROTTLE
:
1221 if(processwide
!= 0)
1222 proc_apply_task_diskacc(current_task(), iop_param
.iop_policy
);
1224 proc_apply_thread_selfdiskacc(iop_param
.iop_policy
);
1234 if(processwide
!= 0)
1235 iop_param
.iop_policy
= proc_get_task_disacc(current_task());
1237 iop_param
.iop_policy
= proc_get_thread_selfdiskacc();
1239 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1243 error
= EINVAL
; // unknown command
1253 boolean_t
thread_is_io_throttled(void);
1256 thread_is_io_throttled(void)
1258 return(proc_get_task_selfdiskacc() == IOPOL_THROTTLE
);
1262 proc_apply_task_networkbg(void * bsd_info
)
1264 proc_t p
= PROC_NULL
;
1265 proc_t curp
= (proc_t
)bsd_info
;
1270 if (p
!= PROC_NULL
) {
1271 do_background_socket(p
, NULL
, PRIO_DARWIN_BG
);
1277 proc_restore_task_networkbg(void * bsd_info
)
1279 proc_t p
= PROC_NULL
;
1280 proc_t curp
= (proc_t
)bsd_info
;
1285 if (p
!= PROC_NULL
) {
1286 do_background_socket(p
, NULL
, 0);
1293 proc_set_task_networkbg(void * bsdinfo
, int setbg
)
1296 proc_apply_task_networkbg(bsdinfo
);
1298 proc_restore_task_networkbg(bsdinfo
);
1302 proc_apply_task_networkbg_internal(proc_t p
, thread_t thread
)
1304 if (p
!= PROC_NULL
) {
1305 do_background_socket(p
, thread
, PRIO_DARWIN_BG
);
1309 proc_restore_task_networkbg_internal(proc_t p
, thread_t thread
)
1311 if (p
!= PROC_NULL
) {
1312 do_background_socket(p
, thread
, PRIO_DARWIN_BG
);