[Spice-devel] [PATCH spice 5/9] reds-stream: add send_msgfd()
Marc-André Lureau
marcandre.lureau at gmail.com
Wed Dec 9 16:04:25 PST 2015
From: Marc-André Lureau <mlureau at redhat.com>
A new function to send fd with unix socket anciliary data.
Signed-off-by: Marc-André Lureau <marcandre.lureau at gmail.com>
---
server/reds-stream.c | 42 ++++++++++++++++++++++++++++++++++++++++++
server/reds-stream.h | 1 +
2 files changed, 43 insertions(+)
diff --git a/server/reds-stream.c b/server/reds-stream.c
index d87cb23..4829b43 100644
--- a/server/reds-stream.c
+++ b/server/reds-stream.c
@@ -28,6 +28,7 @@
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
+#include <fcntl.h>
#include <glib.h>
@@ -254,6 +255,47 @@ int reds_stream_is_plain_unix(const RedsStream *s)
}
+int reds_stream_send_msgfd(RedsStream *stream, int fd)
+{
+ struct msghdr msgh;
+ struct iovec iov;
+ int r;
+
+ size_t fd_size = 1 * sizeof(int);
+ char control[CMSG_SPACE(fd_size)];
+ struct cmsghdr *cmsg;
+
+ spice_return_val_if_fail(reds_stream_is_plain_unix(stream), -1);
+
+ memset(&msgh, 0, sizeof(msgh));
+ memset(control, 0, sizeof(control));
+ /* set the payload */
+ iov.iov_base = (char*)"@";
+ iov.iov_len = 1;
+
+ if (fd != -1) {
+ msgh.msg_iovlen = 1;
+ msgh.msg_iov = &iov;
+ msgh.msg_control = control;
+ msgh.msg_controllen = sizeof(control);
+
+ 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);
+ } else {
+ msgh.msg_iovlen = 0;
+ }
+
+ do {
+ r = sendmsg(stream->socket, &msgh, 0);
+ } while (r < 0 && (errno == EINTR || errno == EAGAIN));
+
+ return r;
+}
+
ssize_t reds_stream_writev(RedsStream *s, const struct iovec *iov, int iovcnt)
{
int i;
diff --git a/server/reds-stream.h b/server/reds-stream.h
index 9e53b22..72e5dd1 100644
--- a/server/reds-stream.h
+++ b/server/reds-stream.h
@@ -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,
--
2.5.0
More information about the Spice-devel
mailing list