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