CHECKPATCH: Add --debug adv_dcs
[qemu/ar7.git] / iohandler.c
blobdea43552d2a37fd4d52cd58a8332694aeaf98d9f
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-char.h"
28 #include "qemu-queue.h"
29 #include "main-loop.h"
31 #ifndef _WIN32
32 #include <sys/wait.h>
33 #endif
35 typedef struct IOHandlerRecord {
36 IOCanReadHandler *fd_read_poll;
37 IOHandler *fd_read;
38 IOHandler *fd_write;
39 void *opaque;
40 QLIST_ENTRY(IOHandlerRecord) next;
41 int fd;
42 bool deleted;
43 } IOHandlerRecord;
45 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
46 QLIST_HEAD_INITIALIZER(io_handlers);
49 /* XXX: fd_read_poll should be suppressed, but an API change is
50 necessary in the character devices to suppress fd_can_read(). */
51 int qemu_set_fd_handler2(int fd,
52 IOCanReadHandler *fd_read_poll,
53 IOHandler *fd_read,
54 IOHandler *fd_write,
55 void *opaque)
57 IOHandlerRecord *ioh;
59 if (!fd_read && !fd_write) {
60 QLIST_FOREACH(ioh, &io_handlers, next) {
61 if (ioh->fd == fd) {
62 ioh->deleted = 1;
63 break;
66 } else {
67 QLIST_FOREACH(ioh, &io_handlers, next) {
68 if (ioh->fd == fd)
69 goto found;
71 ioh = g_malloc0(sizeof(IOHandlerRecord));
72 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
73 found:
74 ioh->fd = fd;
75 ioh->fd_read_poll = fd_read_poll;
76 ioh->fd_read = fd_read;
77 ioh->fd_write = fd_write;
78 ioh->opaque = opaque;
79 ioh->deleted = 0;
80 qemu_notify_event();
82 return 0;
85 int qemu_set_fd_handler(int fd,
86 IOHandler *fd_read,
87 IOHandler *fd_write,
88 void *opaque)
90 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
93 void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
95 IOHandlerRecord *ioh;
97 QLIST_FOREACH(ioh, &io_handlers, next) {
98 if (ioh->deleted)
99 continue;
100 if (ioh->fd_read &&
101 (!ioh->fd_read_poll ||
102 ioh->fd_read_poll(ioh->opaque) != 0)) {
103 FD_SET(ioh->fd, readfds);
104 if (ioh->fd > *pnfds)
105 *pnfds = ioh->fd;
107 if (ioh->fd_write) {
108 FD_SET(ioh->fd, writefds);
109 if (ioh->fd > *pnfds)
110 *pnfds = ioh->fd;
115 void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
117 if (ret > 0) {
118 IOHandlerRecord *pioh, *ioh;
120 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
121 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
122 ioh->fd_read(ioh->opaque);
124 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
125 ioh->fd_write(ioh->opaque);
128 /* Do this last in case read/write handlers marked it for deletion */
129 if (ioh->deleted) {
130 QLIST_REMOVE(ioh, next);
131 g_free(ioh);
137 /* reaping of zombies. right now we're not passing the status to
138 anyone, but it would be possible to add a callback. */
139 #ifndef _WIN32
140 typedef struct ChildProcessRecord {
141 int pid;
142 QLIST_ENTRY(ChildProcessRecord) next;
143 } ChildProcessRecord;
145 static QLIST_HEAD(, ChildProcessRecord) child_watches =
146 QLIST_HEAD_INITIALIZER(child_watches);
148 static QEMUBH *sigchld_bh;
150 static void sigchld_handler(int signal)
152 qemu_bh_schedule(sigchld_bh);
155 static void sigchld_bh_handler(void *opaque)
157 ChildProcessRecord *rec, *next;
159 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
160 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
161 QLIST_REMOVE(rec, next);
162 g_free(rec);
167 static void qemu_init_child_watch(void)
169 struct sigaction act;
170 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
172 act.sa_handler = sigchld_handler;
173 act.sa_flags = SA_NOCLDSTOP;
174 sigaction(SIGCHLD, &act, NULL);
177 int qemu_add_child_watch(pid_t pid)
179 ChildProcessRecord *rec;
181 if (!sigchld_bh) {
182 qemu_init_child_watch();
185 QLIST_FOREACH(rec, &child_watches, next) {
186 if (rec->pid == pid) {
187 return 1;
190 rec = g_malloc0(sizeof(ChildProcessRecord));
191 rec->pid = pid;
192 QLIST_INSERT_HEAD(&child_watches, rec, next);
193 return 0;
195 #endif