[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v7 03/13] Add chardev API qemu_chr_fe_set_msgfds
From: |
Antonios Motakis |
Subject: |
[Qemu-devel] [PATCH v7 03/13] Add chardev API qemu_chr_fe_set_msgfds |
Date: |
Fri, 31 Jan 2014 18:34:32 +0100 |
This will set an array of file descriptors to the internal structures.
The next time a message is send the array will be send as ancillary
data. This feature works on unix domain socket backend only.
Signed-off-by: Antonios Motakis <address@hidden>
Signed-off-by: Nikolay Nikolaev <address@hidden>
---
include/sysemu/char.h | 14 +++++++
qemu-char.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 111 insertions(+), 8 deletions(-)
diff --git a/include/sysemu/char.h b/include/sysemu/char.h
index 9981a6a..d99dcf6 100644
--- a/include/sysemu/char.h
+++ b/include/sysemu/char.h
@@ -62,6 +62,7 @@ struct CharDriverState {
void (*chr_update_read_handler)(struct CharDriverState *s);
int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
int (*get_msgfd)(struct CharDriverState *s);
+ int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
int (*chr_add_client)(struct CharDriverState *chr, int fd);
IOEventHandler *chr_event;
IOCanReadHandler *chr_can_read;
@@ -229,6 +230,19 @@ int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void
*arg);
int qemu_chr_fe_get_msgfd(CharDriverState *s);
/**
+ * @qemu_chr_fe_set_msgfds:
+ *
+ * For backends capable of fd passing, set an array of fds to be passed with
+ * the next send operation.
+ * A subsequent call to this function before calling a write function will
+ * result in overwriting the fd array with the new value without being send.
+ * Upon writing the message the fd array is freed.
+ *
+ * Returns: -1 if fd passing isn't supported.
+ */
+int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num);
+
+/**
* @qemu_chr_fe_claim:
*
* Claim a backend before using it, should be called before calling
diff --git a/qemu-char.c b/qemu-char.c
index 54ac03c..c2e599e 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -207,6 +207,11 @@ int qemu_chr_fe_get_msgfd(CharDriverState *s)
return s->get_msgfd ? s->get_msgfd(s) : -1;
}
+int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num)
+{
+ return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
+}
+
int qemu_chr_add_client(CharDriverState *s, int fd)
{
return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
@@ -2331,16 +2336,77 @@ typedef struct {
int do_telnetopt;
int do_nodelay;
int is_unix;
- int msgfd;
+ int read_msgfd;
+ int *write_msgfds;
+ int write_msgfds_num;
} TCPCharDriver;
static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void
*opaque);
+#ifndef _WIN32
+static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len)
+{
+ TCPCharDriver *s = chr->opaque;
+ struct msghdr msgh;
+ struct iovec iov;
+ int r;
+
+ size_t fd_size = s->write_msgfds_num * sizeof(int);
+ char control[CMSG_SPACE(fd_size)];
+ struct cmsghdr *cmsg;
+
+ memset(&msgh, 0, sizeof(msgh));
+ memset(control, 0, sizeof(control));
+
+ /* set the payload */
+ iov.iov_base = (uint8_t *) buf;
+ iov.iov_len = len;
+
+ msgh.msg_iov = &iov;
+ msgh.msg_iovlen = 1;
+
+ 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), s->write_msgfds, fd_size);
+
+ do {
+ r = sendmsg(s->fd, &msgh, 0);
+ } while (r < 0 && errno == EINTR);
+
+ /* free the written msgfds, no matter what */
+ if (s->write_msgfds_num) {
+ g_free(s->write_msgfds);
+ s->write_msgfds = 0;
+ s->write_msgfds_num = 0;
+ }
+
+ if (r < 0) {
+ error_report("Failed to send fds, reason: %s\n", strerror(errno));
+ return -1;
+ }
+
+ return r;
+}
+#endif
+
static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
TCPCharDriver *s = chr->opaque;
if (s->connected) {
- return io_channel_send(s->chan, buf, len);
+#ifndef _WIN32
+ if (s->is_unix && s->write_msgfds_num) {
+ return unix_send_msgfds(chr, buf, len);
+ } else
+#endif
+ {
+ return io_channel_send(s->chan, buf, len);
+ }
} else {
/* XXX: indicate an error ? */
return len;
@@ -2410,11 +2476,27 @@ static void tcp_chr_process_IAC_bytes(CharDriverState
*chr,
static int tcp_get_msgfd(CharDriverState *chr)
{
TCPCharDriver *s = chr->opaque;
- int fd = s->msgfd;
- s->msgfd = -1;
+ int fd = s->read_msgfd;
+ s->read_msgfd = -1;
return fd;
}
+static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
+{
+ TCPCharDriver *s = chr->opaque;
+
+ /* clear old pending fd array */
+ if (s->write_msgfds) {
+ g_free(s->write_msgfds);
+ }
+
+ s->write_msgfds = g_malloc(num * sizeof(int));
+ memcpy(s->write_msgfds, fds, num * sizeof(int));
+ s->write_msgfds_num = num;
+
+ return 0;
+}
+
#ifndef _WIN32
static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
{
@@ -2439,9 +2521,10 @@ static void unix_process_msgfd(CharDriverState *chr,
struct msghdr *msg)
#ifndef MSG_CMSG_CLOEXEC
qemu_set_cloexec(fd);
#endif
- if (s->msgfd != -1)
- close(s->msgfd);
- s->msgfd = fd;
+ if (s->read_msgfd != -1) {
+ close(s->read_msgfd);
+ }
+ s->read_msgfd = fd;
}
}
@@ -2667,6 +2750,9 @@ static void tcp_chr_close(CharDriverState *chr)
}
closesocket(s->listen_fd);
}
+ if (s->write_msgfds_num) {
+ g_free(s->write_msgfds);
+ }
g_free(s);
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
@@ -2695,7 +2781,9 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd,
bool do_nodelay,
s->connected = 0;
s->fd = -1;
s->listen_fd = -1;
- s->msgfd = -1;
+ s->read_msgfd = -1;
+ s->write_msgfds = 0;
+ s->write_msgfds_num = 0;
chr->filename = g_malloc(256);
switch (ss.ss_family) {
@@ -2727,6 +2815,7 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd,
bool do_nodelay,
chr->chr_sync_read = tcp_chr_sync_read;
chr->chr_close = tcp_chr_close;
chr->get_msgfd = tcp_get_msgfd;
+ chr->set_msgfds = tcp_set_msgfds;
chr->chr_add_client = tcp_chr_add_client;
chr->chr_add_watch = tcp_chr_add_watch;
/* be isn't opened until we get a connection */
--
1.8.3.2
- [Qemu-devel] [PATCH v7 00/13] Vhost and vhost-net support for userspace based backends, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 01/13] Convert -mem-path to QemuOpts and add prealloc and share properties, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 02/13] Add chardev API qemu_chr_fe_read_all, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 03/13] Add chardev API qemu_chr_fe_set_msgfds,
Antonios Motakis <=
- [Qemu-devel] [PATCH v7 04/13] Add G_IO_HUP handler for socket chardev, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 05/13] vhost_net should call the poll callback only when it is set, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 07/13] vhost_net_init will use VhostNetOptions to get all its arguments, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 06/13] Refactor virtio-net to use a generic get_vhost_net, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 08/13] Add vhost_ops to the vhost_dev struct and replace all relevant ioctls, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 09/13] Add vhost-backend and VhostBackendType, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 10/13] Add vhost-user as a vhost backend., Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 11/13] Add new vhost-user netdev backend, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 12/13] Add the vhost-user netdev backend to command line, Antonios Motakis, 2014/01/31
- [Qemu-devel] [PATCH v7 13/13] Add vhost-user protocol documentation, Antonios Motakis, 2014/01/31