2 * Copyright (c) 2006, 2010 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/cdefs.h>
29 #include <sys/errno.h>
30 #include <sys/msgbuf.h>
31 #include <sys/resource.h>
32 #include <sys/process_policy.h>
33 #include <mach/message.h>
35 #include "libproc_internal.h"
37 int __proc_info(int callnum
, int pid
, int flavor
, uint64_t arg
, void * buffer
, int buffersize
);
38 __private_extern__
int proc_setthreadname(void * buffer
, int buffersize
);
39 int __process_policy(int scope
, int action
, int policy
, int policy_subtype
, proc_policy_attribute_t
* attrp
, pid_t target_pid
, uint64_t target_threadid
);
40 int proc_rlimit_control(pid_t pid
, int flavor
, void *arg
);
43 proc_listpids(uint32_t type
, uint32_t typeinfo
, void *buffer
, int buffersize
)
47 if ((type
>= PROC_ALL_PIDS
) || (type
<= PROC_PPID_ONLY
)) {
48 if ((retval
= __proc_info(PROC_INFO_CALL_LISTPIDS
, type
, typeinfo
,(uint64_t)0, buffer
, buffersize
)) == -1)
59 proc_listallpids(void * buffer
, int buffersize
)
62 numpids
= proc_listpids(PROC_ALL_PIDS
, (uint32_t)0, buffer
, buffersize
);
67 return(numpids
/sizeof(int));
71 proc_listpgrppids(pid_t pgrpid
, void * buffer
, int buffersize
)
74 numpids
= proc_listpids(PROC_PGRP_ONLY
, (uint32_t)pgrpid
, buffer
, buffersize
);
78 return(numpids
/sizeof(int));
82 proc_listchildpids(pid_t ppid
, void * buffer
, int buffersize
)
85 numpids
= proc_listpids(PROC_PPID_ONLY
, (uint32_t)ppid
, buffer
, buffersize
);
89 return(numpids
/sizeof(int));
94 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, void *buffer
, int buffersize
)
98 if ((retval
= __proc_info(PROC_INFO_CALL_PIDINFO
, pid
, flavor
, arg
, buffer
, buffersize
)) == -1)
106 proc_pidoriginatorinfo(int flavor
, void *buffer
, int buffersize
)
110 if ((retval
= __proc_info(PROC_INFO_CALL_PIDORIGINATORINFO
, getpid(), flavor
, 0, buffer
, buffersize
)) == -1)
117 proc_pid_rusage(int pid
, int flavor
, rusage_info_t
*buffer
)
119 return (__proc_info(PROC_INFO_CALL_PIDRUSAGE
, pid
, flavor
, 0, buffer
, 0));
123 proc_setthread_cpupercent(uint8_t percentage
, uint32_t ms_refill
)
127 /* Pack percentage and refill into a 32-bit number to match existing kernel implementation */
128 if ((percentage
>= 100) || (ms_refill
& ~0xffffffU
)) {
133 arg
= ((ms_refill
<< 8) | percentage
);
135 return (proc_rlimit_control(-1, RLIMIT_THREAD_CPULIMITS
, (void *)(uintptr_t)arg
));
139 proc_pidfdinfo(int pid
, int fd
, int flavor
, void * buffer
, int buffersize
)
143 if ((retval
= __proc_info(PROC_INFO_CALL_PIDFDINFO
, pid
, flavor
, (uint64_t)fd
, buffer
, buffersize
)) == -1)
151 proc_pidfileportinfo(int pid
, uint32_t fileport
, int flavor
, void *buffer
, int buffersize
)
155 if ((retval
= __proc_info(PROC_INFO_CALL_PIDFILEPORTINFO
, pid
, flavor
, (uint64_t)fileport
, buffer
, buffersize
)) == -1)
162 proc_name(int pid
, void * buffer
, uint32_t buffersize
)
165 struct proc_bsdinfo pbsd
;
168 if (buffersize
< sizeof(pbsd
.pbi_name
)) {
173 retval
= proc_pidinfo(pid
, PROC_PIDTBSDINFO
, (uint64_t)0, &pbsd
, sizeof(struct proc_bsdinfo
));
175 if (pbsd
.pbi_name
[0]) {
176 bcopy(&pbsd
.pbi_name
, buffer
, sizeof(pbsd
.pbi_name
));
178 bcopy(&pbsd
.pbi_comm
, buffer
, sizeof(pbsd
.pbi_comm
));
180 len
= (int)strlen(buffer
);
187 proc_regionfilename(int pid
, uint64_t address
, void * buffer
, uint32_t buffersize
)
190 struct proc_regionwithpathinfo reginfo
;
192 if (buffersize
< MAXPATHLEN
) {
197 retval
= proc_pidinfo(pid
, PROC_PIDREGIONPATHINFO
, (uint64_t)address
, ®info
, sizeof(struct proc_regionwithpathinfo
));
199 len
= (int)strlen(®info
.prp_vip
.vip_path
[0]);
201 if (len
> MAXPATHLEN
)
203 bcopy(®info
.prp_vip
.vip_path
[0], buffer
, len
);
213 proc_kmsgbuf(void * buffer
, uint32_t buffersize
)
217 if ((retval
= __proc_info(PROC_INFO_CALL_KERNMSGBUF
, 0, 0, (uint64_t)0, buffer
, buffersize
)) == -1)
223 proc_pidpath(int pid
, void * buffer
, uint32_t buffersize
)
227 if (buffersize
< PROC_PIDPATHINFO_SIZE
) {
231 if (buffersize
> PROC_PIDPATHINFO_MAXSIZE
) {
236 retval
= __proc_info(PROC_INFO_CALL_PIDINFO
, pid
, PROC_PIDPATHINFO
, (uint64_t)0, buffer
, buffersize
);
238 len
= (int)strlen(buffer
);
246 proc_libversion(int *major
, int * minor
)
257 proc_setpcontrol(const int control
)
261 if (control
< PROC_SETPC_NONE
|| control
> PROC_SETPC_TERMINATE
)
264 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_PCONTROL
, (uint64_t)control
, NULL
, 0)) == -1)
271 __private_extern__
int
272 proc_setthreadname(void * buffer
, int buffersize
)
276 retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_THREADNAME
, (uint64_t)0, buffer
, buffersize
);
285 proc_track_dirty(pid_t pid
, uint32_t flags
)
287 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_TRACK
, flags
, NULL
, 0) == -1) {
295 proc_set_dirty(pid_t pid
, bool dirty
)
297 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_SET
, dirty
, NULL
, 0) == -1) {
305 proc_get_dirty(pid_t pid
, uint32_t *flags
)
313 retval
= __proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_GET
, 0, NULL
, 0);
324 proc_clear_dirty(pid_t pid
, uint32_t flags
)
326 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_CLEAR
, flags
, NULL
, 0) == -1) {
334 proc_terminate(pid_t pid
, int *sig
)
342 retval
= __proc_info(PROC_INFO_CALL_TERMINATE
, pid
, 0, 0, NULL
, 0);
353 proc_set_cpumon_params(pid_t pid
, int percentage
, int interval
)
355 proc_policy_cpuusage_attr_t attr
;
357 attr
.ppattr_cpu_attr
= PROC_POLICY_RSRCACT_NOTIFY_EXC
;
358 attr
.ppattr_cpu_percentage
= percentage
;
359 attr
.ppattr_cpu_attr_interval
= (uint64_t)interval
;
360 attr
.ppattr_cpu_attr_deadline
= 0;
362 return(__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_SET
, PROC_POLICY_RESOURCE_USAGE
,
363 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0));
367 proc_get_cpumon_params(pid_t pid
, int *percentage
, int *interval
)
369 proc_policy_cpuusage_attr_t attr
;
372 ret
= __process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_GET
, PROC_POLICY_RESOURCE_USAGE
,
373 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0);
375 if ((ret
== 0) && (attr
.ppattr_cpu_attr
== PROC_POLICY_RSRCACT_NOTIFY_EXC
)) {
376 *percentage
= attr
.ppattr_cpu_percentage
;
377 *interval
= (int)attr
.ppattr_cpu_attr_interval
;
387 proc_set_cpumon_defaults(pid_t pid
)
389 proc_policy_cpuusage_attr_t attr
;
391 attr
.ppattr_cpu_attr
= PROC_POLICY_RSRCACT_NOTIFY_EXC
;
392 attr
.ppattr_cpu_percentage
= PROC_POLICY_CPUMON_DEFAULTS
;
393 attr
.ppattr_cpu_attr_interval
= 0;
394 attr
.ppattr_cpu_attr_deadline
= 0;
396 return(__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_SET
, PROC_POLICY_RESOURCE_USAGE
,
397 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0));
401 proc_disable_cpumon(pid_t pid
)
403 proc_policy_cpuusage_attr_t attr
;
405 attr
.ppattr_cpu_attr
= PROC_POLICY_RSRCACT_NOTIFY_EXC
;
406 attr
.ppattr_cpu_percentage
= PROC_POLICY_CPUMON_DISABLE
;
407 attr
.ppattr_cpu_attr_interval
= 0;
408 attr
.ppattr_cpu_attr_deadline
= 0;
410 return(__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_SET
, PROC_POLICY_RESOURCE_USAGE
,
411 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0));
416 * Turn on the CPU usage monitor using the supplied parameters, and make
417 * violations of the monitor fatal.
419 * Returns: 0 on success;
420 * -1 on failure and sets errno
423 proc_set_cpumon_params_fatal(pid_t pid
, int percentage
, int interval
)
425 int current_percentage
= 0;
426 int current_interval
= 0; /* intervals are in seconds */
429 if ((percentage
<= 0) || (interval
<= 0)) {
435 * Do a simple query to see if CPU monitoring is
436 * already active. If either the percentage or the
437 * interval is nonzero, then CPU monitoring is
438 * already in use for this process.
440 (void)proc_get_cpumon_params(pid
, ¤t_percentage
, ¤t_interval
);
441 if (current_percentage
|| current_interval
)
444 * The CPU monitor appears to be active.
445 * We choose not to disturb those settings.
451 if ((ret
= proc_set_cpumon_params(pid
, percentage
, interval
)) != 0) {
452 /* Failed to activate the CPU monitor */
456 if ((ret
= proc_rlimit_control(pid
, RLIMIT_CPU_USAGE_MONITOR
, CPUMON_MAKE_FATAL
)) != 0) {
457 /* Failed to set termination, back out the CPU monitor settings. */
458 (void)proc_disable_cpumon(pid
);
465 proc_set_wakemon_params(pid_t pid
, int rate_hz
, int flags __unused
)
467 struct proc_rlimit_control_wakeupmon params
;
469 params
.wm_flags
= WAKEMON_ENABLE
;
470 params
.wm_rate
= rate_hz
;
472 return (proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
));
475 #ifndef WAKEMON_GET_PARAMS
476 #define WAKEMON_GET_PARAMS 0x4
477 #define WAKEMON_SET_DEFAULTS 0x8
481 proc_get_wakemon_params(pid_t pid
, int *rate_hz
, int *flags
)
483 struct proc_rlimit_control_wakeupmon params
;
486 params
.wm_flags
= WAKEMON_GET_PARAMS
;
488 if ((error
= proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
)) != 0) {
492 *rate_hz
= params
.wm_rate
;
493 *flags
= params
.wm_flags
;
499 proc_set_wakemon_defaults(pid_t pid
)
501 struct proc_rlimit_control_wakeupmon params
;
503 params
.wm_flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
506 return (proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
));
510 proc_disable_wakemon(pid_t pid
)
512 struct proc_rlimit_control_wakeupmon params
;
514 params
.wm_flags
= WAKEMON_DISABLE
;
517 return (proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
));
523 /* Donate importance to adaptive processes from this process */
525 proc_donate_importance_boost()
529 rval
= __process_policy(PROC_POLICY_SCOPE_PROCESS
,
530 PROC_POLICY_ACTION_SET
,
532 PROC_POLICY_IMP_DONATION
,
541 static __attribute__((noinline
)) void
542 proc_importance_bad_assertion(char *reason
) {
547 * Use the address of these variables as the token. This way, they can be
548 * printed in the debugger as useful names.
550 uint64_t important_boost_assertion_token
= 0xfafafafafafafafa;
551 uint64_t normal_boost_assertion_token
= 0xfbfbfbfbfbfbfbfb;
552 uint64_t non_boost_assertion_token
= 0xfcfcfcfcfcfcfcfc;
553 uint64_t denap_boost_assertion_token
= 0xfdfdfdfdfdfdfdfd;
556 * Accept the boost on a message, or request another boost assertion
557 * if we have already accepted the implicit boost for this message.
559 * Returns EOVERFLOW if an attempt is made to take an extra assertion when not boosted.
561 * Returns EIO if the message was not a boosting message.
562 * TODO: Return a 'non-boost' token instead.
565 proc_importance_assertion_begin_with_msg(mach_msg_header_t
*msg
,
566 __unused mach_msg_trailer_t
*trailer
,
567 uint64_t *assertion_token
)
571 if (assertion_token
== NULL
)
574 #define LEGACYBOOSTMASK (MACH_MSGH_BITS_VOUCHER_MASK | MACH_MSGH_BITS_RAISEIMP)
575 #define LEGACYBOOSTED(m) (((m)->msgh_bits & LEGACYBOOSTMASK) == MACH_MSGH_BITS_RAISEIMP)
577 /* Is this a legacy boosted message? */
578 if (LEGACYBOOSTED(msg
)) {
581 * Have we accepted the implicit boost for this message yet?
582 * If we haven't accepted it yet, no need to call into kernel.
584 if ((msg
->msgh_bits
& MACH_MSGH_BITS_IMPHOLDASRT
) == 0) {
585 msg
->msgh_bits
|= MACH_MSGH_BITS_IMPHOLDASRT
;
586 *assertion_token
= (uint64_t) &important_boost_assertion_token
;
590 /* Request an additional boost count */
591 rval
= __process_policy(PROC_POLICY_SCOPE_PROCESS
,
592 PROC_POLICY_ACTION_HOLD
,
594 PROC_POLICY_IMP_IMPORTANT
,
597 *assertion_token
= (uint64_t) &important_boost_assertion_token
;
599 } else if (errno
== EOVERFLOW
) {
600 proc_importance_bad_assertion("Attempted to take assertion while not boosted");
612 * Drop a boost assertion.
613 * Returns EOVERFLOW on boost assertion underflow.
616 proc_importance_assertion_complete(uint64_t assertion_token
)
620 if (assertion_token
== 0)
623 if (assertion_token
== (uint64_t) &important_boost_assertion_token
) {
624 rval
= __process_policy(PROC_POLICY_SCOPE_PROCESS
,
625 PROC_POLICY_ACTION_DROP
,
627 PROC_POLICY_IMP_IMPORTANT
,
631 } else if (errno
== EOVERFLOW
) {
632 proc_importance_bad_assertion("Attempted to drop too many assertions");
638 proc_importance_bad_assertion("Attempted to drop assertion with invalid token");
644 * Accept the De-Nap boost on a message, or request another boost assertion
645 * if we have already accepted the implicit boost for this message.
647 * Interface is deprecated before it really got started - just as synonym
648 * for proc_importance_assertion_begin_with_msg() now.
651 proc_denap_assertion_begin_with_msg(mach_msg_header_t
*msg
,
652 uint64_t *assertion_token
)
654 #pragma clang diagnostic push
655 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
656 return proc_importance_assertion_begin_with_msg(msg
, NULL
, assertion_token
);
657 #pragma clang diagnostic pop
662 * Drop a denap boost assertion.
664 * Interface is deprecated before it really got started - just a synonym
665 * for proc_importance_assertion_complete() now.
668 proc_denap_assertion_complete(uint64_t assertion_token
)
670 return proc_importance_assertion_complete(assertion_token
);
675 proc_clear_vmpressure(pid_t pid
)
677 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_RESTORE
, PROC_POLICY_RESOURCE_STARVATION
, PROC_POLICY_RS_VIRTUALMEM
, NULL
, pid
, (uint64_t)0) != -1)
683 /* set the current process as one who can resume suspended processes due to low virtual memory. Need to be root */
685 proc_set_owner_vmpressure(void)
689 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_VMRSRCOWNER
, (uint64_t)0, NULL
, 0)) == -1)
695 /* mark yourself to delay idle sleep on disk IO */
697 proc_set_delayidlesleep(void)
701 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_DELAYIDLESLEEP
, (uint64_t)1, NULL
, 0)) == -1)
707 /* Reset yourself to delay idle sleep on disk IO, if already set */
709 proc_clear_delayidlesleep(void)
713 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_DELAYIDLESLEEP
, (uint64_t)0, NULL
, 0)) == -1)
719 /* disable the launch time backgroudn policy and restore the process to default group */
721 proc_disable_apptype(pid_t pid
, int apptype
)
724 case PROC_POLICY_OSX_APPTYPE_TAL
:
725 case PROC_POLICY_OSX_APPTYPE_DASHCLIENT
:
731 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_DISABLE
, PROC_POLICY_APPTYPE
, apptype
, NULL
, pid
, (uint64_t)0) != -1)
738 /* re-enable the launch time background policy if it had been disabled. */
740 proc_enable_apptype(pid_t pid
, int apptype
)
743 case PROC_POLICY_OSX_APPTYPE_TAL
:
744 case PROC_POLICY_OSX_APPTYPE_DASHCLIENT
:
751 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_ENABLE
, PROC_POLICY_APPTYPE
, apptype
, NULL
, pid
, (uint64_t)0) != -1)
758 #if !TARGET_IPHONE_SIMULATOR
761 proc_suppress(__unused pid_t pid
, __unused
uint64_t *generation
)
766 #endif /* !TARGET_IPHONE_SIMULATOR */