Merge remote-tracking branch 'remotes/ehabkost/tags/python-next-pull-request' into...
[qemu/ar7.git] / util / compatfd.c
blobc296f55d148aa6d5d99410624e5121a4d3c6e49d
1 /*
2 * signalfd/eventfd compatibility
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qemu/thread.h"
19 #include <sys/syscall.h>
21 struct sigfd_compat_info
23 sigset_t mask;
24 int fd;
27 static void *sigwait_compat(void *opaque)
29 struct sigfd_compat_info *info = opaque;
31 while (1) {
32 int sig;
33 int err;
35 err = sigwait(&info->mask, &sig);
36 if (err != 0) {
37 if (errno == EINTR) {
38 continue;
39 } else {
40 return NULL;
42 } else {
43 struct qemu_signalfd_siginfo buffer;
44 size_t offset = 0;
46 memset(&buffer, 0, sizeof(buffer));
47 buffer.ssi_signo = sig;
49 while (offset < sizeof(buffer)) {
50 ssize_t len;
52 len = write(info->fd, (char *)&buffer + offset,
53 sizeof(buffer) - offset);
54 if (len == -1 && errno == EINTR)
55 continue;
57 if (len <= 0) {
58 return NULL;
61 offset += len;
67 static int qemu_signalfd_compat(const sigset_t *mask)
69 struct sigfd_compat_info *info;
70 QemuThread thread;
71 int fds[2];
73 info = malloc(sizeof(*info));
74 if (info == NULL) {
75 errno = ENOMEM;
76 return -1;
79 if (pipe(fds) == -1) {
80 free(info);
81 return -1;
84 qemu_set_cloexec(fds[0]);
85 qemu_set_cloexec(fds[1]);
87 memcpy(&info->mask, mask, sizeof(*mask));
88 info->fd = fds[1];
90 qemu_thread_create(&thread, "signalfd_compat", sigwait_compat, info,
91 QEMU_THREAD_DETACHED);
93 return fds[0];
96 int qemu_signalfd(const sigset_t *mask)
98 #if defined(CONFIG_SIGNALFD)
99 int ret;
101 ret = syscall(SYS_signalfd, -1, mask, _NSIG / 8);
102 if (ret != -1) {
103 qemu_set_cloexec(ret);
104 return ret;
106 #endif
108 return qemu_signalfd_compat(mask);