summaryrefslogtreecommitdiff
path: root/qemu-char.c
diff options
context:
space:
mode:
authorNikolay Nikolaev <n.nikolaev@virtualopensystems.com>2014-05-27 15:04:02 +0300
committerMichael S. Tsirkin <mst@redhat.com>2014-06-19 16:41:54 +0300
commitd39aac7aac2abd78c483f8527c5aa9ae6156bd49 (patch)
tree5b2b8b15a54f6ba2d4ee5bf622eecb6636b8d5e4 /qemu-char.c
parent7b0bfdf52d694c9a3a96505aa42ce3f8d63acd35 (diff)
downloadqemu-d39aac7aac2abd78c483f8527c5aa9ae6156bd49.tar.gz
Add chardev API qemu_chr_fe_set_msgfds
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 the UNIX domain socket backend only. Signed-off-by: Antonios Motakis <a.motakis@virtualopensystems.com> Signed-off-by: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Diffstat (limited to 'qemu-char.c')
-rw-r--r--qemu-char.c88
1 files changed, 87 insertions, 1 deletions
diff --git a/qemu-char.c b/qemu-char.c
index 8fb11c8341..f9709d9d0d 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;
@@ -2333,15 +2338,71 @@ typedef struct {
int do_nodelay;
int is_unix;
int 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;
+ }
+
+ 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;
@@ -2416,6 +2477,25 @@ static int tcp_get_msgfd(CharDriverState *chr)
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);
+ }
+
+ if (num) {
+ 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)
{
@@ -2683,6 +2763,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);
}
@@ -2712,6 +2795,8 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
s->fd = -1;
s->listen_fd = -1;
s->msgfd = -1;
+ s->write_msgfds = 0;
+ s->write_msgfds_num = 0;
chr->filename = g_malloc(256);
switch (ss.ss_family) {
@@ -2743,6 +2828,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;
chr->chr_update_read_handler = tcp_chr_update_read_handler;