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