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
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"
35 typedef struct IOHandlerRecord {
39 QLIST_ENTRY(IOHandlerRecord) next;
45 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
46 QLIST_HEAD_INITIALIZER(io_handlers);
48 void qemu_set_fd_handler(int fd,
57 if (!fd_read && !fd_write) {
58 QLIST_FOREACH(ioh, &io_handlers, next) {
65 QLIST_FOREACH(ioh, &io_handlers, next) {
69 ioh = g_malloc0(sizeof(IOHandlerRecord));
70 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
73 ioh->fd_read = fd_read;
74 ioh->fd_write = fd_write;
76 ioh->pollfds_idx = -1;
82 void qemu_iohandler_fill(GArray *pollfds)
86 QLIST_FOREACH(ioh, &io_handlers, next) {
92 events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
95 events |= G_IO_OUT | G_IO_ERR;
102 ioh->pollfds_idx = pollfds->len;
103 g_array_append_val(pollfds, pfd);
105 ioh->pollfds_idx = -1;
110 void qemu_iohandler_poll(GArray *pollfds, int ret)
113 IOHandlerRecord *pioh, *ioh;
115 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
118 if (!ioh->deleted && ioh->pollfds_idx != -1) {
119 GPollFD *pfd = &g_array_index(pollfds, GPollFD,
121 revents = pfd->revents;
124 if (!ioh->deleted && ioh->fd_read &&
125 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
126 ioh->fd_read(ioh->opaque);
128 if (!ioh->deleted && ioh->fd_write &&
129 (revents & (G_IO_OUT | G_IO_ERR))) {
130 ioh->fd_write(ioh->opaque);
133 /* Do this last in case read/write handlers marked it for deletion */
135 QLIST_REMOVE(ioh, next);
142 /* reaping of zombies. right now we're not passing the status to
143 anyone, but it would be possible to add a callback. */
145 typedef struct ChildProcessRecord {
147 QLIST_ENTRY(ChildProcessRecord) next;
148 } ChildProcessRecord;
150 static QLIST_HEAD(, ChildProcessRecord) child_watches =
151 QLIST_HEAD_INITIALIZER(child_watches);
153 static QEMUBH *sigchld_bh;
155 static void sigchld_handler(int signal)
157 qemu_bh_schedule(sigchld_bh);
160 static void sigchld_bh_handler(void *opaque)
162 ChildProcessRecord *rec, *next;
164 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
165 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
166 QLIST_REMOVE(rec, next);
172 static void qemu_init_child_watch(void)
174 struct sigaction act;
175 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
177 memset(&act, 0, sizeof(act));
178 act.sa_handler = sigchld_handler;
179 act.sa_flags = SA_NOCLDSTOP;
180 sigaction(SIGCHLD, &act, NULL);
183 int qemu_add_child_watch(pid_t pid)
185 ChildProcessRecord *rec;
188 qemu_init_child_watch();
191 QLIST_FOREACH(rec, &child_watches, next) {
192 if (rec->pid == pid) {
196 rec = g_malloc0(sizeof(ChildProcessRecord));
198 QLIST_INSERT_HEAD(&child_watches, rec, next);