reds-stream: add send_msgfd()

A new function to send fd with unix socket anciliary data.

Signed-off-by: Marc-André Lureau <marcandre.lureau@gmail.com>
Acked-by: Frediano Ziglio <fziglio@redhat.com>
This commit is contained in:
Marc-André Lureau 2015-12-16 00:49:19 +01:00 committed by Frediano Ziglio
parent 2628197b99
commit 5c1073266e
2 changed files with 41 additions and 0 deletions

View File

@ -28,6 +28,7 @@
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <glib.h>
@ -254,6 +255,45 @@ int reds_stream_is_plain_unix(const RedsStream *s)
}
int reds_stream_send_msgfd(RedsStream *stream, int fd)
{
struct msghdr msgh = { 0, };
struct iovec iov;
int r;
const size_t fd_size = 1 * sizeof(int);
struct cmsghdr *cmsg;
union {
struct cmsghdr hdr;
char data[CMSG_SPACE(fd_size)];
} control;
spice_return_val_if_fail(reds_stream_is_plain_unix(stream), -1);
/* set the payload */
iov.iov_base = (char*)"@";
iov.iov_len = 1;
msgh.msg_iovlen = 1;
msgh.msg_iov = &iov;
if (fd != -1) {
msgh.msg_control = control.data;
msgh.msg_controllen = sizeof(control.data);
cmsg = CMSG_FIRSTHDR(&msgh);
cmsg->cmsg_len = CMSG_LEN(fd_size);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(cmsg), &fd, fd_size);
}
do {
r = sendmsg(stream->socket, &msgh, MSG_NOSIGNAL);
} while (r < 0 && (errno == EINTR || errno == EAGAIN));
return r;
}
ssize_t reds_stream_writev(RedsStream *s, const struct iovec *iov, int iovcnt)
{
int i;

View File

@ -74,6 +74,7 @@ int reds_stream_enable_ssl(RedsStream *stream, SSL_CTX *ctx);
void reds_stream_set_info_flag(RedsStream *stream, unsigned int flag);
int reds_stream_get_family(const RedsStream *stream);
int reds_stream_is_plain_unix(const RedsStream *stream);
int reds_stream_send_msgfd(RedsStream *stream, int fd);
typedef enum {
REDS_SASL_ERROR_OK,