xref: /freebsd/tools/regression/sockets/unix_cmsg/t_cmsg_len.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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/socket.h>
32 #include <sys/un.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "uc_common.h"
42 #include "t_generic.h"
43 #include "t_cmsg_len.h"
44 
45 #ifndef __LP64__
46 static int
47 t_cmsg_len_client(int fd)
48 {
49 	struct msghdr msghdr;
50 	struct iovec iov[1];
51 	struct cmsghdr *cmsghdr;
52 	void *cmsg_data;
53 	size_t size, cmsg_size;
54 	socklen_t socklen;
55 	int rv;
56 
57 	if (uc_sync_recv() < 0)
58 		return (-2);
59 
60 	rv = -2;
61 
62 	cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));
63 	cmsg_data = malloc(cmsg_size);
64 	if (cmsg_data == NULL) {
65 		uc_logmsg("malloc");
66 		goto done;
67 	}
68 	uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,
69 	    SCM_CREDS, sizeof(struct cmsgcred));
70 	cmsghdr = CMSG_FIRSTHDR(&msghdr);
71 
72 	if (uc_socket_connect(fd) < 0)
73 		goto done;
74 
75 	size = msghdr.msg_iov != NULL ? msghdr.msg_iov->iov_len : 0;
76 	rv = -1;
77 	for (socklen = 0; socklen < CMSG_LEN(0); ++socklen) {
78 		cmsghdr->cmsg_len = socklen;
79 		uc_dbgmsg("send: data size %zu", size);
80 		uc_dbgmsg("send: msghdr.msg_controllen %u",
81 		    (u_int)msghdr.msg_controllen);
82 		uc_dbgmsg("send: cmsghdr.cmsg_len %u",
83 		    (u_int)cmsghdr->cmsg_len);
84 		if (sendmsg(fd, &msghdr, 0) < 0) {
85 			uc_dbgmsg("sendmsg(2) failed: %s; retrying",
86 			    strerror(errno));
87 			continue;
88 		}
89 		uc_logmsgx("sent message with cmsghdr.cmsg_len %u < %u",
90 		    (u_int)cmsghdr->cmsg_len, (u_int)CMSG_LEN(0));
91 		break;
92 	}
93 	if (socklen == CMSG_LEN(0))
94 		rv = 0;
95 
96 	if (uc_sync_send() < 0) {
97 		rv = -2;
98 		goto done;
99 	}
100 done:
101 	free(cmsg_data);
102 	return (rv);
103 }
104 
105 static int
106 t_cmsg_len_server(int fd1)
107 {
108 	int fd2, rv;
109 
110 	if (uc_sync_send() < 0)
111 		return (-2);
112 
113 	rv = -2;
114 
115 	if (uc_cfg.sock_type == SOCK_STREAM) {
116 		fd2 = uc_socket_accept(fd1);
117 		if (fd2 < 0)
118 			goto done;
119 	} else
120 		fd2 = fd1;
121 
122 	if (uc_sync_recv() < 0)
123 		goto done;
124 
125 	rv = 0;
126 done:
127 	if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
128 		if (uc_socket_close(fd2) < 0)
129 			rv = -2;
130 	return (rv);
131 }
132 
133 int
134 t_cmsg_len(void)
135 {
136 	return (t_generic(t_cmsg_len_client, t_cmsg_len_server));
137 }
138 #endif
139