]> git.proxmox.com Git - mirror_qemu.git/blame - util/compatfd.c
qtest: simplify socket_send()
[mirror_qemu.git] / util / compatfd.c
CommitLineData
dcc38d1c
MT
1/*
2 * signalfd/eventfd compatibility
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
dcc38d1c
MT
14 */
15
aafd7584 16#include "qemu/osdep.h"
518420df 17#include "qemu/thread.h"
dcc38d1c 18
81b7b1e2 19#if defined(CONFIG_SIGNALFD)
6bd17dcc 20#include <sys/signalfd.h>
81b7b1e2 21#endif
dcc38d1c 22
7e3a61ce 23struct sigfd_compat_info {
dcc38d1c
MT
24 sigset_t mask;
25 int fd;
26};
27
28static void *sigwait_compat(void *opaque)
29{
30 struct sigfd_compat_info *info = opaque;
dcc38d1c 31
30faaf70
TG
32 while (1) {
33 int sig;
34 int err;
dcc38d1c 35
30faaf70
TG
36 err = sigwait(&info->mask, &sig);
37 if (err != 0) {
38 if (errno == EINTR) {
39 continue;
40 } else {
41 return NULL;
42 }
43 } else {
44 struct qemu_signalfd_siginfo buffer;
dcc38d1c
MT
45 size_t offset = 0;
46
30faaf70
TG
47 memset(&buffer, 0, sizeof(buffer));
48 buffer.ssi_signo = sig;
49
dcc38d1c
MT
50 while (offset < sizeof(buffer)) {
51 ssize_t len;
52
30faaf70 53 len = write(info->fd, (char *)&buffer + offset,
dcc38d1c 54 sizeof(buffer) - offset);
7e3a61ce 55 if (len == -1 && errno == EINTR) {
dcc38d1c 56 continue;
7e3a61ce 57 }
dcc38d1c
MT
58
59 if (len <= 0) {
30faaf70 60 return NULL;
dcc38d1c
MT
61 }
62
63 offset += len;
64 }
65 }
30faaf70 66 }
dcc38d1c
MT
67}
68
69static int qemu_signalfd_compat(const sigset_t *mask)
70{
dcc38d1c 71 struct sigfd_compat_info *info;
518420df 72 QemuThread thread;
dcc38d1c
MT
73 int fds[2];
74
e0c5a18e 75 info = g_malloc(sizeof(*info));
dcc38d1c
MT
76
77 if (pipe(fds) == -1) {
e0c5a18e 78 g_free(info);
dcc38d1c
MT
79 return -1;
80 }
81
82 qemu_set_cloexec(fds[0]);
83 qemu_set_cloexec(fds[1]);
84
85 memcpy(&info->mask, mask, sizeof(*mask));
86 info->fd = fds[1];
87
4900116e
DDAG
88 qemu_thread_create(&thread, "signalfd_compat", sigwait_compat, info,
89 QEMU_THREAD_DETACHED);
dcc38d1c
MT
90
91 return fds[0];
92}
93
94int qemu_signalfd(const sigset_t *mask)
95{
96#if defined(CONFIG_SIGNALFD)
97 int ret;
98
6bd17dcc 99 ret = signalfd(-1, mask, SFD_CLOEXEC);
dcc38d1c 100 if (ret != -1) {
dcc38d1c
MT
101 return ret;
102 }
103#endif
104
105 return qemu_signalfd_compat(mask);
106}