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