1*1fab62b1SMaxim Sobolev /*-
2*1fab62b1SMaxim Sobolev * Copyright (c) 2005 Andrey Simonenko
3*1fab62b1SMaxim Sobolev * All rights reserved.
4*1fab62b1SMaxim Sobolev *
5*1fab62b1SMaxim Sobolev * Redistribution and use in source and binary forms, with or without
6*1fab62b1SMaxim Sobolev * modification, are permitted provided that the following conditions
7*1fab62b1SMaxim Sobolev * are met:
8*1fab62b1SMaxim Sobolev * 1. Redistributions of source code must retain the above copyright
9*1fab62b1SMaxim Sobolev * notice, this list of conditions and the following disclaimer.
10*1fab62b1SMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright
11*1fab62b1SMaxim Sobolev * notice, this list of conditions and the following disclaimer in the
12*1fab62b1SMaxim Sobolev * documentation and/or other materials provided with the distribution.
13*1fab62b1SMaxim Sobolev *
14*1fab62b1SMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*1fab62b1SMaxim Sobolev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*1fab62b1SMaxim Sobolev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*1fab62b1SMaxim Sobolev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*1fab62b1SMaxim Sobolev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*1fab62b1SMaxim Sobolev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*1fab62b1SMaxim Sobolev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*1fab62b1SMaxim Sobolev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*1fab62b1SMaxim Sobolev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*1fab62b1SMaxim Sobolev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*1fab62b1SMaxim Sobolev * SUCH DAMAGE.
25*1fab62b1SMaxim Sobolev */
26*1fab62b1SMaxim Sobolev
27*1fab62b1SMaxim Sobolev #include <sys/types.h>
28*1fab62b1SMaxim Sobolev #include <sys/socket.h>
29*1fab62b1SMaxim Sobolev #include <sys/un.h>
30*1fab62b1SMaxim Sobolev #include <inttypes.h>
31*1fab62b1SMaxim Sobolev #include <errno.h>
32*1fab62b1SMaxim Sobolev #include <stdarg.h>
33*1fab62b1SMaxim Sobolev #include <stdbool.h>
34*1fab62b1SMaxim Sobolev #include <stdio.h>
35*1fab62b1SMaxim Sobolev #include <stdlib.h>
36*1fab62b1SMaxim Sobolev #include <string.h>
37*1fab62b1SMaxim Sobolev
38*1fab62b1SMaxim Sobolev #include "uc_common.h"
39*1fab62b1SMaxim Sobolev #include "t_generic.h"
40*1fab62b1SMaxim Sobolev #include "t_cmsg_len.h"
41*1fab62b1SMaxim Sobolev
42*1fab62b1SMaxim Sobolev #ifndef __LP64__
43*1fab62b1SMaxim Sobolev static int
t_cmsg_len_client(int fd)44*1fab62b1SMaxim Sobolev t_cmsg_len_client(int fd)
45*1fab62b1SMaxim Sobolev {
46*1fab62b1SMaxim Sobolev struct msghdr msghdr;
47*1fab62b1SMaxim Sobolev struct iovec iov[1];
48*1fab62b1SMaxim Sobolev struct cmsghdr *cmsghdr;
49*1fab62b1SMaxim Sobolev void *cmsg_data;
50*1fab62b1SMaxim Sobolev size_t size, cmsg_size;
51*1fab62b1SMaxim Sobolev socklen_t socklen;
52*1fab62b1SMaxim Sobolev int rv;
53*1fab62b1SMaxim Sobolev
54*1fab62b1SMaxim Sobolev if (uc_sync_recv() < 0)
55*1fab62b1SMaxim Sobolev return (-2);
56*1fab62b1SMaxim Sobolev
57*1fab62b1SMaxim Sobolev rv = -2;
58*1fab62b1SMaxim Sobolev
59*1fab62b1SMaxim Sobolev cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));
60*1fab62b1SMaxim Sobolev cmsg_data = malloc(cmsg_size);
61*1fab62b1SMaxim Sobolev if (cmsg_data == NULL) {
62*1fab62b1SMaxim Sobolev uc_logmsg("malloc");
63*1fab62b1SMaxim Sobolev goto done;
64*1fab62b1SMaxim Sobolev }
65*1fab62b1SMaxim Sobolev uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,
66*1fab62b1SMaxim Sobolev SCM_CREDS, sizeof(struct cmsgcred));
67*1fab62b1SMaxim Sobolev cmsghdr = CMSG_FIRSTHDR(&msghdr);
68*1fab62b1SMaxim Sobolev
69*1fab62b1SMaxim Sobolev if (uc_socket_connect(fd) < 0)
70*1fab62b1SMaxim Sobolev goto done;
71*1fab62b1SMaxim Sobolev
72*1fab62b1SMaxim Sobolev size = msghdr.msg_iov != NULL ? msghdr.msg_iov->iov_len : 0;
73*1fab62b1SMaxim Sobolev rv = -1;
74*1fab62b1SMaxim Sobolev for (socklen = 0; socklen < CMSG_LEN(0); ++socklen) {
75*1fab62b1SMaxim Sobolev cmsghdr->cmsg_len = socklen;
76*1fab62b1SMaxim Sobolev uc_dbgmsg("send: data size %zu", size);
77*1fab62b1SMaxim Sobolev uc_dbgmsg("send: msghdr.msg_controllen %u",
78*1fab62b1SMaxim Sobolev (u_int)msghdr.msg_controllen);
79*1fab62b1SMaxim Sobolev uc_dbgmsg("send: cmsghdr.cmsg_len %u",
80*1fab62b1SMaxim Sobolev (u_int)cmsghdr->cmsg_len);
81*1fab62b1SMaxim Sobolev if (sendmsg(fd, &msghdr, 0) < 0) {
82*1fab62b1SMaxim Sobolev uc_dbgmsg("sendmsg(2) failed: %s; retrying",
83*1fab62b1SMaxim Sobolev strerror(errno));
84*1fab62b1SMaxim Sobolev continue;
85*1fab62b1SMaxim Sobolev }
86*1fab62b1SMaxim Sobolev uc_logmsgx("sent message with cmsghdr.cmsg_len %u < %u",
87*1fab62b1SMaxim Sobolev (u_int)cmsghdr->cmsg_len, (u_int)CMSG_LEN(0));
88*1fab62b1SMaxim Sobolev break;
89*1fab62b1SMaxim Sobolev }
90*1fab62b1SMaxim Sobolev if (socklen == CMSG_LEN(0))
91*1fab62b1SMaxim Sobolev rv = 0;
92*1fab62b1SMaxim Sobolev
93*1fab62b1SMaxim Sobolev if (uc_sync_send() < 0) {
94*1fab62b1SMaxim Sobolev rv = -2;
95*1fab62b1SMaxim Sobolev goto done;
96*1fab62b1SMaxim Sobolev }
97*1fab62b1SMaxim Sobolev done:
98*1fab62b1SMaxim Sobolev free(cmsg_data);
99*1fab62b1SMaxim Sobolev return (rv);
100*1fab62b1SMaxim Sobolev }
101*1fab62b1SMaxim Sobolev
102*1fab62b1SMaxim Sobolev static int
t_cmsg_len_server(int fd1)103*1fab62b1SMaxim Sobolev t_cmsg_len_server(int fd1)
104*1fab62b1SMaxim Sobolev {
105*1fab62b1SMaxim Sobolev int fd2, rv;
106*1fab62b1SMaxim Sobolev
107*1fab62b1SMaxim Sobolev if (uc_sync_send() < 0)
108*1fab62b1SMaxim Sobolev return (-2);
109*1fab62b1SMaxim Sobolev
110*1fab62b1SMaxim Sobolev rv = -2;
111*1fab62b1SMaxim Sobolev
112*1fab62b1SMaxim Sobolev if (uc_cfg.sock_type == SOCK_STREAM) {
113*1fab62b1SMaxim Sobolev fd2 = uc_socket_accept(fd1);
114*1fab62b1SMaxim Sobolev if (fd2 < 0)
115*1fab62b1SMaxim Sobolev goto done;
116*1fab62b1SMaxim Sobolev } else
117*1fab62b1SMaxim Sobolev fd2 = fd1;
118*1fab62b1SMaxim Sobolev
119*1fab62b1SMaxim Sobolev if (uc_sync_recv() < 0)
120*1fab62b1SMaxim Sobolev goto done;
121*1fab62b1SMaxim Sobolev
122*1fab62b1SMaxim Sobolev rv = 0;
123*1fab62b1SMaxim Sobolev done:
124*1fab62b1SMaxim Sobolev if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
125*1fab62b1SMaxim Sobolev if (uc_socket_close(fd2) < 0)
126*1fab62b1SMaxim Sobolev rv = -2;
127*1fab62b1SMaxim Sobolev return (rv);
128*1fab62b1SMaxim Sobolev }
129*1fab62b1SMaxim Sobolev
130*1fab62b1SMaxim Sobolev int
t_cmsg_len(void)131*1fab62b1SMaxim Sobolev t_cmsg_len(void)
132*1fab62b1SMaxim Sobolev {
133*1fab62b1SMaxim Sobolev return (t_generic(t_cmsg_len_client, t_cmsg_len_server));
134*1fab62b1SMaxim Sobolev }
135*1fab62b1SMaxim Sobolev #endif
136