blob: 430a41c8559fb8f723598d0e4e12fe077393dbaa [file] [log] [blame]
Marcelo Tosattidcc38d12010-10-11 15:31:15 -03001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030014 */
15
16#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/compatfd.h"
Jan Kiszka518420d2013-05-02 10:21:18 +020018#include "qemu/thread.h"
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030019
20#include <sys/syscall.h>
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030021
22struct sigfd_compat_info
23{
24 sigset_t mask;
25 int fd;
26};
27
28static void *sigwait_compat(void *opaque)
29{
30 struct sigfd_compat_info *info = opaque;
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030031
Tristan Gingold30faaf72011-02-18 14:17:16 +010032 while (1) {
33 int sig;
34 int err;
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030035
Tristan Gingold30faaf72011-02-18 14:17:16 +010036 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;
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030045 size_t offset = 0;
46
Tristan Gingold30faaf72011-02-18 14:17:16 +010047 memset(&buffer, 0, sizeof(buffer));
48 buffer.ssi_signo = sig;
49
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030050 while (offset < sizeof(buffer)) {
51 ssize_t len;
52
Tristan Gingold30faaf72011-02-18 14:17:16 +010053 len = write(info->fd, (char *)&buffer + offset,
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030054 sizeof(buffer) - offset);
55 if (len == -1 && errno == EINTR)
56 continue;
57
58 if (len <= 0) {
Tristan Gingold30faaf72011-02-18 14:17:16 +010059 return NULL;
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030060 }
61
62 offset += len;
63 }
64 }
Tristan Gingold30faaf72011-02-18 14:17:16 +010065 }
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030066}
67
68static int qemu_signalfd_compat(const sigset_t *mask)
69{
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030070 struct sigfd_compat_info *info;
Jan Kiszka518420d2013-05-02 10:21:18 +020071 QemuThread thread;
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030072 int fds[2];
73
74 info = malloc(sizeof(*info));
75 if (info == NULL) {
76 errno = ENOMEM;
77 return -1;
78 }
79
80 if (pipe(fds) == -1) {
81 free(info);
82 return -1;
83 }
84
85 qemu_set_cloexec(fds[0]);
86 qemu_set_cloexec(fds[1]);
87
88 memcpy(&info->mask, mask, sizeof(*mask));
89 info->fd = fds[1];
90
Jan Kiszka518420d2013-05-02 10:21:18 +020091 qemu_thread_create(&thread, sigwait_compat, info, QEMU_THREAD_DETACHED);
Marcelo Tosattidcc38d12010-10-11 15:31:15 -030092
93 return fds[0];
94}
95
96int qemu_signalfd(const sigset_t *mask)
97{
98#if defined(CONFIG_SIGNALFD)
99 int ret;
100
101 ret = syscall(SYS_signalfd, -1, mask, _NSIG / 8);
102 if (ret != -1) {
103 qemu_set_cloexec(ret);
104 return ret;
105 }
106#endif
107
108 return qemu_signalfd_compat(mask);
109}
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200110
111bool qemu_signalfd_available(void)
112{
113#ifdef CONFIG_SIGNALFD
Peter Maydell7f84c122011-10-13 18:45:37 +0100114 sigset_t mask;
115 int fd;
116 bool ok;
117 sigemptyset(&mask);
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200118 errno = 0;
Peter Maydell7f84c122011-10-13 18:45:37 +0100119 fd = syscall(SYS_signalfd, -1, &mask, _NSIG / 8);
120 ok = (errno != ENOSYS);
121 if (fd >= 0) {
122 close(fd);
123 }
124 return ok;
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200125#else
126 return false;
127#endif
128}