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