xref: /xnu-8796.141.3/tests/proc_info.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #define PRIVATE
2*1b191cb5SApple OSS Distributions #include <System/sys/kdebug.h>
3*1b191cb5SApple OSS Distributions #include <darwintest.h>
4*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
5*1b191cb5SApple OSS Distributions #include <dispatch/dispatch.h>
6*1b191cb5SApple OSS Distributions #include <fcntl.h>
7*1b191cb5SApple OSS Distributions #include <inttypes.h>
8*1b191cb5SApple OSS Distributions #include <libproc.h>
9*1b191cb5SApple OSS Distributions #include <libgen.h>
10*1b191cb5SApple OSS Distributions #include <limits.h>
11*1b191cb5SApple OSS Distributions #include <mach/mach.h>
12*1b191cb5SApple OSS Distributions #include <mach/policy.h>
13*1b191cb5SApple OSS Distributions #include <mach/vm_param.h>
14*1b191cb5SApple OSS Distributions #include <os/assumes.h>
15*1b191cb5SApple OSS Distributions #include <os/overflow.h>
16*1b191cb5SApple OSS Distributions #include <pthread.h>
17*1b191cb5SApple OSS Distributions #include <pthread/qos_private.h>
18*1b191cb5SApple OSS Distributions #include <signal.h>
19*1b191cb5SApple OSS Distributions #include <stdint.h>
20*1b191cb5SApple OSS Distributions #include <stdio.h>
21*1b191cb5SApple OSS Distributions #include <stdlib.h>
22*1b191cb5SApple OSS Distributions #include <string.h>
23*1b191cb5SApple OSS Distributions #include <sys/event.h>
24*1b191cb5SApple OSS Distributions #include <sys/mman.h>
25*1b191cb5SApple OSS Distributions #include <sys/proc_info.h>
26*1b191cb5SApple OSS Distributions #include <sys/stat.h>
27*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
28*1b191cb5SApple OSS Distributions #include <sys/vnode.h>
29*1b191cb5SApple OSS Distributions #include <unistd.h>
30*1b191cb5SApple OSS Distributions #undef PRIVATE
31*1b191cb5SApple OSS Distributions 
32*1b191cb5SApple OSS Distributions #include "recount/recount_test_utils.h"
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
35*1b191cb5SApple OSS Distributions 
36*1b191cb5SApple OSS Distributions #define ACT_CHANGE_UID 1
37*1b191cb5SApple OSS Distributions #define ACT_CHANGE_RUID 2
38*1b191cb5SApple OSS Distributions #define ACT_EXIT 127
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions #define ACT_PHASE2 2
41*1b191cb5SApple OSS Distributions #define ACT_PHASE3 3
42*1b191cb5SApple OSS Distributions #define ACT_PHASE4 4
43*1b191cb5SApple OSS Distributions #define ACT_PHASE5 5
44*1b191cb5SApple OSS Distributions 
45*1b191cb5SApple OSS Distributions #define PIPE_IN 0
46*1b191cb5SApple OSS Distributions #define PIPE_OUT 1
47*1b191cb5SApple OSS Distributions 
48*1b191cb5SApple OSS Distributions #define CONF_THREAD_NAME "test_child_thread"
49*1b191cb5SApple OSS Distributions #define CONF_CMD_NAME getprogname()
50*1b191cb5SApple OSS Distributions #define CONF_PROC_COUNT 20
51*1b191cb5SApple OSS Distributions #define CONF_BLK_SIZE 4096
52*1b191cb5SApple OSS Distributions #define CONF_UID_VAL 999U
53*1b191cb5SApple OSS Distributions #define CONF_RUID_VAL 998U
54*1b191cb5SApple OSS Distributions #define CONF_GID_VAL 997U
55*1b191cb5SApple OSS Distributions #define CONF_NICE_VAL 5
56*1b191cb5SApple OSS Distributions #define CONF_NUM_THREADS 2
57*1b191cb5SApple OSS Distributions 
58*1b191cb5SApple OSS Distributions #define BASEPRI_DEFAULT 31
59*1b191cb5SApple OSS Distributions #define MAXPRI_USER 63
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions #define CONF_OPN_FILE_COUNT 3
62*1b191cb5SApple OSS Distributions #define CONF_TMP_FILE_PFX   "/tmp/xnu.tests.proc_info."
63*1b191cb5SApple OSS Distributions static int
CONF_TMP_FILE_OPEN(char path[PATH_MAX])64*1b191cb5SApple OSS Distributions CONF_TMP_FILE_OPEN(char path[PATH_MAX])
65*1b191cb5SApple OSS Distributions {
66*1b191cb5SApple OSS Distributions 	static char stmp_path[PATH_MAX] = {};
67*1b191cb5SApple OSS Distributions 	char *nm;
68*1b191cb5SApple OSS Distributions 	if (path) {
69*1b191cb5SApple OSS Distributions 		nm = path;
70*1b191cb5SApple OSS Distributions 	} else {
71*1b191cb5SApple OSS Distributions 		nm = stmp_path;
72*1b191cb5SApple OSS Distributions 	}
73*1b191cb5SApple OSS Distributions 	strlcpy(nm, CONF_TMP_FILE_PFX "XXXXXXXXXX", PATH_MAX);
74*1b191cb5SApple OSS Distributions 	int fd = mkstemp(nm);
75*1b191cb5SApple OSS Distributions 	T_QUIET;
76*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "mkstemp(" CONF_TMP_FILE_PFX "XXXXXXXXXX)");
77*1b191cb5SApple OSS Distributions 	return fd;
78*1b191cb5SApple OSS Distributions }
79*1b191cb5SApple OSS Distributions 
80*1b191cb5SApple OSS Distributions uint32_t get_tty_dev(void);
81*1b191cb5SApple OSS Distributions 
82*1b191cb5SApple OSS Distributions #define WAIT_FOR_CHILDREN(pipefd, action, child_count)                           \
83*1b191cb5SApple OSS Distributions 	do {                                                                         \
84*1b191cb5SApple OSS Distributions 	        long ret;                                                                \
85*1b191cb5SApple OSS Distributions 	        if (child_count == 1) {                                                  \
86*1b191cb5SApple OSS Distributions 	                int child_ret_action = 999;                                          \
87*1b191cb5SApple OSS Distributions 	                while (child_ret_action != action) {                                 \
88*1b191cb5SApple OSS Distributions 	                        ret = read(pipefd, &child_ret_action, sizeof(child_ret_action)); \
89*1b191cb5SApple OSS Distributions 	                }                                                                    \
90*1b191cb5SApple OSS Distributions 	        } else {                                                                 \
91*1b191cb5SApple OSS Distributions 	                int child_ready_count = child_count * (int)sizeof(action);           \
92*1b191cb5SApple OSS Distributions                                                                                  \
93*1b191cb5SApple OSS Distributions 	                action = 0;                                                          \
94*1b191cb5SApple OSS Distributions 	                while (child_ready_count) {                                          \
95*1b191cb5SApple OSS Distributions 	                        ret = read(pipefd, &action, (int)sizeof(action));                \
96*1b191cb5SApple OSS Distributions 	                        if (ret != -1) {                                                 \
97*1b191cb5SApple OSS Distributions 	                                child_ready_count -= ret;                                    \
98*1b191cb5SApple OSS Distributions 	                        } else {                                                         \
99*1b191cb5SApple OSS Distributions 	                                T_FAIL("ERROR: Could not read from pipe() : %d", errno);     \
100*1b191cb5SApple OSS Distributions 	                        }                                                                \
101*1b191cb5SApple OSS Distributions 	                        if (action) {                                                    \
102*1b191cb5SApple OSS Distributions 	                                T_FAIL("ERROR: Child action failed with error %d", action);  \
103*1b191cb5SApple OSS Distributions 	                        }                                                                \
104*1b191cb5SApple OSS Distributions 	                }                                                                    \
105*1b191cb5SApple OSS Distributions 	        }                                                                        \
106*1b191cb5SApple OSS Distributions 	} while (0)
107*1b191cb5SApple OSS Distributions 
108*1b191cb5SApple OSS Distributions #define PROC_INFO_CALL(struct_name, pid, flavor, proc_arg)                                                     \
109*1b191cb5SApple OSS Distributions 	do {                                                                                                       \
110*1b191cb5SApple OSS Distributions 	        struct struct_name * struct_var = malloc(sizeof(struct struct_name));                                  \
111*1b191cb5SApple OSS Distributions 	        T_QUIET;                                                                                               \
112*1b191cb5SApple OSS Distributions 	        T_ASSERT_NOTNULL(struct_var, "malloc() for " #flavor);                                                 \
113*1b191cb5SApple OSS Distributions 	        retval = __proc_info(PROC_INFO_CALL_PIDINFO, pid, flavor, (uint64_t)proc_arg, (user_addr_t)struct_var, \
114*1b191cb5SApple OSS Distributions 	                             (uint32_t)sizeof(struct struct_name));                                            \
115*1b191cb5SApple OSS Distributions                                                                                                                \
116*1b191cb5SApple OSS Distributions 	        T_QUIET;                                                                                               \
117*1b191cb5SApple OSS Distributions 	        T_EXPECT_POSIX_SUCCESS(retval, "__proc_info call for " #flavor);                                       \
118*1b191cb5SApple OSS Distributions 	        T_ASSERT_EQ_INT(retval, (int)sizeof(struct struct_name), "__proc_info call for " #flavor);             \
119*1b191cb5SApple OSS Distributions 	        ret_structs[i] = (void *)struct_var;                                                                   \
120*1b191cb5SApple OSS Distributions 	        i++;                                                                                                   \
121*1b191cb5SApple OSS Distributions 	} while (0)
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions uint32_t
get_tty_dev()124*1b191cb5SApple OSS Distributions get_tty_dev()
125*1b191cb5SApple OSS Distributions {
126*1b191cb5SApple OSS Distributions 	struct stat buf;
127*1b191cb5SApple OSS Distributions 	stat(ttyname(1), &buf);
128*1b191cb5SApple OSS Distributions 	return (uint32_t)buf.st_rdev;
129*1b191cb5SApple OSS Distributions }
130*1b191cb5SApple OSS Distributions 
131*1b191cb5SApple OSS Distributions /*
132*1b191cb5SApple OSS Distributions  * Defined in libsyscall/wrappers/libproc/libproc.c
133*1b191cb5SApple OSS Distributions  * For API test only. For normal use, please use the libproc API instead.
134*1b191cb5SApple OSS Distributions  * DO NOT COPY
135*1b191cb5SApple OSS Distributions  */
136*1b191cb5SApple OSS Distributions extern int __proc_info(int32_t callnum, int32_t pid, uint32_t flavor, uint64_t arg, user_addr_t buffer, int32_t buffersize);
137*1b191cb5SApple OSS Distributions struct proc_config_s {
138*1b191cb5SApple OSS Distributions 	int parent_pipe[2];
139*1b191cb5SApple OSS Distributions 	int child_count;
140*1b191cb5SApple OSS Distributions 	pid_t proc_grp_id;
141*1b191cb5SApple OSS Distributions 	int child_pipe[CONF_PROC_COUNT][2];
142*1b191cb5SApple OSS Distributions 	int child_pids[CONF_PROC_COUNT];
143*1b191cb5SApple OSS Distributions 	void * cow_map; /* memory for cow test */
144*1b191cb5SApple OSS Distributions };
145*1b191cb5SApple OSS Distributions typedef struct proc_config_s * proc_config_t;
146*1b191cb5SApple OSS Distributions 
147*1b191cb5SApple OSS Distributions typedef void (^child_action_handler_t)(proc_config_t proc_config, int child_id);
148*1b191cb5SApple OSS Distributions 
149*1b191cb5SApple OSS Distributions enum proc_info_opt {
150*1b191cb5SApple OSS Distributions 	P_UNIQIDINFO    = 0x01,
151*1b191cb5SApple OSS Distributions 	C_UNIQIDINFO    = 0x02,
152*1b191cb5SApple OSS Distributions 	PBSD_OLD        = 0x04,
153*1b191cb5SApple OSS Distributions 	PBSD            = 0x08,
154*1b191cb5SApple OSS Distributions 	PBSD_SHORT      = 0x10,
155*1b191cb5SApple OSS Distributions 	PBSD_UNIQID     = 0x20,
156*1b191cb5SApple OSS Distributions 	P_TASK_INFO     = 0x40,
157*1b191cb5SApple OSS Distributions 	P_TASK_INFO_NEW = 0x80,
158*1b191cb5SApple OSS Distributions 	PALL            = 0x100,
159*1b191cb5SApple OSS Distributions 	THREAD_ADDR     = 0x200,
160*1b191cb5SApple OSS Distributions 	PTHINFO_OLD     = 0x400,
161*1b191cb5SApple OSS Distributions 	PTHINFO         = 0x800,
162*1b191cb5SApple OSS Distributions 	PTHINFO_64      = 0x1000,
163*1b191cb5SApple OSS Distributions 	PINFO_PATH      = 0x2000,
164*1b191cb5SApple OSS Distributions 	PAI             = 0x4000,
165*1b191cb5SApple OSS Distributions 	PREGINFO        = 0x8000,
166*1b191cb5SApple OSS Distributions 	PREGINFO_PATH   = 0x10000,
167*1b191cb5SApple OSS Distributions 	PREGINFO_PATH_2 = 0x20000,
168*1b191cb5SApple OSS Distributions 	PREGINFO_PATH_3 = 0x40000,
169*1b191cb5SApple OSS Distributions 	PVNINFO         = 0x80000
170*1b191cb5SApple OSS Distributions };
171*1b191cb5SApple OSS Distributions 
172*1b191cb5SApple OSS Distributions static int tmp_fd = -1;
173*1b191cb5SApple OSS Distributions 
174*1b191cb5SApple OSS Distributions static child_action_handler_t proc_info_listpids_handler = ^void (proc_config_t proc_config, int child_id) {
175*1b191cb5SApple OSS Distributions 	close(proc_config->parent_pipe[PIPE_IN]);
176*1b191cb5SApple OSS Distributions 	close(proc_config->child_pipe[child_id][PIPE_OUT]);
177*1b191cb5SApple OSS Distributions 	long retval      = 0;
178*1b191cb5SApple OSS Distributions 	int child_action = 0;
179*1b191cb5SApple OSS Distributions 	retval           = write(proc_config->parent_pipe[PIPE_OUT], &child_action, sizeof(child_action));
180*1b191cb5SApple OSS Distributions 	if (retval != -1) {
181*1b191cb5SApple OSS Distributions 		while (child_action != ACT_EXIT) {
182*1b191cb5SApple OSS Distributions 			retval = read(proc_config->child_pipe[child_id][PIPE_IN], &child_action, sizeof(child_action));
183*1b191cb5SApple OSS Distributions 			if (retval == 0 || (retval == -1 && errno == EAGAIN)) {
184*1b191cb5SApple OSS Distributions 				continue;
185*1b191cb5SApple OSS Distributions 			}
186*1b191cb5SApple OSS Distributions 			if (retval != -1) {
187*1b191cb5SApple OSS Distributions 				switch (child_action) {
188*1b191cb5SApple OSS Distributions 				case ACT_CHANGE_UID:
189*1b191cb5SApple OSS Distributions 					/*
190*1b191cb5SApple OSS Distributions 					 * Change uid
191*1b191cb5SApple OSS Distributions 					 */
192*1b191cb5SApple OSS Distributions 					retval = setuid(CONF_UID_VAL);
193*1b191cb5SApple OSS Distributions 					break;
194*1b191cb5SApple OSS Distributions 				case ACT_CHANGE_RUID:
195*1b191cb5SApple OSS Distributions 					/*
196*1b191cb5SApple OSS Distributions 					 * Change ruid
197*1b191cb5SApple OSS Distributions 					 */
198*1b191cb5SApple OSS Distributions 					retval = setreuid(CONF_RUID_VAL, (uid_t)-1);
199*1b191cb5SApple OSS Distributions 					break;
200*1b191cb5SApple OSS Distributions 				case ACT_EXIT:
201*1b191cb5SApple OSS Distributions 					/*
202*1b191cb5SApple OSS Distributions 					 * Exit
203*1b191cb5SApple OSS Distributions 					 */
204*1b191cb5SApple OSS Distributions 					break;
205*1b191cb5SApple OSS Distributions 				}
206*1b191cb5SApple OSS Distributions 			}
207*1b191cb5SApple OSS Distributions 			if (child_action != ACT_EXIT) {
208*1b191cb5SApple OSS Distributions 				retval = write(proc_config->parent_pipe[PIPE_OUT], &retval, sizeof(retval));
209*1b191cb5SApple OSS Distributions 				if (retval == -1) {
210*1b191cb5SApple OSS Distributions 					break;
211*1b191cb5SApple OSS Distributions 				}
212*1b191cb5SApple OSS Distributions 			}
213*1b191cb5SApple OSS Distributions 		}
214*1b191cb5SApple OSS Distributions 	}
215*1b191cb5SApple OSS Distributions 	close(proc_config->parent_pipe[PIPE_OUT]);
216*1b191cb5SApple OSS Distributions 	close(proc_config->child_pipe[child_id][PIPE_IN]);
217*1b191cb5SApple OSS Distributions 	exit(0);
218*1b191cb5SApple OSS Distributions };
219*1b191cb5SApple OSS Distributions 
220*1b191cb5SApple OSS Distributions static child_action_handler_t proc_info_call_pidinfo_handler = ^void (proc_config_t proc_config, int child_id) {
221*1b191cb5SApple OSS Distributions 	close(proc_config->parent_pipe[PIPE_IN]);
222*1b191cb5SApple OSS Distributions 	close(proc_config->child_pipe[child_id][PIPE_OUT]);
223*1b191cb5SApple OSS Distributions 	int action  = 0;
224*1b191cb5SApple OSS Distributions 	long retval = 0;
225*1b191cb5SApple OSS Distributions 	int i;
226*1b191cb5SApple OSS Distributions 	void * tmp_map           = NULL;
227*1b191cb5SApple OSS Distributions 	dispatch_queue_t q       = NULL;
228*1b191cb5SApple OSS Distributions 	dispatch_semaphore_t sem = NULL;
229*1b191cb5SApple OSS Distributions 	/*
230*1b191cb5SApple OSS Distributions 	 * PHASE 1: Child ready and waits for parent to send next action
231*1b191cb5SApple OSS Distributions 	 */
232*1b191cb5SApple OSS Distributions 	T_LOG("Child ready to accept action from parent");
233*1b191cb5SApple OSS Distributions 	retval = write(proc_config->parent_pipe[PIPE_OUT], &action, sizeof(action));
234*1b191cb5SApple OSS Distributions 	if (retval != -1) {
235*1b191cb5SApple OSS Distributions 		while (action != ACT_EXIT) {
236*1b191cb5SApple OSS Distributions 			retval = read(proc_config->child_pipe[child_id][PIPE_IN], &action, sizeof(action));
237*1b191cb5SApple OSS Distributions 
238*1b191cb5SApple OSS Distributions 			if (retval != -1) {
239*1b191cb5SApple OSS Distributions 				retval = 0;
240*1b191cb5SApple OSS Distributions 				switch (action) {
241*1b191cb5SApple OSS Distributions 				case ACT_PHASE2: {
242*1b191cb5SApple OSS Distributions 					/*
243*1b191cb5SApple OSS Distributions 					 * Change uid, euid, guid, rgid, nice value
244*1b191cb5SApple OSS Distributions 					 * Also change the svuid and svgid
245*1b191cb5SApple OSS Distributions 					 */
246*1b191cb5SApple OSS Distributions 					T_LOG("Child changing uid, euid, rguid, svuid, svgid and nice value");
247*1b191cb5SApple OSS Distributions 					retval = nice(CONF_NICE_VAL);
248*1b191cb5SApple OSS Distributions 					if (retval == -1) {
249*1b191cb5SApple OSS Distributions 						T_LOG("(child) ERROR: nice() failed");
250*1b191cb5SApple OSS Distributions 						break;
251*1b191cb5SApple OSS Distributions 					}
252*1b191cb5SApple OSS Distributions 					retval = setgid(CONF_GID_VAL);
253*1b191cb5SApple OSS Distributions 					if (retval == -1) {
254*1b191cb5SApple OSS Distributions 						T_LOG("(child) ERROR: setgid() failed");
255*1b191cb5SApple OSS Distributions 						break;
256*1b191cb5SApple OSS Distributions 					}
257*1b191cb5SApple OSS Distributions 					retval = setreuid((uid_t)-1, CONF_RUID_VAL);
258*1b191cb5SApple OSS Distributions 					if (retval == -1) {
259*1b191cb5SApple OSS Distributions 						T_LOG("(child) ERROR: setreuid() failed");
260*1b191cb5SApple OSS Distributions 						break;
261*1b191cb5SApple OSS Distributions 					}
262*1b191cb5SApple OSS Distributions 					break;
263*1b191cb5SApple OSS Distributions 				}
264*1b191cb5SApple OSS Distributions 				case ACT_PHASE3: {
265*1b191cb5SApple OSS Distributions 					/*
266*1b191cb5SApple OSS Distributions 					 * Allocate a page of memory
267*1b191cb5SApple OSS Distributions 					 * Copy on write shared memory
268*1b191cb5SApple OSS Distributions 					 *
269*1b191cb5SApple OSS Distributions 					 * WARNING
270*1b191cb5SApple OSS Distributions 					 * Don't add calls to T_LOG here as they can end up generating unwanted
271*1b191cb5SApple OSS Distributions 					 * calls to mach_msg_send(). If curtask->messages_sent gets incremented
272*1b191cb5SApple OSS Distributions 					 * at this point it will interfere with testing pti_messages_sent.
273*1b191cb5SApple OSS Distributions 					 */
274*1b191cb5SApple OSS Distributions 					retval  = 0;
275*1b191cb5SApple OSS Distributions 					tmp_map = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
276*1b191cb5SApple OSS Distributions 					if (tmp_map == MAP_FAILED) {
277*1b191cb5SApple OSS Distributions 						T_LOG("(child) ERROR: mmap() failed");
278*1b191cb5SApple OSS Distributions 						retval = 1;
279*1b191cb5SApple OSS Distributions 						break;
280*1b191cb5SApple OSS Distributions 					}
281*1b191cb5SApple OSS Distributions 					/*
282*1b191cb5SApple OSS Distributions 					 * Get the page allocated
283*1b191cb5SApple OSS Distributions 					 */
284*1b191cb5SApple OSS Distributions 					int * map_ptr = (int *)tmp_map;
285*1b191cb5SApple OSS Distributions 					for (i = 0; i < (int)(PAGE_SIZE / sizeof(int)); i++) {
286*1b191cb5SApple OSS Distributions 						*map_ptr++ = i;
287*1b191cb5SApple OSS Distributions 					}
288*1b191cb5SApple OSS Distributions 					/*
289*1b191cb5SApple OSS Distributions 					 * Cause copy on write to the page
290*1b191cb5SApple OSS Distributions 					 */
291*1b191cb5SApple OSS Distributions 					*((int *)(proc_config->cow_map)) = 20;
292*1b191cb5SApple OSS Distributions 
293*1b191cb5SApple OSS Distributions 					break;
294*1b191cb5SApple OSS Distributions 				}
295*1b191cb5SApple OSS Distributions 				case ACT_PHASE4: {
296*1b191cb5SApple OSS Distributions 					T_LOG("Child spending CPU cycles and changing thread name");
297*1b191cb5SApple OSS Distributions 					retval                       = 0;
298*1b191cb5SApple OSS Distributions 					int number                   = 1000;
299*1b191cb5SApple OSS Distributions 					unsigned long long factorial = 1;
300*1b191cb5SApple OSS Distributions 					int j;
301*1b191cb5SApple OSS Distributions 					for (j = 1; j <= number; j++) {
302*1b191cb5SApple OSS Distributions 						factorial *= (unsigned long long)j;
303*1b191cb5SApple OSS Distributions 					}
304*1b191cb5SApple OSS Distributions 					sysctlbyname("kern.threadname", NULL, 0, CONF_THREAD_NAME, strlen(CONF_THREAD_NAME));
305*1b191cb5SApple OSS Distributions 					break;
306*1b191cb5SApple OSS Distributions 				}
307*1b191cb5SApple OSS Distributions 				case ACT_PHASE5: {
308*1b191cb5SApple OSS Distributions 					/*
309*1b191cb5SApple OSS Distributions 					 * Dispatch for Workq test
310*1b191cb5SApple OSS Distributions 					 */
311*1b191cb5SApple OSS Distributions 					T_LOG("Child creating a dispatch queue, and dispatching blocks on it");
312*1b191cb5SApple OSS Distributions 					q = dispatch_queue_create("com.apple.test_proc_info.workqtest",
313*1b191cb5SApple OSS Distributions 					    DISPATCH_QUEUE_CONCURRENT);                     // dispatch_get_global_queue(0, 0);
314*1b191cb5SApple OSS Distributions 					sem = dispatch_semaphore_create(0);
315*1b191cb5SApple OSS Distributions 
316*1b191cb5SApple OSS Distributions 					for (i = 0; i < CONF_NUM_THREADS; i++) {
317*1b191cb5SApple OSS Distributions 						dispatch_async(q, ^{
318*1b191cb5SApple OSS Distributions 								/*
319*1b191cb5SApple OSS Distributions 								 * Block the thread, do nothing
320*1b191cb5SApple OSS Distributions 								 */
321*1b191cb5SApple OSS Distributions 								dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
322*1b191cb5SApple OSS Distributions 							});
323*1b191cb5SApple OSS Distributions 					}
324*1b191cb5SApple OSS Distributions 					break;
325*1b191cb5SApple OSS Distributions 				}
326*1b191cb5SApple OSS Distributions 				case ACT_EXIT: {
327*1b191cb5SApple OSS Distributions 					/*
328*1b191cb5SApple OSS Distributions 					 * Exit
329*1b191cb5SApple OSS Distributions 					 */
330*1b191cb5SApple OSS Distributions 					if (sem) {
331*1b191cb5SApple OSS Distributions 						for (i = 0; i < CONF_NUM_THREADS; i++) {
332*1b191cb5SApple OSS Distributions 							dispatch_semaphore_signal(sem);
333*1b191cb5SApple OSS Distributions 						}
334*1b191cb5SApple OSS Distributions 					}
335*1b191cb5SApple OSS Distributions 
336*1b191cb5SApple OSS Distributions 					if (tmp_map) {
337*1b191cb5SApple OSS Distributions 						munmap(tmp_map, PAGE_SIZE);
338*1b191cb5SApple OSS Distributions 					}
339*1b191cb5SApple OSS Distributions 
340*1b191cb5SApple OSS Distributions 					if (proc_config->cow_map) {
341*1b191cb5SApple OSS Distributions 						munmap(proc_config->cow_map, PAGE_SIZE);
342*1b191cb5SApple OSS Distributions 					}
343*1b191cb5SApple OSS Distributions 
344*1b191cb5SApple OSS Distributions 					break;
345*1b191cb5SApple OSS Distributions 				}
346*1b191cb5SApple OSS Distributions 				}
347*1b191cb5SApple OSS Distributions 			}
348*1b191cb5SApple OSS Distributions 			if (action != ACT_EXIT) {
349*1b191cb5SApple OSS Distributions 				retval = write(proc_config->parent_pipe[PIPE_OUT], &action, sizeof(action));
350*1b191cb5SApple OSS Distributions 				if (retval == -1) {
351*1b191cb5SApple OSS Distributions 					break;
352*1b191cb5SApple OSS Distributions 				}
353*1b191cb5SApple OSS Distributions 			}
354*1b191cb5SApple OSS Distributions 		}
355*1b191cb5SApple OSS Distributions 		close(proc_config->parent_pipe[PIPE_OUT]);
356*1b191cb5SApple OSS Distributions 		close(proc_config->child_pipe[child_id][PIPE_IN]);
357*1b191cb5SApple OSS Distributions 		exit(0);
358*1b191cb5SApple OSS Distributions 	}
359*1b191cb5SApple OSS Distributions };
360*1b191cb5SApple OSS Distributions 
361*1b191cb5SApple OSS Distributions static void
free_proc_config(proc_config_t proc_config)362*1b191cb5SApple OSS Distributions free_proc_config(proc_config_t proc_config)
363*1b191cb5SApple OSS Distributions {
364*1b191cb5SApple OSS Distributions 	free(proc_config);
365*1b191cb5SApple OSS Distributions }
366*1b191cb5SApple OSS Distributions 
367*1b191cb5SApple OSS Distributions static void
send_action_to_child_processes(proc_config_t proc_config,int action)368*1b191cb5SApple OSS Distributions send_action_to_child_processes(proc_config_t proc_config, int action)
369*1b191cb5SApple OSS Distributions {
370*1b191cb5SApple OSS Distributions 	long err;
371*1b191cb5SApple OSS Distributions 	for (int i = 0; i < proc_config->child_count; i++) {
372*1b191cb5SApple OSS Distributions 		err = write(proc_config->child_pipe[i][PIPE_OUT], &action, sizeof(action));
373*1b191cb5SApple OSS Distributions 		T_QUIET;
374*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(err, "write() to child in send_action");
375*1b191cb5SApple OSS Distributions 	}
376*1b191cb5SApple OSS Distributions 	if (action != ACT_EXIT) {
377*1b191cb5SApple OSS Distributions 		WAIT_FOR_CHILDREN(proc_config->parent_pipe[PIPE_IN], action, proc_config->child_count);
378*1b191cb5SApple OSS Distributions 	}
379*1b191cb5SApple OSS Distributions }
380*1b191cb5SApple OSS Distributions 
381*1b191cb5SApple OSS Distributions static void
kill_child_processes(proc_config_t proc_config)382*1b191cb5SApple OSS Distributions kill_child_processes(proc_config_t proc_config)
383*1b191cb5SApple OSS Distributions {
384*1b191cb5SApple OSS Distributions 	int ret = 0;
385*1b191cb5SApple OSS Distributions 	T_LOG("Killing child processes");
386*1b191cb5SApple OSS Distributions 	send_action_to_child_processes(proc_config, ACT_EXIT);
387*1b191cb5SApple OSS Distributions 	for (int child_id = 0; child_id < proc_config->child_count; child_id++) {
388*1b191cb5SApple OSS Distributions 		close(proc_config->child_pipe[child_id][PIPE_OUT]);
389*1b191cb5SApple OSS Distributions 		dt_waitpid(proc_config->child_pids[child_id], NULL, NULL, 5);
390*1b191cb5SApple OSS Distributions 		T_QUIET;
391*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "killed child %d", child_id);
392*1b191cb5SApple OSS Distributions 	}
393*1b191cb5SApple OSS Distributions 	close(proc_config->parent_pipe[PIPE_IN]);
394*1b191cb5SApple OSS Distributions 	munmap(proc_config->cow_map, PAGE_SIZE);
395*1b191cb5SApple OSS Distributions 	T_LOG("Killed child processes");
396*1b191cb5SApple OSS Distributions }
397*1b191cb5SApple OSS Distributions 
398*1b191cb5SApple OSS Distributions static proc_config_t
spawn_child_processes(int child_count,child_action_handler_t child_handler)399*1b191cb5SApple OSS Distributions spawn_child_processes(int child_count, child_action_handler_t child_handler)
400*1b191cb5SApple OSS Distributions {
401*1b191cb5SApple OSS Distributions 	/*
402*1b191cb5SApple OSS Distributions 	 * Spawn procs for Tests 1.2 and 1.3
403*1b191cb5SApple OSS Distributions 	 */
404*1b191cb5SApple OSS Distributions 	T_LOG("Spawning child processes...");
405*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = malloc(sizeof(*proc_config));
406*1b191cb5SApple OSS Distributions 	int action                = 0;
407*1b191cb5SApple OSS Distributions 	int err;
408*1b191cb5SApple OSS Distributions 
409*1b191cb5SApple OSS Distributions 	setpgid(0, 0);
410*1b191cb5SApple OSS Distributions 	proc_config->proc_grp_id = getpgid(0);
411*1b191cb5SApple OSS Distributions 
412*1b191cb5SApple OSS Distributions 	proc_config->child_count = child_count;
413*1b191cb5SApple OSS Distributions 
414*1b191cb5SApple OSS Distributions 	err = pipe(proc_config->parent_pipe);
415*1b191cb5SApple OSS Distributions 	T_QUIET;
416*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(err, "pipe() call");
417*1b191cb5SApple OSS Distributions 
418*1b191cb5SApple OSS Distributions 	/*
419*1b191cb5SApple OSS Distributions 	 * Needed for ACT_PHASE3 tests
420*1b191cb5SApple OSS Distributions 	 */
421*1b191cb5SApple OSS Distributions 	proc_config->cow_map = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
422*1b191cb5SApple OSS Distributions 	T_QUIET;
423*1b191cb5SApple OSS Distributions 	T_ASSERT_NE_PTR(proc_config->cow_map, MAP_FAILED, "cow_map mmap()");
424*1b191cb5SApple OSS Distributions 	*((int *)(proc_config->cow_map)) = 10;
425*1b191cb5SApple OSS Distributions 
426*1b191cb5SApple OSS Distributions 	pid_t child_pid;
427*1b191cb5SApple OSS Distributions 	int i;
428*1b191cb5SApple OSS Distributions 	int child_id;
429*1b191cb5SApple OSS Distributions 	for (i = 0; i < child_count; i++) {
430*1b191cb5SApple OSS Distributions 		err = pipe(proc_config->child_pipe[i]);
431*1b191cb5SApple OSS Distributions 		T_QUIET;
432*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(err, "pipe() call");
433*1b191cb5SApple OSS Distributions 
434*1b191cb5SApple OSS Distributions 		child_pid = fork();
435*1b191cb5SApple OSS Distributions 		child_id  = i;
436*1b191cb5SApple OSS Distributions 		T_QUIET;
437*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(child_pid, "fork() in parent process for child %d", child_id);
438*1b191cb5SApple OSS Distributions 
439*1b191cb5SApple OSS Distributions 		if (child_pid == 0) {
440*1b191cb5SApple OSS Distributions 			child_handler(proc_config, child_id);
441*1b191cb5SApple OSS Distributions 		} else {
442*1b191cb5SApple OSS Distributions 			proc_config->child_pids[child_id] = child_pid;
443*1b191cb5SApple OSS Distributions 		}
444*1b191cb5SApple OSS Distributions 		close(proc_config->child_pipe[child_id][PIPE_IN]);
445*1b191cb5SApple OSS Distributions 	}
446*1b191cb5SApple OSS Distributions 	/*
447*1b191cb5SApple OSS Distributions 	 * Wait for the children processes to spawn
448*1b191cb5SApple OSS Distributions 	 */
449*1b191cb5SApple OSS Distributions 	close(proc_config->parent_pipe[PIPE_OUT]);
450*1b191cb5SApple OSS Distributions 	WAIT_FOR_CHILDREN(proc_config->parent_pipe[PIPE_IN], action, child_count);
451*1b191cb5SApple OSS Distributions 
452*1b191cb5SApple OSS Distributions 	return proc_config;
453*1b191cb5SApple OSS Distributions }
454*1b191cb5SApple OSS Distributions 
455*1b191cb5SApple OSS Distributions /*
456*1b191cb5SApple OSS Distributions  *  All PROC_INFO_CALL_PIDINFO __proc_info calls fire from this function.
457*1b191cb5SApple OSS Distributions  *  T_DECLs require different combinations of structs and different actions
458*1b191cb5SApple OSS Distributions  *  must occur in the child to get the data.  Instead of performing the setup
459*1b191cb5SApple OSS Distributions  *  in each T_DECL, this function accepts a bitmap and performs the necessary setup
460*1b191cb5SApple OSS Distributions  *  and cleanup work
461*1b191cb5SApple OSS Distributions  */
462*1b191cb5SApple OSS Distributions 
463*1b191cb5SApple OSS Distributions static void
proc_info_caller(int proc_info_opts,void ** ret_structs,int * ret_child_pid)464*1b191cb5SApple OSS Distributions proc_info_caller(int proc_info_opts, void ** ret_structs, int * ret_child_pid)
465*1b191cb5SApple OSS Distributions {
466*1b191cb5SApple OSS Distributions 	int retval, i = 0;
467*1b191cb5SApple OSS Distributions 	uint64_t * thread_addr = NULL;
468*1b191cb5SApple OSS Distributions 	void * map_tmp         = NULL;
469*1b191cb5SApple OSS Distributions 	static char tmp_path[PATH_MAX] = {};
470*1b191cb5SApple OSS Distributions 
471*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(1, proc_info_call_pidinfo_handler);
472*1b191cb5SApple OSS Distributions 	int child_pid             = proc_config->child_pids[0];
473*1b191cb5SApple OSS Distributions 	/*
474*1b191cb5SApple OSS Distributions 	 * These tests only require one child.
475*1b191cb5SApple OSS Distributions 	 * Some DECLs need to know the child pid, so we pass that back if applicable
476*1b191cb5SApple OSS Distributions 	 */
477*1b191cb5SApple OSS Distributions 	if (ret_child_pid != NULL) {
478*1b191cb5SApple OSS Distributions 		*ret_child_pid = child_pid;
479*1b191cb5SApple OSS Distributions 	}
480*1b191cb5SApple OSS Distributions 
481*1b191cb5SApple OSS Distributions 	if (proc_info_opts & P_UNIQIDINFO) {
482*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_uniqidentifierinfo, getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0);
483*1b191cb5SApple OSS Distributions 	}
484*1b191cb5SApple OSS Distributions 	if (proc_info_opts & C_UNIQIDINFO) {
485*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_uniqidentifierinfo, child_pid, PROC_PIDUNIQIDENTIFIERINFO, 0);
486*1b191cb5SApple OSS Distributions 	}
487*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PBSD_OLD) {
488*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_bsdinfo, child_pid, PROC_PIDTBSDINFO, 0);
489*1b191cb5SApple OSS Distributions 	}
490*1b191cb5SApple OSS Distributions 
491*1b191cb5SApple OSS Distributions 	/*
492*1b191cb5SApple OSS Distributions 	 * Child Phase 2 Fires if opts require it
493*1b191cb5SApple OSS Distributions 	 * Small nap after call to give child time to receive and execute the action
494*1b191cb5SApple OSS Distributions 	 */
495*1b191cb5SApple OSS Distributions 
496*1b191cb5SApple OSS Distributions 	if (proc_info_opts >= PBSD) {
497*1b191cb5SApple OSS Distributions 		send_action_to_child_processes(proc_config, ACT_PHASE2);
498*1b191cb5SApple OSS Distributions 	}
499*1b191cb5SApple OSS Distributions 
500*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PBSD) {
501*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_bsdinfo, child_pid, PROC_PIDTBSDINFO, 0);
502*1b191cb5SApple OSS Distributions 	}
503*1b191cb5SApple OSS Distributions 
504*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PBSD_SHORT) {
505*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_bsdshortinfo, child_pid, PROC_PIDT_SHORTBSDINFO, 0);
506*1b191cb5SApple OSS Distributions 	}
507*1b191cb5SApple OSS Distributions 
508*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PBSD_UNIQID) {
509*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_bsdinfowithuniqid, child_pid, PROC_PIDT_BSDINFOWITHUNIQID, 0);
510*1b191cb5SApple OSS Distributions 	}
511*1b191cb5SApple OSS Distributions 	if (proc_info_opts & P_TASK_INFO) {
512*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_taskinfo, child_pid, PROC_PIDTASKINFO, 0);
513*1b191cb5SApple OSS Distributions 	}
514*1b191cb5SApple OSS Distributions 
515*1b191cb5SApple OSS Distributions 	/*
516*1b191cb5SApple OSS Distributions 	 * Child Phase 3 Fires
517*1b191cb5SApple OSS Distributions 	 */
518*1b191cb5SApple OSS Distributions 	if (proc_info_opts >= P_TASK_INFO_NEW) {
519*1b191cb5SApple OSS Distributions 		send_action_to_child_processes(proc_config, ACT_PHASE3);
520*1b191cb5SApple OSS Distributions 	}
521*1b191cb5SApple OSS Distributions 
522*1b191cb5SApple OSS Distributions 	if (proc_info_opts & P_TASK_INFO_NEW) {
523*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_taskinfo, child_pid, PROC_PIDTASKINFO, 0);
524*1b191cb5SApple OSS Distributions 	}
525*1b191cb5SApple OSS Distributions 
526*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PALL) {
527*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_taskallinfo, child_pid, PROC_PIDTASKALLINFO, 0);
528*1b191cb5SApple OSS Distributions 	}
529*1b191cb5SApple OSS Distributions 	/*
530*1b191cb5SApple OSS Distributions 	 * This case breaks the pattern in that its proc_info call requires PALL,
531*1b191cb5SApple OSS Distributions 	 * its value is required in some other proc_info calls
532*1b191cb5SApple OSS Distributions 	 * and we never put the retval into our ret_structs
533*1b191cb5SApple OSS Distributions 	 */
534*1b191cb5SApple OSS Distributions 	if (proc_info_opts & THREAD_ADDR || proc_info_opts & PTHINFO_OLD || proc_info_opts & PTHINFO || proc_info_opts & PINFO_PATH) {
535*1b191cb5SApple OSS Distributions 		struct proc_taskallinfo * pall = malloc(sizeof(struct proc_taskallinfo));
536*1b191cb5SApple OSS Distributions 		T_QUIET;
537*1b191cb5SApple OSS Distributions 		T_ASSERT_NOTNULL(pall, "malloc() for PROC_TASKALLINFO");
538*1b191cb5SApple OSS Distributions 
539*1b191cb5SApple OSS Distributions 		retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDTASKALLINFO, (uint32_t)0, (user_addr_t)pall,
540*1b191cb5SApple OSS Distributions 		    (uint32_t)sizeof(struct proc_taskallinfo));
541*1b191cb5SApple OSS Distributions 		T_QUIET;
542*1b191cb5SApple OSS Distributions 		T_ASSERT_EQ_INT(retval, (int)sizeof(struct proc_taskallinfo), "__proc_info call for PROC_PIDTASKALLINFO in THREAD_ADDR");
543*1b191cb5SApple OSS Distributions 
544*1b191cb5SApple OSS Distributions 		thread_addr = malloc(sizeof(uint64_t) * (unsigned long)(pall->ptinfo.pti_threadnum + 1));
545*1b191cb5SApple OSS Distributions 		memset(thread_addr, 0, sizeof(uint64_t) * (unsigned long)(pall->ptinfo.pti_threadnum + 1));
546*1b191cb5SApple OSS Distributions 		T_QUIET;
547*1b191cb5SApple OSS Distributions 		T_ASSERT_NOTNULL(thread_addr, "malloc() for PROC_PIDLISTTHREADS");
548*1b191cb5SApple OSS Distributions 
549*1b191cb5SApple OSS Distributions 		retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDLISTTHREADS, (uint32_t)0, (user_addr_t)thread_addr,
550*1b191cb5SApple OSS Distributions 		    (int32_t)(sizeof(uint64_t) * (unsigned long)(pall->ptinfo.pti_threadnum + 1)));
551*1b191cb5SApple OSS Distributions 		T_LOG("(int)((unsigned long)retval / PROC_PIDLISTTHREADS_SIZE: %d",
552*1b191cb5SApple OSS Distributions 		    (int)((unsigned long)retval / PROC_PIDLISTTHREADS_SIZE));
553*1b191cb5SApple OSS Distributions 		T_ASSERT_GE_INT((int)((unsigned long)retval / PROC_PIDLISTTHREADS_SIZE), pall->ptinfo.pti_threadnum,
554*1b191cb5SApple OSS Distributions 		    "__proc_info call for PROC_PIDLISTTHREADS");
555*1b191cb5SApple OSS Distributions 
556*1b191cb5SApple OSS Distributions 		free(pall);
557*1b191cb5SApple OSS Distributions 	}
558*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PTHINFO_OLD) {
559*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_threadinfo, child_pid, PROC_PIDTHREADINFO, thread_addr[0]);
560*1b191cb5SApple OSS Distributions 	}
561*1b191cb5SApple OSS Distributions 
562*1b191cb5SApple OSS Distributions 	/*
563*1b191cb5SApple OSS Distributions 	 * Child Phase 4 Fires
564*1b191cb5SApple OSS Distributions 	 */
565*1b191cb5SApple OSS Distributions 	if (proc_info_opts >= PTHINFO) {
566*1b191cb5SApple OSS Distributions 		send_action_to_child_processes(proc_config, ACT_PHASE4);
567*1b191cb5SApple OSS Distributions 	}
568*1b191cb5SApple OSS Distributions 
569*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PTHINFO) {
570*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_threadinfo, child_pid, PROC_PIDTHREADINFO, thread_addr[0]);
571*1b191cb5SApple OSS Distributions 	}
572*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PTHINFO_64) {
573*1b191cb5SApple OSS Distributions 		mach_port_name_t child_task  = MACH_PORT_NULL;
574*1b191cb5SApple OSS Distributions 		thread_array_t child_threads = NULL;
575*1b191cb5SApple OSS Distributions 		mach_msg_type_number_t child_thread_count;
576*1b191cb5SApple OSS Distributions 		thread_identifier_info_data_t child_thread_threadinfo;
577*1b191cb5SApple OSS Distributions 		mach_msg_type_number_t thread_info_count = THREAD_IDENTIFIER_INFO_COUNT;
578*1b191cb5SApple OSS Distributions 		struct proc_threadinfo * pthinfo_64      = malloc(sizeof(struct proc_threadinfo));
579*1b191cb5SApple OSS Distributions 		T_QUIET;
580*1b191cb5SApple OSS Distributions 		T_ASSERT_NOTNULL(pthinfo_64, "malloc() for PROC_THREADINFO");
581*1b191cb5SApple OSS Distributions 
582*1b191cb5SApple OSS Distributions 		retval = task_for_pid(mach_task_self(), child_pid, &child_task);
583*1b191cb5SApple OSS Distributions 		T_ASSERT_EQ_INT(retval, 0, "task_for_pid for PROC_PIDTHREADID64INFO");
584*1b191cb5SApple OSS Distributions 
585*1b191cb5SApple OSS Distributions 		retval = task_threads(child_task, &child_threads, &child_thread_count);
586*1b191cb5SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(retval, "task_threads() call for PROC_PIDTHREADID64INFO");
587*1b191cb5SApple OSS Distributions 
588*1b191cb5SApple OSS Distributions 		retval = thread_info(child_threads[0], THREAD_IDENTIFIER_INFO, (thread_info_t)&child_thread_threadinfo, &thread_info_count);
589*1b191cb5SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(retval, "thread_info call for PROC_PIDTHREADID64INFO");
590*1b191cb5SApple OSS Distributions 
591*1b191cb5SApple OSS Distributions 		retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDTHREADID64INFO, (uint64_t)child_thread_threadinfo.thread_id,
592*1b191cb5SApple OSS Distributions 		    (user_addr_t)pthinfo_64, (uint32_t)sizeof(struct proc_threadinfo));
593*1b191cb5SApple OSS Distributions 		T_ASSERT_EQ_INT(retval, (int)sizeof(struct proc_threadinfo), "__proc_info call for PROC_PIDTHREADID64INFO");
594*1b191cb5SApple OSS Distributions 
595*1b191cb5SApple OSS Distributions 		ret_structs[i] = (void *)pthinfo_64;
596*1b191cb5SApple OSS Distributions 		i++;
597*1b191cb5SApple OSS Distributions 
598*1b191cb5SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), child_task);
599*1b191cb5SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), child_threads[0]);
600*1b191cb5SApple OSS Distributions 		child_threads[0] = MACH_PORT_NULL;
601*1b191cb5SApple OSS Distributions 		child_task       = MACH_PORT_NULL;
602*1b191cb5SApple OSS Distributions 	}
603*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PINFO_PATH) {
604*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_threadwithpathinfo, child_pid, PROC_PIDTHREADPATHINFO, thread_addr[0]);
605*1b191cb5SApple OSS Distributions 	}
606*1b191cb5SApple OSS Distributions 
607*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PAI) {
608*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_archinfo, getpid(), PROC_PIDARCHINFO, 0);
609*1b191cb5SApple OSS Distributions 	}
610*1b191cb5SApple OSS Distributions 
611*1b191cb5SApple OSS Distributions 	vm_map_size_t map_tmp_sz = 0;
612*1b191cb5SApple OSS Distributions 	if ((proc_info_opts & PREGINFO) | (proc_info_opts & PREGINFO_PATH) | (proc_info_opts & PREGINFO_PATH_2) |
613*1b191cb5SApple OSS Distributions 	    (proc_info_opts & PREGINFO_PATH_3)) {
614*1b191cb5SApple OSS Distributions 		tmp_fd = CONF_TMP_FILE_OPEN(tmp_path);
615*1b191cb5SApple OSS Distributions 
616*1b191cb5SApple OSS Distributions 		/*
617*1b191cb5SApple OSS Distributions 		 * subsequent checks assume that this data does *not* stay
618*1b191cb5SApple OSS Distributions 		 * resident in the buffer cache, so set F_NOCACHE for direct
619*1b191cb5SApple OSS Distributions 		 * to storage writing. NOTE: this works if the writes are
620*1b191cb5SApple OSS Distributions 		 * page-aligned and > 2 pages in length.
621*1b191cb5SApple OSS Distributions 		 */
622*1b191cb5SApple OSS Distributions 		retval = fcntl(tmp_fd, F_NOCACHE, 1);
623*1b191cb5SApple OSS Distributions 		T_QUIET;
624*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(retval, "fcntl(%d, F_NOCACHE) failed", tmp_fd);
625*1b191cb5SApple OSS Distributions 
626*1b191cb5SApple OSS Distributions 		int npages_to_write = 10;
627*1b191cb5SApple OSS Distributions 		map_tmp_sz = (vm_map_size_t)npages_to_write * (vm_map_size_t)PAGE_SIZE;
628*1b191cb5SApple OSS Distributions 
629*1b191cb5SApple OSS Distributions 		/*
630*1b191cb5SApple OSS Distributions 		 * To make sure we don't go through the cached write paths in
631*1b191cb5SApple OSS Distributions 		 * the VM, we allocate a PAGE-aligned buffer that is > 2
632*1b191cb5SApple OSS Distributions 		 * pages, and perform a write of the entire buffer (not in
633*1b191cb5SApple OSS Distributions 		 * small page-aligned chunks).
634*1b191cb5SApple OSS Distributions 		 */
635*1b191cb5SApple OSS Distributions 		char *buf = valloc((size_t)map_tmp_sz);
636*1b191cb5SApple OSS Distributions 		T_QUIET;
637*1b191cb5SApple OSS Distributions 		T_ASSERT_NOTNULL(buf, "valloc(%d) failed", (int)map_tmp_sz);
638*1b191cb5SApple OSS Distributions 
639*1b191cb5SApple OSS Distributions 		memset(buf, 0x5, map_tmp_sz);
640*1b191cb5SApple OSS Distributions 		ssize_t bw = write(tmp_fd, buf, (size_t)map_tmp_sz);
641*1b191cb5SApple OSS Distributions 		T_QUIET;
642*1b191cb5SApple OSS Distributions 		T_ASSERT_GT_INT((int)bw, 0, "write(%d, buf, %d) failed", tmp_fd, (int)map_tmp_sz);
643*1b191cb5SApple OSS Distributions 
644*1b191cb5SApple OSS Distributions 		free(buf);
645*1b191cb5SApple OSS Distributions 
646*1b191cb5SApple OSS Distributions 		map_tmp_sz -= PAGE_SIZE;
647*1b191cb5SApple OSS Distributions 		map_tmp = mmap(0, (size_t)map_tmp_sz, PROT_WRITE, MAP_PRIVATE, tmp_fd, (off_t)PAGE_SIZE);
648*1b191cb5SApple OSS Distributions 		T_ASSERT_NE_PTR(map_tmp, MAP_FAILED, "mmap() for PROC_PIDREGIONINFO");
649*1b191cb5SApple OSS Distributions 
650*1b191cb5SApple OSS Distributions 		T_LOG("file: %s is opened as fd %d and mapped at %llx with size %lu", tmp_path, tmp_fd, (uint64_t)map_tmp,
651*1b191cb5SApple OSS Distributions 		    (unsigned long)PAGE_SIZE);
652*1b191cb5SApple OSS Distributions 
653*1b191cb5SApple OSS Distributions 		/*
654*1b191cb5SApple OSS Distributions 		 * unlink() the file to be nice, but do it _after_ we've
655*1b191cb5SApple OSS Distributions 		 * already flushed and mapped the file. This will ensure that
656*1b191cb5SApple OSS Distributions 		 * we don't end up writing to the buffer cache because the
657*1b191cb5SApple OSS Distributions 		 * file is unlinked.
658*1b191cb5SApple OSS Distributions 		 */
659*1b191cb5SApple OSS Distributions 		if (!(proc_info_opts & PREGINFO_PATH_3)) {
660*1b191cb5SApple OSS Distributions 			retval = unlink(tmp_path);
661*1b191cb5SApple OSS Distributions 			T_QUIET;
662*1b191cb5SApple OSS Distributions 			T_ASSERT_POSIX_SUCCESS(retval, "unlink(%s) failed", tmp_path);
663*1b191cb5SApple OSS Distributions 		}
664*1b191cb5SApple OSS Distributions 	}
665*1b191cb5SApple OSS Distributions 
666*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PREGINFO) {
667*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_regioninfo, getpid(), PROC_PIDREGIONINFO, map_tmp);
668*1b191cb5SApple OSS Distributions 		ret_structs[i] = map_tmp;
669*1b191cb5SApple OSS Distributions 		i++;
670*1b191cb5SApple OSS Distributions 		ret_structs[i] = (void *)(uintptr_t)map_tmp_sz;
671*1b191cb5SApple OSS Distributions 		i++;
672*1b191cb5SApple OSS Distributions 	}
673*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PREGINFO_PATH) {
674*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_regionwithpathinfo, getpid(), PROC_PIDREGIONPATHINFO, map_tmp);
675*1b191cb5SApple OSS Distributions 		ret_structs[i] = map_tmp;
676*1b191cb5SApple OSS Distributions 		i++;
677*1b191cb5SApple OSS Distributions 		ret_structs[i] = (void *)(uintptr_t)map_tmp_sz;
678*1b191cb5SApple OSS Distributions 		i++;
679*1b191cb5SApple OSS Distributions 	}
680*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PREGINFO_PATH_2) {
681*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_regionwithpathinfo, getpid(), PROC_PIDREGIONPATHINFO2, map_tmp);
682*1b191cb5SApple OSS Distributions 		ret_structs[i] = map_tmp;
683*1b191cb5SApple OSS Distributions 		i++;
684*1b191cb5SApple OSS Distributions 		ret_structs[i] = (void *)(uintptr_t)map_tmp_sz;
685*1b191cb5SApple OSS Distributions 		i++;
686*1b191cb5SApple OSS Distributions 	}
687*1b191cb5SApple OSS Distributions 
688*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PREGINFO_PATH_3) {
689*1b191cb5SApple OSS Distributions 		struct proc_regionwithpathinfo * preginfo_path = malloc(sizeof(struct proc_regionwithpathinfo));
690*1b191cb5SApple OSS Distributions 
691*1b191cb5SApple OSS Distributions 		retval = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDREGIONPATHINFO2, (uint64_t)map_tmp,
692*1b191cb5SApple OSS Distributions 		    (user_addr_t)preginfo_path, (uint32_t)sizeof(struct proc_regionwithpathinfo));
693*1b191cb5SApple OSS Distributions 
694*1b191cb5SApple OSS Distributions 		T_ASSERT_EQ_INT(retval, (int)sizeof(struct proc_regionwithpathinfo), "__proc_info call for PROC_PIDREGIONPATHINFO2");
695*1b191cb5SApple OSS Distributions 
696*1b191cb5SApple OSS Distributions 		T_LOG("preginfo_path.prp_vip.vip_vi.vi_fsid.val 0: %d", preginfo_path->prp_vip.vip_vi.vi_fsid.val[0]);
697*1b191cb5SApple OSS Distributions 		T_LOG("preginfo_path.prp_vip.vip_vi.vi_fsid.val 1: %d", preginfo_path->prp_vip.vip_vi.vi_fsid.val[1]);
698*1b191cb5SApple OSS Distributions 		ret_structs[3] = (void *)(uintptr_t)preginfo_path->prp_vip.vip_vi.vi_fsid.val[0];
699*1b191cb5SApple OSS Distributions 		ret_structs[4] = (void *)(uintptr_t)preginfo_path->prp_vip.vip_vi.vi_fsid.val[1];
700*1b191cb5SApple OSS Distributions 
701*1b191cb5SApple OSS Distributions 		retval = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDREGIONPATHINFO3,
702*1b191cb5SApple OSS Distributions 		    (uint64_t)preginfo_path->prp_vip.vip_vi.vi_fsid.val[0] +
703*1b191cb5SApple OSS Distributions 		    ((uint64_t)preginfo_path->prp_vip.vip_vi.vi_fsid.val[1] << 32),
704*1b191cb5SApple OSS Distributions 		    (user_addr_t)preginfo_path,
705*1b191cb5SApple OSS Distributions 		    (uint32_t)sizeof(struct proc_regionwithpathinfo));
706*1b191cb5SApple OSS Distributions 		T_ASSERT_EQ_INT(retval, (int)sizeof(struct proc_regionwithpathinfo), "__proc_info call for PROC_PIDREGIONPATHWITHINFO3");
707*1b191cb5SApple OSS Distributions 		ret_structs[0] = (void *)preginfo_path;
708*1b191cb5SApple OSS Distributions 		ret_structs[1] = (void *)map_tmp;
709*1b191cb5SApple OSS Distributions 		ret_structs[2] = (void *)(uintptr_t)map_tmp_sz;
710*1b191cb5SApple OSS Distributions 
711*1b191cb5SApple OSS Distributions 		retval = unlink(tmp_path);
712*1b191cb5SApple OSS Distributions 		T_QUIET;
713*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(retval, "unlink(%s) failed", preginfo_path->prp_vip.vip_path);
714*1b191cb5SApple OSS Distributions 	}
715*1b191cb5SApple OSS Distributions 
716*1b191cb5SApple OSS Distributions 	if (proc_info_opts & PVNINFO) {
717*1b191cb5SApple OSS Distributions 		PROC_INFO_CALL(proc_vnodepathinfo, getpid(), PROC_PIDVNODEPATHINFO, 0);
718*1b191cb5SApple OSS Distributions 	}
719*1b191cb5SApple OSS Distributions 
720*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
721*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
722*1b191cb5SApple OSS Distributions 	free(thread_addr);
723*1b191cb5SApple OSS Distributions 	thread_addr = NULL;
724*1b191cb5SApple OSS Distributions 	close(tmp_fd);
725*1b191cb5SApple OSS Distributions 	tmp_fd = -1;
726*1b191cb5SApple OSS Distributions }
727*1b191cb5SApple OSS Distributions 
728*1b191cb5SApple OSS Distributions static void
free_proc_info(void ** proc_info,int num)729*1b191cb5SApple OSS Distributions free_proc_info(void ** proc_info, int num)
730*1b191cb5SApple OSS Distributions {
731*1b191cb5SApple OSS Distributions 	for (int i = 0; i < num; i++) {
732*1b191cb5SApple OSS Distributions 		free(proc_info[i]);
733*1b191cb5SApple OSS Distributions 	}
734*1b191cb5SApple OSS Distributions 
735*1b191cb5SApple OSS Distributions 	return;
736*1b191cb5SApple OSS Distributions }
737*1b191cb5SApple OSS Distributions 
738*1b191cb5SApple OSS Distributions /*
739*1b191cb5SApple OSS Distributions  *	Start DECLs
740*1b191cb5SApple OSS Distributions  */
741*1b191cb5SApple OSS Distributions 
742*1b191cb5SApple OSS Distributions T_DECL(proc_info_listpids_all_pids,
743*1b191cb5SApple OSS Distributions     "proc_info API test to verify PROC_INFO_CALL_LISTPIDS",
744*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
745*1b191cb5SApple OSS Distributions {
746*1b191cb5SApple OSS Distributions 	/*
747*1b191cb5SApple OSS Distributions 	 * Get the value of nprocs with no buffer sent in
748*1b191cb5SApple OSS Distributions 	 */
749*1b191cb5SApple OSS Distributions 	int num_procs;
750*1b191cb5SApple OSS Distributions 	num_procs = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_ALL_PIDS, (uint32_t)getpid(), (uint32_t)0, (user_addr_t)0, (uint32_t)0);
751*1b191cb5SApple OSS Distributions 	T_ASSERT_GE_INT(num_procs, 1, "verify valid value for nprocs: %d", num_procs);
752*1b191cb5SApple OSS Distributions 
753*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(CONF_PROC_COUNT, proc_info_listpids_handler);
754*1b191cb5SApple OSS Distributions 
755*1b191cb5SApple OSS Distributions 	num_procs = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_ALL_PIDS, (uint32_t)getpid(), (uint32_t)0, (user_addr_t)0, (uint32_t)0);
756*1b191cb5SApple OSS Distributions 
757*1b191cb5SApple OSS Distributions 	int proc_count     = num_procs / (int)sizeof(pid_t);
758*1b191cb5SApple OSS Distributions 	int proc_count_all = num_procs / (int)sizeof(pid_t);
759*1b191cb5SApple OSS Distributions 	if (proc_count > (CONF_PROC_COUNT + 1)) {
760*1b191cb5SApple OSS Distributions 		proc_count = CONF_PROC_COUNT + 1;
761*1b191cb5SApple OSS Distributions 	}
762*1b191cb5SApple OSS Distributions 	pid_t * proc_ids = malloc(sizeof(pid_t) * (unsigned long)proc_count);
763*1b191cb5SApple OSS Distributions 	num_procs        = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_ALL_PIDS, (uint32_t)getpid(), (uint32_t)0, (user_addr_t)proc_ids,
764*1b191cb5SApple OSS Distributions 	    (int32_t)(proc_count * (int)sizeof(pid_t)));
765*1b191cb5SApple OSS Distributions 	num_procs = num_procs / (int)sizeof(pid_t);
766*1b191cb5SApple OSS Distributions 	T_ASSERT_GE_INT(num_procs, proc_count, "Valid number of pids obtained for PROC_ALL_PIDS.");
767*1b191cb5SApple OSS Distributions 
768*1b191cb5SApple OSS Distributions 	free(proc_ids);
769*1b191cb5SApple OSS Distributions 
770*1b191cb5SApple OSS Distributions 	/*
771*1b191cb5SApple OSS Distributions 	 * Grab list of all procs and make sure our spawned children are in the list.
772*1b191cb5SApple OSS Distributions 	 */
773*1b191cb5SApple OSS Distributions 
774*1b191cb5SApple OSS Distributions 	proc_ids  = malloc(sizeof(pid_t) * (unsigned long)proc_count_all);
775*1b191cb5SApple OSS Distributions 	num_procs = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_ALL_PIDS, (uint32_t)getpid(), (uint32_t)0, (user_addr_t)proc_ids,
776*1b191cb5SApple OSS Distributions 	    (int32_t)(proc_count_all * (int)sizeof(pid_t)));
777*1b191cb5SApple OSS Distributions 	num_procs = num_procs / (int)sizeof(pid_t);
778*1b191cb5SApple OSS Distributions 
779*1b191cb5SApple OSS Distributions 	int pid_match = 1;
780*1b191cb5SApple OSS Distributions 
781*1b191cb5SApple OSS Distributions 	for (int i = 0; i < (CONF_PROC_COUNT - 1); i++) {
782*1b191cb5SApple OSS Distributions 		for (int j = 0; j < num_procs; j++) {
783*1b191cb5SApple OSS Distributions 			if (proc_ids[j] == proc_config->child_pids[i]) {
784*1b191cb5SApple OSS Distributions 				break;
785*1b191cb5SApple OSS Distributions 			} else if (j == (num_procs - 1)) {
786*1b191cb5SApple OSS Distributions 				pid_match = 0;
787*1b191cb5SApple OSS Distributions 				break;
788*1b191cb5SApple OSS Distributions 			}
789*1b191cb5SApple OSS Distributions 		}
790*1b191cb5SApple OSS Distributions 
791*1b191cb5SApple OSS Distributions 		if (!pid_match) {
792*1b191cb5SApple OSS Distributions 			break;
793*1b191cb5SApple OSS Distributions 		}
794*1b191cb5SApple OSS Distributions 	}
795*1b191cb5SApple OSS Distributions 
796*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(pid_match, 1, "PROC_INFO_CALL_LISTPIDS contains our spawned children's pids");
797*1b191cb5SApple OSS Distributions 
798*1b191cb5SApple OSS Distributions 	free(proc_ids);
799*1b191cb5SApple OSS Distributions 
800*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
801*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
802*1b191cb5SApple OSS Distributions 
803*1b191cb5SApple OSS Distributions 	errno     = 0;
804*1b191cb5SApple OSS Distributions 	num_procs = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_ALL_PIDS, (uint32_t)getpid(), (uint32_t)0, (user_addr_t)proc_ids,
805*1b191cb5SApple OSS Distributions 	    (uint32_t)(sizeof(pid_t) - 1));
806*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_ERROR(errno, ENOMEM, "Valid proc_info behavior when bufsize < sizeof(pid_t).");
807*1b191cb5SApple OSS Distributions }
808*1b191cb5SApple OSS Distributions 
809*1b191cb5SApple OSS Distributions T_DECL(proc_info_listpids_pgrp_only,
810*1b191cb5SApple OSS Distributions     "proc_info API test to verify PROC_INFO_CALL_LISTPIDS",
811*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
812*1b191cb5SApple OSS Distributions {
813*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(CONF_PROC_COUNT, proc_info_listpids_handler);
814*1b191cb5SApple OSS Distributions 	T_LOG("Test to verify PROC_PGRP_ONLY returns correct value");
815*1b191cb5SApple OSS Distributions 	/*
816*1b191cb5SApple OSS Distributions 	 * The number of obtained pids depends on size of buffer.
817*1b191cb5SApple OSS Distributions 	 * count = childCount + 1(parent)
818*1b191cb5SApple OSS Distributions 	 * So, we set it to one more than expected to capture any error.
819*1b191cb5SApple OSS Distributions 	 */
820*1b191cb5SApple OSS Distributions 	int proc_count   = CONF_PROC_COUNT + 2;
821*1b191cb5SApple OSS Distributions 	pid_t * proc_ids = malloc(sizeof(*proc_ids) * (unsigned long)proc_count);
822*1b191cb5SApple OSS Distributions 	int num_procs    = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_PGRP_ONLY, (uint32_t)proc_config->proc_grp_id, (uint32_t)0,
823*1b191cb5SApple OSS Distributions 	    (user_addr_t)proc_ids, (int32_t)(proc_count * (int)sizeof(*proc_ids)));
824*1b191cb5SApple OSS Distributions 	num_procs = num_procs / (int)sizeof(pid_t);
825*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_INT(num_procs, CONF_PROC_COUNT + 1, "Valid number of pids obtained for PROC_PGRP_ONLY.");
826*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
827*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
828*1b191cb5SApple OSS Distributions 	free(proc_ids);
829*1b191cb5SApple OSS Distributions }
830*1b191cb5SApple OSS Distributions 
831*1b191cb5SApple OSS Distributions T_DECL(proc_info_listpids_ppid_only,
832*1b191cb5SApple OSS Distributions     "proc_info API test to verify PROC_INFO_CALL_LISTPIDS",
833*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
834*1b191cb5SApple OSS Distributions {
835*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(CONF_PROC_COUNT, proc_info_listpids_handler);
836*1b191cb5SApple OSS Distributions 	T_LOG("Test to verify PROC_PPID_ONLY returns correct value");
837*1b191cb5SApple OSS Distributions 	/*
838*1b191cb5SApple OSS Distributions 	 * Pass in the same (bigger) buffer but expect only the pids where ppid is pid of current proc.
839*1b191cb5SApple OSS Distributions 	 */
840*1b191cb5SApple OSS Distributions 	int proc_count   = CONF_PROC_COUNT + 2;
841*1b191cb5SApple OSS Distributions 	pid_t * proc_ids = malloc(sizeof(*proc_ids) * (unsigned long)proc_count);
842*1b191cb5SApple OSS Distributions 	int num_procs    = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_PPID_ONLY, (uint32_t)getpid(), (uint32_t)0, (user_addr_t)proc_ids,
843*1b191cb5SApple OSS Distributions 	    (int32_t)(proc_count * (int)sizeof(*proc_ids)));
844*1b191cb5SApple OSS Distributions 	num_procs = num_procs / (int)sizeof(pid_t);
845*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_INT(num_procs, CONF_PROC_COUNT, "Valid number of pids obtained for PROC_PPID_ONLY.");
846*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
847*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
848*1b191cb5SApple OSS Distributions 	free(proc_ids);
849*1b191cb5SApple OSS Distributions }
850*1b191cb5SApple OSS Distributions 
851*1b191cb5SApple OSS Distributions T_DECL(proc_info_listpids_uid_only,
852*1b191cb5SApple OSS Distributions     "proc_info API test to verify PROC_INFO_CALL_LISTPIDS",
853*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
854*1b191cb5SApple OSS Distributions {
855*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(CONF_PROC_COUNT, proc_info_listpids_handler);
856*1b191cb5SApple OSS Distributions 	T_LOG("Test to verify PROC_UID_ONLY returns correct value");
857*1b191cb5SApple OSS Distributions 	int proc_count   = CONF_PROC_COUNT + 2;
858*1b191cb5SApple OSS Distributions 	pid_t * proc_ids = malloc(sizeof(*proc_ids) * (unsigned long)proc_count);
859*1b191cb5SApple OSS Distributions 	send_action_to_child_processes(proc_config, ACT_CHANGE_UID);
860*1b191cb5SApple OSS Distributions 	usleep(10000);
861*1b191cb5SApple OSS Distributions 	int num_procs = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_UID_ONLY, CONF_UID_VAL, (uint32_t)0, (user_addr_t)proc_ids,
862*1b191cb5SApple OSS Distributions 	    (int32_t)(proc_count * (int)sizeof(*proc_ids)));
863*1b191cb5SApple OSS Distributions 	T_ASSERT_GE_ULONG((unsigned long)num_procs / sizeof(pid_t), (unsigned long)CONF_PROC_COUNT,
864*1b191cb5SApple OSS Distributions 	    "Valid number of pids obtained for PROC_UID_ONLY.");
865*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
866*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
867*1b191cb5SApple OSS Distributions 	free(proc_ids);
868*1b191cb5SApple OSS Distributions }
869*1b191cb5SApple OSS Distributions 
870*1b191cb5SApple OSS Distributions T_DECL(proc_info_listpids_ruid_only,
871*1b191cb5SApple OSS Distributions     "proc_info API test to verify PROC_INFO_CALL_LISTPIDS",
872*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
873*1b191cb5SApple OSS Distributions {
874*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(CONF_PROC_COUNT, proc_info_listpids_handler);
875*1b191cb5SApple OSS Distributions 	T_LOG("Test to verify PROC_RUID_ONLY returns correct value");
876*1b191cb5SApple OSS Distributions 	int proc_count   = CONF_PROC_COUNT + 2;
877*1b191cb5SApple OSS Distributions 	pid_t * proc_ids = malloc(sizeof(*proc_ids) * (unsigned long)proc_count);
878*1b191cb5SApple OSS Distributions 	send_action_to_child_processes(proc_config, ACT_CHANGE_RUID);
879*1b191cb5SApple OSS Distributions 	usleep(10000);
880*1b191cb5SApple OSS Distributions 	int num_procs = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_RUID_ONLY, CONF_RUID_VAL, (uint32_t)0, (user_addr_t)proc_ids,
881*1b191cb5SApple OSS Distributions 	    (int32_t)(proc_count * (int)sizeof(*proc_ids)));
882*1b191cb5SApple OSS Distributions 	T_ASSERT_GE_ULONG((unsigned long)num_procs / sizeof(pid_t), (unsigned long)CONF_PROC_COUNT,
883*1b191cb5SApple OSS Distributions 	    "Valid number of pids obtained for PROC_RUID_ONLY.");
884*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
885*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
886*1b191cb5SApple OSS Distributions 	free(proc_ids);
887*1b191cb5SApple OSS Distributions }
888*1b191cb5SApple OSS Distributions 
889*1b191cb5SApple OSS Distributions T_DECL(proc_info_listpids_tty_only,
890*1b191cb5SApple OSS Distributions     "proc_info API test to verify PROC_INFO_CALL_LISTPIDS",
891*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
892*1b191cb5SApple OSS Distributions {
893*1b191cb5SApple OSS Distributions 	int ret = isatty(STDOUT_FILENO);
894*1b191cb5SApple OSS Distributions 	if (ret != 1) {
895*1b191cb5SApple OSS Distributions 		T_SKIP("Not connected to tty...skipping test");
896*1b191cb5SApple OSS Distributions 	}
897*1b191cb5SApple OSS Distributions 
898*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(CONF_PROC_COUNT, proc_info_listpids_handler);
899*1b191cb5SApple OSS Distributions 
900*1b191cb5SApple OSS Distributions 	T_LOG("Test to verify PROC_TTY_ONLY returns correct value");
901*1b191cb5SApple OSS Distributions 	int proc_count   = CONF_PROC_COUNT + 2;
902*1b191cb5SApple OSS Distributions 	pid_t * proc_ids = malloc(sizeof(*proc_ids) * (unsigned long)proc_count);
903*1b191cb5SApple OSS Distributions 	int num_procs    = __proc_info(PROC_INFO_CALL_LISTPIDS, PROC_TTY_ONLY, get_tty_dev(), (uint32_t)0, (user_addr_t)proc_ids,
904*1b191cb5SApple OSS Distributions 	    (int32_t)(proc_count * (int)sizeof(*proc_ids)));
905*1b191cb5SApple OSS Distributions 	num_procs = num_procs / (int)sizeof(pid_t);
906*1b191cb5SApple OSS Distributions 	T_ASSERT_GE_INT(num_procs, 0, "Valid number of pids returned by PROC_TTY_ONLY.");
907*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
908*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
909*1b191cb5SApple OSS Distributions 	free(proc_ids);
910*1b191cb5SApple OSS Distributions }
911*1b191cb5SApple OSS Distributions 
912*1b191cb5SApple OSS Distributions /*
913*1b191cb5SApple OSS Distributions  * Most of the following PROC_INFO_CALL_PIDINFO tests rely on a helper function (proc_info_caller) to make the necessary proc_info
914*1b191cb5SApple OSS Distributions  * calls on their behalf
915*1b191cb5SApple OSS Distributions  * In a previous iteration, these tests were all in one giant T_DECL and the helper function handles inter-DECL dependencies such as
916*1b191cb5SApple OSS Distributions  * a proc_info call relying on the results of a previous proc_info call or an assumed state that a child should be in.
917*1b191cb5SApple OSS Distributions  */
918*1b191cb5SApple OSS Distributions 
919*1b191cb5SApple OSS Distributions T_DECL(proc_info_pidinfo_proc_piduniqidentifierinfo,
920*1b191cb5SApple OSS Distributions     "Test to identify PROC_PIDUNIQIDENTIFIERINFO returns correct unique identifiers for process",
921*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
922*1b191cb5SApple OSS Distributions {
923*1b191cb5SApple OSS Distributions 	void * proc_info[2];
924*1b191cb5SApple OSS Distributions 	proc_info_caller(P_UNIQIDINFO | C_UNIQIDINFO, proc_info, NULL);
925*1b191cb5SApple OSS Distributions 	struct proc_uniqidentifierinfo * p_uniqidinfo = (struct proc_uniqidentifierinfo *)proc_info[0];
926*1b191cb5SApple OSS Distributions 	struct proc_uniqidentifierinfo * c_uniqidinfo = (struct proc_uniqidentifierinfo *)proc_info[1];
927*1b191cb5SApple OSS Distributions 
928*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_ULLONG(c_uniqidinfo->p_uniqueid, p_uniqidinfo->p_uniqueid, "p_uniqueid not unique for the process");
929*1b191cb5SApple OSS Distributions 
930*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < 16; i++) {
931*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_UCHAR(c_uniqidinfo->p_uuid[i], p_uniqidinfo->p_uuid[i], "p_uuid should be the same unique id");
932*1b191cb5SApple OSS Distributions 	}
933*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(c_uniqidinfo->p_puniqueid, p_uniqidinfo->p_uniqueid,
934*1b191cb5SApple OSS Distributions 	    "p_puniqueid of child should be same as p_uniqueid for parent");
935*1b191cb5SApple OSS Distributions 
936*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 2);
937*1b191cb5SApple OSS Distributions }
938*1b191cb5SApple OSS Distributions 
939*1b191cb5SApple OSS Distributions T_DECL(proc_info_pidinfo_proc_pidtbsdinfo,
940*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDTBSDINFO returns valid information about the process",
941*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
942*1b191cb5SApple OSS Distributions {
943*1b191cb5SApple OSS Distributions 	void * proc_info[2];
944*1b191cb5SApple OSS Distributions 	int child_pid = 0;
945*1b191cb5SApple OSS Distributions 	proc_info_caller(PBSD_OLD | PBSD, proc_info, &child_pid);
946*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo * pbsd_old = (struct proc_bsdinfo *)proc_info[0];
947*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo * pbsd     = (struct proc_bsdinfo *)proc_info[1];
948*1b191cb5SApple OSS Distributions 
949*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((unsigned int)SRUN, pbsd->pbi_status, "PROC_PIDTBSDINFO shows Correct status");
950*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(0U, pbsd->pbi_xstatus, "PROC_PIDTBSDINFO show Correct xstatus (exit status)");
951*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_pid, (unsigned int)child_pid, "PROC_PIDTBSDINFO returns valid pid");
952*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_ppid, (unsigned int)getpid(), "PROC_PIDTBSDINFO returns valid ppid");
953*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_uid, CONF_RUID_VAL, "PROC_PIDTBSDINFO returns valid uid");
954*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_gid, CONF_GID_VAL, "PROC_PIDTBSDINFO returns valid gid");
955*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_ruid, 0U, "PROC_PIDTBSDINFO returns valid ruid");
956*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_rgid, CONF_GID_VAL, "PROC_PIDTBSDINFO returns valid rgid");
957*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_svuid, CONF_RUID_VAL, "PROC_PIDTBSDINFO returns valid svuid");
958*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_svgid, CONF_GID_VAL, "PROC_PIDTBSDINFO returns valid svgid");
959*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_nice, CONF_NICE_VAL, "PROC_PIDTBSDINFO returns valid nice value");
960*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pbsd->pbi_comm, CONF_CMD_NAME, "PROC_PIDTBSDINFO returns valid p_comm name");
961*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pbsd->pbi_name, CONF_CMD_NAME, "PROC_PIDTBSDINFO returns valid p_name name");
962*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_flags, (pbsd_old->pbi_flags | PROC_FLAG_PSUGID), "PROC_PIDTBSDINFO returns valid flags");
963*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_nfiles, pbsd_old->pbi_nfiles, "PROC_PIDTBSDINFO returned valid pbi_nfiles");
964*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_pgid, (uint32_t)getpgid(getpid()), "PROC_PIDTBSDINFO returned valid pbi_pgid");
965*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd->pbi_pjobc, pbsd->pbi_pjobc, "PROC_PIDTBSDINFO returned valid pbi_pjobc");
966*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(pbsd->e_tdev, 0U, "PROC_PIDTBSDINFO returned valid e_tdev");
967*1b191cb5SApple OSS Distributions 
968*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 2);
969*1b191cb5SApple OSS Distributions }
970*1b191cb5SApple OSS Distributions 
971*1b191cb5SApple OSS Distributions T_DECL(proc_info_pidt_shortbsdinfo,
972*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDT_SHORTBSDINFO returns valid information about the process",
973*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
974*1b191cb5SApple OSS Distributions {
975*1b191cb5SApple OSS Distributions 	void * proc_info[2];
976*1b191cb5SApple OSS Distributions 	int child_pid = 0;
977*1b191cb5SApple OSS Distributions 	proc_info_caller(PBSD | PBSD_SHORT, proc_info, &child_pid);
978*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo * pbsd            = (struct proc_bsdinfo *)proc_info[0];
979*1b191cb5SApple OSS Distributions 	struct proc_bsdshortinfo * pbsd_short = (struct proc_bsdshortinfo *)proc_info[1];
980*1b191cb5SApple OSS Distributions 
981*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_pid, (unsigned int)child_pid, "PROC_PIDT_SHORTBSDINFO returns valid pid");
982*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_ppid, (unsigned int)getpid(), "PROC_PIDT_SHORTBSDINFO returns valid ppid");
983*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_pgid, (uint32_t)getpgid(getpid()), "PROC_PIDT_SHORTBSDINFO returned valid pbi_pgid");
984*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((unsigned int)SRUN, pbsd_short->pbsi_status, "PROC_PIDT_SHORTBSDINFO shows Correct status");
985*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pbsd_short->pbsi_comm, CONF_CMD_NAME, "PROC_PIDT_SHORTBSDINFO returns valid p_comm name");
986*1b191cb5SApple OSS Distributions 	/*
987*1b191cb5SApple OSS Distributions 	 * The short variant returns all flags except session flags, hence ignoring them here.
988*1b191cb5SApple OSS Distributions 	 */
989*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_flags, (pbsd->pbi_flags & (unsigned int)(~PROC_FLAG_CTTY)),
990*1b191cb5SApple OSS Distributions 	    "PROC_PIDT_SHORTBSDINFO returns valid flags");
991*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_uid, CONF_RUID_VAL, "PROC_PIDT_SHORTBSDINFO returns valid uid");
992*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_gid, CONF_GID_VAL, "PROC_PIDT_SHORTBSDINFO returns valid gid");
993*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_ruid, 0U, "PROC_PIDT_SHORTBSDINFO returns valid ruid");
994*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_svuid, CONF_RUID_VAL, "PROC_PIDT_SHORTBSDINFO returns valid svuid");
995*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_short->pbsi_svgid, CONF_GID_VAL, "PROC_PIDT_SHORTBSDINFO returns valid svgid");
996*1b191cb5SApple OSS Distributions 
997*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 2);
998*1b191cb5SApple OSS Distributions }
999*1b191cb5SApple OSS Distributions 
1000*1b191cb5SApple OSS Distributions T_DECL(proc_info_pidt_bsdinfowithuniqid,
1001*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDT_BSDINFOWITHUNIQID returns valid information about the process",
1002*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1003*1b191cb5SApple OSS Distributions {
1004*1b191cb5SApple OSS Distributions 	void * proc_info[4];
1005*1b191cb5SApple OSS Distributions 	int child_pid = 0;
1006*1b191cb5SApple OSS Distributions 	proc_info_caller(P_UNIQIDINFO | PBSD_OLD | PBSD | PBSD_UNIQID, proc_info, &child_pid);
1007*1b191cb5SApple OSS Distributions 	struct proc_uniqidentifierinfo * p_uniqidinfo = (struct proc_uniqidentifierinfo *)proc_info[0];
1008*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo * pbsd_old                = (struct proc_bsdinfo *)proc_info[1];
1009*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo * pbsd                    = (struct proc_bsdinfo *)proc_info[2];
1010*1b191cb5SApple OSS Distributions 	struct proc_bsdinfowithuniqid * pbsd_uniqid   = (struct proc_bsdinfowithuniqid *)proc_info[3];
1011*1b191cb5SApple OSS Distributions 
1012*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((unsigned int)SRUN, pbsd->pbi_status, "PROC_PIDT_BSDINFOWITHUNIQID shows Correct status");
1013*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(0U, pbsd->pbi_xstatus, "PROC_PIDT_BSDINFOWITHUNIQID show Correct xstatus");
1014*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_pid, (unsigned int)child_pid, "PROC_PIDT_BSDINFOWITHUNIQID returns valid pid");
1015*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_ppid, (unsigned int)getpid(), "PROC_PIDT_BSDINFOWITHUNIQID returns valid ppid");
1016*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_uid, CONF_RUID_VAL, "PROC_PIDT_BSDINFOWITHUNIQID returns valid uid");
1017*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_gid, CONF_GID_VAL, "PROC_PIDT_BSDINFOWITHUNIQID returns valid gid");
1018*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_ruid, 0U, "PROC_PIDT_BSDINFOWITHUNIQID returns valid ruid");
1019*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_rgid, CONF_GID_VAL, "PROC_PIDT_BSDINFOWITHUNIQID returns valid rgid");
1020*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_svuid, CONF_RUID_VAL, "PROC_PIDT_BSDINFOWITHUNIQID returns valid svuid");
1021*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_svgid, CONF_GID_VAL, "PROC_PIDT_BSDINFOWITHUNIQID returns valid svgid");
1022*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_nice, CONF_NICE_VAL, "PROC_PIDT_BSDINFOWITHUNIQID returns valid nice value");
1023*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pbsd_uniqid->pbsd.pbi_comm, CONF_CMD_NAME, "PROC_PIDT_BSDINFOWITHUNIQID returns valid p_comm name");
1024*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pbsd_uniqid->pbsd.pbi_name, CONF_CMD_NAME, "PROC_PIDT_BSDINFOWITHUNIQID returns valid p_name name");
1025*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_flags, (pbsd_old->pbi_flags | PROC_FLAG_PSUGID),
1026*1b191cb5SApple OSS Distributions 	    "PROC_PIDT_BSDINFOWITHUNIQID returns valid flags");
1027*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_nfiles, pbsd_old->pbi_nfiles, "PROC_PIDT_BSDINFOWITHUNIQID returned valid pbi_nfiles");
1028*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_pgid, (uint32_t)getpgid(getpid()),
1029*1b191cb5SApple OSS Distributions 	    "PROC_PIDT_BSDINFOWITHUNIQID returned valid pbi_pgid");
1030*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pbsd_uniqid->pbsd.pbi_pjobc, pbsd->pbi_pjobc, "PROC_PIDT_BSDINFOWITHUNIQID returned valid pbi_pjobc");
1031*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(pbsd_uniqid->pbsd.e_tdev, 0U, "PROC_PIDT_BSDINFOWITHUNIQID returned valid e_tdev");
1032*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_ULLONG(pbsd_uniqid->p_uniqidentifier.p_uniqueid, p_uniqidinfo->p_uniqueid,
1033*1b191cb5SApple OSS Distributions 	    "PROC_PIDT_BSDINFOWITHUNIQID returned valid p_uniqueid");
1034*1b191cb5SApple OSS Distributions 	for (int i = 0; i < 16; i++) {
1035*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_UCHAR(pbsd_uniqid->p_uniqidentifier.p_uuid[i], p_uniqidinfo->p_uuid[i],
1036*1b191cb5SApple OSS Distributions 		    "PROC_PIDT_BSDINFOWITHUNIQID reported valid p_uniqueid");
1037*1b191cb5SApple OSS Distributions 	}
1038*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(pbsd_uniqid->p_uniqidentifier.p_puniqueid, p_uniqidinfo->p_uniqueid,
1039*1b191cb5SApple OSS Distributions 	    "p_puniqueid of child should be same as p_uniqueid for parent");
1040*1b191cb5SApple OSS Distributions 
1041*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 4);
1042*1b191cb5SApple OSS Distributions }
1043*1b191cb5SApple OSS Distributions 
1044*1b191cb5SApple OSS Distributions static void
_expect_increasing_taskinfo_times(const char * name,struct proc_taskinfo * early,struct proc_taskinfo * late)1045*1b191cb5SApple OSS Distributions _expect_increasing_taskinfo_times(const char *name, struct proc_taskinfo *early,
1046*1b191cb5SApple OSS Distributions     struct proc_taskinfo *late)
1047*1b191cb5SApple OSS Distributions {
1048*1b191cb5SApple OSS Distributions 	if (has_user_system_times()) {
1049*1b191cb5SApple OSS Distributions 		T_EXPECT_GT(late->pti_total_system, early->pti_total_system,
1050*1b191cb5SApple OSS Distributions 		    "%s returned increasing pti_total_system time", name);
1051*1b191cb5SApple OSS Distributions 		T_EXPECT_GT(late->pti_threads_system, early->pti_threads_system,
1052*1b191cb5SApple OSS Distributions 		    "%s returned increasing pti_threads_system time", name);
1053*1b191cb5SApple OSS Distributions 	}
1054*1b191cb5SApple OSS Distributions 
1055*1b191cb5SApple OSS Distributions 	T_EXPECT_GT(late->pti_threads_user, early->pti_threads_user,
1056*1b191cb5SApple OSS Distributions 	    "%s returned increasing pti_threads_user time", name);
1057*1b191cb5SApple OSS Distributions 	T_EXPECT_GT(late->pti_total_user, early->pti_total_user,
1058*1b191cb5SApple OSS Distributions 	    "%s returned increasing pti_total_user time", name);
1059*1b191cb5SApple OSS Distributions }
1060*1b191cb5SApple OSS Distributions 
1061*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidtask_info,
1062*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDTASKINFO returns valid information about the process",
1063*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1064*1b191cb5SApple OSS Distributions {
1065*1b191cb5SApple OSS Distributions 	void * proc_info[2];
1066*1b191cb5SApple OSS Distributions 	proc_info_caller(P_TASK_INFO | P_TASK_INFO_NEW, proc_info, NULL);
1067*1b191cb5SApple OSS Distributions 	struct proc_taskinfo * p_task_info     = (struct proc_taskinfo *)proc_info[0];
1068*1b191cb5SApple OSS Distributions 	struct proc_taskinfo * p_task_info_new = (struct proc_taskinfo *)proc_info[1];
1069*1b191cb5SApple OSS Distributions 
1070*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG((p_task_info_new->pti_virtual_size - p_task_info->pti_virtual_size), (unsigned long long)PAGE_SIZE,
1071*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_virtual_size");
1072*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG((p_task_info_new->pti_resident_size - p_task_info->pti_resident_size), (unsigned long long)PAGE_SIZE,
1073*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_resident_size");
1074*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(p_task_info_new->pti_policy, POLICY_TIMESHARE, "PROC_PIDTASKINFO returned valid value for pti_policy");
1075*1b191cb5SApple OSS Distributions 	_expect_increasing_taskinfo_times("PROC_PIDTASKINFO", p_task_info, p_task_info_new);
1076*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((p_task_info_new->pti_faults - p_task_info->pti_faults), 1,
1077*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_faults");
1078*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((p_task_info_new->pti_cow_faults - p_task_info->pti_cow_faults), 1,
1079*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_cow_faults");
1080*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((p_task_info_new->pti_syscalls_mach - p_task_info->pti_syscalls_mach), 0,
1081*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_syscalls_mach");
1082*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((p_task_info_new->pti_syscalls_unix - p_task_info->pti_syscalls_unix), 2,
1083*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_syscalls_unix");
1084*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT((p_task_info_new->pti_messages_sent - p_task_info->pti_messages_sent), 0,
1085*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_messages_sent");
1086*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT((p_task_info_new->pti_messages_received - p_task_info->pti_messages_received), 0,
1087*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_messages_received");
1088*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(p_task_info_new->pti_priority, p_task_info->pti_priority,
1089*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKINFO returned valid value for pti_priority");
1090*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(p_task_info_new->pti_threadnum, 1, "PROC_PIDTASKINFO returned valid value for pti_threadnum");
1091*1b191cb5SApple OSS Distributions 
1092*1b191cb5SApple OSS Distributions 	if (p_task_info_new->pti_threadnum > 1) {
1093*1b191cb5SApple OSS Distributions 		T_LOG("WARN: PROC_PIDTASKINFO returned threadnum greater than 1");
1094*1b191cb5SApple OSS Distributions 	}
1095*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(p_task_info_new->pti_numrunning, 0, "PROC_PIDTASKINFO returned valid value for pti_numrunning");
1096*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(p_task_info_new->pti_pageins, 0, "PROC_PIDTASKINFO returned valid value for pti_pageins");
1097*1b191cb5SApple OSS Distributions 
1098*1b191cb5SApple OSS Distributions 	if (p_task_info_new->pti_pageins > 0) {
1099*1b191cb5SApple OSS Distributions 		T_LOG("WARN: PROC_PIDTASKINFO returned pageins greater than 0");
1100*1b191cb5SApple OSS Distributions 	}
1101*1b191cb5SApple OSS Distributions 
1102*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(p_task_info_new->pti_csw, p_task_info->pti_csw, "PROC_PIDTASKINFO returned valid value for pti_csw");
1103*1b191cb5SApple OSS Distributions 
1104*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 2);
1105*1b191cb5SApple OSS Distributions }
1106*1b191cb5SApple OSS Distributions 
1107*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidtaskallinfo,
1108*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDTASKALLINFO returns valid information about the process",
1109*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1110*1b191cb5SApple OSS Distributions {
1111*1b191cb5SApple OSS Distributions 	void * proc_info[4];
1112*1b191cb5SApple OSS Distributions 	int child_pid = 0;
1113*1b191cb5SApple OSS Distributions 	proc_info_caller(PBSD | PBSD_OLD | P_TASK_INFO | PALL, proc_info, &child_pid);
1114*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo * pbsd         = (struct proc_bsdinfo *)proc_info[0];
1115*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo * pbsd_old     = (struct proc_bsdinfo *)proc_info[1];
1116*1b191cb5SApple OSS Distributions 	struct proc_taskinfo * p_task_info = (struct proc_taskinfo *)proc_info[2];
1117*1b191cb5SApple OSS Distributions 	struct proc_taskallinfo * pall     = (struct proc_taskallinfo *)proc_info[3];
1118*1b191cb5SApple OSS Distributions 
1119*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((unsigned int)SRUN, pbsd->pbi_status, "PROC_PIDTASKALLINFO shows Correct status");
1120*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(0U, pbsd->pbi_xstatus, "PROC_PIDTASKALLINFO show Correct xstatus");
1121*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_pid, (unsigned int)child_pid, "PROC_PIDTASKALLINFO returns valid pid");
1122*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_ppid, (unsigned int)getpid(), "PROC_PIDTASKALLINFO returns valid ppid");
1123*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_uid, CONF_RUID_VAL, "PROC_PIDTASKALLINFO returns valid uid");
1124*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_gid, CONF_GID_VAL, "PROC_PIDTASKALLINFO returns valid gid");
1125*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_ruid, 0U, "PROC_PIDTASKALLINFO returns valid ruid");
1126*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_rgid, CONF_GID_VAL, "PROC_PIDTASKALLINFO returns valid rgid");
1127*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_svuid, CONF_RUID_VAL, "PROC_PIDTASKALLINFO returns valid svuid");
1128*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_svgid, CONF_GID_VAL, "PROC_PIDTASKALLINFO returns valid svgid");
1129*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pall->pbsd.pbi_nice, CONF_NICE_VAL, "PROC_PIDTASKALLINFO returns valid nice value");
1130*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pall->pbsd.pbi_comm, CONF_CMD_NAME, "PROC_PIDTASKALLINFO returns valid p_comm name");
1131*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pall->pbsd.pbi_name, CONF_CMD_NAME, "PROC_PIDTASKALLINFO returns valid p_name name");
1132*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_flags, (pbsd_old->pbi_flags | PROC_FLAG_PSUGID), "PROC_PIDTASKALLINFO returns valid flags");
1133*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_nfiles, pbsd_old->pbi_nfiles, "PROC_PIDTASKALLINFO returned valid pbi_nfiles");
1134*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_pgid, (uint32_t)getpgid(getpid()), "PROC_PIDTASKALLINFO returned valid pbi_pgid");
1135*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pall->pbsd.pbi_pjobc, pbsd->pbi_pjobc, "PROC_PIDTASKALLINFO returned valid pbi_pjobc");
1136*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(pall->pbsd.e_tdev, 0U, "PROC_PIDTASKALLINFO returned valid e_tdev");
1137*1b191cb5SApple OSS Distributions 
1138*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG((pall->ptinfo.pti_virtual_size - p_task_info->pti_virtual_size), (unsigned long long)PAGE_SIZE,
1139*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_virtual_size");
1140*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG((pall->ptinfo.pti_resident_size - p_task_info->pti_resident_size), (unsigned long long)PAGE_SIZE,
1141*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_resident_size");
1142*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pall->ptinfo.pti_policy, POLICY_TIMESHARE, "PROC_PIDTASKALLINFO returned valid value for pti_policy");
1143*1b191cb5SApple OSS Distributions 	_expect_increasing_taskinfo_times("PROC_PIDTASKALLLINFO", p_task_info, &pall->ptinfo);
1144*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((pall->ptinfo.pti_faults - p_task_info->pti_faults), 1,
1145*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_faults");
1146*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((pall->ptinfo.pti_cow_faults - p_task_info->pti_cow_faults), 1,
1147*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_cow_faults");
1148*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((pall->ptinfo.pti_syscalls_mach - p_task_info->pti_syscalls_mach), 0,
1149*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_syscalls_mach");
1150*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT((pall->ptinfo.pti_syscalls_unix - p_task_info->pti_syscalls_unix), 2,
1151*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_syscalls_unix");
1152*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT((pall->ptinfo.pti_messages_sent - p_task_info->pti_messages_sent), 0,
1153*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_messages_sent");
1154*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT((pall->ptinfo.pti_messages_received - p_task_info->pti_messages_received), 0,
1155*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_messages_received");
1156*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pall->ptinfo.pti_priority, p_task_info->pti_priority,
1157*1b191cb5SApple OSS Distributions 	    "PROC_PIDTASKALLINFO returned valid value for pti_priority");
1158*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(pall->ptinfo.pti_threadnum, 1, "PROC_PIDTASKALLINFO returned valid value for pti_threadnum");
1159*1b191cb5SApple OSS Distributions 	if (pall->ptinfo.pti_threadnum > 1) {
1160*1b191cb5SApple OSS Distributions 		T_LOG("WARN: PROC_PIDTASKALLINFO returned threadnum greater than 1");
1161*1b191cb5SApple OSS Distributions 	}
1162*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(pall->ptinfo.pti_numrunning, 0, "PROC_PIDTASKALLINFO returned valid value for pti_numrunning");
1163*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(pall->ptinfo.pti_pageins, 0, "PROC_PIDTASKALLINFO returned valid value for pti_pageins");
1164*1b191cb5SApple OSS Distributions 	if (pall->ptinfo.pti_pageins > 0) {
1165*1b191cb5SApple OSS Distributions 		T_LOG("WARN: PROC_PIDTASKALLINFO returned pageins greater than 0");
1166*1b191cb5SApple OSS Distributions 	}
1167*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(pall->ptinfo.pti_csw, p_task_info->pti_csw, "PROC_PIDTASKALLINFO returned valid value for pti_csw");
1168*1b191cb5SApple OSS Distributions 
1169*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 4);
1170*1b191cb5SApple OSS Distributions }
1171*1b191cb5SApple OSS Distributions 
1172*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidlistthreads,
1173*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDLISTTHREADS returns valid information about process",
1174*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1175*1b191cb5SApple OSS Distributions {
1176*1b191cb5SApple OSS Distributions 	void * proc_info[1];
1177*1b191cb5SApple OSS Distributions 	proc_info_caller(THREAD_ADDR, proc_info, NULL);
1178*1b191cb5SApple OSS Distributions }
1179*1b191cb5SApple OSS Distributions 
1180*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidthreadinfo,
1181*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDTHREADINFO returns valid information about the process",
1182*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1183*1b191cb5SApple OSS Distributions {
1184*1b191cb5SApple OSS Distributions 	void * proc_info[2];
1185*1b191cb5SApple OSS Distributions 	int child_pid = 0;
1186*1b191cb5SApple OSS Distributions 	proc_info_caller(PTHINFO_OLD | PTHINFO, proc_info, &child_pid);
1187*1b191cb5SApple OSS Distributions 	struct proc_threadinfo * pthinfo_old = (struct proc_threadinfo *)proc_info[0];
1188*1b191cb5SApple OSS Distributions 	struct proc_threadinfo * pthinfo     = (struct proc_threadinfo *)proc_info[1];
1189*1b191cb5SApple OSS Distributions 
1190*1b191cb5SApple OSS Distributions 	T_EXPECT_GT_ULLONG((pthinfo->pth_user_time - pthinfo_old->pth_user_time), 0ULL,
1191*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADINFO returns valid value for pth_user_time");
1192*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG((pthinfo->pth_system_time - pthinfo_old->pth_system_time), 0ULL,
1193*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADINFO returns valid value for pth_system_time");
1194*1b191cb5SApple OSS Distributions 	/*
1195*1b191cb5SApple OSS Distributions 	 * This is the scaled cpu usage percentage, since we are not
1196*1b191cb5SApple OSS Distributions 	 * doing a really long CPU bound task, it is (nearly) zero
1197*1b191cb5SApple OSS Distributions 	 */
1198*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(pthinfo->pth_cpu_usage, 0, "PROC_PIDTHREADINFO returns valid value for pth_cpu_usage");
1199*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo->pth_policy, POLICY_TIMESHARE, "PROC_PIDTHREADINFO returns valid value for pth_policy");
1200*1b191cb5SApple OSS Distributions 	if (!(pthinfo->pth_run_state == TH_STATE_WAITING) && !(pthinfo->pth_run_state == TH_STATE_RUNNING)) {
1201*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_INT(pthinfo->pth_run_state, -1, "PROC_PIDTHREADINFO returns valid value for pth_run_state");
1202*1b191cb5SApple OSS Distributions 	}
1203*1b191cb5SApple OSS Distributions 	/*
1204*1b191cb5SApple OSS Distributions 	 * This value is hardcoded to 0 in the source, hence it will always
1205*1b191cb5SApple OSS Distributions 	 * unconditionally return 0
1206*1b191cb5SApple OSS Distributions 	 */
1207*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo->pth_sleep_time, 0, "PROC_PIDTHREADINFO returns valid value for pth_sleep_time");
1208*1b191cb5SApple OSS Distributions 	T_EXPECT_LE_INT(pthinfo->pth_curpri, (BASEPRI_DEFAULT - CONF_NICE_VAL),
1209*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADINFO returns valid value for pth_curpri");
1210*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo->pth_priority, (BASEPRI_DEFAULT - CONF_NICE_VAL),
1211*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADINFO returns valid value for pth_priority");
1212*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo->pth_maxpriority, MAXPRI_USER, "PROC_PIDTHREADINFO returns valid value for pth_maxpriority");
1213*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pthinfo->pth_name, CONF_THREAD_NAME, "PROC_PIDTHREADINFO returns valid value for pth_name");
1214*1b191cb5SApple OSS Distributions 
1215*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 2);
1216*1b191cb5SApple OSS Distributions }
1217*1b191cb5SApple OSS Distributions 
1218*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_threadid64info,
1219*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDTHREADID64INFO returns valid information about the process",
1220*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1221*1b191cb5SApple OSS Distributions {
1222*1b191cb5SApple OSS Distributions 	void * proc_info[2];
1223*1b191cb5SApple OSS Distributions 	proc_info_caller(PTHINFO | PTHINFO_64, proc_info, NULL);
1224*1b191cb5SApple OSS Distributions 	struct proc_threadinfo pthinfo    = *((struct proc_threadinfo *)proc_info[0]);
1225*1b191cb5SApple OSS Distributions 	struct proc_threadinfo pthinfo_64 = *((struct proc_threadinfo *)proc_info[1]);
1226*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG(pthinfo_64.pth_user_time, pthinfo.pth_user_time,
1227*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADID64INFO returns valid value for pth_user_time");
1228*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG(pthinfo_64.pth_system_time, pthinfo.pth_system_time,
1229*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADID64INFO returns valid value for pth_system_time");
1230*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(pthinfo_64.pth_cpu_usage, pthinfo.pth_cpu_usage,
1231*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADID64INFO returns valid value for pth_cpu_usage");
1232*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo_64.pth_policy, POLICY_TIMESHARE, "PROC_PIDTHREADID64INFO returns valid value for pth_policy");
1233*1b191cb5SApple OSS Distributions 	if (!(pthinfo_64.pth_run_state == TH_STATE_WAITING) && !(pthinfo_64.pth_run_state == TH_STATE_RUNNING)) {
1234*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_INT(pthinfo_64.pth_run_state, -1, "PROC_PIDTHREADID64INFO returns valid value for pth_run_state");
1235*1b191cb5SApple OSS Distributions 	}
1236*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo_64.pth_sleep_time, 0, "PROC_PIDTHREADID64INFO returns valid value for pth_sleep_time");
1237*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo_64.pth_curpri, pthinfo.pth_curpri, "PROC_PIDTHREADID64INFO returns valid value for pth_curpri");
1238*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo_64.pth_priority, pthinfo.pth_priority, "PROC_PIDTHREADID64INFO returns valid value for pth_priority");
1239*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pthinfo_64.pth_maxpriority, pthinfo.pth_maxpriority,
1240*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADID64INFO returns valid value for pth_maxpriority");
1241*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pthinfo_64.pth_name, CONF_THREAD_NAME, "PROC_PIDTHREADID64INFO returns valid value for pth_name");
1242*1b191cb5SApple OSS Distributions 
1243*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 2);
1244*1b191cb5SApple OSS Distributions }
1245*1b191cb5SApple OSS Distributions 
1246*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidthreadpathinfo,
1247*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDTHREADPATHINFO returns valid information about the process",
1248*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1249*1b191cb5SApple OSS Distributions {
1250*1b191cb5SApple OSS Distributions 	void * proc_info[2];
1251*1b191cb5SApple OSS Distributions 	proc_info_caller(PTHINFO | PINFO_PATH, proc_info, NULL);
1252*1b191cb5SApple OSS Distributions 	struct proc_threadinfo pthinfo            = *((struct proc_threadinfo *)proc_info[0]);
1253*1b191cb5SApple OSS Distributions 	struct proc_threadwithpathinfo pinfo_path = *((struct proc_threadwithpathinfo *)proc_info[1]);
1254*1b191cb5SApple OSS Distributions 
1255*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG(pinfo_path.pt.pth_user_time, pthinfo.pth_user_time,
1256*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADPATHINFO returns valid value for pth_user_time");
1257*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_ULLONG(pinfo_path.pt.pth_system_time, pthinfo.pth_system_time,
1258*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADPATHINFO returns valid value for pth_system_time");
1259*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(pinfo_path.pt.pth_cpu_usage, pthinfo.pth_cpu_usage,
1260*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADPATHINFO returns valid value for pth_cpu_usage");
1261*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pinfo_path.pt.pth_policy, POLICY_TIMESHARE, "PROC_PIDTHREADPATHINFO returns valid value for pth_policy");
1262*1b191cb5SApple OSS Distributions 	if (!(pinfo_path.pt.pth_run_state == TH_STATE_WAITING) && !(pinfo_path.pt.pth_run_state == TH_STATE_RUNNING)) {
1263*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_INT(pinfo_path.pt.pth_run_state, -1, "PROC_PIDTHREADPATHINFO returns valid value for pth_run_state");
1264*1b191cb5SApple OSS Distributions 	}
1265*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pinfo_path.pt.pth_sleep_time, 0, "PROC_PIDTHREADPATHINFO returns valid value for pth_sleep_time");
1266*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pinfo_path.pt.pth_curpri, pthinfo.pth_curpri, "PROC_PIDTHREADPATHINFO returns valid value for pth_curpri");
1267*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pinfo_path.pt.pth_priority, pthinfo.pth_priority,
1268*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADPATHINFO returns valid value for pth_priority");
1269*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pinfo_path.pt.pth_maxpriority, pthinfo.pth_maxpriority,
1270*1b191cb5SApple OSS Distributions 	    "PROC_PIDTHREADPATHINFO returns valid value for pth_maxpriority");
1271*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(pinfo_path.pt.pth_name, CONF_THREAD_NAME, "PROC_PIDTHREADPATHINFO returns valid value for pth_name");
1272*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pinfo_path.pvip.vip_vi.vi_type, VNON, "PROC_PIDTHREADPATHINFO valid vnode information");
1273*1b191cb5SApple OSS Distributions 
1274*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 2);
1275*1b191cb5SApple OSS Distributions }
1276*1b191cb5SApple OSS Distributions 
1277*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidarchinfo,
1278*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDARCHINFO returns valid information about the process",
1279*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1280*1b191cb5SApple OSS Distributions {
1281*1b191cb5SApple OSS Distributions 	void * proc_info[1];
1282*1b191cb5SApple OSS Distributions 	proc_info_caller(PAI, proc_info, NULL);
1283*1b191cb5SApple OSS Distributions 	struct proc_archinfo pai = *((struct proc_archinfo *)proc_info[0]);
1284*1b191cb5SApple OSS Distributions 
1285*1b191cb5SApple OSS Distributions #if defined(__arm64__)
1286*1b191cb5SApple OSS Distributions 	if (!((pai.p_cputype & CPU_TYPE_ARM) == CPU_TYPE_ARM) && !((pai.p_cputype & CPU_TYPE_ARM64) == CPU_TYPE_ARM64)) {
1287*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_INT(pai.p_cputype, CPU_TYPE_ARM, "PROC_PIDARCHINFO returned valid value for p_cputype");
1288*1b191cb5SApple OSS Distributions 	}
1289*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT((pai.p_cpusubtype & CPU_SUBTYPE_ARM_ALL), CPU_SUBTYPE_ARM_ALL,
1290*1b191cb5SApple OSS Distributions 	    "PROC_PIDARCHINFO returned valid value for p_cpusubtype");
1291*1b191cb5SApple OSS Distributions #else
1292*1b191cb5SApple OSS Distributions 	if (!((pai.p_cputype & CPU_TYPE_X86) == CPU_TYPE_X86) && !((pai.p_cputype & CPU_TYPE_X86_64) == CPU_TYPE_X86_64)) {
1293*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_INT(pai.p_cputype, CPU_TYPE_X86, "PROC_PIDARCHINFO returned valid value for p_cputype");
1294*1b191cb5SApple OSS Distributions 	}
1295*1b191cb5SApple OSS Distributions #endif
1296*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 1);
1297*1b191cb5SApple OSS Distributions }
1298*1b191cb5SApple OSS Distributions 
1299*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidregioninfo,
1300*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDREGIONINFO returns valid information about the process",
1301*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1302*1b191cb5SApple OSS Distributions {
1303*1b191cb5SApple OSS Distributions 	void * proc_info[3];
1304*1b191cb5SApple OSS Distributions 	proc_info_caller(PREGINFO, proc_info, NULL);
1305*1b191cb5SApple OSS Distributions 
1306*1b191cb5SApple OSS Distributions 	struct proc_regioninfo preginfo = *((struct proc_regioninfo *)proc_info[0]);
1307*1b191cb5SApple OSS Distributions 	/*
1308*1b191cb5SApple OSS Distributions 	 *	map_tmp isn't a struct like the rest of our ret_structs, but we sneak it back because we need it
1309*1b191cb5SApple OSS Distributions 	 */
1310*1b191cb5SApple OSS Distributions 	void *map_tmp = proc_info[1];
1311*1b191cb5SApple OSS Distributions 	vm_map_size_t map_tmp_sz = (vm_map_size_t)(uintptr_t)proc_info[2];
1312*1b191cb5SApple OSS Distributions 
1313*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo.pri_offset, (unsigned long long)PAGE_SIZE, "PROC_PIDREGIONINFO returns valid value for pri_offset");
1314*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo.pri_protection ^ (VM_PROT_READ | VM_PROT_WRITE)), 0U,
1315*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONINFO returns valid value for pri_protection, expected read/write only");
1316*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo.pri_max_protection & (VM_PROT_READ | VM_PROT_WRITE)), (unsigned int)(VM_PROT_READ | VM_PROT_WRITE),
1317*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONINFO returns valid value for pri_max_protection");
1318*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo.pri_inheritance ^ VM_INHERIT_COPY), 0U,
1319*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONINFO returns valid value for pri_inheritance");
1320*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo.pri_behavior ^ VM_BEHAVIOR_DEFAULT), 0U, "PROC_PIDREGIONINFO returns valid value for pri_behavior");
1321*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_user_wired_count, 0U, "PROC_PIDREGIONINFO returns valid value for pri_user_wired_count");
1322*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_user_tag, 0U, "PROC_PIDREGIONINFO returns valid value for pri_user_tag");
1323*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT((preginfo.pri_flags ^ (PROC_REGION_SUBMAP | PROC_REGION_SHARED)), 0U,
1324*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONINFO returns valid value for pri_flags");
1325*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_pages_resident, 0U, "PROC_PIDREGIONINFO returns valid value for pri_pages_resident");
1326*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_pages_shared_now_private, 0U,
1327*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONINFO returns valid value for pri_pages_shared_now_private");
1328*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_pages_swapped_out, 0U, "PROC_PIDREGIONINFO returns valid value for pri_pages_swapped_out");
1329*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_pages_dirtied, 0U, "PROC_PIDREGIONINFO returns valid value for pri_pages_dirtied");
1330*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_ref_count, 2U, "PROC_PIDREGIONINFO returns valid value for pri_ref_count");
1331*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_shadow_depth, 1U, "PROC_PIDREGIONINFO returns valid value for pri_shadow_depth");
1332*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_share_mode, (unsigned int)SM_COW, "PROC_PIDREGIONINFO returns valid value for pri_share_mode");
1333*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_private_pages_resident, 0U,
1334*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONINFO returns valid value for pri_private_pages_resident");
1335*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(preginfo.pri_shared_pages_resident, 0U,
1336*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONINFO returns valid value for pri_shared_pages_resident");
1337*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo.pri_address, (uint64_t)map_tmp, "PROC_PIDREGIONINFO returns valid value for pri_addr");
1338*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(preginfo.pri_obj_id, 0U, "PROC_PIDREGIONINFO returns valid value for pri_obj_id");
1339*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo.pri_size, (unsigned long long)map_tmp_sz, "PROC_PIDREGIONINFO returns valid value for pri_size");
1340*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo.pri_depth, 0U, "PROC_PIDREGIONINFO returns valid value for pri_depth");
1341*1b191cb5SApple OSS Distributions 
1342*1b191cb5SApple OSS Distributions 	int ret = 0;
1343*1b191cb5SApple OSS Distributions 	ret     = munmap(map_tmp, (size_t)map_tmp_sz);
1344*1b191cb5SApple OSS Distributions 	T_QUIET;
1345*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "munmap of map_tmp");
1346*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 1);
1347*1b191cb5SApple OSS Distributions }
1348*1b191cb5SApple OSS Distributions 
1349*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidregionpathinfo,
1350*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDREGIONPATHINFO returns valid information about the process",
1351*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1352*1b191cb5SApple OSS Distributions {
1353*1b191cb5SApple OSS Distributions 	void * proc_info[3];
1354*1b191cb5SApple OSS Distributions 	proc_info_caller(PREGINFO_PATH, proc_info, NULL);
1355*1b191cb5SApple OSS Distributions 
1356*1b191cb5SApple OSS Distributions 	struct proc_regionwithpathinfo preginfo_path = *((struct proc_regionwithpathinfo *)proc_info[0]);
1357*1b191cb5SApple OSS Distributions 	/*
1358*1b191cb5SApple OSS Distributions 	 *	map_tmp isn't a struct like the rest of our ret_structs, but we sneak it back because we need it
1359*1b191cb5SApple OSS Distributions 	 */
1360*1b191cb5SApple OSS Distributions 	void *map_tmp = proc_info[1];
1361*1b191cb5SApple OSS Distributions 	vm_map_size_t map_tmp_sz = (vm_map_size_t)(uintptr_t)proc_info[2];
1362*1b191cb5SApple OSS Distributions 
1363*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo_path.prp_prinfo.pri_offset, (uint64_t)PAGE_SIZE,
1364*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_offset");
1365*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_protection ^ (VM_PROT_READ | VM_PROT_WRITE)), 0U,
1366*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_protection, expected read/write only");
1367*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_max_protection & (VM_PROT_READ | VM_PROT_WRITE)),
1368*1b191cb5SApple OSS Distributions 	    (unsigned int)(VM_PROT_READ | VM_PROT_WRITE),
1369*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_max_protection");
1370*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_inheritance ^ VM_INHERIT_COPY), 0U,
1371*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_inheritance");
1372*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_behavior ^ VM_BEHAVIOR_DEFAULT), 0U,
1373*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_behavior");
1374*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_user_wired_count, 0U,
1375*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_user_wired_count");
1376*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_user_tag, 0U, "PROC_PIDREGIONPATHINFO returns valid value for pri_user_tag");
1377*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT((preginfo_path.prp_prinfo.pri_flags ^ (PROC_REGION_SUBMAP | PROC_REGION_SHARED)), 0U,
1378*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_flags");
1379*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_resident, 0U,
1380*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_pages_resident");
1381*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_shared_now_private, 0U,
1382*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_pages_shared_now_private");
1383*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_swapped_out, 0U,
1384*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_pages_swapped_out");
1385*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_dirtied, 0U,
1386*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_pages_dirtied");
1387*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_ref_count, 2U, "PROC_PIDREGIONPATHINFO returns valid value for pri_ref_count");
1388*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_shadow_depth, 1U,
1389*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_shadow_depth");
1390*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_share_mode, (unsigned int)SM_COW,
1391*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_share_mode");
1392*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_private_pages_resident, 0U,
1393*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_private_pages_resident");
1394*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(preginfo_path.prp_prinfo.pri_shared_pages_resident, 0U,
1395*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_shared_pages_resident");
1396*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo_path.prp_prinfo.pri_address, (uint64_t)map_tmp,
1397*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_addr");
1398*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(preginfo_path.prp_prinfo.pri_obj_id, 0U, "PROC_PIDREGIONPATHINFO returns valid value for pri_obj_id");
1399*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo_path.prp_prinfo.pri_size, (uint64_t)map_tmp_sz,
1400*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for pri_size");
1401*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_depth, 0U, "PROC_PIDREGIONPATHINFO returns valid value for pri_depth");
1402*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(preginfo_path.prp_vip.vip_vi.vi_type, VREG, "PROC_PIDREGIONPATHINFO returns valid value for vi_type");
1403*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(preginfo_path.prp_vip.vip_vi.vi_pad, 0, "PROC_PIDREGIONPATHINFO returns valid value for vi_pad");
1404*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_INT(preginfo_path.prp_vip.vip_vi.vi_fsid.val[0], 0,
1405*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vi_fsid.val[0]");
1406*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_INT(preginfo_path.prp_vip.vip_vi.vi_fsid.val[1], 0,
1407*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vi_fsid.val[1]");
1408*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_PTR((void *)(strcasestr(preginfo_path.prp_vip.vip_path, CONF_TMP_FILE_PFX)), NULL,
1409*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vi_path");
1410*1b191cb5SApple OSS Distributions 	/*
1411*1b191cb5SApple OSS Distributions 	 * Basic sanity checks for vnode stat returned by the API
1412*1b191cb5SApple OSS Distributions 	 */
1413*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_dev, 0U, "PROC_PIDREGIONPATHINFO returns valid value for vst_dev");
1414*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(((preginfo_path.prp_vip.vip_vi.vi_stat.vst_mode & S_IFMT) ^ S_IFREG), 0,
1415*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vst_mode");
1416*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_USHORT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_nlink, (unsigned short)0, /* the file was unlink()'d! */
1417*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vst_nlink");
1418*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_ULLONG(preginfo_path.prp_vip.vip_vi.vi_stat.vst_ino, 0ULL,
1419*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vst_ino");
1420*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_uid, 0U, "PROC_PIDREGIONPATHINFO returns valid value for vst_uid");
1421*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_gid, 0U, "PROC_PIDREGIONPATHINFO returns valid value for vst_gid");
1422*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_LLONG(preginfo_path.prp_vip.vip_vi.vi_stat.vst_size, (off_t)CONF_BLK_SIZE,
1423*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vst_size");
1424*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_LLONG(preginfo_path.prp_vip.vip_vi.vi_stat.vst_blocks, 1LL,
1425*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vst_blocks");
1426*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_blksize, CONF_BLK_SIZE,
1427*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO returns valid value for vst_blksize");
1428*1b191cb5SApple OSS Distributions 
1429*1b191cb5SApple OSS Distributions 	int ret = 0;
1430*1b191cb5SApple OSS Distributions 	ret     = munmap(map_tmp, (size_t)map_tmp_sz);
1431*1b191cb5SApple OSS Distributions 	T_QUIET;
1432*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "munmap of map_tmp");
1433*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 1);
1434*1b191cb5SApple OSS Distributions }
1435*1b191cb5SApple OSS Distributions 
1436*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidregionpathinfo2,
1437*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDREGIONPATHINFO2 returns valid information about the process",
1438*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1439*1b191cb5SApple OSS Distributions {
1440*1b191cb5SApple OSS Distributions 	void * proc_info[3];
1441*1b191cb5SApple OSS Distributions 	proc_info_caller(PREGINFO_PATH_2, proc_info, NULL);
1442*1b191cb5SApple OSS Distributions 
1443*1b191cb5SApple OSS Distributions 	struct proc_regionwithpathinfo preginfo_path = *((struct proc_regionwithpathinfo *)proc_info[0]);
1444*1b191cb5SApple OSS Distributions 	/*
1445*1b191cb5SApple OSS Distributions 	 *	map_tmp isn't a struct like the rest of our ret_structs, but we sneak it back because we need it
1446*1b191cb5SApple OSS Distributions 	 */
1447*1b191cb5SApple OSS Distributions 	void *map_tmp = proc_info[1];
1448*1b191cb5SApple OSS Distributions 	vm_map_size_t map_tmp_sz = (vm_map_size_t)(uintptr_t)proc_info[2];
1449*1b191cb5SApple OSS Distributions 
1450*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo_path.prp_prinfo.pri_offset, (uint64_t)PAGE_SIZE,
1451*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_offset");
1452*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_protection ^ (VM_PROT_READ | VM_PROT_WRITE)), 0U,
1453*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_protection, expected read/write only");
1454*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_max_protection & (VM_PROT_READ | VM_PROT_WRITE)),
1455*1b191cb5SApple OSS Distributions 	    (unsigned int)(VM_PROT_READ | VM_PROT_WRITE),
1456*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_max_protection");
1457*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_inheritance ^ VM_INHERIT_COPY), 0U,
1458*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_inheritance");
1459*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT((preginfo_path.prp_prinfo.pri_behavior ^ VM_BEHAVIOR_DEFAULT), 0U,
1460*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_behavior");
1461*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_user_wired_count, 0U,
1462*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_user_wired_count");
1463*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_user_tag, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for pri_user_tag");
1464*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT((preginfo_path.prp_prinfo.pri_flags ^ (PROC_REGION_SUBMAP | PROC_REGION_SHARED)), 0U,
1465*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_flags");
1466*1b191cb5SApple OSS Distributions 	/*
1467*1b191cb5SApple OSS Distributions 	 * Following values are hard-coded to be zero in source
1468*1b191cb5SApple OSS Distributions 	 */
1469*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_resident, 0U,
1470*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_pages_resident");
1471*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_shared_now_private, 0U,
1472*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_pages_shared_now_private");
1473*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_swapped_out, 0U,
1474*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_pages_swapped_out");
1475*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_pages_dirtied, 0U,
1476*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_pages_dirtied");
1477*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_ref_count, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for pri_ref_count");
1478*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_shadow_depth, 0U,
1479*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_shadow_depth");
1480*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_share_mode, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for pri_share_mode");
1481*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_private_pages_resident, 0U,
1482*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_private_pages_resident");
1483*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_shared_pages_resident, 0U,
1484*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_shared_pages_resident");
1485*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo_path.prp_prinfo.pri_address, (uint64_t)map_tmp,
1486*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_addr");
1487*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_obj_id, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for pri_obj_id");
1488*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(preginfo_path.prp_prinfo.pri_size, (unsigned long long)map_tmp_sz,
1489*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for pri_size");
1490*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_prinfo.pri_depth, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for pri_depth");
1491*1b191cb5SApple OSS Distributions 
1492*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(preginfo_path.prp_vip.vip_vi.vi_type, VREG, "PROC_PIDREGIONPATHINFO2 returns valid value for vi_type");
1493*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(preginfo_path.prp_vip.vip_vi.vi_pad, 0, "PROC_PIDREGIONPATHINFO2 returns valid value for vi_pad");
1494*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_INT(preginfo_path.prp_vip.vip_vi.vi_fsid.val[0], 0,
1495*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vi_fsid.val[0]:%d",
1496*1b191cb5SApple OSS Distributions 	    preginfo_path.prp_vip.vip_vi.vi_fsid.val[0]);
1497*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_INT(preginfo_path.prp_vip.vip_vi.vi_fsid.val[1], 0,
1498*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vi_fsid.val[1]:%d",
1499*1b191cb5SApple OSS Distributions 	    preginfo_path.prp_vip.vip_vi.vi_fsid.val[1]);
1500*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_PTR((void *)(strcasestr(preginfo_path.prp_vip.vip_path, CONF_TMP_FILE_PFX)), NULL,
1501*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vi_path");
1502*1b191cb5SApple OSS Distributions 	/*
1503*1b191cb5SApple OSS Distributions 	 * Basic sanity checks for vnode stat returned by the API
1504*1b191cb5SApple OSS Distributions 	 */
1505*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_dev, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for vst_dev");
1506*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(((preginfo_path.prp_vip.vip_vi.vi_stat.vst_mode & S_IFMT) ^ S_IFREG), 0,
1507*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vst_mode");
1508*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_USHORT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_nlink, (unsigned short)0, /* the file was unlink()'d! */
1509*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vst_nlink");
1510*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_ULLONG(preginfo_path.prp_vip.vip_vi.vi_stat.vst_ino, 0ULL,
1511*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vst_ino");
1512*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_uid, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for vst_uid");
1513*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_gid, 0U, "PROC_PIDREGIONPATHINFO2 returns valid value for vst_gid");
1514*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_LLONG(preginfo_path.prp_vip.vip_vi.vi_stat.vst_size, (off_t)CONF_BLK_SIZE,
1515*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vst_size");
1516*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_LLONG(preginfo_path.prp_vip.vip_vi.vi_stat.vst_blocks, 1LL,
1517*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vst_blocks");
1518*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(preginfo_path.prp_vip.vip_vi.vi_stat.vst_blksize, CONF_BLK_SIZE,
1519*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO2 returns valid value for vst_blksize");
1520*1b191cb5SApple OSS Distributions 
1521*1b191cb5SApple OSS Distributions 	int ret = 0;
1522*1b191cb5SApple OSS Distributions 	ret     = munmap(map_tmp, (size_t)map_tmp_sz);
1523*1b191cb5SApple OSS Distributions 	T_QUIET;
1524*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "munmap of map_tmp");
1525*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 1);
1526*1b191cb5SApple OSS Distributions }
1527*1b191cb5SApple OSS Distributions 
1528*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidregionpathinfo3,
1529*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDREGIONPATHINFO3 returns valid information about the process",
1530*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1531*1b191cb5SApple OSS Distributions {
1532*1b191cb5SApple OSS Distributions 	void * proc_info[5];
1533*1b191cb5SApple OSS Distributions 	proc_info_caller(PREGINFO_PATH_3, proc_info, NULL);
1534*1b191cb5SApple OSS Distributions 
1535*1b191cb5SApple OSS Distributions 	struct proc_regionwithpathinfo preginfo_path = *((struct proc_regionwithpathinfo *)proc_info[0]);
1536*1b191cb5SApple OSS Distributions 	void *map_tmp = proc_info[1];
1537*1b191cb5SApple OSS Distributions 	vm_map_size_t map_tmp_sz = (vm_map_size_t)(uintptr_t)proc_info[2];
1538*1b191cb5SApple OSS Distributions 
1539*1b191cb5SApple OSS Distributions 	/* The *info3 version of this call returns any open file that lives on the same file system */
1540*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(preginfo_path.prp_vip.vip_vi.vi_fsid.val[0], (int)(uintptr_t)proc_info[3],
1541*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO3 returns valid value for vi_fsid.val[0]");
1542*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(preginfo_path.prp_vip.vip_vi.vi_fsid.val[1], (int)(uintptr_t)proc_info[4],
1543*1b191cb5SApple OSS Distributions 	    "PROC_PIDREGIONPATHINFO3 returns valid value for vi_fsid.val[1]");
1544*1b191cb5SApple OSS Distributions 
1545*1b191cb5SApple OSS Distributions 	int ret = 0;
1546*1b191cb5SApple OSS Distributions 	ret     = munmap(map_tmp, (size_t)map_tmp_sz);
1547*1b191cb5SApple OSS Distributions 	T_QUIET;
1548*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "munmap of map_tmp");
1549*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 1);
1550*1b191cb5SApple OSS Distributions }
1551*1b191cb5SApple OSS Distributions 
1552*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidvnodepathinfo,
1553*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDVNODEPATHINFO returns valid information about the process",
1554*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1555*1b191cb5SApple OSS Distributions {
1556*1b191cb5SApple OSS Distributions 	void * proc_info[1];
1557*1b191cb5SApple OSS Distributions 	proc_info_caller(PVNINFO, proc_info, NULL);
1558*1b191cb5SApple OSS Distributions 	struct proc_vnodepathinfo pvninfo = *((struct proc_vnodepathinfo *)proc_info[0]);
1559*1b191cb5SApple OSS Distributions 
1560*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pvninfo.pvi_cdir.vip_vi.vi_type, VDIR, "PROC_PIDVNODEPATHINFO returns valid value for vi_type");
1561*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(pvninfo.pvi_cdir.vip_vi.vi_pad, 0, "PROC_PIDVNODEPATHINFO returns valid value for vi_pad");
1562*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_INT(pvninfo.pvi_cdir.vip_vi.vi_fsid.val[0], 0, "PROC_PIDVNODEPATHINFO returns valid value for vi_fsid.val[0]");
1563*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_INT(pvninfo.pvi_cdir.vip_vi.vi_fsid.val[1], 0, "PROC_PIDVNODEPATHINFO returns valid value for vi_fsid.val[1]");
1564*1b191cb5SApple OSS Distributions 	/*
1565*1b191cb5SApple OSS Distributions 	 * Basic sanity checks for vnode stat returned by the API
1566*1b191cb5SApple OSS Distributions 	 */
1567*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_dev, 0U, "PROC_PIDVNODEPATHINFO returns valid value for vst_dev");
1568*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(((pvninfo.pvi_cdir.vip_vi.vi_stat.vst_mode & S_IFMT) ^ S_IFDIR), 0,
1569*1b191cb5SApple OSS Distributions 	    "PROC_PIDVNODEPATHINFO returns valid value for vst_mode");
1570*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_USHORT(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_nlink, (unsigned short)2,
1571*1b191cb5SApple OSS Distributions 	    "PROC_PIDVNODEPATHINFO returns valid value for vst_nlink");
1572*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_ULLONG(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_ino, 0ULL, "PROC_PIDVNODEPATHINFO returns valid value for vst_ino");
1573*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_uid, 0U, "PROC_PIDVNODEPATHINFO returns valid value for vst_uid");
1574*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_gid, 0U, "PROC_PIDVNODEPATHINFO returns valid value for vst_gid");
1575*1b191cb5SApple OSS Distributions 	T_EXPECT_GT_LLONG(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_size, 0LL, "PROC_PIDVNODEPATHINFO returns valid value for vst_size");
1576*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_LLONG(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_blocks, 0LL, "PROC_PIDVNODEPATHINFO returns valid value for vst_blocks");
1577*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(pvninfo.pvi_cdir.vip_vi.vi_stat.vst_blksize, CONF_BLK_SIZE,
1578*1b191cb5SApple OSS Distributions 	    "PROC_PIDVNODEPATHINFO returns valid value for vst_blksize");
1579*1b191cb5SApple OSS Distributions 
1580*1b191cb5SApple OSS Distributions 	free_proc_info(proc_info, 1);
1581*1b191cb5SApple OSS Distributions }
1582*1b191cb5SApple OSS Distributions /*
1583*1b191cb5SApple OSS Distributions  * The remaining tests break from the pattern of the other PROC_INFO_CALL_PIDINFO tests.
1584*1b191cb5SApple OSS Distributions  * We call proc_info directly as it's more efficient
1585*1b191cb5SApple OSS Distributions  */
1586*1b191cb5SApple OSS Distributions 
1587*1b191cb5SApple OSS Distributions T_DECL(proc_info_pidinfo_proc_pidlistfds,
1588*1b191cb5SApple OSS Distributions     "proc_info API tests to verify PROC_INFO_CALL_PIDINFO/PROC_PIDLISTFDS",
1589*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1590*1b191cb5SApple OSS Distributions {
1591*1b191cb5SApple OSS Distributions 	int retval;
1592*1b191cb5SApple OSS Distributions 	int orig_nfiles              = 0;
1593*1b191cb5SApple OSS Distributions 	struct proc_fdinfo * fd_info = NULL;
1594*1b191cb5SApple OSS Distributions 
1595*1b191cb5SApple OSS Distributions 	T_LOG("Test to verify PROC_PIDLISTFDS returns sane number of open files");
1596*1b191cb5SApple OSS Distributions 	retval      = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDLISTFDS, (uint32_t)0, (user_addr_t)0, (uint32_t)0);
1597*1b191cb5SApple OSS Distributions 	orig_nfiles = retval / (int)sizeof(struct proc_fdinfo);
1598*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(orig_nfiles, CONF_OPN_FILE_COUNT, "The number of open files is lower than expected.");
1599*1b191cb5SApple OSS Distributions 
1600*1b191cb5SApple OSS Distributions 	/*
1601*1b191cb5SApple OSS Distributions 	 * Allocate a buffer of expected size + 1 to ensure that
1602*1b191cb5SApple OSS Distributions 	 * the API still returns expected size
1603*1b191cb5SApple OSS Distributions 	 * i.e. 3 + 1 = 4 open fds
1604*1b191cb5SApple OSS Distributions 	 */
1605*1b191cb5SApple OSS Distributions 	T_LOG("Test to verify PROC_PIDLISTFDS returns valid fd information");
1606*1b191cb5SApple OSS Distributions 	fd_info = malloc(sizeof(*fd_info) * 5);
1607*1b191cb5SApple OSS Distributions 	tmp_fd = CONF_TMP_FILE_OPEN(NULL);
1608*1b191cb5SApple OSS Distributions 	T_LOG("tmp_fd val:%d", tmp_fd);
1609*1b191cb5SApple OSS Distributions 	T_QUIET;
1610*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(tmp_fd, "open() for PROC_PIDLISTFDS");
1611*1b191cb5SApple OSS Distributions 
1612*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDLISTFDS, (uint32_t)0, (user_addr_t)fd_info,
1613*1b191cb5SApple OSS Distributions 	    (uint32_t)(sizeof(*fd_info) * 5));
1614*1b191cb5SApple OSS Distributions 	retval = retval / (int)sizeof(struct proc_fdinfo);
1615*1b191cb5SApple OSS Distributions 
1616*1b191cb5SApple OSS Distributions 	close(tmp_fd);
1617*1b191cb5SApple OSS Distributions 
1618*1b191cb5SApple OSS Distributions 	for (int i = 0; i < retval; i++) {
1619*1b191cb5SApple OSS Distributions 		/*
1620*1b191cb5SApple OSS Distributions 		 * Check only for the fd that we control.
1621*1b191cb5SApple OSS Distributions 		 */
1622*1b191cb5SApple OSS Distributions 		if (tmp_fd != fd_info[i].proc_fd) {
1623*1b191cb5SApple OSS Distributions 			continue;
1624*1b191cb5SApple OSS Distributions 		}
1625*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_UINT(fd_info[i].proc_fdtype, (unsigned int)PROX_FDTYPE_VNODE, "Correct proc_fdtype for returned fd");
1626*1b191cb5SApple OSS Distributions 	}
1627*1b191cb5SApple OSS Distributions 
1628*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(retval, 4, "Correct number of fds was returned.");
1629*1b191cb5SApple OSS Distributions 
1630*1b191cb5SApple OSS Distributions 	tmp_fd = -1;
1631*1b191cb5SApple OSS Distributions 	free(fd_info);
1632*1b191cb5SApple OSS Distributions 	fd_info = NULL;
1633*1b191cb5SApple OSS Distributions }
1634*1b191cb5SApple OSS Distributions 
1635*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidpathinfo,
1636*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDPATHINFO returns valid information about the process",
1637*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1638*1b191cb5SApple OSS Distributions {
1639*1b191cb5SApple OSS Distributions 	char * pid_path = NULL;
1640*1b191cb5SApple OSS Distributions 	pid_path        = malloc(sizeof(char) * PROC_PIDPATHINFO_MAXSIZE);
1641*1b191cb5SApple OSS Distributions 	T_EXPECT_NOTNULL(pid_path, "malloc for PROC_PIDPATHINFO");
1642*1b191cb5SApple OSS Distributions 	int retval = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDPATHINFO, (uint64_t)0, (user_addr_t)pid_path,
1643*1b191cb5SApple OSS Distributions 	    (uint32_t)PROC_PIDPATHINFO_MAXSIZE);
1644*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval, 0, "__proc_info call for PROC_PIDPATHINFO");
1645*1b191cb5SApple OSS Distributions 
1646*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_PTR((void *)(strcasestr(pid_path, CONF_CMD_NAME)), NULL, "PROC_PIDPATHINFOreturns valid value for pid_path");
1647*1b191cb5SApple OSS Distributions 	free(pid_path);
1648*1b191cb5SApple OSS Distributions 	pid_path = NULL;
1649*1b191cb5SApple OSS Distributions }
1650*1b191cb5SApple OSS Distributions 
1651*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidlistfileports,
1652*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDLISTFILEPORTS returns valid information about the process",
1653*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1654*1b191cb5SApple OSS Distributions {
1655*1b191cb5SApple OSS Distributions 	struct proc_fileportinfo * fileport_info = NULL;
1656*1b191cb5SApple OSS Distributions 	mach_port_t tmp_file_port                = MACH_PORT_NULL;
1657*1b191cb5SApple OSS Distributions 	proc_config_t proc_config                = spawn_child_processes(1, proc_info_call_pidinfo_handler);
1658*1b191cb5SApple OSS Distributions 	int child_pid                            = proc_config->child_pids[0];
1659*1b191cb5SApple OSS Distributions 
1660*1b191cb5SApple OSS Distributions 	/*
1661*1b191cb5SApple OSS Distributions 	 * Create a file port
1662*1b191cb5SApple OSS Distributions 	 */
1663*1b191cb5SApple OSS Distributions 	tmp_fd     = CONF_TMP_FILE_OPEN(NULL);
1664*1b191cb5SApple OSS Distributions 	int retval = fileport_makeport(tmp_fd, &tmp_file_port);
1665*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(retval, "fileport_makeport() for PROC_PIDLISTFILEPORTS");
1666*1b191cb5SApple OSS Distributions 
1667*1b191cb5SApple OSS Distributions 	/*
1668*1b191cb5SApple OSS Distributions 	 * Like the other APIs, this returns the actual count + 20. Hence we expect it to be atleast 1 (that we created)
1669*1b191cb5SApple OSS Distributions 	 */
1670*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDLISTFILEPORTS, (uint64_t)0, (user_addr_t)0, (uint32_t)0);
1671*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_INT(retval / (int)sizeof(fileport_info), 1,
1672*1b191cb5SApple OSS Distributions 	    "__proc_info call for PROC_PIDLISTFILEPORTS to get total ports in parent");
1673*1b191cb5SApple OSS Distributions 
1674*1b191cb5SApple OSS Distributions 	/*
1675*1b191cb5SApple OSS Distributions 	 * Child doesn't have any fileports, should return zero
1676*1b191cb5SApple OSS Distributions 	 */
1677*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDLISTFILEPORTS, (uint64_t)0, (user_addr_t)0, (uint32_t)0);
1678*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval / (int)sizeof(fileport_info), 0,
1679*1b191cb5SApple OSS Distributions 	    "__proc_info call for PROC_PIDLISTFILEPORTS to get total ports in child");
1680*1b191cb5SApple OSS Distributions 
1681*1b191cb5SApple OSS Distributions 	fileport_info = malloc(sizeof(*fileport_info) * (size_t)retval);
1682*1b191cb5SApple OSS Distributions 	retval        = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDLISTFILEPORTS, (uint64_t)0, (user_addr_t)fileport_info,
1683*1b191cb5SApple OSS Distributions 	    (uint32_t)sizeof(*fileport_info));
1684*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval, (int)sizeof(*fileport_info), "__proc_info call for PROC_PIDLISTFILEPORTS");
1685*1b191cb5SApple OSS Distributions 
1686*1b191cb5SApple OSS Distributions 	T_EXPECT_NE_UINT(fileport_info->proc_fileport, (uint32_t)0, "PROC_PIDLISTFILEPORTS returns valid value for proc_fileport");
1687*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(fileport_info->proc_fdtype, (uint32_t)PROX_FDTYPE_VNODE,
1688*1b191cb5SApple OSS Distributions 	    "PROC_PIDLISTFILEPORTS returns valid value for proc_fdtype");
1689*1b191cb5SApple OSS Distributions 
1690*1b191cb5SApple OSS Distributions 	/*
1691*1b191cb5SApple OSS Distributions 	 * Cleanup for the fileport
1692*1b191cb5SApple OSS Distributions 	 */
1693*1b191cb5SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), tmp_file_port);
1694*1b191cb5SApple OSS Distributions 	tmp_file_port = MACH_PORT_NULL;
1695*1b191cb5SApple OSS Distributions 	free(fileport_info);
1696*1b191cb5SApple OSS Distributions 	fileport_info = NULL;
1697*1b191cb5SApple OSS Distributions 	close(tmp_fd);
1698*1b191cb5SApple OSS Distributions 	tmp_fd = -1;
1699*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
1700*1b191cb5SApple OSS Distributions }
1701*1b191cb5SApple OSS Distributions 
1702*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidcoalitioninfo,
1703*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDCOALITIONINFO returns valid information about the process",
1704*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1705*1b191cb5SApple OSS Distributions {
1706*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(1, proc_info_call_pidinfo_handler);
1707*1b191cb5SApple OSS Distributions 	int child_pid             = proc_config->child_pids[0];
1708*1b191cb5SApple OSS Distributions 
1709*1b191cb5SApple OSS Distributions 	struct proc_pidcoalitioninfo pci_parent;
1710*1b191cb5SApple OSS Distributions 	struct proc_pidcoalitioninfo pci_child;
1711*1b191cb5SApple OSS Distributions 	int retval = __proc_info(PROC_INFO_CALL_PIDINFO, getpid(), PROC_PIDCOALITIONINFO, (uint64_t)0, (user_addr_t)&pci_parent,
1712*1b191cb5SApple OSS Distributions 	    (uint32_t)sizeof(pci_parent));
1713*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval, (int)sizeof(pci_parent), "__proc_info call for PROC_PIDCOALITIONINFO (parent)");
1714*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDCOALITIONINFO, (uint64_t)0, (user_addr_t)&pci_child,
1715*1b191cb5SApple OSS Distributions 	    (uint32_t)sizeof(pci_child));
1716*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval, (int)sizeof(pci_child), "__proc_info call for PROC_PIDCOALITIONINFO (child)");
1717*1b191cb5SApple OSS Distributions 
1718*1b191cb5SApple OSS Distributions 	/*
1719*1b191cb5SApple OSS Distributions 	 * Coalition IDs should match for child and parent
1720*1b191cb5SApple OSS Distributions 	 */
1721*1b191cb5SApple OSS Distributions 	for (int i = 0; i < COALITION_NUM_TYPES; i++) {
1722*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ_ULLONG(pci_parent.coalition_id[i], pci_child.coalition_id[i],
1723*1b191cb5SApple OSS Distributions 		    "PROC_PIDCOALITIONINFO returns valid value for coalition_id");
1724*1b191cb5SApple OSS Distributions 	}
1725*1b191cb5SApple OSS Distributions 
1726*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
1727*1b191cb5SApple OSS Distributions }
1728*1b191cb5SApple OSS Distributions 
1729*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidworkqueueinfo,
1730*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDWORKQUEUEINFO returns valid information about the process",
1731*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1732*1b191cb5SApple OSS Distributions {
1733*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(1, proc_info_call_pidinfo_handler);
1734*1b191cb5SApple OSS Distributions 	int child_pid             = proc_config->child_pids[0];
1735*1b191cb5SApple OSS Distributions 	send_action_to_child_processes(proc_config, ACT_PHASE5);
1736*1b191cb5SApple OSS Distributions 
1737*1b191cb5SApple OSS Distributions 	struct proc_workqueueinfo pwqinfo;
1738*1b191cb5SApple OSS Distributions 	usleep(10000);
1739*1b191cb5SApple OSS Distributions 	int retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDWORKQUEUEINFO, (uint64_t)0, (user_addr_t)&pwqinfo,
1740*1b191cb5SApple OSS Distributions 	    (uint32_t)sizeof(pwqinfo));
1741*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval, (int)sizeof(pwqinfo), "__proc_info call for PROC_PIDWORKQUEUEINFO");
1742*1b191cb5SApple OSS Distributions 
1743*1b191cb5SApple OSS Distributions 	int ncpu         = 0;
1744*1b191cb5SApple OSS Distributions 	size_t ncpu_size = sizeof(ncpu);
1745*1b191cb5SApple OSS Distributions 	retval           = sysctlbyname("hw.ncpu", (void *)&ncpu, &ncpu_size, NULL, 0);
1746*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval, 0, "sysctl() for PROC_PIDWORKQUEUEINFO");
1747*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(pwqinfo.pwq_nthreads, (uint32_t)1, "PROC_PIDWORKQUEUEINFO returns valid value for pwq_nthreads");
1748*1b191cb5SApple OSS Distributions 	T_EXPECT_GE_UINT(pwqinfo.pwq_blockedthreads + pwqinfo.pwq_runthreads, (uint32_t)1,
1749*1b191cb5SApple OSS Distributions 	    "PROC_PIDWORKQUEUEINFO returns valid value for pwqinfo.pwq_runthreads/pwq_blockedthreads");
1750*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(pwqinfo.pwq_state, (uint32_t)0, "PROC_PIDWORKQUEUEINFO returns valid value for pwq_state");
1751*1b191cb5SApple OSS Distributions 
1752*1b191cb5SApple OSS Distributions 	kill_child_processes(proc_config);
1753*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
1754*1b191cb5SApple OSS Distributions }
1755*1b191cb5SApple OSS Distributions T_DECL(proc_info_proc_pidnoteexit,
1756*1b191cb5SApple OSS Distributions     "Test to verify PROC_PIDNOTEEXIT returns valid information about the process",
1757*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1758*1b191cb5SApple OSS Distributions {
1759*1b191cb5SApple OSS Distributions 	/*
1760*1b191cb5SApple OSS Distributions 	 * Ask the child to close pipe and quit, cleanup pipes for parent
1761*1b191cb5SApple OSS Distributions 	 */
1762*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(1, proc_info_call_pidinfo_handler);
1763*1b191cb5SApple OSS Distributions 	int child_pid             = proc_config->child_pids[0];
1764*1b191cb5SApple OSS Distributions 	send_action_to_child_processes(proc_config, ACT_EXIT);
1765*1b191cb5SApple OSS Distributions 
1766*1b191cb5SApple OSS Distributions 	uint32_t exit_data = 0;
1767*1b191cb5SApple OSS Distributions 	int retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDNOTEEXIT, (uint64_t)(NOTE_EXITSTATUS | NOTE_EXIT_DETAIL),
1768*1b191cb5SApple OSS Distributions 	    (user_addr_t)&exit_data, (uint32_t)sizeof(exit_data));
1769*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(retval, (int)sizeof(exit_data), "__proc_info call for PROC_PIDNOTEEXIT");
1770*1b191cb5SApple OSS Distributions 
1771*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_UINT(exit_data, 0U, "PROC_PIDNOTEEXIT returned valid value for exit_data");
1772*1b191cb5SApple OSS Distributions 
1773*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
1774*1b191cb5SApple OSS Distributions }
1775*1b191cb5SApple OSS Distributions 
1776*1b191cb5SApple OSS Distributions T_DECL(proc_info_negative_tests,
1777*1b191cb5SApple OSS Distributions     "Test to validate PROC_INFO_CALL_PIDINFO for invalid arguments",
1778*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
1779*1b191cb5SApple OSS Distributions {
1780*1b191cb5SApple OSS Distributions 	proc_config_t proc_config = spawn_child_processes(1, proc_info_call_pidinfo_handler);
1781*1b191cb5SApple OSS Distributions 	int child_pid             = proc_config->child_pids[0];
1782*1b191cb5SApple OSS Distributions 	uint32_t exit_data        = 0;
1783*1b191cb5SApple OSS Distributions 
1784*1b191cb5SApple OSS Distributions 	int retval =
1785*1b191cb5SApple OSS Distributions 	    __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDNOTEEXIT, (uint64_t)0, (user_addr_t)&exit_data, (uint32_t)0);
1786*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(errno, ENOMEM, "PROC_INFO_CALL_PIDINFO call should fail with ENOMEM if buffersize is zero");
1787*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, PROC_PIDPATHINFO, (uint64_t)0, (user_addr_t)&exit_data,
1788*1b191cb5SApple OSS Distributions 	    (uint32_t)PROC_PIDPATHINFO_MAXSIZE + 1);
1789*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(errno, EOVERFLOW,
1790*1b191cb5SApple OSS Distributions 	    "PROC_INFO_CALL_PIDINFO call should fail with EOVERFLOW if buffersize is larger than PROC_PIDPATHINFO_MAXSIZE");
1791*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, -1, PROC_PIDNOTEEXIT, (uint64_t)0, (user_addr_t)&exit_data,
1792*1b191cb5SApple OSS Distributions 	    (uint32_t)sizeof(exit_data));
1793*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(errno, ESRCH, "PROC_INFO_CALL_PIDINFO call should fail with ESRCH for invalid process id");
1794*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, child_pid, -1U, (uint64_t)0, (user_addr_t)&exit_data, (uint32_t)sizeof(exit_data));
1795*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(errno, EINVAL, "PROC_INFO_CALL_PIDINFO call should fail with EINVAL for invalid flavor");
1796*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, 0, PROC_PIDWORKQUEUEINFO, (uint64_t)0, (user_addr_t)0, (uint32_t)0);
1797*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_INT(errno, EINVAL,
1798*1b191cb5SApple OSS Distributions 	    "PROC_INFO_CALL_PIDINFO call should fail with EINVAL if flavor is PROC_PIDWORKQUEUEINFO and pid=0");
1799*1b191cb5SApple OSS Distributions 
1800*1b191cb5SApple OSS Distributions 	free_proc_config(proc_config);
1801*1b191cb5SApple OSS Distributions }
1802*1b191cb5SApple OSS Distributions 
1803*1b191cb5SApple OSS Distributions /*
1804*1b191cb5SApple OSS Distributions  * END PROC_INFO_CALL_PIDINFO DECLs
1805*1b191cb5SApple OSS Distributions  */
1806*1b191cb5SApple OSS Distributions 
1807*1b191cb5SApple OSS Distributions #pragma mark proc_list_uptrs
1808*1b191cb5SApple OSS Distributions 
1809*1b191cb5SApple OSS Distributions #define NUPTRS 4
1810*1b191cb5SApple OSS Distributions static uint64_t uptrs[NUPTRS] = {0x1122334455667788ULL, 0x99aabbccddeeff00ULL, 0xaabbaaddccaaffeeULL, 0xcc000011ccaa7755ULL};
1811*1b191cb5SApple OSS Distributions 
1812*1b191cb5SApple OSS Distributions static const char * uptr_names[NUPTRS];
1813*1b191cb5SApple OSS Distributions 
1814*1b191cb5SApple OSS Distributions static void
print_uptrs(int argc,char * const * argv)1815*1b191cb5SApple OSS Distributions print_uptrs(int argc, char * const * argv)
1816*1b191cb5SApple OSS Distributions {
1817*1b191cb5SApple OSS Distributions 	for (int i = 0; i < argc; i++) {
1818*1b191cb5SApple OSS Distributions 		char * end;
1819*1b191cb5SApple OSS Distributions 		unsigned long pid = strtoul(argv[i], &end, 0);
1820*1b191cb5SApple OSS Distributions 		if (pid > INT_MAX) {
1821*1b191cb5SApple OSS Distributions 			printf("error: pid '%lu' would overflow an integer\n", pid);
1822*1b191cb5SApple OSS Distributions 		}
1823*1b191cb5SApple OSS Distributions 		if (end == argv[i]) {
1824*1b191cb5SApple OSS Distributions 			printf("error: could not parse '%s' as a pid\n", argv[i]);
1825*1b191cb5SApple OSS Distributions 			continue;
1826*1b191cb5SApple OSS Distributions 		}
1827*1b191cb5SApple OSS Distributions 		int uptrs_count = proc_list_uptrs((int)pid, NULL, 0);
1828*1b191cb5SApple OSS Distributions 		if (uptrs_count == 0) {
1829*1b191cb5SApple OSS Distributions 			printf("no uptrs for process %d\n", (int)pid);
1830*1b191cb5SApple OSS Distributions 			return;
1831*1b191cb5SApple OSS Distributions 		}
1832*1b191cb5SApple OSS Distributions 
1833*1b191cb5SApple OSS Distributions 		/* extra space */
1834*1b191cb5SApple OSS Distributions 		unsigned int uptrs_len = (unsigned int)uptrs_count + 32;
1835*1b191cb5SApple OSS Distributions 
1836*1b191cb5SApple OSS Distributions 		uint64_t * uptrs_alloc = malloc(sizeof(uint64_t) * uptrs_len);
1837*1b191cb5SApple OSS Distributions 		os_assert(uptrs_alloc != NULL);
1838*1b191cb5SApple OSS Distributions 
1839*1b191cb5SApple OSS Distributions 		uptrs_count = proc_list_uptrs((int)pid, uptrs_alloc, (uint32_t)(sizeof(uint64_t) * uptrs_len));
1840*1b191cb5SApple OSS Distributions 		printf("process %d has %d uptrs:\n", (int)pid, uptrs_count);
1841*1b191cb5SApple OSS Distributions 		if (uptrs_count > (int)uptrs_len) {
1842*1b191cb5SApple OSS Distributions 			uptrs_count = (int)uptrs_len;
1843*1b191cb5SApple OSS Distributions 		}
1844*1b191cb5SApple OSS Distributions 		for (int j = 0; j < uptrs_count; j++) {
1845*1b191cb5SApple OSS Distributions 			printf("%#17" PRIx64 "\n", uptrs_alloc[j]);
1846*1b191cb5SApple OSS Distributions 		}
1847*1b191cb5SApple OSS Distributions 	}
1848*1b191cb5SApple OSS Distributions }
1849*1b191cb5SApple OSS Distributions 
1850*1b191cb5SApple OSS Distributions T_DECL(proc_list_uptrs, "the kernel should return any up-pointers it knows about")
1851*1b191cb5SApple OSS Distributions {
1852*1b191cb5SApple OSS Distributions 	if (argc > 0) {
1853*1b191cb5SApple OSS Distributions 		print_uptrs(argc, argv);
1854*1b191cb5SApple OSS Distributions 		T_SKIP("command line invocation of tool, not test");
1855*1b191cb5SApple OSS Distributions 	}
1856*1b191cb5SApple OSS Distributions 
1857*1b191cb5SApple OSS Distributions 	unsigned int cur_uptr = 0;
1858*1b191cb5SApple OSS Distributions 
1859*1b191cb5SApple OSS Distributions 	int kq = kqueue();
1860*1b191cb5SApple OSS Distributions 	T_QUIET;
1861*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(kq, "kqueue");
1862*1b191cb5SApple OSS Distributions 
1863*1b191cb5SApple OSS Distributions 	/*
1864*1b191cb5SApple OSS Distributions 	 * Should find uptrs on file-type knotes and generic knotes (two
1865*1b191cb5SApple OSS Distributions 	 * different search locations, internally).
1866*1b191cb5SApple OSS Distributions 	 */
1867*1b191cb5SApple OSS Distributions 	struct kevent64_s events[2];
1868*1b191cb5SApple OSS Distributions 	memset(events, 0, sizeof(events));
1869*1b191cb5SApple OSS Distributions 
1870*1b191cb5SApple OSS Distributions 	uptr_names[cur_uptr] = "kqueue file-backed knote";
1871*1b191cb5SApple OSS Distributions 	events[0].filter     = EVFILT_WRITE;
1872*1b191cb5SApple OSS Distributions 	events[0].ident      = STDOUT_FILENO;
1873*1b191cb5SApple OSS Distributions 	events[0].flags      = EV_ADD;
1874*1b191cb5SApple OSS Distributions 	events[0].udata      = uptrs[cur_uptr++];
1875*1b191cb5SApple OSS Distributions 
1876*1b191cb5SApple OSS Distributions 	uptr_names[cur_uptr] = "kqueue non-file-backed knote";
1877*1b191cb5SApple OSS Distributions 	events[1].filter     = EVFILT_USER;
1878*1b191cb5SApple OSS Distributions 	events[1].ident      = 1;
1879*1b191cb5SApple OSS Distributions 	events[1].flags      = EV_ADD;
1880*1b191cb5SApple OSS Distributions 	events[1].udata      = uptrs[cur_uptr++];
1881*1b191cb5SApple OSS Distributions 
1882*1b191cb5SApple OSS Distributions 	int kev_err = kevent64(kq, events, sizeof(events) / sizeof(events[0]), NULL, 0, KEVENT_FLAG_IMMEDIATE, NULL);
1883*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(kev_err, "register events with kevent64");
1884*1b191cb5SApple OSS Distributions 
1885*1b191cb5SApple OSS Distributions 	/*
1886*1b191cb5SApple OSS Distributions 	 * Should find uptrs both on a kevent_id kqueue and in a workloop
1887*1b191cb5SApple OSS Distributions 	 * kqueue's knote's udata field.
1888*1b191cb5SApple OSS Distributions 	 */
1889*1b191cb5SApple OSS Distributions 	uptr_names[cur_uptr] = "dynamic kqueue non-file-backed knote";
1890*1b191cb5SApple OSS Distributions 	struct kevent_qos_s events_id[] = {{
1891*1b191cb5SApple OSS Distributions 						   .filter = EVFILT_USER,
1892*1b191cb5SApple OSS Distributions 						   .ident = 1,
1893*1b191cb5SApple OSS Distributions 						   .flags = EV_ADD,
1894*1b191cb5SApple OSS Distributions 						   .qos = (int)_pthread_qos_class_encode(QOS_CLASS_DEFAULT, 0, 0),
1895*1b191cb5SApple OSS Distributions 						   .udata = uptrs[cur_uptr++]
1896*1b191cb5SApple OSS Distributions 					   }};
1897*1b191cb5SApple OSS Distributions 
1898*1b191cb5SApple OSS Distributions 	uptr_names[cur_uptr] = "dynamic kqueue ID";
1899*1b191cb5SApple OSS Distributions 	kev_err = kevent_id(uptrs[cur_uptr++], events_id, 1, NULL, 0, NULL, NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_IMMEDIATE);
1900*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(kev_err, "register event with kevent_id");
1901*1b191cb5SApple OSS Distributions 
1902*1b191cb5SApple OSS Distributions 	errno           = 0;
1903*1b191cb5SApple OSS Distributions 	int uptrs_count = proc_list_uptrs(getpid(), NULL, 0);
1904*1b191cb5SApple OSS Distributions 	T_QUIET;
1905*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(uptrs_count, "proc_list_uptrs");
1906*1b191cb5SApple OSS Distributions 	T_QUIET;
1907*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(uptrs_count, NUPTRS, "should see correct number of up-pointers");
1908*1b191cb5SApple OSS Distributions 
1909*1b191cb5SApple OSS Distributions 	uint64_t uptrs_obs[NUPTRS] = {0};
1910*1b191cb5SApple OSS Distributions 	uptrs_count                = proc_list_uptrs(getpid(), uptrs_obs, sizeof(uptrs_obs));
1911*1b191cb5SApple OSS Distributions 	T_QUIET;
1912*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(uptrs_count, "proc_list_uptrs");
1913*1b191cb5SApple OSS Distributions 
1914*1b191cb5SApple OSS Distributions 	for (int i = 0; i < uptrs_count; i++) {
1915*1b191cb5SApple OSS Distributions 		int found = -1;
1916*1b191cb5SApple OSS Distributions 		for (int j = 0; j < NUPTRS; j++) {
1917*1b191cb5SApple OSS Distributions 			if (uptrs_obs[i] == uptrs[j]) {
1918*1b191cb5SApple OSS Distributions 				found = j;
1919*1b191cb5SApple OSS Distributions 				goto next;
1920*1b191cb5SApple OSS Distributions 			}
1921*1b191cb5SApple OSS Distributions 		}
1922*1b191cb5SApple OSS Distributions 		T_FAIL("unexpected up-pointer found: %#" PRIx64, uptrs_obs[i]);
1923*1b191cb5SApple OSS Distributions next:           ;
1924*1b191cb5SApple OSS Distributions 		if (found != -1) {
1925*1b191cb5SApple OSS Distributions 			T_PASS("found up-pointer for %s", uptr_names[found]);
1926*1b191cb5SApple OSS Distributions 		}
1927*1b191cb5SApple OSS Distributions 	}
1928*1b191cb5SApple OSS Distributions 
1929*1b191cb5SApple OSS Distributions 	uint64_t up_overflow[2] = {0};
1930*1b191cb5SApple OSS Distributions 	uptrs_count = proc_list_uptrs(getpid(), up_overflow, sizeof(uint64_t) + 1);
1931*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(up_overflow[1], (uint64_t)0, "overflow check");
1932*1b191cb5SApple OSS Distributions }
1933*1b191cb5SApple OSS Distributions 
1934*1b191cb5SApple OSS Distributions #pragma mark dynamic kqueue info
1935*1b191cb5SApple OSS Distributions 
1936*1b191cb5SApple OSS Distributions #define EXPECTED_ID UINT64_C(0x1122334455667788)
1937*1b191cb5SApple OSS Distributions #define EXPECTED_UDATA UINT64_C(0x99aabbccddeeff00)
1938*1b191cb5SApple OSS Distributions #ifndef KQ_WORKLOOP
1939*1b191cb5SApple OSS Distributions #define KQ_WORKLOOP 0x80
1940*1b191cb5SApple OSS Distributions #endif
1941*1b191cb5SApple OSS Distributions 
1942*1b191cb5SApple OSS Distributions static void
setup_kevent_id(kqueue_id_t id)1943*1b191cb5SApple OSS Distributions setup_kevent_id(kqueue_id_t id)
1944*1b191cb5SApple OSS Distributions {
1945*1b191cb5SApple OSS Distributions 	struct kevent_qos_s events_id[] = {{
1946*1b191cb5SApple OSS Distributions 						   .filter = EVFILT_USER,
1947*1b191cb5SApple OSS Distributions 						   .ident = 1,
1948*1b191cb5SApple OSS Distributions 						   .flags = EV_ADD,
1949*1b191cb5SApple OSS Distributions 						   .qos = (int)_pthread_qos_class_encode(QOS_CLASS_DEFAULT, 0, 0),
1950*1b191cb5SApple OSS Distributions 						   .udata = EXPECTED_UDATA
1951*1b191cb5SApple OSS Distributions 					   }};
1952*1b191cb5SApple OSS Distributions 
1953*1b191cb5SApple OSS Distributions 	int err = kevent_id(id, events_id, 1, NULL, 0, NULL, NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_IMMEDIATE);
1954*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(err, "register event with kevent_id");
1955*1b191cb5SApple OSS Distributions }
1956*1b191cb5SApple OSS Distributions 
1957*1b191cb5SApple OSS Distributions static kqueue_id_t *
list_kqids(pid_t pid,int * nkqids_out)1958*1b191cb5SApple OSS Distributions list_kqids(pid_t pid, int * nkqids_out)
1959*1b191cb5SApple OSS Distributions {
1960*1b191cb5SApple OSS Distributions 	int kqids_len = 256;
1961*1b191cb5SApple OSS Distributions 	int nkqids;
1962*1b191cb5SApple OSS Distributions 	kqueue_id_t * kqids = NULL;
1963*1b191cb5SApple OSS Distributions 	uint32_t kqids_size;
1964*1b191cb5SApple OSS Distributions 
1965*1b191cb5SApple OSS Distributions retry:
1966*1b191cb5SApple OSS Distributions 	if (os_mul_overflow(sizeof(kqueue_id_t), kqids_len, &kqids_size)) {
1967*1b191cb5SApple OSS Distributions 		T_QUIET;
1968*1b191cb5SApple OSS Distributions 		T_ASSERT_GT(kqids_len, PROC_PIDDYNKQUEUES_MAX, NULL);
1969*1b191cb5SApple OSS Distributions 		kqids_len = PROC_PIDDYNKQUEUES_MAX;
1970*1b191cb5SApple OSS Distributions 		goto retry;
1971*1b191cb5SApple OSS Distributions 	}
1972*1b191cb5SApple OSS Distributions 	if (!kqids) {
1973*1b191cb5SApple OSS Distributions 		kqids = malloc(kqids_size);
1974*1b191cb5SApple OSS Distributions 		T_QUIET;
1975*1b191cb5SApple OSS Distributions 		T_ASSERT_NOTNULL(kqids, "malloc(%" PRIu32 ")", kqids_size);
1976*1b191cb5SApple OSS Distributions 	}
1977*1b191cb5SApple OSS Distributions 
1978*1b191cb5SApple OSS Distributions 	nkqids = proc_list_dynkqueueids(pid, kqids, kqids_size);
1979*1b191cb5SApple OSS Distributions 	if (nkqids > kqids_len && kqids_len < PROC_PIDDYNKQUEUES_MAX) {
1980*1b191cb5SApple OSS Distributions 		kqids_len *= 2;
1981*1b191cb5SApple OSS Distributions 		if (kqids_len > PROC_PIDDYNKQUEUES_MAX) {
1982*1b191cb5SApple OSS Distributions 			kqids_len = PROC_PIDDYNKQUEUES_MAX;
1983*1b191cb5SApple OSS Distributions 		}
1984*1b191cb5SApple OSS Distributions 		free(kqids);
1985*1b191cb5SApple OSS Distributions 		kqids = NULL;
1986*1b191cb5SApple OSS Distributions 		goto retry;
1987*1b191cb5SApple OSS Distributions 	}
1988*1b191cb5SApple OSS Distributions 
1989*1b191cb5SApple OSS Distributions 	*nkqids_out = nkqids;
1990*1b191cb5SApple OSS Distributions 	return kqids;
1991*1b191cb5SApple OSS Distributions }
1992*1b191cb5SApple OSS Distributions 
1993*1b191cb5SApple OSS Distributions T_DECL(list_dynamic_kqueues, "the kernel should list IDs of dynamic kqueues", T_META_ALL_VALID_ARCHS(true))
1994*1b191cb5SApple OSS Distributions {
1995*1b191cb5SApple OSS Distributions 	int nkqids;
1996*1b191cb5SApple OSS Distributions 	bool found = false;
1997*1b191cb5SApple OSS Distributions 
1998*1b191cb5SApple OSS Distributions 	setup_kevent_id(EXPECTED_ID);
1999*1b191cb5SApple OSS Distributions 	kqueue_id_t * kqids = list_kqids(getpid(), &nkqids);
2000*1b191cb5SApple OSS Distributions 	T_ASSERT_GE(nkqids, 1, "at least one dynamic kqueue is listed");
2001*1b191cb5SApple OSS Distributions 	for (int i = 0; i < nkqids; i++) {
2002*1b191cb5SApple OSS Distributions 		if (kqids[i] == EXPECTED_ID) {
2003*1b191cb5SApple OSS Distributions 			found = true;
2004*1b191cb5SApple OSS Distributions 			T_PASS("found expected dynamic kqueue ID");
2005*1b191cb5SApple OSS Distributions 		} else {
2006*1b191cb5SApple OSS Distributions 			T_LOG("found another dynamic kqueue with ID %#" PRIx64, kqids[i]);
2007*1b191cb5SApple OSS Distributions 		}
2008*1b191cb5SApple OSS Distributions 	}
2009*1b191cb5SApple OSS Distributions 
2010*1b191cb5SApple OSS Distributions 	if (!found) {
2011*1b191cb5SApple OSS Distributions 		T_FAIL("could not find dynamic ID of kqueue created");
2012*1b191cb5SApple OSS Distributions 	}
2013*1b191cb5SApple OSS Distributions 
2014*1b191cb5SApple OSS Distributions 	free(kqids);
2015*1b191cb5SApple OSS Distributions }
2016*1b191cb5SApple OSS Distributions 
2017*1b191cb5SApple OSS Distributions T_DECL(dynamic_kqueue_basic_info, "the kernel should report valid basic dynamic kqueue info", T_META_ALL_VALID_ARCHS(true))
2018*1b191cb5SApple OSS Distributions {
2019*1b191cb5SApple OSS Distributions 	struct kqueue_info kqinfo;
2020*1b191cb5SApple OSS Distributions 	int ret;
2021*1b191cb5SApple OSS Distributions 
2022*1b191cb5SApple OSS Distributions 	setup_kevent_id(EXPECTED_ID);
2023*1b191cb5SApple OSS Distributions 	ret = proc_piddynkqueueinfo(getpid(), PROC_PIDDYNKQUEUE_INFO, EXPECTED_ID, &kqinfo, sizeof(kqinfo));
2024*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_piddynkqueueinfo(... PROC_PIDDYNKQUEUE_INFO ...)");
2025*1b191cb5SApple OSS Distributions 	T_QUIET;
2026*1b191cb5SApple OSS Distributions 	T_ASSERT_GE(ret, (int)sizeof(kqinfo), "PROC_PIDDYNKQUEUE_INFO should return the right size");
2027*1b191cb5SApple OSS Distributions 
2028*1b191cb5SApple OSS Distributions 	T_EXPECT_NE(kqinfo.kq_state & KQ_WORKLOOP, 0U, "kqueue info should be for a workloop kqueue");
2029*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(kqinfo.kq_stat.vst_ino, EXPECTED_ID, "inode field should be the kqueue's ID");
2030*1b191cb5SApple OSS Distributions }
2031*1b191cb5SApple OSS Distributions 
2032*1b191cb5SApple OSS Distributions T_DECL(dynamic_kqueue_extended_info, "the kernel should report valid extended dynamic kqueue info", T_META_ALL_VALID_ARCHS(true))
2033*1b191cb5SApple OSS Distributions {
2034*1b191cb5SApple OSS Distributions 	struct kevent_extinfo kqextinfo[1];
2035*1b191cb5SApple OSS Distributions 	int ret;
2036*1b191cb5SApple OSS Distributions 
2037*1b191cb5SApple OSS Distributions 	setup_kevent_id(EXPECTED_ID);
2038*1b191cb5SApple OSS Distributions 	ret = proc_piddynkqueueinfo(getpid(), PROC_PIDDYNKQUEUE_EXTINFO, EXPECTED_ID, kqextinfo, sizeof(kqextinfo));
2039*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "proc_piddynkqueueinfo(... PROC_PIDDYNKQUEUE_EXTINFO ...)");
2040*1b191cb5SApple OSS Distributions 	T_QUIET;
2041*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(ret, 1, "PROC_PIDDYNKQUEUE_EXTINFO should return a single knote");
2042*1b191cb5SApple OSS Distributions 
2043*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(kqextinfo[0].kqext_kev.ident, 1ULL, "kevent identifier matches what was configured");
2044*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(kqextinfo[0].kqext_kev.filter, (short)EVFILT_USER, "kevent filter matches what was configured");
2045*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(kqextinfo[0].kqext_kev.udata, EXPECTED_UDATA, "kevent udata matches what was configured");
2046*1b191cb5SApple OSS Distributions }
2047*1b191cb5SApple OSS Distributions 
2048*1b191cb5SApple OSS Distributions #pragma mark proc_listpids
2049*1b191cb5SApple OSS Distributions 
2050*1b191cb5SApple OSS Distributions T_DECL(list_kdebug_pids, "the kernel should report processes that are filtered by kdebug",
2051*1b191cb5SApple OSS Distributions     T_META_ASROOT(YES), T_META_RUN_CONCURRENTLY(false))
2052*1b191cb5SApple OSS Distributions {
2053*1b191cb5SApple OSS Distributions 	int mib[4] = {CTL_KERN, KERN_KDEBUG};
2054*1b191cb5SApple OSS Distributions 	int npids;
2055*1b191cb5SApple OSS Distributions 	int pids[1];
2056*1b191cb5SApple OSS Distributions 	int ret;
2057*1b191cb5SApple OSS Distributions 	kd_regtype reg;
2058*1b191cb5SApple OSS Distributions 	size_t regsize = sizeof(reg);
2059*1b191cb5SApple OSS Distributions 
2060*1b191cb5SApple OSS Distributions 	mib[2] = KERN_KDREMOVE;
2061*1b191cb5SApple OSS Distributions 	ret    = sysctl(mib, 3, NULL, NULL, NULL, 0);
2062*1b191cb5SApple OSS Distributions 	T_QUIET;
2063*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "KERN_KDREMOVE sysctl");
2064*1b191cb5SApple OSS Distributions 
2065*1b191cb5SApple OSS Distributions 	mib[2] = KERN_KDSETBUF;
2066*1b191cb5SApple OSS Distributions 	mib[3] = 100000;
2067*1b191cb5SApple OSS Distributions 	ret    = sysctl(mib, 4, NULL, NULL, NULL, 0);
2068*1b191cb5SApple OSS Distributions 	T_QUIET;
2069*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "KERN_KDSETBUF sysctl");
2070*1b191cb5SApple OSS Distributions 
2071*1b191cb5SApple OSS Distributions 	mib[2] = KERN_KDSETUP;
2072*1b191cb5SApple OSS Distributions 	ret    = sysctl(mib, 3, NULL, NULL, NULL, 0);
2073*1b191cb5SApple OSS Distributions 	T_QUIET;
2074*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "KERN_KDSETUP sysctl");
2075*1b191cb5SApple OSS Distributions 
2076*1b191cb5SApple OSS Distributions 	npids = proc_listpids(PROC_KDBG_ONLY, 0, pids, sizeof(pids));
2077*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(npids, 0, "no processes should be filtered initially");
2078*1b191cb5SApple OSS Distributions 
2079*1b191cb5SApple OSS Distributions 	reg.type   = KDBG_TYPENONE;
2080*1b191cb5SApple OSS Distributions 	reg.value1 = (unsigned int)getpid();
2081*1b191cb5SApple OSS Distributions 	reg.value2 = 1; /* set the pid in the filter */
2082*1b191cb5SApple OSS Distributions 	mib[2]     = KERN_KDPIDTR;
2083*1b191cb5SApple OSS Distributions 	ret        = sysctl(mib, 3, &reg, &regsize, NULL, 0);
2084*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "KERN_KDPIDTR sysctl to set a pid in the filter");
2085*1b191cb5SApple OSS Distributions 
2086*1b191cb5SApple OSS Distributions 	npids = proc_listpids(PROC_KDBG_ONLY, 0, pids, sizeof(pids));
2087*1b191cb5SApple OSS Distributions 	npids /= 4;
2088*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(npids, 1, "a process should be filtered");
2089*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(pids[0], getpid(), "process filtered should be the one that was set");
2090*1b191cb5SApple OSS Distributions 
2091*1b191cb5SApple OSS Distributions 	mib[2] = KERN_KDREMOVE;
2092*1b191cb5SApple OSS Distributions 	ret    = sysctl(mib, 3, NULL, NULL, NULL, 0);
2093*1b191cb5SApple OSS Distributions 	T_QUIET;
2094*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "KERN_KDREMOVE sysctl");
2095*1b191cb5SApple OSS Distributions }
2096*1b191cb5SApple OSS Distributions 
2097*1b191cb5SApple OSS Distributions #pragma mark misc
2098*1b191cb5SApple OSS Distributions 
2099*1b191cb5SApple OSS Distributions static int prf_fd;
2100*1b191cb5SApple OSS Distributions static char prf_path[PATH_MAX];
2101*1b191cb5SApple OSS Distributions static void
prf_end(void)2102*1b191cb5SApple OSS Distributions prf_end(void)
2103*1b191cb5SApple OSS Distributions {
2104*1b191cb5SApple OSS Distributions 	close(prf_fd);
2105*1b191cb5SApple OSS Distributions 	unlink(prf_path);
2106*1b191cb5SApple OSS Distributions }
2107*1b191cb5SApple OSS Distributions 
2108*1b191cb5SApple OSS Distributions T_DECL(proc_regionfilename, "proc_regionfilename() should work")
2109*1b191cb5SApple OSS Distributions {
2110*1b191cb5SApple OSS Distributions 	static char expected[] = "'very rigorous maritime engineering standards' && the front fell off";
2111*1b191cb5SApple OSS Distributions 	static char real[sizeof(expected)];
2112*1b191cb5SApple OSS Distributions 	int rc;
2113*1b191cb5SApple OSS Distributions 	void *addr;
2114*1b191cb5SApple OSS Distributions 
2115*1b191cb5SApple OSS Distributions 	prf_fd = CONF_TMP_FILE_OPEN(prf_path);
2116*1b191cb5SApple OSS Distributions 	T_ATEND(prf_end);
2117*1b191cb5SApple OSS Distributions 
2118*1b191cb5SApple OSS Distributions 	rc = (int) write(prf_fd, expected, sizeof(expected));
2119*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(rc, "write to tmpfile");
2120*1b191cb5SApple OSS Distributions 
2121*1b191cb5SApple OSS Distributions 	addr = mmap(0, 0x1000, PROT_READ, MAP_PRIVATE, prf_fd, 0);
2122*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO;
2123*1b191cb5SApple OSS Distributions 	T_ASSERT_NE_PTR(addr, MAP_FAILED, "mmap of tmpfile");
2124*1b191cb5SApple OSS Distributions 
2125*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO;
2126*1b191cb5SApple OSS Distributions 	T_ASSERT_GT(proc_regionfilename(getpid(), (uint64_t) addr, real, MAXPATHLEN), 0, "proc_regionfilename");
2127*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ_STR(basename(prf_path), basename(real), "filename");
2128*1b191cb5SApple OSS Distributions }
2129*1b191cb5SApple OSS Distributions 
2130*1b191cb5SApple OSS Distributions T_DECL(proc_regionpath, "PROC_PIDREGIONPATH should return addr, length and path")
2131*1b191cb5SApple OSS Distributions {
2132*1b191cb5SApple OSS Distributions 	int rc;
2133*1b191cb5SApple OSS Distributions 	struct proc_regionpath path;
2134*1b191cb5SApple OSS Distributions 	static char some_text[] = "'very rigorous maritime engineering standards' && the front fell off";
2135*1b191cb5SApple OSS Distributions 	unsigned long rounded_length = (sizeof(some_text) & (unsigned long) ~(PAGE_SIZE - 1)) + PAGE_SIZE;
2136*1b191cb5SApple OSS Distributions 	void *addr;
2137*1b191cb5SApple OSS Distributions 
2138*1b191cb5SApple OSS Distributions 	prf_fd = CONF_TMP_FILE_OPEN(prf_path);
2139*1b191cb5SApple OSS Distributions 	T_ATEND(prf_end);
2140*1b191cb5SApple OSS Distributions 
2141*1b191cb5SApple OSS Distributions 	rc = (int) write(prf_fd, some_text, sizeof(some_text));
2142*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(rc, "write to tmpfile");
2143*1b191cb5SApple OSS Distributions 
2144*1b191cb5SApple OSS Distributions 	addr = mmap(0, PAGE_SIZE, PROT_READ, MAP_PRIVATE, prf_fd, 0);
2145*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO;
2146*1b191cb5SApple OSS Distributions 	T_ASSERT_NE_PTR(addr, MAP_FAILED, "mmap of tmpfile");
2147*1b191cb5SApple OSS Distributions 
2148*1b191cb5SApple OSS Distributions 	rc = proc_pidinfo(getpid(), PROC_PIDREGIONPATH, (uint64_t)addr, &path, sizeof(struct proc_regionpath));
2149*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(rc, "proc_pidinfo");
2150*1b191cb5SApple OSS Distributions 
2151*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ((unsigned long) path.prpo_regionlength, rounded_length, "regionlength must match");
2152*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_PTR((void *) path.prpo_addr, addr, "addr must match");
2153*1b191cb5SApple OSS Distributions 
2154*1b191cb5SApple OSS Distributions 	rc = proc_pidinfo(getpid(), PROC_PIDREGIONPATH, (uint64_t)((char *) addr + 20), &path, sizeof(struct proc_regionpath));
2155*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(rc, "proc_pidinfo 20 bytes past the base address");
2156*1b191cb5SApple OSS Distributions 
2157*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ((unsigned long) path.prpo_regionlength, rounded_length, "regionlength must match, even when 20 bytes past the base address");
2158*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_PTR((void *) path.prpo_addr, addr, "addr must match, even when 20 bytes past the base address");
2159*1b191cb5SApple OSS Distributions }
2160