xref: /xnu-10063.101.15/libsyscall/wrappers/libproc/libproc.c (revision 94d3b452840153a99b38a3a9659680b2a006908e)
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_with_audittoken(audit_token_t * audittoken,int sig)450 proc_signal_with_audittoken(audit_token_t *audittoken, int sig)
451 {
452 	int retval = __proc_info(PROC_INFO_CALL_SIGNAL_AUDITTOKEN, 0, sig, 0, audittoken, 0);
453 	if (retval == -1) {
454 		return errno;
455 	}
456 
457 	return 0;
458 }
459 
460 int
proc_terminate_with_audittoken(audit_token_t * audittoken,int * sig)461 proc_terminate_with_audittoken(audit_token_t *audittoken, int *sig)
462 {
463 	int retval;
464 
465 	if (!sig) {
466 		return EINVAL;
467 	}
468 
469 	retval = __proc_info(PROC_INFO_CALL_TERMINATE_AUDITTOKEN, 0, 0, 0, audittoken, 0);
470 	if (retval == -1) {
471 		return errno;
472 	}
473 
474 	*sig = retval;
475 
476 	return 0;
477 }
478 
479 int
proc_terminate_all_rsr(int sig)480 proc_terminate_all_rsr(int sig)
481 {
482 	int retval = 0;
483 
484 	if (sig != SIGKILL && sig != SIGTERM) {
485 		return EINVAL;
486 	}
487 
488 	retval = __proc_info(PROC_INFO_CALL_TERMINATE_RSR, 0, 0, sig, NULL, 0);
489 	if (retval == -1) {
490 		return errno;
491 	}
492 
493 	return 0;
494 }
495 
496 /*
497  * XXX the _fatal() variant both checks for an existing monitor
498  * (with important policy effects on first party background apps)
499  * and validates inputs.
500  */
501 int
proc_set_cpumon_params(pid_t pid,int percentage,int interval)502 proc_set_cpumon_params(pid_t pid, int percentage, int interval)
503 {
504 	proc_policy_cpuusage_attr_t attr;
505 
506 	/* no argument validation ...
507 	 * task_set_cpuusage() ignores 0 values and squashes negative
508 	 * values into uint32_t.
509 	 */
510 
511 	attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
512 	attr.ppattr_cpu_percentage = percentage;
513 	attr.ppattr_cpu_attr_interval = (uint64_t)interval;
514 	attr.ppattr_cpu_attr_deadline = 0;
515 
516 	return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
517 	           PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
518 }
519 
520 int
proc_get_cpumon_params(pid_t pid,int * percentage,int * interval)521 proc_get_cpumon_params(pid_t pid, int *percentage, int *interval)
522 {
523 	proc_policy_cpuusage_attr_t attr;
524 	int ret;
525 
526 	ret = __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_GET, PROC_POLICY_RESOURCE_USAGE,
527 	    PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
528 
529 	if ((ret == 0) && (attr.ppattr_cpu_attr == PROC_POLICY_RSRCACT_NOTIFY_EXC)) {
530 		*percentage = attr.ppattr_cpu_percentage;
531 		*interval = (int)attr.ppattr_cpu_attr_interval;
532 	} else {
533 		*percentage = 0;
534 		*interval = 0;
535 	}
536 
537 	return ret;
538 }
539 
540 int
proc_set_cpumon_defaults(pid_t pid)541 proc_set_cpumon_defaults(pid_t pid)
542 {
543 	proc_policy_cpuusage_attr_t attr;
544 
545 	attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
546 	attr.ppattr_cpu_percentage = PROC_POLICY_CPUMON_DEFAULTS;
547 	attr.ppattr_cpu_attr_interval = 0;
548 	attr.ppattr_cpu_attr_deadline = 0;
549 
550 	return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
551 	           PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
552 }
553 
554 int
proc_resume_cpumon(pid_t pid)555 proc_resume_cpumon(pid_t pid)
556 {
557 	return __process_policy(PROC_POLICY_SCOPE_PROCESS,
558 	           PROC_POLICY_ACTION_ENABLE,
559 	           PROC_POLICY_RESOURCE_USAGE,
560 	           PROC_POLICY_RUSAGE_CPU,
561 	           NULL, pid, 0);
562 }
563 
564 int
proc_disable_cpumon(pid_t pid)565 proc_disable_cpumon(pid_t pid)
566 {
567 	proc_policy_cpuusage_attr_t attr;
568 
569 	attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
570 	attr.ppattr_cpu_percentage = PROC_POLICY_CPUMON_DISABLE;
571 	attr.ppattr_cpu_attr_interval = 0;
572 	attr.ppattr_cpu_attr_deadline = 0;
573 
574 	return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
575 	           PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
576 }
577 
578 
579 /*
580  * Turn on the CPU usage monitor using the supplied parameters, and make
581  * violations of the monitor fatal.
582  *
583  * Returns:  0 on success;
584  *	    -1 on failure and sets errno
585  */
586 int
proc_set_cpumon_params_fatal(pid_t pid,int percentage,int interval)587 proc_set_cpumon_params_fatal(pid_t pid, int percentage, int interval)
588 {
589 	int current_percentage = 0;
590 	int current_interval = 0;   /* intervals are in seconds */
591 	int ret = 0;
592 
593 	if ((percentage <= 0) || (interval <= 0)) {
594 		errno = EINVAL;
595 		return -1;
596 	}
597 
598 	/*
599 	 * Do a simple query to see if CPU monitoring is
600 	 * already active.  If either the percentage or the
601 	 * interval is nonzero, then CPU monitoring is
602 	 * already in use for this process.
603 	 *
604 	 * XXX: need set...() and set..fatal() to behave similarly.
605 	 * Currently, this check prevents 1st party apps (which get a
606 	 * default non-fatal monitor) not to get a fatal monitor.
607 	 */
608 	(void)proc_get_cpumon_params(pid, &current_percentage, &current_interval);
609 	if (current_percentage || current_interval) {
610 		/*
611 		 * The CPU monitor appears to be active.
612 		 * We choose not to disturb those settings.
613 		 */
614 		errno = EBUSY;
615 		return -1;
616 	}
617 
618 	if ((ret = proc_set_cpumon_params(pid, percentage, interval)) != 0) {
619 		/* Failed to activate the CPU monitor */
620 		return ret;
621 	}
622 
623 	if ((ret = proc_rlimit_control(pid, RLIMIT_CPU_USAGE_MONITOR, (void *)(uintptr_t)CPUMON_MAKE_FATAL)) != 0) {
624 		/* Failed to set termination, back out the CPU monitor settings. */
625 		(void)proc_disable_cpumon(pid);
626 	}
627 
628 	return ret;
629 }
630 
631 int
proc_set_wakemon_params(pid_t pid,int rate_hz,int flags __unused)632 proc_set_wakemon_params(pid_t pid, int rate_hz, int flags __unused)
633 {
634 	struct proc_rlimit_control_wakeupmon params;
635 
636 	params.wm_flags = WAKEMON_ENABLE;
637 	params.wm_rate = rate_hz;
638 
639 	return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params);
640 }
641 
642 #ifndef WAKEMON_GET_PARAMS
643 #define WAKEMON_GET_PARAMS 0x4
644 #define WAKEMON_SET_DEFAULTS 0x8
645 #endif
646 
647 int
proc_get_wakemon_params(pid_t pid,int * rate_hz,int * flags)648 proc_get_wakemon_params(pid_t pid, int *rate_hz, int *flags)
649 {
650 	struct proc_rlimit_control_wakeupmon params;
651 	int error;
652 
653 	params.wm_flags = WAKEMON_GET_PARAMS;
654 
655 	if ((error = proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params)) != 0) {
656 		return error;
657 	}
658 
659 	*rate_hz = params.wm_rate;
660 	*flags = params.wm_flags;
661 
662 	return 0;
663 }
664 
665 int
proc_set_wakemon_defaults(pid_t pid)666 proc_set_wakemon_defaults(pid_t pid)
667 {
668 	struct proc_rlimit_control_wakeupmon params;
669 
670 	params.wm_flags = WAKEMON_ENABLE | WAKEMON_SET_DEFAULTS;
671 	params.wm_rate = -1;
672 
673 	return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params);
674 }
675 
676 int
proc_disable_wakemon(pid_t pid)677 proc_disable_wakemon(pid_t pid)
678 {
679 	struct proc_rlimit_control_wakeupmon params;
680 
681 	params.wm_flags = WAKEMON_DISABLE;
682 	params.wm_rate = -1;
683 
684 	return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params);
685 }
686 
687 int
proc_list_uptrs(int pid,uint64_t * buf,uint32_t bufsz)688 proc_list_uptrs(int pid, uint64_t *buf, uint32_t bufsz)
689 {
690 	return __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDLISTUPTRS, 0,
691 	           buf, bufsz);
692 }
693 
694 int
proc_list_dynkqueueids(int pid,kqueue_id_t * buf,uint32_t bufsz)695 proc_list_dynkqueueids(int pid, kqueue_id_t *buf, uint32_t bufsz)
696 {
697 	return __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDLISTDYNKQUEUES, 0,
698 	           buf, bufsz);
699 }
700 
701 
702 int
proc_setcpu_percentage(pid_t pid,int action,int percentage)703 proc_setcpu_percentage(pid_t pid, int action, int percentage)
704 {
705 	proc_policy_cpuusage_attr_t attr;
706 
707 	bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
708 	attr.ppattr_cpu_attr = action;
709 	attr.ppattr_cpu_percentage = percentage;
710 	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) {
711 		return 0;
712 	} else {
713 		return errno;
714 	}
715 }
716 
717 int
proc_reset_footprint_interval(pid_t pid)718 proc_reset_footprint_interval(pid_t pid)
719 {
720 	return proc_rlimit_control(pid, RLIMIT_FOOTPRINT_INTERVAL, (void *)(uintptr_t)FOOTPRINT_INTERVAL_RESET);
721 }
722 
723 int
proc_clear_cpulimits(pid_t pid)724 proc_clear_cpulimits(pid_t pid)
725 {
726 	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) {
727 		return 0;
728 	} else {
729 		return errno;
730 	}
731 }
732 
733 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
734 
735 int
proc_setcpu_deadline(pid_t pid,int action,uint64_t deadline)736 proc_setcpu_deadline(pid_t pid, int action, uint64_t deadline)
737 {
738 	proc_policy_cpuusage_attr_t attr;
739 
740 	bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
741 	attr.ppattr_cpu_attr = action;
742 	attr.ppattr_cpu_attr_deadline = deadline;
743 	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) {
744 		return 0;
745 	} else {
746 		return errno;
747 	}
748 }
749 
750 int
proc_setcpu_percentage_withdeadline(pid_t pid,int action,int percentage,uint64_t deadline)751 proc_setcpu_percentage_withdeadline(pid_t pid, int action, int percentage, uint64_t deadline)
752 {
753 	proc_policy_cpuusage_attr_t attr;
754 
755 	bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
756 	attr.ppattr_cpu_attr = action;
757 	attr.ppattr_cpu_percentage = percentage;
758 	attr.ppattr_cpu_attr_deadline = deadline;
759 	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) {
760 		return 0;
761 	} else {
762 		return errno;
763 	}
764 }
765 
766 int
proc_appstate(int pid,int * appstatep)767 proc_appstate(int pid, int * appstatep)
768 {
769 	int state;
770 
771 	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) {
772 		if (appstatep != NULL) {
773 			*appstatep = state;
774 		}
775 		return 0;
776 	} else {
777 		return errno;
778 	}
779 }
780 
781 int
proc_setappstate(int pid,int appstate)782 proc_setappstate(int pid, int appstate)
783 {
784 	int state = appstate;
785 
786 	switch (state) {
787 	case PROC_APPSTATE_NONE:
788 	case PROC_APPSTATE_ACTIVE:
789 	case PROC_APPSTATE_INACTIVE:
790 	case PROC_APPSTATE_BACKGROUND:
791 	case PROC_APPSTATE_NONUI:
792 		break;
793 	default:
794 		return EINVAL;
795 	}
796 	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) {
797 		return 0;
798 	} else {
799 		return errno;
800 	}
801 }
802 
803 int
proc_devstatusnotify(int devicestatus)804 proc_devstatusnotify(int devicestatus)
805 {
806 	int state = devicestatus;
807 
808 	switch (devicestatus) {
809 	case PROC_DEVSTATUS_SHORTTERM:
810 	case PROC_DEVSTATUS_LONGTERM:
811 		break;
812 	default:
813 		return EINVAL;
814 	}
815 
816 	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) {
817 		return 0;
818 	} else {
819 		return errno;
820 	}
821 }
822 
823 int
proc_pidbind(int pid,uint64_t threadid,int bind)824 proc_pidbind(int pid, uint64_t threadid, int bind)
825 {
826 	int state = bind;
827 	pid_t passpid = pid;
828 
829 	switch (bind) {
830 	case PROC_PIDBIND_CLEAR:
831 		passpid = getpid();             /* ignore pid on clear */
832 		break;
833 	case PROC_PIDBIND_SET:
834 		break;
835 	default:
836 		return EINVAL;
837 	}
838 	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) {
839 		return 0;
840 	} else {
841 		return errno;
842 	}
843 }
844 
845 int
proc_can_use_foreground_hw(int pid,uint32_t * reason)846 proc_can_use_foreground_hw(int pid, uint32_t *reason)
847 {
848 	return __proc_info(PROC_INFO_CALL_CANUSEFGHW, pid, 0, 0, reason, sizeof(*reason));
849 }
850 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
851 
852 
853 /* Donate importance to adaptive processes from this process */
854 int
proc_donate_importance_boost(void)855 proc_donate_importance_boost(void)
856 {
857 	int rval;
858 
859 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
860 	rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
861 	    PROC_POLICY_ACTION_ENABLE,
862 	    PROC_POLICY_APPTYPE,
863 	    PROC_POLICY_IOS_DONATEIMP,
864 	    NULL, getpid(), (uint64_t)0);
865 #else /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
866 	rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
867 	    PROC_POLICY_ACTION_SET,
868 	    PROC_POLICY_BOOST,
869 	    PROC_POLICY_IMP_DONATION,
870 	    NULL, getpid(), 0);
871 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
872 
873 	if (rval == 0) {
874 		return 0;
875 	} else {
876 		return errno;
877 	}
878 }
879 
880 static __attribute__((noinline)) void
proc_importance_bad_assertion(char * reason)881 proc_importance_bad_assertion(char *reason)
882 {
883 	(void)reason;
884 }
885 
886 /*
887  * Use the address of these variables as the token.  This way, they can be
888  * printed in the debugger as useful names.
889  */
890 uint64_t important_boost_assertion_token = 0xfafafafafafafafa;
891 uint64_t normal_boost_assertion_token    = 0xfbfbfbfbfbfbfbfb;
892 uint64_t non_boost_assertion_token       = 0xfcfcfcfcfcfcfcfc;
893 uint64_t denap_boost_assertion_token     = 0xfdfdfdfdfdfdfdfd;
894 
895 /*
896  * Accept the boost on a message, or request another boost assertion
897  * if we have already accepted the implicit boost for this message.
898  *
899  * Returns EOVERFLOW if an attempt is made to take an extra assertion when not boosted.
900  *
901  * Returns EIO if the message was not a boosting message.
902  * TODO: Return a 'non-boost' token instead.
903  */
904 int
proc_importance_assertion_begin_with_msg(mach_msg_header_t * msg,__unused mach_msg_trailer_t * trailer,uint64_t * assertion_token)905 proc_importance_assertion_begin_with_msg(mach_msg_header_t  *msg,
906     __unused mach_msg_trailer_t *trailer,
907     uint64_t           *assertion_token)
908 {
909 	int rval = 0;
910 
911 	if (assertion_token == NULL) {
912 		return EINVAL;
913 	}
914 
915 #define LEGACYBOOSTMASK (MACH_MSGH_BITS_VOUCHER_MASK | MACH_MSGH_BITS_RAISEIMP)
916 #define LEGACYBOOSTED(m) (((m)->msgh_bits & LEGACYBOOSTMASK) == MACH_MSGH_BITS_RAISEIMP)
917 
918 	/* Is this a legacy boosted message? */
919 	if (LEGACYBOOSTED(msg)) {
920 		/*
921 		 * Have we accepted the implicit boost for this message yet?
922 		 * If we haven't accepted it yet, no need to call into kernel.
923 		 */
924 		if ((msg->msgh_bits & MACH_MSGH_BITS_IMPHOLDASRT) == 0) {
925 			msg->msgh_bits |= MACH_MSGH_BITS_IMPHOLDASRT;
926 			*assertion_token = (uint64_t) &important_boost_assertion_token;
927 			return 0;
928 		}
929 
930 		/* Request an additional boost count */
931 		rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
932 		    PROC_POLICY_ACTION_HOLD,
933 		    PROC_POLICY_BOOST,
934 		    PROC_POLICY_IMP_IMPORTANT,
935 		    NULL, getpid(), 0);
936 		if (rval == 0) {
937 			*assertion_token = (uint64_t) &important_boost_assertion_token;
938 			return 0;
939 		} else if (errno == EOVERFLOW) {
940 			proc_importance_bad_assertion("Attempted to take assertion while not boosted");
941 			return errno;
942 		} else {
943 			return errno;
944 		}
945 	}
946 
947 	return EIO;
948 }
949 
950 
951 /*
952  * Drop a boost assertion.
953  * Returns EOVERFLOW on boost assertion underflow.
954  */
955 int
proc_importance_assertion_complete(uint64_t assertion_token)956 proc_importance_assertion_complete(uint64_t assertion_token)
957 {
958 	int rval = 0;
959 
960 	if (assertion_token == 0) {
961 		return 0;
962 	}
963 
964 	if (assertion_token == (uint64_t) &important_boost_assertion_token) {
965 		rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
966 		    PROC_POLICY_ACTION_DROP,
967 		    PROC_POLICY_BOOST,
968 		    PROC_POLICY_IMP_IMPORTANT,
969 		    NULL, getpid(), 0);
970 		if (rval == 0) {
971 			return 0;
972 		} else if (errno == EOVERFLOW) {
973 			proc_importance_bad_assertion("Attempted to drop too many assertions");
974 			return errno;
975 		} else {
976 			return errno;
977 		}
978 	} else {
979 		proc_importance_bad_assertion("Attempted to drop assertion with invalid token");
980 		return EIO;
981 	}
982 }
983 
984 /*
985  * Accept the De-Nap boost on a message, or request another boost assertion
986  * if we have already accepted the implicit boost for this message.
987  *
988  * Interface is deprecated before it really got started - just as synonym
989  * for proc_importance_assertion_begin_with_msg() now.
990  */
991 int
proc_denap_assertion_begin_with_msg(mach_msg_header_t * msg,uint64_t * assertion_token)992 proc_denap_assertion_begin_with_msg(mach_msg_header_t  *msg,
993     uint64_t           *assertion_token)
994 {
995 #pragma clang diagnostic push
996 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
997 	return proc_importance_assertion_begin_with_msg(msg, NULL, assertion_token);
998 #pragma clang diagnostic pop
999 }
1000 
1001 
1002 /*
1003  * Drop a denap boost assertion.
1004  *
1005  * Interface is deprecated before it really got started - just a synonym
1006  * for proc_importance_assertion_complete() now.
1007  */
1008 int
proc_denap_assertion_complete(uint64_t assertion_token)1009 proc_denap_assertion_complete(uint64_t assertion_token)
1010 {
1011 	return proc_importance_assertion_complete(assertion_token);
1012 }
1013 
1014 #if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
1015 
1016 int
proc_clear_vmpressure(pid_t pid)1017 proc_clear_vmpressure(pid_t pid)
1018 {
1019 	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) {
1020 		return 0;
1021 	} else {
1022 		return errno;
1023 	}
1024 }
1025 
1026 /* set the current process as one who can resume suspended processes due to low virtual memory. Need to be root */
1027 int
proc_set_owner_vmpressure(void)1028 proc_set_owner_vmpressure(void)
1029 {
1030 	int retval;
1031 
1032 	if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_VMRSRCOWNER, (uint64_t)0, NULL, 0)) == -1) {
1033 		return errno;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 /* mark yourself to delay idle sleep on disk IO */
1040 int
proc_set_delayidlesleep(void)1041 proc_set_delayidlesleep(void)
1042 {
1043 	int retval;
1044 
1045 	if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_DELAYIDLESLEEP, (uint64_t)1, NULL, 0)) == -1) {
1046 		return errno;
1047 	}
1048 
1049 	return 0;
1050 }
1051 
1052 /* Reset yourself to delay idle sleep on disk IO, if already set */
1053 int
proc_clear_delayidlesleep(void)1054 proc_clear_delayidlesleep(void)
1055 {
1056 	int retval;
1057 
1058 	if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_DELAYIDLESLEEP, (uint64_t)0, NULL, 0)) == -1) {
1059 		return errno;
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 /* disable the launch time backgroudn policy and restore the process to default group */
1066 int
proc_disable_apptype(pid_t pid,int apptype)1067 proc_disable_apptype(pid_t pid, int apptype)
1068 {
1069 	switch (apptype) {
1070 	case PROC_POLICY_OSX_APPTYPE_TAL:
1071 	case PROC_POLICY_OSX_APPTYPE_DASHCLIENT:
1072 		break;
1073 	default:
1074 		return EINVAL;
1075 	}
1076 
1077 	if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_DISABLE, PROC_POLICY_APPTYPE, apptype, NULL, pid, (uint64_t)0) != -1) {
1078 		return 0;
1079 	} else {
1080 		return errno;
1081 	}
1082 }
1083 
1084 /* re-enable the launch time background policy if it had been disabled. */
1085 int
proc_enable_apptype(pid_t pid,int apptype)1086 proc_enable_apptype(pid_t pid, int apptype)
1087 {
1088 	switch (apptype) {
1089 	case PROC_POLICY_OSX_APPTYPE_TAL:
1090 	case PROC_POLICY_OSX_APPTYPE_DASHCLIENT:
1091 		break;
1092 	default:
1093 		return EINVAL;
1094 	}
1095 
1096 	if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_ENABLE, PROC_POLICY_APPTYPE, apptype, NULL, pid, (uint64_t)0) != -1) {
1097 		return 0;
1098 	} else {
1099 		return errno;
1100 	}
1101 }
1102 
1103 #if !TARGET_OS_SIMULATOR
1104 
1105 int
proc_suppress(__unused pid_t pid,__unused uint64_t * generation)1106 proc_suppress(__unused pid_t pid, __unused uint64_t *generation)
1107 {
1108 	return 0;
1109 }
1110 
1111 #endif /* !TARGET_OS_SIMULATOR */
1112 
1113 #endif /* !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
1114 
1115 int
proc_set_no_smt(void)1116 proc_set_no_smt(void)
1117 {
1118 	if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, getpid(), (uint64_t)0) == -1) {
1119 		return errno;
1120 	}
1121 	return 0;
1122 }
1123 
1124 int
proc_setthread_no_smt(void)1125 proc_setthread_no_smt(void)
1126 {
1127 	extern uint64_t __thread_selfid(void);
1128 	if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, 0, __thread_selfid()) == -1) {
1129 		return errno;
1130 	}
1131 	return 0;
1132 }
1133 
1134 int
proc_set_csm(uint32_t flags)1135 proc_set_csm(uint32_t flags)
1136 {
1137 	const uint32_t mask = PROC_CSM_ALL | PROC_CSM_TECS | PROC_CSM_NOSMT;
1138 	if ((flags & ~mask) != 0) {
1139 		return EINVAL;
1140 	}
1141 
1142 	if (flags & (PROC_CSM_NOSMT | PROC_CSM_ALL)) {
1143 		if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, getpid(), (uint64_t)0) == -1) {
1144 			return errno;
1145 		}
1146 	}
1147 
1148 	if (flags & (PROC_CSM_TECS | PROC_CSM_ALL)) {
1149 		if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_TECS, 0, NULL, getpid(), (uint64_t)0) == -1) {
1150 			return errno;
1151 		}
1152 	}
1153 
1154 	return 0;
1155 }
1156 
1157 int
proc_setthread_csm(uint32_t flags)1158 proc_setthread_csm(uint32_t flags)
1159 {
1160 	extern uint64_t __thread_selfid(void);
1161 	const uint32_t mask = PROC_CSM_ALL | PROC_CSM_TECS | PROC_CSM_NOSMT;
1162 	if ((flags & ~mask) != 0) {
1163 		return EINVAL;
1164 	}
1165 
1166 	if (flags & (PROC_CSM_NOSMT | PROC_CSM_ALL)) {
1167 		if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, 0, __thread_selfid()) == -1) {
1168 			return errno;
1169 		}
1170 	}
1171 
1172 	if (flags & (PROC_CSM_TECS | PROC_CSM_ALL)) {
1173 		if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_TECS, 0, NULL, 0, __thread_selfid()) == -1) {
1174 			return errno;
1175 		}
1176 	}
1177 
1178 	return 0;
1179 }
1180