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 #include <fcntl.h>
10 #include <sys/errno.h>
11 #include <sys/resource.h>
12 #include <dispatch/dispatch.h>
13 
14 static int
15 connect_to_server(void);
16 
17 typedef struct {
18 	mach_msg_header_t   header;
19 	mach_msg_body_t     body;
20 	mach_msg_port_descriptor_t port_descriptor;
21 	mach_msg_trailer_t  trailer;            // subtract this when sending
22 } ipc_complex_message;
23 
24 static ipc_complex_message icm_request = {};
25 
26 struct args {
27 	const char *progname;
28 	int verbose;
29 	int voucher;
30 	int num_msgs;
31 	const char *server_port_name;
32 	mach_port_t server_port;
33 	mach_port_t reply_port;
34 	int request_msg_size;
35 	void *request_msg;
36 	int reply_msg_size;
37 	void *reply_msg;
38 	uint32_t persona_id;
39 	long client_pid;
40 };
41 
42 static void
parse_args(struct args * args)43 parse_args(struct args *args)
44 {
45 	args->verbose = 0;
46 	args->voucher = 0;
47 	args->server_port_name = "TEST_KQWORKLOOP_LIMITS";
48 	args->server_port = MACH_PORT_NULL;
49 	args->reply_port = MACH_PORT_NULL;
50 	args->num_msgs = 1;
51 	args->request_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
52 	args->reply_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
53 	args->request_msg = &icm_request;
54 	args->reply_msg = NULL;
55 	args->client_pid = getpid();
56 }
57 
58 static int
connect_to_server(void)59 connect_to_server(void)
60 {
61 	struct args client_args = {};
62 	parse_args(&client_args);
63 
64 	/* Find the bootstrap port */
65 	mach_port_t bsport;
66 	kern_return_t ret = task_get_bootstrap_port(mach_task_self(), &bsport);
67 	if (ret) {
68 		mach_error("client: task_get_bootstrap_port()", ret);
69 		exit(1);
70 	}
71 
72 	printf("client: Look up bootstrap service port\n");
73 	ret = bootstrap_look_up(bsport, client_args.server_port_name,
74 	    &client_args.server_port);
75 	if (ret) {
76 		mach_error("client: bootstrap_look_up()", ret);
77 		exit(1);
78 	}
79 
80 	printf("client: Set service port as the resource notify port\n");
81 	ret = task_set_special_port(mach_task_self(), TASK_RESOURCE_NOTIFY_PORT, client_args.server_port);
82 	if (ret) {
83 		mach_error("client: task_set_special_port()", ret);
84 		exit(1);
85 	}
86 
87 	return 0;
88 }
89 
90 int
main(int argc,char * argv[])91 main(int argc, char *argv[])
92 {
93 	int fd = 0;
94 	int soft_limit = 0;
95 	int hard_limit = 0;
96 	int test_num = 0;
97 	int ret = 0;
98 	if (argc == 4) {
99 		soft_limit = atoi(argv[1]);
100 		hard_limit = atoi(argv[2]);
101 		test_num = atoi(argv[3]);
102 	} else {
103 		printf("Usage: ./kqworkloop_limits_client <soft limit> <hard limit> <test_num>\n");
104 		goto fail_and_exit;
105 	}
106 
107 	printf("client: soft limit = %d, hard limit = %d, test_num = %d\n", soft_limit, hard_limit, test_num);
108 
109 	// Connect to the server first before then spawning kqworkloops to hit the
110 	// limits
111 	if (test_num == 2) {
112 		ret = connect_to_server();
113 		if (ret) {
114 			goto fail_and_exit;
115 		}
116 	}
117 
118 
119 	printf("client: Starting the kqworkloop allocation loop\n");
120 	int i = 0;
121 	while (ret == 0) {
122 		/* Only hang 10 kqworkloops off of a single port */
123 		mach_port_t port;
124 		kern_return_t kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
125 		assert(kr == KERN_SUCCESS);
126 
127 		for (int j = 0; j < 10; j++) {
128 			/* Allocate a kqworkloop */
129 			dispatch_queue_t dq = dispatch_queue_create("leaked queue", DISPATCH_QUEUE_SERIAL);
130 			dispatch_source_t ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, port, 0, dq);
131 			dispatch_source_set_event_handler(ds, ^{
132 				printf("Received a message on port");
133 			});
134 			dispatch_activate(ds);
135 			dispatch_release(dq);
136 
137 			if ((i % 20) == 0) {
138 				/* Print the sources in the multiple of 20 */
139 				printf("client: Allocating source #%d\n", i);
140 				sleep(1);
141 			}
142 
143 			if (i == soft_limit) {
144 				printf("client: Hit the soft limit \n");
145 				if (!hard_limit) {
146 					exit(0);
147 				}
148 			}
149 
150 			if (hard_limit && i == hard_limit) {
151 				printf("client: Hit the hard limit\n");
152 				pause();
153 			}
154 
155 			i++;
156 		}
157 	}
158 
159 fail_and_exit:
160 	exit(91);
161 }
162