1 /*
2 * Copyright (c) 2006-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/cdefs.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <stdlib.h>
30 #include <sys/errno.h>
31 #include <sys/msgbuf.h>
32 #include <sys/resource.h>
33 #include <sys/process_policy.h>
34 #include <sys/event.h>
35 #include <mach/message.h>
36
37 #include "libproc_internal.h"
38
39 int __proc_info(int callnum, int pid, int flavor, uint64_t arg, void * buffer, int buffersize);
40 int __proc_info_extended_id(int32_t callnum, int32_t pid, uint32_t flavor, uint32_t flags, uint64_t ext_id, uint64_t arg, user_addr_t buffer, int32_t buffersize);
41 __private_extern__ int proc_setthreadname(void * buffer, int buffersize);
42 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);
43 int proc_rlimit_control(pid_t pid, int flavor, void *arg);
44
45 int
proc_listpids(uint32_t type,uint32_t typeinfo,void * buffer,int buffersize)46 proc_listpids(uint32_t type, uint32_t typeinfo, void *buffer, int buffersize)
47 {
48 int retval;
49
50 if ((type >= PROC_ALL_PIDS) || (type <= PROC_PPID_ONLY)) {
51 if ((retval = __proc_info(PROC_INFO_CALL_LISTPIDS, type, typeinfo, (uint64_t)0, buffer, buffersize)) == -1) {
52 return 0;
53 }
54 } else {
55 errno = EINVAL;
56 retval = 0;
57 }
58 return retval;
59 }
60
61
62 int
proc_listallpids(void * buffer,int buffersize)63 proc_listallpids(void * buffer, int buffersize)
64 {
65 int numpids;
66 numpids = proc_listpids(PROC_ALL_PIDS, (uint32_t)0, buffer, buffersize);
67
68 if (numpids == -1) {
69 return -1;
70 } else {
71 return numpids / sizeof(int);
72 }
73 }
74
75 int
proc_listpgrppids(pid_t pgrpid,void * buffer,int buffersize)76 proc_listpgrppids(pid_t pgrpid, void * buffer, int buffersize)
77 {
78 int numpids;
79 numpids = proc_listpids(PROC_PGRP_ONLY, (uint32_t)pgrpid, buffer, buffersize);
80 if (numpids == -1) {
81 return -1;
82 } else {
83 return numpids / sizeof(int);
84 }
85 }
86
87 int
proc_listchildpids(pid_t ppid,void * buffer,int buffersize)88 proc_listchildpids(pid_t ppid, void * buffer, int buffersize)
89 {
90 int numpids;
91 numpids = proc_listpids(PROC_PPID_ONLY, (uint32_t)ppid, buffer, buffersize);
92 if (numpids == -1) {
93 return -1;
94 } else {
95 return numpids / sizeof(int);
96 }
97 }
98
99
100 int
proc_pidinfo(int pid,int flavor,uint64_t arg,void * buffer,int buffersize)101 proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize)
102 {
103 int retval;
104
105 if ((retval = __proc_info(PROC_INFO_CALL_PIDINFO, pid, flavor, arg, buffer, buffersize)) == -1) {
106 return 0;
107 }
108
109 return retval;
110 }
111
112
113 int
proc_pidoriginatorinfo(int flavor,void * buffer,int buffersize)114 proc_pidoriginatorinfo(int flavor, void *buffer, int buffersize)
115 {
116 int retval;
117
118 if ((retval = __proc_info(PROC_INFO_CALL_PIDORIGINATORINFO, getpid(), flavor, 0, buffer, buffersize)) == -1) {
119 return 0;
120 }
121
122 return retval;
123 }
124
125 int
proc_listcoalitions(int flavor,int coaltype,void * buffer,int buffersize)126 proc_listcoalitions(int flavor, int coaltype, void *buffer, int buffersize)
127 {
128 int retval;
129
130 if ((retval = __proc_info(PROC_INFO_CALL_LISTCOALITIONS, flavor, coaltype, 0, buffer, buffersize)) == -1) {
131 return 0;
132 }
133
134 return retval;
135 }
136
137 int
proc_pid_rusage(int pid,int flavor,rusage_info_t * buffer)138 proc_pid_rusage(int pid, int flavor, rusage_info_t *buffer)
139 {
140 return __proc_info(PROC_INFO_CALL_PIDRUSAGE, pid, flavor, 0, buffer, 0);
141 }
142
143 int
proc_setthread_cpupercent(uint8_t percentage,uint32_t ms_refill)144 proc_setthread_cpupercent(uint8_t percentage, uint32_t ms_refill)
145 {
146 uint32_t arg = 0;
147
148 /* Pack percentage and refill into a 32-bit number to match existing kernel implementation */
149 if ((percentage >= 100) || (ms_refill & ~0xffffffU)) {
150 errno = EINVAL;
151 return -1;
152 }
153
154 arg = ((ms_refill << 8) | percentage);
155
156 return proc_rlimit_control(-1, RLIMIT_THREAD_CPULIMITS, (void *)(uintptr_t)arg);
157 }
158
159 int
proc_pidfdinfo(int pid,int fd,int flavor,void * buffer,int buffersize)160 proc_pidfdinfo(int pid, int fd, int flavor, void * buffer, int buffersize)
161 {
162 int retval;
163
164 if ((retval = __proc_info(PROC_INFO_CALL_PIDFDINFO, pid, flavor, (uint64_t)fd, buffer, buffersize)) == -1) {
165 return 0;
166 }
167
168 return retval;
169 }
170
171
172 int
proc_pidfileportinfo(int pid,uint32_t fileport,int flavor,void * buffer,int buffersize)173 proc_pidfileportinfo(int pid, uint32_t fileport, int flavor, void *buffer, int buffersize)
174 {
175 int retval;
176
177 if ((retval = __proc_info(PROC_INFO_CALL_PIDFILEPORTINFO, pid, flavor, (uint64_t)fileport, buffer, buffersize)) == -1) {
178 return 0;
179 }
180 return retval;
181 }
182
183 int
proc_piddynkqueueinfo(int pid,int flavor,kqueue_id_t kq_id,void * buffer,int buffersize)184 proc_piddynkqueueinfo(int pid, int flavor, kqueue_id_t kq_id, void *buffer, int buffersize)
185 {
186 int ret;
187
188 if ((ret = __proc_info(PROC_INFO_CALL_PIDDYNKQUEUEINFO, pid, flavor, (uint64_t)kq_id, buffer, buffersize)) == -1) {
189 return 0;
190 }
191
192 return ret;
193 }
194
195 int
proc_udata_info(int pid,int flavor,void * buffer,int buffersize)196 proc_udata_info(int pid, int flavor, void *buffer, int buffersize)
197 {
198 return __proc_info(PROC_INFO_CALL_UDATA_INFO, pid, flavor, 0, buffer, buffersize);
199 }
200
201 /* only used by dyld which links with libsystem_kernel.a */
202 __private_extern__ int
proc_set_dyld_all_image_info(void * buffer,int buffersize)203 proc_set_dyld_all_image_info(void *buffer, int buffersize)
204 {
205 return __proc_info(PROC_INFO_CALL_SET_DYLD_IMAGES, getpid(), 0, 0, buffer, buffersize);
206 }
207
208
209 int
proc_name(int pid,void * buffer,uint32_t buffersize)210 proc_name(int pid, void * buffer, uint32_t buffersize)
211 {
212 int retval = 0, len;
213 struct proc_bsdinfo pbsd;
214
215
216 if (buffersize < sizeof(pbsd.pbi_name)) {
217 errno = ENOMEM;
218 return 0;
219 }
220
221 retval = proc_pidinfo(pid, PROC_PIDTBSDINFO, (uint64_t)0, &pbsd, sizeof(struct proc_bsdinfo));
222 if (retval != 0) {
223 if (pbsd.pbi_name[0]) {
224 bcopy(&pbsd.pbi_name, buffer, sizeof(pbsd.pbi_name));
225 } else {
226 bcopy(&pbsd.pbi_comm, buffer, sizeof(pbsd.pbi_comm));
227 }
228 len = (int)strlen(buffer);
229 return len;
230 }
231 return 0;
232 }
233
234 int
proc_regionfilename(int pid,uint64_t address,void * buffer,uint32_t buffersize)235 proc_regionfilename(int pid, uint64_t address, void * buffer, uint32_t buffersize)
236 {
237 int retval;
238 struct proc_regionpath path;
239
240 if (buffersize < MAXPATHLEN) {
241 errno = ENOMEM;
242 return 0;
243 }
244
245 retval = proc_pidinfo(pid, PROC_PIDREGIONPATH, (uint64_t)address, &path, sizeof(struct proc_regionpath));
246 if (retval != 0) {
247 return (int)(strlcpy(buffer, path.prpo_path, buffersize));
248 }
249 return 0;
250 }
251
252 int
proc_kmsgbuf(void * buffer,uint32_t buffersize)253 proc_kmsgbuf(void * buffer, uint32_t buffersize)
254 {
255 int retval;
256
257 if ((retval = __proc_info(PROC_INFO_CALL_KERNMSGBUF, 0, 0, (uint64_t)0, buffer, buffersize)) == -1) {
258 return 0;
259 }
260 return retval;
261 }
262
263 int
proc_pidpath(int pid,void * buffer,uint32_t buffersize)264 proc_pidpath(int pid, void * buffer, uint32_t buffersize)
265 {
266 int retval, len;
267
268 if (buffersize < PROC_PIDPATHINFO_SIZE) {
269 errno = ENOMEM;
270 return 0;
271 }
272 if (buffersize > PROC_PIDPATHINFO_MAXSIZE) {
273 errno = EOVERFLOW;
274 return 0;
275 }
276
277 retval = __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDPATHINFO, (uint64_t)0, buffer, buffersize);
278 if (retval != -1) {
279 len = (int)strlen(buffer);
280 return len;
281 }
282 return 0;
283 }
284
285 int
proc_pidpath_audittoken(audit_token_t * audittoken,void * buffer,uint32_t buffersize)286 proc_pidpath_audittoken(audit_token_t *audittoken, void * buffer, uint32_t buffersize)
287 {
288 int retval, len;
289
290 if (buffersize < PROC_PIDPATHINFO_SIZE) {
291 errno = ENOMEM;
292 return 0;
293 }
294 if (buffersize > PROC_PIDPATHINFO_MAXSIZE) {
295 errno = EOVERFLOW;
296 return 0;
297 }
298
299 int pid = audittoken->val[5];
300 int idversion = audittoken->val[7];
301
302 retval = __proc_info_extended_id(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDPATHINFO, PIF_COMPARE_IDVERSION, (uint64_t)idversion,
303 (uint64_t)0, buffer, buffersize);
304 if (retval != -1) {
305 len = (int)strlen(buffer);
306 return len;
307 }
308 return 0;
309 }
310
311 int
proc_current_thread_schedinfo(void * buffer,size_t buffersize)312 proc_current_thread_schedinfo(void *buffer, size_t buffersize)
313 {
314 extern uint64_t __thread_selfid(void);
315
316 int retval;
317
318 if (buffersize < PROC_PIDTHREADSCHEDINFO_SIZE) {
319 errno = ENOMEM;
320 return errno;
321 }
322 if (buffersize > PROC_PIDTHREADSCHEDINFO_SIZE) {
323 errno = EOVERFLOW;
324 return errno;
325 }
326
327 pid_t pid = getpid();
328 uint64_t threadid = __thread_selfid();
329
330 retval = __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDTHREADSCHEDINFO, threadid, buffer, buffersize);
331
332 if (retval == -1) {
333 return errno;
334 }
335 return 0;
336 }
337
338 int
proc_libversion(int * major,int * minor)339 proc_libversion(int *major, int * minor)
340 {
341 if (major != NULL) {
342 *major = 1;
343 }
344 if (minor != NULL) {
345 *minor = 1;
346 }
347 return 0;
348 }
349
350 int
proc_setpcontrol(const int control)351 proc_setpcontrol(const int control)
352 {
353 int retval;
354
355 if (control < PROC_SETPC_NONE || control > PROC_SETPC_TERMINATE) {
356 return EINVAL;
357 }
358
359 if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_PCONTROL, (uint64_t)control, NULL, 0)) == -1) {
360 return errno;
361 }
362
363 return 0;
364 }
365
366
367 __private_extern__ int
proc_setthreadname(void * buffer,int buffersize)368 proc_setthreadname(void * buffer, int buffersize)
369 {
370 int retval;
371
372 retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_THREADNAME, (uint64_t)0, buffer, buffersize);
373
374 if (retval == -1) {
375 return errno;
376 } else {
377 return 0;
378 }
379 }
380
381 int
proc_track_dirty(pid_t pid,uint32_t flags)382 proc_track_dirty(pid_t pid, uint32_t flags)
383 {
384 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_TRACK, flags, NULL, 0) == -1) {
385 return errno;
386 }
387
388 return 0;
389 }
390
391 int
proc_set_dirty(pid_t pid,bool dirty)392 proc_set_dirty(pid_t pid, bool dirty)
393 {
394 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_SET, dirty, NULL, 0) == -1) {
395 return errno;
396 }
397
398 return 0;
399 }
400
401 int
proc_get_dirty(pid_t pid,uint32_t * flags)402 proc_get_dirty(pid_t pid, uint32_t *flags)
403 {
404 int retval;
405
406 if (!flags) {
407 return EINVAL;
408 }
409
410 retval = __proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_GET, 0, NULL, 0);
411 if (retval == -1) {
412 return errno;
413 }
414
415 *flags = retval;
416
417 return 0;
418 }
419
420 int
proc_clear_dirty(pid_t pid,uint32_t flags)421 proc_clear_dirty(pid_t pid, uint32_t flags)
422 {
423 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_CLEAR, flags, NULL, 0) == -1) {
424 return errno;
425 }
426
427 return 0;
428 }
429
430 int
proc_terminate(pid_t pid,int * sig)431 proc_terminate(pid_t pid, int *sig)
432 {
433 int retval;
434
435 if (!sig) {
436 return EINVAL;
437 }
438
439 retval = __proc_info(PROC_INFO_CALL_TERMINATE, pid, 0, 0, NULL, 0);
440 if (retval == -1) {
441 return errno;
442 }
443
444 *sig = retval;
445
446 return 0;
447 }
448
449 int
proc_signal_delegate(audit_token_t instigator,audit_token_t target,int sig)450 proc_signal_delegate(audit_token_t instigator, audit_token_t target, int sig)
451 {
452 struct proc_delegated_signal_info args = {
453 .instigator = instigator,
454 .target = target,
455 };
456 int retval = __proc_info(PROC_INFO_CALL_DELEGATE_SIGNAL, 0, sig, 0, &args, sizeof(struct proc_delegated_signal_info));
457 if (retval == -1) {
458 return errno;
459 }
460
461 return 0;
462 }
463
464 int
proc_terminate_delegate(audit_token_t instigator,audit_token_t target,int * sig)465 proc_terminate_delegate(audit_token_t instigator, audit_token_t target, int *sig)
466 {
467 struct proc_delegated_signal_info args = {
468 .instigator = instigator,
469 .target = target,
470 };
471
472 if (!sig) {
473 return EINVAL;
474 }
475
476 int retval = __proc_info(PROC_INFO_CALL_DELEGATE_TERMINATE, 0, 0, 0, &args, sizeof(struct proc_delegated_signal_info));
477 if (retval == -1) {
478 return errno;
479 }
480
481 // Retval contains the type of signal that was sent, either SIGKILL or SIGTERM
482 *sig = retval;
483 return 0;
484 }
485
486 int
proc_signal_with_audittoken(audit_token_t * audittoken,int sig)487 proc_signal_with_audittoken(audit_token_t *audittoken, int sig)
488 {
489 int retval = __proc_info(PROC_INFO_CALL_SIGNAL_AUDITTOKEN, 0, sig, 0, audittoken, sizeof(audit_token_t));
490 if (retval == -1) {
491 return errno;
492 }
493
494 return 0;
495 }
496
497 int
proc_terminate_with_audittoken(audit_token_t * audittoken,int * sig)498 proc_terminate_with_audittoken(audit_token_t *audittoken, int *sig)
499 {
500 int retval;
501
502 if (!sig) {
503 return EINVAL;
504 }
505
506 retval = __proc_info(PROC_INFO_CALL_TERMINATE_AUDITTOKEN, 0, 0, 0, audittoken, sizeof(audit_token_t));
507 if (retval == -1) {
508 return errno;
509 }
510
511 *sig = retval;
512
513 return 0;
514 }
515
516 int
proc_terminate_all_rsr(int sig)517 proc_terminate_all_rsr(int sig)
518 {
519 int retval = 0;
520
521 if (sig != SIGKILL && sig != SIGTERM) {
522 return EINVAL;
523 }
524
525 retval = __proc_info(PROC_INFO_CALL_TERMINATE_RSR, 0, 0, sig, NULL, 0);
526 if (retval == -1) {
527 return errno;
528 }
529
530 return 0;
531 }
532
533 /*
534 * XXX the _fatal() variant both checks for an existing monitor
535 * (with important policy effects on first party background apps)
536 * and validates inputs.
537 */
538 int
proc_set_cpumon_params(pid_t pid,int percentage,int interval)539 proc_set_cpumon_params(pid_t pid, int percentage, int interval)
540 {
541 proc_policy_cpuusage_attr_t attr;
542
543 /* no argument validation ...
544 * task_set_cpuusage() ignores 0 values and squashes negative
545 * values into uint32_t.
546 */
547
548 attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
549 attr.ppattr_cpu_percentage = percentage;
550 attr.ppattr_cpu_attr_interval = (uint64_t)interval;
551 attr.ppattr_cpu_attr_deadline = 0;
552
553 return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
554 PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
555 }
556
557 int
proc_get_cpumon_params(pid_t pid,int * percentage,int * interval)558 proc_get_cpumon_params(pid_t pid, int *percentage, int *interval)
559 {
560 proc_policy_cpuusage_attr_t attr;
561 int ret;
562
563 ret = __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_GET, PROC_POLICY_RESOURCE_USAGE,
564 PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
565
566 if ((ret == 0) && (attr.ppattr_cpu_attr == PROC_POLICY_RSRCACT_NOTIFY_EXC)) {
567 *percentage = attr.ppattr_cpu_percentage;
568 *interval = (int)attr.ppattr_cpu_attr_interval;
569 } else {
570 *percentage = 0;
571 *interval = 0;
572 }
573
574 return ret;
575 }
576
577 int
proc_set_cpumon_defaults(pid_t pid)578 proc_set_cpumon_defaults(pid_t pid)
579 {
580 proc_policy_cpuusage_attr_t attr;
581
582 attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
583 attr.ppattr_cpu_percentage = PROC_POLICY_CPUMON_DEFAULTS;
584 attr.ppattr_cpu_attr_interval = 0;
585 attr.ppattr_cpu_attr_deadline = 0;
586
587 return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
588 PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
589 }
590
591 int
proc_resume_cpumon(pid_t pid)592 proc_resume_cpumon(pid_t pid)
593 {
594 return __process_policy(PROC_POLICY_SCOPE_PROCESS,
595 PROC_POLICY_ACTION_ENABLE,
596 PROC_POLICY_RESOURCE_USAGE,
597 PROC_POLICY_RUSAGE_CPU,
598 NULL, pid, 0);
599 }
600
601 int
proc_disable_cpumon(pid_t pid)602 proc_disable_cpumon(pid_t pid)
603 {
604 proc_policy_cpuusage_attr_t attr;
605
606 attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
607 attr.ppattr_cpu_percentage = PROC_POLICY_CPUMON_DISABLE;
608 attr.ppattr_cpu_attr_interval = 0;
609 attr.ppattr_cpu_attr_deadline = 0;
610
611 return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
612 PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
613 }
614
615
616 /*
617 * Turn on the CPU usage monitor using the supplied parameters, and make
618 * violations of the monitor fatal.
619 *
620 * Returns: 0 on success;
621 * -1 on failure and sets errno
622 */
623 int
proc_set_cpumon_params_fatal(pid_t pid,int percentage,int interval)624 proc_set_cpumon_params_fatal(pid_t pid, int percentage, int interval)
625 {
626 int current_percentage = 0;
627 int current_interval = 0; /* intervals are in seconds */
628 int ret = 0;
629
630 if ((percentage <= 0) || (interval <= 0)) {
631 errno = EINVAL;
632 return -1;
633 }
634
635 /*
636 * Do a simple query to see if CPU monitoring is
637 * already active. If either the percentage or the
638 * interval is nonzero, then CPU monitoring is
639 * already in use for this process.
640 *
641 * XXX: need set...() and set..fatal() to behave similarly.
642 * Currently, this check prevents 1st party apps (which get a
643 * default non-fatal monitor) not to get a fatal monitor.
644 */
645 (void)proc_get_cpumon_params(pid, ¤t_percentage, ¤t_interval);
646 if (current_percentage || current_interval) {
647 /*
648 * The CPU monitor appears to be active.
649 * We choose not to disturb those settings.
650 */
651 errno = EBUSY;
652 return -1;
653 }
654
655 if ((ret = proc_set_cpumon_params(pid, percentage, interval)) != 0) {
656 /* Failed to activate the CPU monitor */
657 return ret;
658 }
659
660 if ((ret = proc_rlimit_control(pid, RLIMIT_CPU_USAGE_MONITOR, (void *)(uintptr_t)CPUMON_MAKE_FATAL)) != 0) {
661 /* Failed to set termination, back out the CPU monitor settings. */
662 (void)proc_disable_cpumon(pid);
663 }
664
665 return ret;
666 }
667
668 int
proc_set_wakemon_params(pid_t pid,int rate_hz,int flags __unused)669 proc_set_wakemon_params(pid_t pid, int rate_hz, int flags __unused)
670 {
671 struct proc_rlimit_control_wakeupmon params;
672
673 params.wm_flags = WAKEMON_ENABLE;
674 params.wm_rate = rate_hz;
675
676 return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, ¶ms);
677 }
678
679 #ifndef WAKEMON_GET_PARAMS
680 #define WAKEMON_GET_PARAMS 0x4
681 #define WAKEMON_SET_DEFAULTS 0x8
682 #endif
683
684 int
proc_get_wakemon_params(pid_t pid,int * rate_hz,int * flags)685 proc_get_wakemon_params(pid_t pid, int *rate_hz, int *flags)
686 {
687 struct proc_rlimit_control_wakeupmon params;
688 int error;
689
690 params.wm_flags = WAKEMON_GET_PARAMS;
691
692 if ((error = proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, ¶ms)) != 0) {
693 return error;
694 }
695
696 *rate_hz = params.wm_rate;
697 *flags = params.wm_flags;
698
699 return 0;
700 }
701
702 int
proc_set_wakemon_defaults(pid_t pid)703 proc_set_wakemon_defaults(pid_t pid)
704 {
705 struct proc_rlimit_control_wakeupmon params;
706
707 params.wm_flags = WAKEMON_ENABLE | WAKEMON_SET_DEFAULTS;
708 params.wm_rate = -1;
709
710 return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, ¶ms);
711 }
712
713 int
proc_disable_wakemon(pid_t pid)714 proc_disable_wakemon(pid_t pid)
715 {
716 struct proc_rlimit_control_wakeupmon params;
717
718 params.wm_flags = WAKEMON_DISABLE;
719 params.wm_rate = -1;
720
721 return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, ¶ms);
722 }
723
724 int
proc_list_uptrs(int pid,uint64_t * buf,uint32_t bufsz)725 proc_list_uptrs(int pid, uint64_t *buf, uint32_t bufsz)
726 {
727 return __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDLISTUPTRS, 0,
728 buf, bufsz);
729 }
730
731 int
proc_list_dynkqueueids(int pid,kqueue_id_t * buf,uint32_t bufsz)732 proc_list_dynkqueueids(int pid, kqueue_id_t *buf, uint32_t bufsz)
733 {
734 return __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDLISTDYNKQUEUES, 0,
735 buf, bufsz);
736 }
737
738
739 int
proc_setcpu_percentage(pid_t pid,int action,int percentage)740 proc_setcpu_percentage(pid_t pid, int action, int percentage)
741 {
742 proc_policy_cpuusage_attr_t attr;
743
744 bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
745 attr.ppattr_cpu_attr = action;
746 attr.ppattr_cpu_percentage = percentage;
747 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_RESOURCE_USAGE, PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, (uint64_t)0) != -1) {
748 return 0;
749 } else {
750 return errno;
751 }
752 }
753
754 int
proc_reset_footprint_interval(pid_t pid)755 proc_reset_footprint_interval(pid_t pid)
756 {
757 return proc_rlimit_control(pid, RLIMIT_FOOTPRINT_INTERVAL, (void *)(uintptr_t)FOOTPRINT_INTERVAL_RESET);
758 }
759
760 int
proc_clear_cpulimits(pid_t pid)761 proc_clear_cpulimits(pid_t pid)
762 {
763 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_RESTORE, PROC_POLICY_RESOURCE_USAGE, PROC_POLICY_RUSAGE_CPU, NULL, pid, (uint64_t)0) != -1) {
764 return 0;
765 } else {
766 return errno;
767 }
768 }
769
770 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
771
772 int
proc_setcpu_deadline(pid_t pid,int action,uint64_t deadline)773 proc_setcpu_deadline(pid_t pid, int action, uint64_t deadline)
774 {
775 proc_policy_cpuusage_attr_t attr;
776
777 bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
778 attr.ppattr_cpu_attr = action;
779 attr.ppattr_cpu_attr_deadline = deadline;
780 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_RESOURCE_USAGE, PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, (uint64_t)0) != -1) {
781 return 0;
782 } else {
783 return errno;
784 }
785 }
786
787 int
proc_setcpu_percentage_withdeadline(pid_t pid,int action,int percentage,uint64_t deadline)788 proc_setcpu_percentage_withdeadline(pid_t pid, int action, int percentage, uint64_t deadline)
789 {
790 proc_policy_cpuusage_attr_t attr;
791
792 bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
793 attr.ppattr_cpu_attr = action;
794 attr.ppattr_cpu_percentage = percentage;
795 attr.ppattr_cpu_attr_deadline = deadline;
796 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_RESOURCE_USAGE, PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, (uint64_t)0) != -1) {
797 return 0;
798 } else {
799 return errno;
800 }
801 }
802
803 int
proc_appstate(int pid,int * appstatep)804 proc_appstate(int pid, int * appstatep)
805 {
806 int state;
807
808 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_GET, PROC_POLICY_APP_LIFECYCLE, PROC_POLICY_APPLIFE_STATE, (proc_policy_attribute_t*)&state, pid, (uint64_t)0) != -1) {
809 if (appstatep != NULL) {
810 *appstatep = state;
811 }
812 return 0;
813 } else {
814 return errno;
815 }
816 }
817
818 int
proc_setappstate(int pid,int appstate)819 proc_setappstate(int pid, int appstate)
820 {
821 int state = appstate;
822
823 switch (state) {
824 case PROC_APPSTATE_NONE:
825 case PROC_APPSTATE_ACTIVE:
826 case PROC_APPSTATE_INACTIVE:
827 case PROC_APPSTATE_BACKGROUND:
828 case PROC_APPSTATE_NONUI:
829 break;
830 default:
831 return EINVAL;
832 }
833 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_APP_LIFECYCLE, PROC_POLICY_APPLIFE_STATE, (proc_policy_attribute_t*)&state, pid, (uint64_t)0) != -1) {
834 return 0;
835 } else {
836 return errno;
837 }
838 }
839
840 int
proc_devstatusnotify(int devicestatus)841 proc_devstatusnotify(int devicestatus)
842 {
843 int state = devicestatus;
844
845 switch (devicestatus) {
846 case PROC_DEVSTATUS_SHORTTERM:
847 case PROC_DEVSTATUS_LONGTERM:
848 break;
849 default:
850 return EINVAL;
851 }
852
853 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_APP_LIFECYCLE, PROC_POLICY_APPLIFE_DEVSTATUS, (proc_policy_attribute_t*)&state, getpid(), (uint64_t)0) != -1) {
854 return 0;
855 } else {
856 return errno;
857 }
858 }
859
860 int
proc_pidbind(int pid,uint64_t threadid,int bind)861 proc_pidbind(int pid, uint64_t threadid, int bind)
862 {
863 int state = bind;
864 pid_t passpid = pid;
865
866 switch (bind) {
867 case PROC_PIDBIND_CLEAR:
868 passpid = getpid(); /* ignore pid on clear */
869 break;
870 case PROC_PIDBIND_SET:
871 break;
872 default:
873 return EINVAL;
874 }
875 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_APP_LIFECYCLE, PROC_POLICY_APPLIFE_PIDBIND, (proc_policy_attribute_t*)&state, passpid, threadid) != -1) {
876 return 0;
877 } else {
878 return errno;
879 }
880 }
881
882 int
proc_can_use_foreground_hw(int pid,uint32_t * reason)883 proc_can_use_foreground_hw(int pid, uint32_t *reason)
884 {
885 return __proc_info(PROC_INFO_CALL_CANUSEFGHW, pid, 0, 0, reason, sizeof(*reason));
886 }
887 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
888
889
890 /* Donate importance to adaptive processes from this process */
891 int
proc_donate_importance_boost(void)892 proc_donate_importance_boost(void)
893 {
894 int rval;
895
896 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
897 rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
898 PROC_POLICY_ACTION_ENABLE,
899 PROC_POLICY_APPTYPE,
900 PROC_POLICY_IOS_DONATEIMP,
901 NULL, getpid(), (uint64_t)0);
902 #else /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
903 rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
904 PROC_POLICY_ACTION_SET,
905 PROC_POLICY_BOOST,
906 PROC_POLICY_IMP_DONATION,
907 NULL, getpid(), 0);
908 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
909
910 if (rval == 0) {
911 return 0;
912 } else {
913 return errno;
914 }
915 }
916
917 static __attribute__((noinline)) void
proc_importance_bad_assertion(char * reason)918 proc_importance_bad_assertion(char *reason)
919 {
920 (void)reason;
921 }
922
923 /*
924 * Use the address of these variables as the token. This way, they can be
925 * printed in the debugger as useful names.
926 */
927 uint64_t important_boost_assertion_token = 0xfafafafafafafafa;
928 uint64_t normal_boost_assertion_token = 0xfbfbfbfbfbfbfbfb;
929 uint64_t non_boost_assertion_token = 0xfcfcfcfcfcfcfcfc;
930 uint64_t denap_boost_assertion_token = 0xfdfdfdfdfdfdfdfd;
931
932 /*
933 * Accept the boost on a message, or request another boost assertion
934 * if we have already accepted the implicit boost for this message.
935 *
936 * Returns EOVERFLOW if an attempt is made to take an extra assertion when not boosted.
937 *
938 * Returns EIO if the message was not a boosting message.
939 * TODO: Return a 'non-boost' token instead.
940 */
941 int
proc_importance_assertion_begin_with_msg(mach_msg_header_t * msg,__unused mach_msg_trailer_t * trailer,uint64_t * assertion_token)942 proc_importance_assertion_begin_with_msg(mach_msg_header_t *msg,
943 __unused mach_msg_trailer_t *trailer,
944 uint64_t *assertion_token)
945 {
946 int rval = 0;
947
948 if (assertion_token == NULL) {
949 return EINVAL;
950 }
951
952 #define LEGACYBOOSTMASK (MACH_MSGH_BITS_VOUCHER_MASK | MACH_MSGH_BITS_RAISEIMP)
953 #define LEGACYBOOSTED(m) (((m)->msgh_bits & LEGACYBOOSTMASK) == MACH_MSGH_BITS_RAISEIMP)
954
955 /* Is this a legacy boosted message? */
956 if (LEGACYBOOSTED(msg)) {
957 /*
958 * Have we accepted the implicit boost for this message yet?
959 * If we haven't accepted it yet, no need to call into kernel.
960 */
961 if ((msg->msgh_bits & MACH_MSGH_BITS_IMPHOLDASRT) == 0) {
962 msg->msgh_bits |= MACH_MSGH_BITS_IMPHOLDASRT;
963 *assertion_token = (uint64_t) &important_boost_assertion_token;
964 return 0;
965 }
966
967 /* Request an additional boost count */
968 rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
969 PROC_POLICY_ACTION_HOLD,
970 PROC_POLICY_BOOST,
971 PROC_POLICY_IMP_IMPORTANT,
972 NULL, getpid(), 0);
973 if (rval == 0) {
974 *assertion_token = (uint64_t) &important_boost_assertion_token;
975 return 0;
976 } else if (errno == EOVERFLOW) {
977 proc_importance_bad_assertion("Attempted to take assertion while not boosted");
978 return errno;
979 } else {
980 return errno;
981 }
982 }
983
984 return EIO;
985 }
986
987
988 /*
989 * Drop a boost assertion.
990 * Returns EOVERFLOW on boost assertion underflow.
991 */
992 int
proc_importance_assertion_complete(uint64_t assertion_token)993 proc_importance_assertion_complete(uint64_t assertion_token)
994 {
995 int rval = 0;
996
997 if (assertion_token == 0) {
998 return 0;
999 }
1000
1001 if (assertion_token == (uint64_t) &important_boost_assertion_token) {
1002 rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
1003 PROC_POLICY_ACTION_DROP,
1004 PROC_POLICY_BOOST,
1005 PROC_POLICY_IMP_IMPORTANT,
1006 NULL, getpid(), 0);
1007 if (rval == 0) {
1008 return 0;
1009 } else if (errno == EOVERFLOW) {
1010 proc_importance_bad_assertion("Attempted to drop too many assertions");
1011 return errno;
1012 } else {
1013 return errno;
1014 }
1015 } else {
1016 proc_importance_bad_assertion("Attempted to drop assertion with invalid token");
1017 return EIO;
1018 }
1019 }
1020
1021 /*
1022 * Accept the De-Nap boost on a message, or request another boost assertion
1023 * if we have already accepted the implicit boost for this message.
1024 *
1025 * Interface is deprecated before it really got started - just as synonym
1026 * for proc_importance_assertion_begin_with_msg() now.
1027 */
1028 int
proc_denap_assertion_begin_with_msg(mach_msg_header_t * msg,uint64_t * assertion_token)1029 proc_denap_assertion_begin_with_msg(mach_msg_header_t *msg,
1030 uint64_t *assertion_token)
1031 {
1032 #pragma clang diagnostic push
1033 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1034 return proc_importance_assertion_begin_with_msg(msg, NULL, assertion_token);
1035 #pragma clang diagnostic pop
1036 }
1037
1038
1039 /*
1040 * Drop a denap boost assertion.
1041 *
1042 * Interface is deprecated before it really got started - just a synonym
1043 * for proc_importance_assertion_complete() now.
1044 */
1045 int
proc_denap_assertion_complete(uint64_t assertion_token)1046 proc_denap_assertion_complete(uint64_t assertion_token)
1047 {
1048 return proc_importance_assertion_complete(assertion_token);
1049 }
1050
1051 #if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
1052
1053 int
proc_clear_vmpressure(pid_t pid)1054 proc_clear_vmpressure(pid_t pid)
1055 {
1056 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) {
1057 return 0;
1058 } else {
1059 return errno;
1060 }
1061 }
1062
1063 /* set the current process as one who can resume suspended processes due to low virtual memory. Need to be root */
1064 int
proc_set_owner_vmpressure(void)1065 proc_set_owner_vmpressure(void)
1066 {
1067 int retval;
1068
1069 if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_VMRSRCOWNER, (uint64_t)0, NULL, 0)) == -1) {
1070 return errno;
1071 }
1072
1073 return 0;
1074 }
1075
1076 /* mark yourself to delay idle sleep on disk IO */
1077 int
proc_set_delayidlesleep(void)1078 proc_set_delayidlesleep(void)
1079 {
1080 int retval;
1081
1082 if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_DELAYIDLESLEEP, (uint64_t)1, NULL, 0)) == -1) {
1083 return errno;
1084 }
1085
1086 return 0;
1087 }
1088
1089 /* Reset yourself to delay idle sleep on disk IO, if already set */
1090 int
proc_clear_delayidlesleep(void)1091 proc_clear_delayidlesleep(void)
1092 {
1093 int retval;
1094
1095 if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_DELAYIDLESLEEP, (uint64_t)0, NULL, 0)) == -1) {
1096 return errno;
1097 }
1098
1099 return 0;
1100 }
1101
1102 /* disable the launch time backgroudn policy and restore the process to default group */
1103 int
proc_disable_apptype(pid_t pid,int apptype)1104 proc_disable_apptype(pid_t pid, int apptype)
1105 {
1106 switch (apptype) {
1107 case PROC_POLICY_OSX_APPTYPE_TAL:
1108 case PROC_POLICY_OSX_APPTYPE_DASHCLIENT:
1109 break;
1110 default:
1111 return EINVAL;
1112 }
1113
1114 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_DISABLE, PROC_POLICY_APPTYPE, apptype, NULL, pid, (uint64_t)0) != -1) {
1115 return 0;
1116 } else {
1117 return errno;
1118 }
1119 }
1120
1121 /* re-enable the launch time background policy if it had been disabled. */
1122 int
proc_enable_apptype(pid_t pid,int apptype)1123 proc_enable_apptype(pid_t pid, int apptype)
1124 {
1125 switch (apptype) {
1126 case PROC_POLICY_OSX_APPTYPE_TAL:
1127 case PROC_POLICY_OSX_APPTYPE_DASHCLIENT:
1128 break;
1129 default:
1130 return EINVAL;
1131 }
1132
1133 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_ENABLE, PROC_POLICY_APPTYPE, apptype, NULL, pid, (uint64_t)0) != -1) {
1134 return 0;
1135 } else {
1136 return errno;
1137 }
1138 }
1139
1140 #if !TARGET_OS_SIMULATOR
1141
1142 int
proc_suppress(__unused pid_t pid,__unused uint64_t * generation)1143 proc_suppress(__unused pid_t pid, __unused uint64_t *generation)
1144 {
1145 return 0;
1146 }
1147
1148 #endif /* !TARGET_OS_SIMULATOR */
1149
1150 #endif /* !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
1151
1152 int
proc_set_no_smt(void)1153 proc_set_no_smt(void)
1154 {
1155 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, getpid(), (uint64_t)0) == -1) {
1156 return errno;
1157 }
1158 return 0;
1159 }
1160
1161 int
proc_setthread_no_smt(void)1162 proc_setthread_no_smt(void)
1163 {
1164 extern uint64_t __thread_selfid(void);
1165 if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, 0, __thread_selfid()) == -1) {
1166 return errno;
1167 }
1168 return 0;
1169 }
1170
1171 int
proc_set_csm(uint32_t flags)1172 proc_set_csm(uint32_t flags)
1173 {
1174 const uint32_t mask = PROC_CSM_ALL | PROC_CSM_TECS | PROC_CSM_NOSMT;
1175 if ((flags & ~mask) != 0) {
1176 return EINVAL;
1177 }
1178
1179 if (flags & (PROC_CSM_NOSMT | PROC_CSM_ALL)) {
1180 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, getpid(), (uint64_t)0) == -1) {
1181 return errno;
1182 }
1183 }
1184
1185 if (flags & (PROC_CSM_TECS | PROC_CSM_ALL)) {
1186 if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_TECS, 0, NULL, getpid(), (uint64_t)0) == -1) {
1187 return errno;
1188 }
1189 }
1190
1191 return 0;
1192 }
1193
1194 int
proc_setthread_csm(uint32_t flags)1195 proc_setthread_csm(uint32_t flags)
1196 {
1197 extern uint64_t __thread_selfid(void);
1198 const uint32_t mask = PROC_CSM_ALL | PROC_CSM_TECS | PROC_CSM_NOSMT;
1199 if ((flags & ~mask) != 0) {
1200 return EINVAL;
1201 }
1202
1203 if (flags & (PROC_CSM_NOSMT | PROC_CSM_ALL)) {
1204 if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, 0, __thread_selfid()) == -1) {
1205 return errno;
1206 }
1207 }
1208
1209 if (flags & (PROC_CSM_TECS | PROC_CSM_ALL)) {
1210 if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_TECS, 0, NULL, 0, __thread_selfid()) == -1) {
1211 return errno;
1212 }
1213 }
1214
1215 return 0;
1216 }
1217