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