qtest: Use strtoull() for uint64_t
[qemu/pbrook.git] / iohandler.c
blob2523adc11d62ce9549e35254a7b92390564afbb6
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 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 assert(fd >= 0);
61 if (!fd_read && !fd_write) {
62 QLIST_FOREACH(ioh, &io_handlers, next) {
63 if (ioh->fd == fd) {
64 ioh->deleted = 1;
65 break;
68 } else {
69 QLIST_FOREACH(ioh, &io_handlers, next) {
70 if (ioh->fd == fd)
71 goto found;
73 ioh = g_malloc0(sizeof(IOHandlerRecord));
74 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
75 found:
76 ioh->fd = fd;
77 ioh->fd_read_poll = fd_read_poll;
78 ioh->fd_read = fd_read;
79 ioh->fd_write = fd_write;
80 ioh->opaque = opaque;
81 ioh->deleted = 0;
82 qemu_notify_event();
84 return 0;
87 int qemu_set_fd_handler(int fd,
88 IOHandler *fd_read,
89 IOHandler *fd_write,
90 void *opaque)
92 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
95 void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
97 IOHandlerRecord *ioh;
99 QLIST_FOREACH(ioh, &io_handlers, next) {
100 if (ioh->deleted)
101 continue;
102 if (ioh->fd_read &&
103 (!ioh->fd_read_poll ||
104 ioh->fd_read_poll(ioh->opaque) != 0)) {
105 FD_SET(ioh->fd, readfds);
106 if (ioh->fd > *pnfds)
107 *pnfds = ioh->fd;
109 if (ioh->fd_write) {
110 FD_SET(ioh->fd, writefds);
111 if (ioh->fd > *pnfds)
112 *pnfds = ioh->fd;
117 void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
119 if (ret > 0) {
120 IOHandlerRecord *pioh, *ioh;
122 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
123 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
124 ioh->fd_read(ioh->opaque);
126 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
127 ioh->fd_write(ioh->opaque);
130 /* Do this last in case read/write handlers marked it for deletion */
131 if (ioh->deleted) {
132 QLIST_REMOVE(ioh, next);
133 g_free(ioh);
139 /* reaping of zombies. right now we're not passing the status to
140 anyone, but it would be possible to add a callback. */
141 #ifndef _WIN32
142 typedef struct ChildProcessRecord {
143 int pid;
144 QLIST_ENTRY(ChildProcessRecord) next;
145 } ChildProcessRecord;
147 static QLIST_HEAD(, ChildProcessRecord) child_watches =
148 QLIST_HEAD_INITIALIZER(child_watches);
150 static QEMUBH *sigchld_bh;
152 static void sigchld_handler(int signal)
154 qemu_bh_schedule(sigchld_bh);
157 static void sigchld_bh_handler(void *opaque)
159 ChildProcessRecord *rec, *next;
161 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
162 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
163 QLIST_REMOVE(rec, next);
164 g_free(rec);
169 static void qemu_init_child_watch(void)
171 struct sigaction act;
172 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
174 act.sa_handler = sigchld_handler;
175 act.sa_flags = SA_NOCLDSTOP;
176 sigaction(SIGCHLD, &act, NULL);
179 int qemu_add_child_watch(pid_t pid)
181 ChildProcessRecord *rec;
183 if (!sigchld_bh) {
184 qemu_init_child_watch();
187 QLIST_FOREACH(rec, &child_watches, next) {
188 if (rec->pid == pid) {
189 return 1;
192 rec = g_malloc0(sizeof(ChildProcessRecord));
193 rec->pid = pid;
194 QLIST_INSERT_HEAD(&child_watches, rec, next);
195 return 0;
197 #endif