xref: /xnu-12377.41.6/tests/port_table_limits_client.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions #include <stdio.h>
2*bbb1b6f9SApple OSS Distributions #include <mach/mach.h>
3*bbb1b6f9SApple OSS Distributions #include <mach/message.h>
4*bbb1b6f9SApple OSS Distributions #include <unistd.h>
5*bbb1b6f9SApple OSS Distributions #include <assert.h>
6*bbb1b6f9SApple OSS Distributions #include <string.h>
7*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
8*bbb1b6f9SApple OSS Distributions #include <bootstrap.h>
9*bbb1b6f9SApple OSS Distributions 
10*bbb1b6f9SApple OSS Distributions static int
11*bbb1b6f9SApple OSS Distributions connect_to_server(void);
12*bbb1b6f9SApple OSS Distributions 
13*bbb1b6f9SApple OSS Distributions typedef struct {
14*bbb1b6f9SApple OSS Distributions 	mach_msg_header_t   header;
15*bbb1b6f9SApple OSS Distributions 	mach_msg_body_t     body;
16*bbb1b6f9SApple OSS Distributions 	mach_msg_port_descriptor_t port_descriptor;
17*bbb1b6f9SApple OSS Distributions 	mach_msg_trailer_t  trailer;            // subtract this when sending
18*bbb1b6f9SApple OSS Distributions } ipc_complex_message;
19*bbb1b6f9SApple OSS Distributions 
20*bbb1b6f9SApple OSS Distributions static ipc_complex_message icm_request = {};
21*bbb1b6f9SApple OSS Distributions 
22*bbb1b6f9SApple OSS Distributions struct args {
23*bbb1b6f9SApple OSS Distributions 	const char *progname;
24*bbb1b6f9SApple OSS Distributions 	int verbose;
25*bbb1b6f9SApple OSS Distributions 	int voucher;
26*bbb1b6f9SApple OSS Distributions 	int num_msgs;
27*bbb1b6f9SApple OSS Distributions 	const char *server_port_name;
28*bbb1b6f9SApple OSS Distributions 	mach_port_t server_port;
29*bbb1b6f9SApple OSS Distributions 	mach_port_t reply_port;
30*bbb1b6f9SApple OSS Distributions 	int request_msg_size;
31*bbb1b6f9SApple OSS Distributions 	void *request_msg;
32*bbb1b6f9SApple OSS Distributions 	int reply_msg_size;
33*bbb1b6f9SApple OSS Distributions 	void *reply_msg;
34*bbb1b6f9SApple OSS Distributions 	uint32_t persona_id;
35*bbb1b6f9SApple OSS Distributions 	long client_pid;
36*bbb1b6f9SApple OSS Distributions };
37*bbb1b6f9SApple OSS Distributions 
38*bbb1b6f9SApple OSS Distributions static void
parse_args(struct args * args)39*bbb1b6f9SApple OSS Distributions parse_args(struct args *args)
40*bbb1b6f9SApple OSS Distributions {
41*bbb1b6f9SApple OSS Distributions 	args->verbose = 0;
42*bbb1b6f9SApple OSS Distributions 	args->voucher = 0;
43*bbb1b6f9SApple OSS Distributions 	args->server_port_name = "TEST_PORT_TABLE_LIMITS";
44*bbb1b6f9SApple OSS Distributions 	args->server_port = MACH_PORT_NULL;
45*bbb1b6f9SApple OSS Distributions 	args->reply_port = MACH_PORT_NULL;
46*bbb1b6f9SApple OSS Distributions 	args->num_msgs = 1;
47*bbb1b6f9SApple OSS Distributions 	args->request_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
48*bbb1b6f9SApple OSS Distributions 	args->reply_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
49*bbb1b6f9SApple OSS Distributions 	args->request_msg = &icm_request;
50*bbb1b6f9SApple OSS Distributions 	args->reply_msg = NULL;
51*bbb1b6f9SApple OSS Distributions 	args->client_pid = getpid();
52*bbb1b6f9SApple OSS Distributions }
53*bbb1b6f9SApple OSS Distributions 
54*bbb1b6f9SApple OSS Distributions static int
connect_to_server(void)55*bbb1b6f9SApple OSS Distributions connect_to_server(void)
56*bbb1b6f9SApple OSS Distributions {
57*bbb1b6f9SApple OSS Distributions 	struct args client_args = {};
58*bbb1b6f9SApple OSS Distributions 	parse_args(&client_args);
59*bbb1b6f9SApple OSS Distributions 	mach_port_t reply_port, dummy_port;
60*bbb1b6f9SApple OSS Distributions 
61*bbb1b6f9SApple OSS Distributions 	/* Find the bootstrap port */
62*bbb1b6f9SApple OSS Distributions 	mach_port_t bsport;
63*bbb1b6f9SApple OSS Distributions 	kern_return_t ret = task_get_bootstrap_port(mach_task_self(), &bsport);
64*bbb1b6f9SApple OSS Distributions 	if (ret) {
65*bbb1b6f9SApple OSS Distributions 		mach_error("client: task_get_bootstrap_port()", ret);
66*bbb1b6f9SApple OSS Distributions 		exit(1);
67*bbb1b6f9SApple OSS Distributions 	}
68*bbb1b6f9SApple OSS Distributions 
69*bbb1b6f9SApple OSS Distributions 	printf("client: Look up bootstrap service port\n");
70*bbb1b6f9SApple OSS Distributions 	ret = bootstrap_look_up(bsport, client_args.server_port_name,
71*bbb1b6f9SApple OSS Distributions 	    &client_args.server_port);
72*bbb1b6f9SApple OSS Distributions 	if (ret) {
73*bbb1b6f9SApple OSS Distributions 		mach_error("client: bootstrap_look_up()", ret);
74*bbb1b6f9SApple OSS Distributions 		exit(1);
75*bbb1b6f9SApple OSS Distributions 	}
76*bbb1b6f9SApple OSS Distributions 
77*bbb1b6f9SApple OSS Distributions 	printf("client: Allocate reply port\n");
78*bbb1b6f9SApple OSS Distributions 	ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &reply_port);
79*bbb1b6f9SApple OSS Distributions 	if (ret) {
80*bbb1b6f9SApple OSS Distributions 		mach_error("client: allocate reply port", ret);
81*bbb1b6f9SApple OSS Distributions 		exit(1);
82*bbb1b6f9SApple OSS Distributions 	}
83*bbb1b6f9SApple OSS Distributions 	ret = mach_port_insert_right(mach_task_self(), reply_port, reply_port, MACH_MSG_TYPE_MAKE_SEND );
84*bbb1b6f9SApple OSS Distributions 	if (ret) {
85*bbb1b6f9SApple OSS Distributions 		mach_error("client: allocate reply port", ret);
86*bbb1b6f9SApple OSS Distributions 		exit(1);
87*bbb1b6f9SApple OSS Distributions 	}
88*bbb1b6f9SApple OSS Distributions 
89*bbb1b6f9SApple OSS Distributions 	printf("client: Allocate dummy port\n");
90*bbb1b6f9SApple OSS Distributions 	ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &dummy_port);
91*bbb1b6f9SApple OSS Distributions 	if (ret) {
92*bbb1b6f9SApple OSS Distributions 		mach_error("client: allocate dummy port", ret);
93*bbb1b6f9SApple OSS Distributions 		exit(1);
94*bbb1b6f9SApple OSS Distributions 	}
95*bbb1b6f9SApple OSS Distributions 
96*bbb1b6f9SApple OSS Distributions 	/* Construct the message */
97*bbb1b6f9SApple OSS Distributions 	mach_msg_header_t *request = (mach_msg_header_t *)client_args.request_msg;
98*bbb1b6f9SApple OSS Distributions 	request->msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_COPY_SEND,
99*bbb1b6f9SApple OSS Distributions 	    0, 0) | MACH_MSGH_BITS_COMPLEX;
100*bbb1b6f9SApple OSS Distributions 	request->msgh_size = (mach_msg_size_t)client_args.request_msg_size;
101*bbb1b6f9SApple OSS Distributions 	request->msgh_remote_port = client_args.server_port;
102*bbb1b6f9SApple OSS Distributions 	request->msgh_local_port = reply_port;
103*bbb1b6f9SApple OSS Distributions 	request->msgh_id = 1;
104*bbb1b6f9SApple OSS Distributions 
105*bbb1b6f9SApple OSS Distributions 	ipc_complex_message *complexmsg = (ipc_complex_message *)request;
106*bbb1b6f9SApple OSS Distributions 	complexmsg->body.msgh_descriptor_count = 1;
107*bbb1b6f9SApple OSS Distributions 	complexmsg->port_descriptor.name = dummy_port;
108*bbb1b6f9SApple OSS Distributions 	complexmsg->port_descriptor.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;
109*bbb1b6f9SApple OSS Distributions 	complexmsg->port_descriptor.type = MACH_MSG_PORT_DESCRIPTOR;
110*bbb1b6f9SApple OSS Distributions 
111*bbb1b6f9SApple OSS Distributions 	mach_msg_option_t option = MACH_SEND_MSG | MACH_RCV_MSG;
112*bbb1b6f9SApple OSS Distributions 
113*bbb1b6f9SApple OSS Distributions 	printf("client: Sending request\n");
114*bbb1b6f9SApple OSS Distributions 	mach_msg_return_t mret = mach_msg(request,
115*bbb1b6f9SApple OSS Distributions 	    option,
116*bbb1b6f9SApple OSS Distributions 	    (mach_msg_size_t)client_args.request_msg_size,
117*bbb1b6f9SApple OSS Distributions 	    sizeof(ipc_complex_message),
118*bbb1b6f9SApple OSS Distributions 	    reply_port,
119*bbb1b6f9SApple OSS Distributions 	    MACH_MSG_TIMEOUT_NONE,
120*bbb1b6f9SApple OSS Distributions 	    MACH_PORT_NULL);
121*bbb1b6f9SApple OSS Distributions 
122*bbb1b6f9SApple OSS Distributions 	printf("client: Received reply\n");
123*bbb1b6f9SApple OSS Distributions 	if (mret) {
124*bbb1b6f9SApple OSS Distributions 		mach_error("client: mach_msg", mret);
125*bbb1b6f9SApple OSS Distributions 		exit(1);
126*bbb1b6f9SApple OSS Distributions 	}
127*bbb1b6f9SApple OSS Distributions 
128*bbb1b6f9SApple OSS Distributions 	return 0;
129*bbb1b6f9SApple OSS Distributions }
130*bbb1b6f9SApple OSS Distributions 
131*bbb1b6f9SApple OSS Distributions static inline mach_port_type_t
get_port_type(mach_port_t mp)132*bbb1b6f9SApple OSS Distributions get_port_type(mach_port_t mp)
133*bbb1b6f9SApple OSS Distributions {
134*bbb1b6f9SApple OSS Distributions 	mach_port_type_t type = 0;
135*bbb1b6f9SApple OSS Distributions 	mach_port_type(mach_task_self(), mp, &type);
136*bbb1b6f9SApple OSS Distributions 	return type;
137*bbb1b6f9SApple OSS Distributions }
138*bbb1b6f9SApple OSS Distributions 
139*bbb1b6f9SApple OSS Distributions int
main(int argc,char * argv[])140*bbb1b6f9SApple OSS Distributions main(int argc, char *argv[])
141*bbb1b6f9SApple OSS Distributions {
142*bbb1b6f9SApple OSS Distributions 	mach_port_t port = MACH_PORT_NULL;
143*bbb1b6f9SApple OSS Distributions 	kern_return_t retval = KERN_SUCCESS;
144*bbb1b6f9SApple OSS Distributions 	int soft_limit = 0;
145*bbb1b6f9SApple OSS Distributions 	int hard_limit = 0;
146*bbb1b6f9SApple OSS Distributions 	int test_num = 0;
147*bbb1b6f9SApple OSS Distributions 	if (argc == 4) {
148*bbb1b6f9SApple OSS Distributions 		soft_limit = atoi(argv[1]);
149*bbb1b6f9SApple OSS Distributions 		hard_limit = atoi(argv[2]);
150*bbb1b6f9SApple OSS Distributions 		test_num = atoi(argv[3]);
151*bbb1b6f9SApple OSS Distributions 	} else {
152*bbb1b6f9SApple OSS Distributions 		printf("Usage: ./port_table_limits_client <soft limit> <hard limit> <test_num>\n");
153*bbb1b6f9SApple OSS Distributions 		goto fail_and_exit;
154*bbb1b6f9SApple OSS Distributions 	}
155*bbb1b6f9SApple OSS Distributions 
156*bbb1b6f9SApple OSS Distributions 	mach_port_t task = mach_task_self();
157*bbb1b6f9SApple OSS Distributions 
158*bbb1b6f9SApple OSS Distributions 	if (test_num == 2) {
159*bbb1b6f9SApple OSS Distributions 		printf("client: Wait for a reply message from server before continuing port allocation\n");
160*bbb1b6f9SApple OSS Distributions 		int ret = connect_to_server();
161*bbb1b6f9SApple OSS Distributions 		if (ret) {
162*bbb1b6f9SApple OSS Distributions 			goto fail_and_exit;
163*bbb1b6f9SApple OSS Distributions 		}
164*bbb1b6f9SApple OSS Distributions 	}
165*bbb1b6f9SApple OSS Distributions 
166*bbb1b6f9SApple OSS Distributions 	printf("client: Starting the receive right allocation loop\n");
167*bbb1b6f9SApple OSS Distributions 	int i = 0;
168*bbb1b6f9SApple OSS Distributions 	while (!retval) {
169*bbb1b6f9SApple OSS Distributions 		retval = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port);
170*bbb1b6f9SApple OSS Distributions 		assert(retval == 0);
171*bbb1b6f9SApple OSS Distributions 		assert(get_port_type(port) == MACH_PORT_TYPE_RECEIVE);
172*bbb1b6f9SApple OSS Distributions 		if ((i % 1000) == 0) {
173*bbb1b6f9SApple OSS Distributions 			/* Print every port in the multiple of 1000 */
174*bbb1b6f9SApple OSS Distributions 			printf("client: Port #%d\n", i);
175*bbb1b6f9SApple OSS Distributions 			sleep(1);
176*bbb1b6f9SApple OSS Distributions 		}
177*bbb1b6f9SApple OSS Distributions 		if (i == hard_limit) {
178*bbb1b6f9SApple OSS Distributions 			printf("client: Hitting the hard limit\n");
179*bbb1b6f9SApple OSS Distributions 		}
180*bbb1b6f9SApple OSS Distributions 		if (i > hard_limit) {
181*bbb1b6f9SApple OSS Distributions 			printf("client: Putting child to sleep\n");
182*bbb1b6f9SApple OSS Distributions 			/* Add a sleep so that there is time for server to collect data */
183*bbb1b6f9SApple OSS Distributions 			sleep(5);
184*bbb1b6f9SApple OSS Distributions 		}
185*bbb1b6f9SApple OSS Distributions 		i++;
186*bbb1b6f9SApple OSS Distributions 	}
187*bbb1b6f9SApple OSS Distributions 
188*bbb1b6f9SApple OSS Distributions fail_and_exit:
189*bbb1b6f9SApple OSS Distributions 	exit(1);
190*bbb1b6f9SApple OSS Distributions }
191