xen_backend: Remove unused error handling of qemu_set_fd_handler
[qemu.git] / iohandler.c
blobd361cf2c03689cf8b8906b354bb4c404f01acabc
1 /*
2 * QEMU System Emulator - managing I/O handler
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "config-host.h"
26 #include "qemu-common.h"
27 #include "qemu/queue.h"
28 #include "block/aio.h"
29 #include "qemu/main-loop.h"
31 #ifndef _WIN32
32 #include <sys/wait.h>
33 #endif
35 typedef struct IOHandlerRecord {
36 IOHandler *fd_read;
37 IOHandler *fd_write;
38 void *opaque;
39 QLIST_ENTRY(IOHandlerRecord) next;
40 int fd;
41 int pollfds_idx;
42 bool deleted;
43 } IOHandlerRecord;
45 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
46 QLIST_HEAD_INITIALIZER(io_handlers);
48 int qemu_set_fd_handler(int fd,
49 IOHandler *fd_read,
50 IOHandler *fd_write,
51 void *opaque)
53 IOHandlerRecord *ioh;
55 assert(fd >= 0);
57 if (!fd_read && !fd_write) {
58 QLIST_FOREACH(ioh, &io_handlers, next) {
59 if (ioh->fd == fd) {
60 ioh->deleted = 1;
61 break;
64 } else {
65 QLIST_FOREACH(ioh, &io_handlers, next) {
66 if (ioh->fd == fd)
67 goto found;
69 ioh = g_malloc0(sizeof(IOHandlerRecord));
70 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
71 found:
72 ioh->fd = fd;
73 ioh->fd_read = fd_read;
74 ioh->fd_write = fd_write;
75 ioh->opaque = opaque;
76 ioh->pollfds_idx = -1;
77 ioh->deleted = 0;
78 qemu_notify_event();
80 return 0;
83 void qemu_iohandler_fill(GArray *pollfds)
85 IOHandlerRecord *ioh;
87 QLIST_FOREACH(ioh, &io_handlers, next) {
88 int events = 0;
90 if (ioh->deleted)
91 continue;
92 if (ioh->fd_read) {
93 events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
95 if (ioh->fd_write) {
96 events |= G_IO_OUT | G_IO_ERR;
98 if (events) {
99 GPollFD pfd = {
100 .fd = ioh->fd,
101 .events = events,
103 ioh->pollfds_idx = pollfds->len;
104 g_array_append_val(pollfds, pfd);
105 } else {
106 ioh->pollfds_idx = -1;
111 void qemu_iohandler_poll(GArray *pollfds, int ret)
113 if (ret > 0) {
114 IOHandlerRecord *pioh, *ioh;
116 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
117 int revents = 0;
119 if (!ioh->deleted && ioh->pollfds_idx != -1) {
120 GPollFD *pfd = &g_array_index(pollfds, GPollFD,
121 ioh->pollfds_idx);
122 revents = pfd->revents;
125 if (!ioh->deleted && ioh->fd_read &&
126 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
127 ioh->fd_read(ioh->opaque);
129 if (!ioh->deleted && ioh->fd_write &&
130 (revents & (G_IO_OUT | G_IO_ERR))) {
131 ioh->fd_write(ioh->opaque);
134 /* Do this last in case read/write handlers marked it for deletion */
135 if (ioh->deleted) {
136 QLIST_REMOVE(ioh, next);
137 g_free(ioh);
143 /* reaping of zombies. right now we're not passing the status to
144 anyone, but it would be possible to add a callback. */
145 #ifndef _WIN32
146 typedef struct ChildProcessRecord {
147 int pid;
148 QLIST_ENTRY(ChildProcessRecord) next;
149 } ChildProcessRecord;
151 static QLIST_HEAD(, ChildProcessRecord) child_watches =
152 QLIST_HEAD_INITIALIZER(child_watches);
154 static QEMUBH *sigchld_bh;
156 static void sigchld_handler(int signal)
158 qemu_bh_schedule(sigchld_bh);
161 static void sigchld_bh_handler(void *opaque)
163 ChildProcessRecord *rec, *next;
165 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
166 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
167 QLIST_REMOVE(rec, next);
168 g_free(rec);
173 static void qemu_init_child_watch(void)
175 struct sigaction act;
176 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
178 memset(&act, 0, sizeof(act));
179 act.sa_handler = sigchld_handler;
180 act.sa_flags = SA_NOCLDSTOP;
181 sigaction(SIGCHLD, &act, NULL);
184 int qemu_add_child_watch(pid_t pid)
186 ChildProcessRecord *rec;
188 if (!sigchld_bh) {
189 qemu_init_child_watch();
192 QLIST_FOREACH(rec, &child_watches, next) {
193 if (rec->pid == pid) {
194 return 1;
197 rec = g_malloc0(sizeof(ChildProcessRecord));
198 rec->pid = pid;
199 QLIST_INSERT_HEAD(&child_watches, rec, next);
200 return 0;
202 #endif