xref: /freebsd/tools/regression/sockets/unix_cmsg/unix_cmsg.c (revision 1bde3b70660fe02e45b1d5b3b8b89eaa7a563b82)
1 /*-
2  * Copyright (c) 2005 Andrey Simonenko
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/limits.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 
35 #include <err.h>
36 #include <inttypes.h>
37 #include <paths.h>
38 #include <signal.h>
39 #include <stdarg.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "uc_common.h"
47 #include "t_cmsgcred.h"
48 #include "t_bintime.h"
49 #include "t_generic.h"
50 #include "t_peercred.h"
51 #include "t_timeval.h"
52 #include "t_sockcred.h"
53 #include "t_cmsgcred_sockcred.h"
54 #include "t_cmsg_len.h"
55 
56 /*
57  * There are tables with tests descriptions and pointers to test
58  * functions.  Each t_*() function returns 0 if its test passed,
59  * -1 if its test failed, -2 if some system error occurred.
60  * If a test function returns -2, then a program exits.
61  *
62  * If a test function forks a client process, then it waits for its
63  * termination.  If a return code of a client process is not equal
64  * to zero, or if a client process was terminated by a signal, then
65  * a test function returns -1 or -2 depending on exit status of
66  * a client process.
67  *
68  * Each function which can block, is run under TIMEOUT.  If timeout
69  * occurs, then a test function returns -2 or a client process exits
70  * with a non-zero return code.
71  */
72 
73 struct test_func {
74 	int		(*func)(void);
75 	const char	*desc;
76 };
77 
78 static const struct test_func test_stream_tbl[] = {
79 	{
80 	  .func = NULL,
81 	  .desc = "All tests"
82 	},
83 	{
84 	  .func = t_cmsgcred,
85 	  .desc = "Sending, receiving cmsgcred"
86 	},
87 	{
88 	  .func = t_sockcred_1,
89 	  .desc = "Receiving sockcred (listening socket)"
90 	},
91 	{
92 	  .func = t_sockcred_2,
93 	  .desc = "Receiving sockcred (accepted socket)"
94 	},
95 	{
96 	  .func = t_cmsgcred_sockcred,
97 	  .desc = "Sending cmsgcred, receiving sockcred"
98 	},
99 	{
100 	  .func = t_timeval,
101 	  .desc = "Sending, receiving timeval"
102 	},
103 	{
104 	  .func = t_bintime,
105 	  .desc = "Sending, receiving bintime"
106 	},
107 /*
108  * The testcase fails on 64-bit architectures (amd64), but passes on 32-bit
109  * architectures (i386); see bug 206543
110  */
111 #ifndef __LP64__
112 	{
113 	  .func = t_cmsg_len,
114 	  .desc = "Check cmsghdr.cmsg_len"
115 	},
116 #endif
117 	{
118 	  .func = t_peercred,
119 	  .desc = "Check LOCAL_PEERCRED socket option"
120 	}
121 };
122 
123 #define TEST_STREAM_TBL_SIZE \
124 	(sizeof(test_stream_tbl) / sizeof(test_stream_tbl[0]))
125 
126 static const struct test_func test_dgram_tbl[] = {
127 	{
128 	  .func = NULL,
129 	  .desc = "All tests"
130 	},
131 	{
132 	  .func = t_cmsgcred,
133 	  .desc = "Sending, receiving cmsgcred"
134 	},
135 	{
136 	  .func = t_sockcred_2,
137 	  .desc = "Receiving sockcred"
138 	},
139 	{
140 	  .func = t_cmsgcred_sockcred,
141 	  .desc = "Sending cmsgcred, receiving sockcred"
142 	},
143 	{
144 	  .func = t_timeval,
145 	  .desc = "Sending, receiving timeval"
146 	},
147 	{
148 	  .func = t_bintime,
149 	  .desc = "Sending, receiving bintime"
150 	},
151 #ifndef __LP64__
152 	{
153 	  .func = t_cmsg_len,
154 	  .desc = "Check cmsghdr.cmsg_len"
155 	}
156 #endif
157 };
158 
159 #define TEST_DGRAM_TBL_SIZE \
160 	(sizeof(test_dgram_tbl) / sizeof(test_dgram_tbl[0]))
161 
162 static bool	failed_flag = false;
163 
164 struct uc_cfg uc_cfg;
165 
166 static char	work_dir[] = _PATH_TMP "unix_cmsg.XXXXXXX";
167 
168 #define IPC_MSG_NUM_DEF		5
169 #define IPC_MSG_NUM_MAX		10
170 #define IPC_MSG_SIZE_DEF	7
171 #define IPC_MSG_SIZE_MAX	128
172 
173 static void
174 usage(bool verbose)
175 {
176 	u_int i;
177 
178 	printf("usage: %s [-dh] [-n num] [-s size] [-t type] "
179 	    "[-z value] [testno]\n", getprogname());
180 	if (!verbose)
181 		return;
182 	printf("\n Options are:\n\
183   -d            Output debugging information\n\
184   -h            Output the help message and exit\n\
185   -n num        Number of messages to send\n\
186   -s size       Specify size of data for IPC\n\
187   -t type       Specify socket type (stream, dgram) for tests\n\
188   -z value      Do not send data in a message (bit 0x1), do not send\n\
189                 data array associated with a cmsghdr structure (bit 0x2)\n\
190   testno        Run one test by its number (require the -t option)\n\n");
191 	printf(" Available tests for stream sockets:\n");
192 	for (i = 0; i < TEST_STREAM_TBL_SIZE; ++i)
193 		printf("   %u: %s\n", i, test_stream_tbl[i].desc);
194 	printf("\n Available tests for datagram sockets:\n");
195 	for (i = 0; i < TEST_DGRAM_TBL_SIZE; ++i)
196 		printf("   %u: %s\n", i, test_dgram_tbl[i].desc);
197 }
198 
199 static int
200 run_tests(int type, u_int testno1)
201 {
202 	const struct test_func *tf;
203 	u_int i, testno2, failed_num;
204 
205 	uc_cfg.sock_type = type;
206 	if (type == SOCK_STREAM) {
207 		uc_cfg.sock_type_str = "SOCK_STREAM";
208 		tf = test_stream_tbl;
209 		i = TEST_STREAM_TBL_SIZE - 1;
210 	} else {
211 		uc_cfg.sock_type_str = "SOCK_DGRAM";
212 		tf = test_dgram_tbl;
213 		i = TEST_DGRAM_TBL_SIZE - 1;
214 	}
215 	if (testno1 == 0) {
216 		testno1 = 1;
217 		testno2 = i;
218 	} else
219 		testno2 = testno1;
220 
221 	uc_output("Running tests for %s sockets:\n", uc_cfg.sock_type_str);
222 	failed_num = 0;
223 	for (i = testno1, tf += testno1; i <= testno2; ++tf, ++i) {
224 		uc_output("  %u: %s\n", i, tf->desc);
225 		switch (tf->func()) {
226 		case -1:
227 			++failed_num;
228 			break;
229 		case -2:
230 			uc_logmsgx("some system error or timeout occurred");
231 			return (-1);
232 		}
233 	}
234 
235 	if (failed_num != 0)
236 		failed_flag = true;
237 
238 	if (testno1 != testno2) {
239 		if (failed_num == 0)
240 			uc_output("-- all tests passed!\n");
241 		else
242 			uc_output("-- %u test%s failed!\n",
243 			    failed_num, failed_num == 1 ? "" : "s");
244 	} else {
245 		if (failed_num == 0)
246 			uc_output("-- test passed!\n");
247 		else
248 			uc_output("-- test failed!\n");
249 	}
250 
251 	return (0);
252 }
253 
254 static int
255 init(void)
256 {
257 	struct sigaction sigact;
258 	size_t idx;
259 	int rv;
260 
261 	uc_cfg.proc_name = "SERVER";
262 
263 	sigact.sa_handler = SIG_IGN;
264 	sigact.sa_flags = 0;
265 	sigemptyset(&sigact.sa_mask);
266 	if (sigaction(SIGPIPE, &sigact, (struct sigaction *)NULL) < 0) {
267 		uc_logmsg("init: sigaction");
268 		return (-1);
269 	}
270 
271 	if (uc_cfg.ipc_msg.buf_size == 0)
272 		uc_cfg.ipc_msg.buf_send = uc_cfg.ipc_msg.buf_recv = NULL;
273 	else {
274 		uc_cfg.ipc_msg.buf_send = malloc(uc_cfg.ipc_msg.buf_size);
275 		uc_cfg.ipc_msg.buf_recv = malloc(uc_cfg.ipc_msg.buf_size);
276 		if (uc_cfg.ipc_msg.buf_send == NULL || uc_cfg.ipc_msg.buf_recv == NULL) {
277 			uc_logmsg("init: malloc");
278 			return (-1);
279 		}
280 		for (idx = 0; idx < uc_cfg.ipc_msg.buf_size; ++idx)
281 			uc_cfg.ipc_msg.buf_send[idx] = (char)idx;
282 	}
283 
284 	uc_cfg.proc_cred.uid = getuid();
285 	uc_cfg.proc_cred.euid = geteuid();
286 	uc_cfg.proc_cred.gid = getgid();
287 	uc_cfg.proc_cred.egid = getegid();
288 	uc_cfg.proc_cred.gid_num = getgroups(0, (gid_t *)NULL);
289 	if (uc_cfg.proc_cred.gid_num < 0) {
290 		uc_logmsg("init: getgroups");
291 		return (-1);
292 	}
293 	uc_cfg.proc_cred.gid_arr = malloc(uc_cfg.proc_cred.gid_num *
294 	    sizeof(*uc_cfg.proc_cred.gid_arr));
295 	if (uc_cfg.proc_cred.gid_arr == NULL) {
296 		uc_logmsg("init: malloc");
297 		return (-1);
298 	}
299 	if (getgroups(uc_cfg.proc_cred.gid_num, uc_cfg.proc_cred.gid_arr) < 0) {
300 		uc_logmsg("init: getgroups");
301 		return (-1);
302 	}
303 
304 	memset(&uc_cfg.serv_addr_sun, 0, sizeof(uc_cfg.serv_addr_sun));
305 	rv = snprintf(uc_cfg.serv_addr_sun.sun_path, sizeof(uc_cfg.serv_addr_sun.sun_path),
306 	    "%s/%s", work_dir, uc_cfg.proc_name);
307 	if (rv < 0) {
308 		uc_logmsg("init: snprintf");
309 		return (-1);
310 	}
311 	if ((size_t)rv >= sizeof(uc_cfg.serv_addr_sun.sun_path)) {
312 		uc_logmsgx("init: not enough space for socket pathname");
313 		return (-1);
314 	}
315 	uc_cfg.serv_addr_sun.sun_family = PF_LOCAL;
316 	uc_cfg.serv_addr_sun.sun_len = SUN_LEN(&uc_cfg.serv_addr_sun);
317 
318 	return (0);
319 }
320 
321 int
322 main(int argc, char *argv[])
323 {
324 	const char *errstr;
325 	u_int testno, zvalue;
326 	int opt, rv;
327 	bool dgram_flag, stream_flag;
328 
329 	memset(&uc_cfg, '\0', sizeof(uc_cfg));
330 	uc_cfg.debug = false;
331 	uc_cfg.server_flag = true;
332 	uc_cfg.send_data_flag = true;
333 	uc_cfg.send_array_flag = true;
334 	uc_cfg.ipc_msg.buf_size = IPC_MSG_SIZE_DEF;
335 	uc_cfg.ipc_msg.msg_num = IPC_MSG_NUM_DEF;
336 	dgram_flag = stream_flag = false;
337 	while ((opt = getopt(argc, argv, "dhn:s:t:z:")) != -1)
338 		switch (opt) {
339 		case 'd':
340 			uc_cfg.debug = true;
341 			break;
342 		case 'h':
343 			usage(true);
344 			return (EXIT_SUCCESS);
345 		case 'n':
346 			uc_cfg.ipc_msg.msg_num = strtonum(optarg, 1,
347 			    IPC_MSG_NUM_MAX, &errstr);
348 			if (errstr != NULL)
349 				errx(EXIT_FAILURE, "option -n: number is %s",
350 				    errstr);
351 			break;
352 		case 's':
353 			uc_cfg.ipc_msg.buf_size = strtonum(optarg, 0,
354 			    IPC_MSG_SIZE_MAX, &errstr);
355 			if (errstr != NULL)
356 				errx(EXIT_FAILURE, "option -s: number is %s",
357 				    errstr);
358 			break;
359 		case 't':
360 			if (strcmp(optarg, "stream") == 0)
361 				stream_flag = true;
362 			else if (strcmp(optarg, "dgram") == 0)
363 				dgram_flag = true;
364 			else
365 				errx(EXIT_FAILURE, "option -t: "
366 				    "wrong socket type");
367 			break;
368 		case 'z':
369 			zvalue = strtonum(optarg, 0, 3, &errstr);
370 			if (errstr != NULL)
371 				errx(EXIT_FAILURE, "option -z: number is %s",
372 				    errstr);
373 			if (zvalue & 0x1)
374 				uc_cfg.send_data_flag = false;
375 			if (zvalue & 0x2)
376 				uc_cfg.send_array_flag = false;
377 			break;
378 		default:
379 			usage(false);
380 			return (EXIT_FAILURE);
381 		}
382 
383 	if (optind < argc) {
384 		if (optind + 1 != argc)
385 			errx(EXIT_FAILURE, "too many arguments");
386 		testno = strtonum(argv[optind], 0, UINT_MAX, &errstr);
387 		if (errstr != NULL)
388 			errx(EXIT_FAILURE, "test number is %s", errstr);
389 		if (stream_flag && testno >= TEST_STREAM_TBL_SIZE)
390 			errx(EXIT_FAILURE, "given test %u for stream "
391 			    "sockets does not exist", testno);
392 		if (dgram_flag && testno >= TEST_DGRAM_TBL_SIZE)
393 			errx(EXIT_FAILURE, "given test %u for datagram "
394 			    "sockets does not exist", testno);
395 	} else
396 		testno = 0;
397 
398 	if (!dgram_flag && !stream_flag) {
399 		if (testno != 0)
400 			errx(EXIT_FAILURE, "particular test number "
401 			    "can be used with the -t option only");
402 		dgram_flag = stream_flag = true;
403 	}
404 
405 	if (mkdtemp(work_dir) == NULL)
406 		err(EXIT_FAILURE, "mkdtemp(%s)", work_dir);
407 
408 	rv = EXIT_FAILURE;
409 	if (init() < 0)
410 		goto done;
411 
412 	if (stream_flag)
413 		if (run_tests(SOCK_STREAM, testno) < 0)
414 			goto done;
415 	if (dgram_flag)
416 		if (run_tests(SOCK_DGRAM, testno) < 0)
417 			goto done;
418 
419 	rv = EXIT_SUCCESS;
420 done:
421 	if (rmdir(work_dir) < 0) {
422 		uc_logmsg("rmdir(%s)", work_dir);
423 		rv = EXIT_FAILURE;
424 	}
425 	return (failed_flag ? EXIT_FAILURE : rv);
426 }
427