target-xtensa: handle cache options in the overlay tool
[qemu.git] / compatfd.c
blob02306a4f71ea5b32b88b1468084a621a2012fba0
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.
14 #include "qemu-common.h"
15 #include "compatfd.h"
17 #include <sys/syscall.h>
18 #include <pthread.h>
20 struct sigfd_compat_info
22 sigset_t mask;
23 int fd;
26 static void *sigwait_compat(void *opaque)
28 struct sigfd_compat_info *info = opaque;
29 sigset_t all;
31 sigfillset(&all);
32 pthread_sigmask(SIG_BLOCK, &all, NULL);
34 while (1) {
35 int sig;
36 int err;
38 err = sigwait(&info->mask, &sig);
39 if (err != 0) {
40 if (errno == EINTR) {
41 continue;
42 } else {
43 return NULL;
45 } else {
46 struct qemu_signalfd_siginfo buffer;
47 size_t offset = 0;
49 memset(&buffer, 0, sizeof(buffer));
50 buffer.ssi_signo = sig;
52 while (offset < sizeof(buffer)) {
53 ssize_t len;
55 len = write(info->fd, (char *)&buffer + offset,
56 sizeof(buffer) - offset);
57 if (len == -1 && errno == EINTR)
58 continue;
60 if (len <= 0) {
61 return NULL;
64 offset += len;
70 static int qemu_signalfd_compat(const sigset_t *mask)
72 pthread_attr_t attr;
73 pthread_t tid;
74 struct sigfd_compat_info *info;
75 int fds[2];
77 info = malloc(sizeof(*info));
78 if (info == NULL) {
79 errno = ENOMEM;
80 return -1;
83 if (pipe(fds) == -1) {
84 free(info);
85 return -1;
88 qemu_set_cloexec(fds[0]);
89 qemu_set_cloexec(fds[1]);
91 memcpy(&info->mask, mask, sizeof(*mask));
92 info->fd = fds[1];
94 pthread_attr_init(&attr);
95 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
97 pthread_create(&tid, &attr, sigwait_compat, info);
99 pthread_attr_destroy(&attr);
101 return fds[0];
104 int qemu_signalfd(const sigset_t *mask)
106 #if defined(CONFIG_SIGNALFD)
107 int ret;
109 ret = syscall(SYS_signalfd, -1, mask, _NSIG / 8);
110 if (ret != -1) {
111 qemu_set_cloexec(ret);
112 return ret;
114 #endif
116 return qemu_signalfd_compat(mask);
119 bool qemu_signalfd_available(void)
121 #ifdef CONFIG_SIGNALFD
122 sigset_t mask;
123 int fd;
124 bool ok;
125 sigemptyset(&mask);
126 errno = 0;
127 fd = syscall(SYS_signalfd, -1, &mask, _NSIG / 8);
128 ok = (errno != ENOSYS);
129 if (fd >= 0) {
130 close(fd);
132 return ok;
133 #else
134 return false;
135 #endif