xref: /xnu-8796.121.2/tests/fd_table_limits_client.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions #include <stdio.h>
2*c54f35caSApple OSS Distributions #include <mach/mach.h>
3*c54f35caSApple OSS Distributions #include <mach/message.h>
4*c54f35caSApple OSS Distributions #include <unistd.h>
5*c54f35caSApple OSS Distributions #include <assert.h>
6*c54f35caSApple OSS Distributions #include <string.h>
7*c54f35caSApple OSS Distributions #include <stdlib.h>
8*c54f35caSApple OSS Distributions #include <bootstrap.h>
9*c54f35caSApple OSS Distributions #include <fcntl.h>
10*c54f35caSApple OSS Distributions #include <sys/errno.h>
11*c54f35caSApple OSS Distributions #include <sys/resource.h>
12*c54f35caSApple OSS Distributions 
13*c54f35caSApple OSS Distributions static int
14*c54f35caSApple OSS Distributions connect_to_server(void);
15*c54f35caSApple OSS Distributions 
16*c54f35caSApple OSS Distributions typedef struct {
17*c54f35caSApple OSS Distributions 	mach_msg_header_t   header;
18*c54f35caSApple OSS Distributions 	mach_msg_body_t     body;
19*c54f35caSApple OSS Distributions 	mach_msg_port_descriptor_t port_descriptor;
20*c54f35caSApple OSS Distributions 	mach_msg_trailer_t  trailer;            // subtract this when sending
21*c54f35caSApple OSS Distributions } ipc_complex_message;
22*c54f35caSApple OSS Distributions 
23*c54f35caSApple OSS Distributions static ipc_complex_message icm_request = {};
24*c54f35caSApple OSS Distributions 
25*c54f35caSApple OSS Distributions struct args {
26*c54f35caSApple OSS Distributions 	const char *progname;
27*c54f35caSApple OSS Distributions 	int verbose;
28*c54f35caSApple OSS Distributions 	int voucher;
29*c54f35caSApple OSS Distributions 	int num_msgs;
30*c54f35caSApple OSS Distributions 	const char *server_port_name;
31*c54f35caSApple OSS Distributions 	mach_port_t server_port;
32*c54f35caSApple OSS Distributions 	mach_port_t reply_port;
33*c54f35caSApple OSS Distributions 	int request_msg_size;
34*c54f35caSApple OSS Distributions 	void *request_msg;
35*c54f35caSApple OSS Distributions 	int reply_msg_size;
36*c54f35caSApple OSS Distributions 	void *reply_msg;
37*c54f35caSApple OSS Distributions 	uint32_t persona_id;
38*c54f35caSApple OSS Distributions 	long client_pid;
39*c54f35caSApple OSS Distributions };
40*c54f35caSApple OSS Distributions 
41*c54f35caSApple OSS Distributions static void
parse_args(struct args * args)42*c54f35caSApple OSS Distributions parse_args(struct args *args)
43*c54f35caSApple OSS Distributions {
44*c54f35caSApple OSS Distributions 	args->verbose = 0;
45*c54f35caSApple OSS Distributions 	args->voucher = 0;
46*c54f35caSApple OSS Distributions 	args->server_port_name = "TEST_FD_TABLE_LIMITS";
47*c54f35caSApple OSS Distributions 	args->server_port = MACH_PORT_NULL;
48*c54f35caSApple OSS Distributions 	args->reply_port = MACH_PORT_NULL;
49*c54f35caSApple OSS Distributions 	args->num_msgs = 1;
50*c54f35caSApple OSS Distributions 	args->request_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
51*c54f35caSApple OSS Distributions 	args->reply_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
52*c54f35caSApple OSS Distributions 	args->request_msg = &icm_request;
53*c54f35caSApple OSS Distributions 	args->reply_msg = NULL;
54*c54f35caSApple OSS Distributions 	args->client_pid = getpid();
55*c54f35caSApple OSS Distributions }
56*c54f35caSApple OSS Distributions 
57*c54f35caSApple OSS Distributions static int
connect_to_server(void)58*c54f35caSApple OSS Distributions connect_to_server(void)
59*c54f35caSApple OSS Distributions {
60*c54f35caSApple OSS Distributions 	struct args client_args = {};
61*c54f35caSApple OSS Distributions 	parse_args(&client_args);
62*c54f35caSApple OSS Distributions 
63*c54f35caSApple OSS Distributions 	/* Find the bootstrap port */
64*c54f35caSApple OSS Distributions 	mach_port_t bsport;
65*c54f35caSApple OSS Distributions 	kern_return_t ret = task_get_bootstrap_port(mach_task_self(), &bsport);
66*c54f35caSApple OSS Distributions 	if (ret) {
67*c54f35caSApple OSS Distributions 		mach_error("client: task_get_bootstrap_port()", ret);
68*c54f35caSApple OSS Distributions 		exit(1);
69*c54f35caSApple OSS Distributions 	}
70*c54f35caSApple OSS Distributions 
71*c54f35caSApple OSS Distributions 	printf("client: Look up bootstrap service port\n");
72*c54f35caSApple OSS Distributions 	ret = bootstrap_look_up(bsport, client_args.server_port_name,
73*c54f35caSApple OSS Distributions 	    &client_args.server_port);
74*c54f35caSApple OSS Distributions 	if (ret) {
75*c54f35caSApple OSS Distributions 		mach_error("client: bootstrap_look_up()", ret);
76*c54f35caSApple OSS Distributions 		exit(1);
77*c54f35caSApple OSS Distributions 	}
78*c54f35caSApple OSS Distributions 
79*c54f35caSApple OSS Distributions 	printf("client: Set service port as the resource notify port\n");
80*c54f35caSApple OSS Distributions 	ret = task_set_special_port(mach_task_self(), TASK_RESOURCE_NOTIFY_PORT, client_args.server_port);
81*c54f35caSApple OSS Distributions 	if (ret) {
82*c54f35caSApple OSS Distributions 		mach_error("client: task_set_special_port()", ret);
83*c54f35caSApple OSS Distributions 		exit(1);
84*c54f35caSApple OSS Distributions 	}
85*c54f35caSApple OSS Distributions 
86*c54f35caSApple OSS Distributions 	return 0;
87*c54f35caSApple OSS Distributions }
88*c54f35caSApple OSS Distributions 
89*c54f35caSApple OSS Distributions int
main(int argc,char * argv[])90*c54f35caSApple OSS Distributions main(int argc, char *argv[])
91*c54f35caSApple OSS Distributions {
92*c54f35caSApple OSS Distributions 	int fd = 0;
93*c54f35caSApple OSS Distributions 	int soft_limit = 0;
94*c54f35caSApple OSS Distributions 	int hard_limit = 0;
95*c54f35caSApple OSS Distributions 	int test_num = 0;
96*c54f35caSApple OSS Distributions 	int ret;
97*c54f35caSApple OSS Distributions 	if (argc == 4) {
98*c54f35caSApple OSS Distributions 		soft_limit = atoi(argv[1]);
99*c54f35caSApple OSS Distributions 		hard_limit = atoi(argv[2]);
100*c54f35caSApple OSS Distributions 		test_num = atoi(argv[3]);
101*c54f35caSApple OSS Distributions 	} else {
102*c54f35caSApple OSS Distributions 		printf("Usage: ./fd_table_limits_client <soft limit> <hard limit> <test_num>\n");
103*c54f35caSApple OSS Distributions 		goto fail_and_exit;
104*c54f35caSApple OSS Distributions 	}
105*c54f35caSApple OSS Distributions 
106*c54f35caSApple OSS Distributions 	printf("client: soft limit = %d, hard limit = %d, test_num = %d\n", soft_limit, hard_limit, test_num);
107*c54f35caSApple OSS Distributions 	if (test_num == 2) {
108*c54f35caSApple OSS Distributions 		ret = connect_to_server();
109*c54f35caSApple OSS Distributions 		if (ret) {
110*c54f35caSApple OSS Distributions 			goto fail_and_exit;
111*c54f35caSApple OSS Distributions 		}
112*c54f35caSApple OSS Distributions 	}
113*c54f35caSApple OSS Distributions 
114*c54f35caSApple OSS Distributions 	struct rlimit rl;
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions 	ret = getrlimit(RLIMIT_NOFILE, &rl);
117*c54f35caSApple OSS Distributions 	if (ret < 0) {
118*c54f35caSApple OSS Distributions 		printf("client: getrlimit failed with err = %d\n", errno);
119*c54f35caSApple OSS Distributions 		goto fail_and_exit;
120*c54f35caSApple OSS Distributions 	}
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions 	int new_rlim_max = 2000;
123*c54f35caSApple OSS Distributions 	if (rl.rlim_cur < new_rlim_max) {
124*c54f35caSApple OSS Distributions 		printf("client: raising file open limit from %llu (max %llu) to %d\n",
125*c54f35caSApple OSS Distributions 		    rl.rlim_cur, rl.rlim_max, new_rlim_max);
126*c54f35caSApple OSS Distributions 		rl.rlim_cur = new_rlim_max;
127*c54f35caSApple OSS Distributions 		rl.rlim_max = new_rlim_max;
128*c54f35caSApple OSS Distributions 		ret = setrlimit(RLIMIT_NOFILE, &rl);
129*c54f35caSApple OSS Distributions 		if (ret < 0) {
130*c54f35caSApple OSS Distributions 			printf("client: setrlimit failed with err = %d\n", errno);
131*c54f35caSApple OSS Distributions 			goto fail_and_exit;
132*c54f35caSApple OSS Distributions 		}
133*c54f35caSApple OSS Distributions 	}
134*c54f35caSApple OSS Distributions 
135*c54f35caSApple OSS Distributions 
136*c54f35caSApple OSS Distributions 	printf("client: Starting the fd allocation loop\n");
137*c54f35caSApple OSS Distributions 	int i = 0;
138*c54f35caSApple OSS Distributions 	while (i < new_rlim_max) {
139*c54f35caSApple OSS Distributions 		fd = open("/bin/ls", O_RDONLY | 0x08000000 /* O_CLOFORK */);
140*c54f35caSApple OSS Distributions 		if (fd < 0) {
141*c54f35caSApple OSS Distributions 			printf("open(/bin/ls) FD #%d failed with err = %d ", i, errno);
142*c54f35caSApple OSS Distributions 			goto fail_and_exit;
143*c54f35caSApple OSS Distributions 		}
144*c54f35caSApple OSS Distributions 
145*c54f35caSApple OSS Distributions 		if ((i % 20) == 0) {
146*c54f35caSApple OSS Distributions 			/* Print every port in the multiple of 20 */
147*c54f35caSApple OSS Distributions 			printf("client: FD #%d\n", i);
148*c54f35caSApple OSS Distributions 			sleep(1);
149*c54f35caSApple OSS Distributions 		}
150*c54f35caSApple OSS Distributions 
151*c54f35caSApple OSS Distributions 		if (i == soft_limit && !hard_limit) {
152*c54f35caSApple OSS Distributions 			printf("client: Hit the soft limit \n");
153*c54f35caSApple OSS Distributions 			exit(0);
154*c54f35caSApple OSS Distributions 		}
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions 		if (i == hard_limit) {
157*c54f35caSApple OSS Distributions 			printf("client: Hit the hard limit\n");
158*c54f35caSApple OSS Distributions 		}
159*c54f35caSApple OSS Distributions 
160*c54f35caSApple OSS Distributions 		i++;
161*c54f35caSApple OSS Distributions 	}
162*c54f35caSApple OSS Distributions 
163*c54f35caSApple OSS Distributions fail_and_exit:
164*c54f35caSApple OSS Distributions 	exit(91);
165*c54f35caSApple OSS Distributions }
166