Switch to using the CMSG_* macros for FD passing

Use these instead of computing the values directly so that it might
work on BSD or other non-Linux systems

Signed-off-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
(cherry picked from commit 83f28ef865)
This commit is contained in:
Keith Packard 2013-11-07 17:28:45 -08:00 committed by Alan Coopersmith
parent be65846cf1
commit e7aa524bcb
3 changed files with 19 additions and 25 deletions

View File

@ -216,18 +216,23 @@ static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
#if HAVE_SENDMSG #if HAVE_SENDMSG
if (c->out.out_fd.nfd) { if (c->out.out_fd.nfd) {
char cmsgbuf[CMSG_SPACE(sizeof(int) * XCB_MAX_PASS_FD)];
struct msghdr msg = { struct msghdr msg = {
.msg_name = NULL, .msg_name = NULL,
.msg_namelen = 0, .msg_namelen = 0,
.msg_iov = *vector, .msg_iov = *vector,
.msg_iovlen = n, .msg_iovlen = n,
.msg_control = &c->out.out_fd, .msg_control = cmsgbuf,
.msg_controllen = sizeof (struct cmsghdr) + c->out.out_fd.nfd * sizeof (int), .msg_controllen = CMSG_LEN(c->out.out_fd.nfd * sizeof (int)),
}; };
int i; int i;
c->out.out_fd.cmsghdr.cmsg_len = msg.msg_controllen; struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
c->out.out_fd.cmsghdr.cmsg_level = SOL_SOCKET;
c->out.out_fd.cmsghdr.cmsg_type = SCM_RIGHTS; hdr->cmsg_len = msg.msg_controllen;
hdr->cmsg_level = SOL_SOCKET;
hdr->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(hdr), c->out.out_fd.fd, c->out.out_fd.nfd * sizeof (int));
n = sendmsg(c->fd, &msg, 0); n = sendmsg(c->fd, &msg, 0);
if(n < 0 && errno == EAGAIN) if(n < 0 && errno == EAGAIN)
return 1; return 1;

View File

@ -888,17 +888,14 @@ int _xcb_in_read(xcb_connection_t *c)
.iov_base = c->in.queue + c->in.queue_len, .iov_base = c->in.queue + c->in.queue_len,
.iov_len = sizeof(c->in.queue) - c->in.queue_len, .iov_len = sizeof(c->in.queue) - c->in.queue_len,
}; };
struct { char cmsgbuf[CMSG_SPACE(sizeof(int) * XCB_MAX_PASS_FD)];
struct cmsghdr cmsghdr;
int fd[XCB_MAX_PASS_FD];
} fds;
struct msghdr msg = { struct msghdr msg = {
.msg_name = NULL, .msg_name = NULL,
.msg_namelen = 0, .msg_namelen = 0,
.msg_iov = &iov, .msg_iov = &iov,
.msg_iovlen = 1, .msg_iovlen = 1,
.msg_control = &fds, .msg_control = cmsgbuf,
.msg_controllen = sizeof (struct cmsghdr) + sizeof(int) * (XCB_MAX_PASS_FD - c->in.in_fd.nfd), .msg_controllen = CMSG_SPACE(sizeof(int) * (XCB_MAX_PASS_FD - c->in.in_fd.nfd)),
}; };
n = recvmsg(c->fd, &msg, 0); n = recvmsg(c->fd, &msg, 0);
@ -916,15 +913,12 @@ int _xcb_in_read(xcb_connection_t *c)
#endif #endif
if(n > 0) { if(n > 0) {
#if HAVE_SENDMSG #if HAVE_SENDMSG
if (msg.msg_controllen > sizeof (struct cmsghdr)) struct cmsghdr *hdr;
{
if (fds.cmsghdr.cmsg_level == SOL_SOCKET && for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) {
fds.cmsghdr.cmsg_type == SCM_RIGHTS) if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) {
{ int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int);
int nfd = (msg.msg_controllen - sizeof (struct cmsghdr)) / sizeof (int); memcpy(&c->in.in_fd.fd[c->in.in_fd.nfd], CMSG_DATA(hdr), nfd * sizeof (int));
memmove(&c->in.in_fd.fd[c->in.in_fd.nfd],
fds.fd,
nfd);
c->in.in_fd.nfd += nfd; c->in.in_fd.nfd += nfd;
} }
} }

View File

@ -34,10 +34,6 @@
#include "config.h" #include "config.h"
#endif #endif
#if HAVE_SENDMSG
#include <sys/socket.h>
#endif
#ifdef GCC_HAS_VISIBILITY #ifdef GCC_HAS_VISIBILITY
#pragma GCC visibility push(hidden) #pragma GCC visibility push(hidden)
#endif #endif
@ -89,7 +85,6 @@ typedef void (*xcb_return_socket_func_t)(void *closure);
#define XCB_MAX_PASS_FD 16 #define XCB_MAX_PASS_FD 16
typedef struct _xcb_fd { typedef struct _xcb_fd {
struct cmsghdr cmsghdr;
int fd[XCB_MAX_PASS_FD]; int fd[XCB_MAX_PASS_FD];
int nfd; int nfd;
int ifd; int ifd;