]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - iohandler.c
main-loop: create main-loop.h
[lisovros/qemu_apohw.git] / iohandler.c
1 /*
2  * QEMU System Emulator - managing I/O handler
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24
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"
30
31 #ifndef _WIN32
32 #include <sys/wait.h>
33 #endif
34
35 typedef struct IOHandlerRecord {
36     int fd;
37     IOCanReadHandler *fd_read_poll;
38     IOHandler *fd_read;
39     IOHandler *fd_write;
40     int deleted;
41     void *opaque;
42     QLIST_ENTRY(IOHandlerRecord) next;
43 } IOHandlerRecord;
44
45 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
46     QLIST_HEAD_INITIALIZER(io_handlers);
47
48
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)
56 {
57     IOHandlerRecord *ioh;
58
59     if (!fd_read && !fd_write) {
60         QLIST_FOREACH(ioh, &io_handlers, next) {
61             if (ioh->fd == fd) {
62                 ioh->deleted = 1;
63                 break;
64             }
65         }
66     } else {
67         QLIST_FOREACH(ioh, &io_handlers, next) {
68             if (ioh->fd == fd)
69                 goto found;
70         }
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     }
81     return 0;
82 }
83
84 typedef struct IOTrampoline
85 {
86     GIOChannel *chan;
87     IOHandler *fd_read;
88     IOHandler *fd_write;
89     void *opaque;
90     guint tag;
91 } IOTrampoline;
92
93 static gboolean fd_trampoline(GIOChannel *chan, GIOCondition cond, gpointer opaque)
94 {
95     IOTrampoline *tramp = opaque;
96
97     if ((cond & G_IO_IN) && tramp->fd_read) {
98         tramp->fd_read(tramp->opaque);
99     }
100
101     if ((cond & G_IO_OUT) && tramp->fd_write) {
102         tramp->fd_write(tramp->opaque);
103     }
104
105     return TRUE;
106 }
107
108 int qemu_set_fd_handler(int fd,
109                         IOHandler *fd_read,
110                         IOHandler *fd_write,
111                         void *opaque)
112 {
113     static IOTrampoline fd_trampolines[FD_SETSIZE];
114     IOTrampoline *tramp = &fd_trampolines[fd];
115
116     if (tramp->tag != 0) {
117         g_io_channel_unref(tramp->chan);
118         g_source_remove(tramp->tag);
119         tramp->tag = 0;
120     }
121
122     if (fd_read || fd_write || opaque) {
123         GIOCondition cond = 0;
124
125         tramp->fd_read = fd_read;
126         tramp->fd_write = fd_write;
127         tramp->opaque = opaque;
128
129         if (fd_read) {
130             cond |= G_IO_IN | G_IO_ERR;
131         }
132
133         if (fd_write) {
134             cond |= G_IO_OUT | G_IO_ERR;
135         }
136
137         tramp->chan = g_io_channel_unix_new(fd);
138         tramp->tag = g_io_add_watch(tramp->chan, cond, fd_trampoline, tramp);
139     }
140
141     return 0;
142 }
143
144 void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
145 {
146     IOHandlerRecord *ioh;
147
148     QLIST_FOREACH(ioh, &io_handlers, next) {
149         if (ioh->deleted)
150             continue;
151         if (ioh->fd_read &&
152             (!ioh->fd_read_poll ||
153              ioh->fd_read_poll(ioh->opaque) != 0)) {
154             FD_SET(ioh->fd, readfds);
155             if (ioh->fd > *pnfds)
156                 *pnfds = ioh->fd;
157         }
158         if (ioh->fd_write) {
159             FD_SET(ioh->fd, writefds);
160             if (ioh->fd > *pnfds)
161                 *pnfds = ioh->fd;
162         }
163     }
164 }
165
166 void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
167 {
168     if (ret > 0) {
169         IOHandlerRecord *pioh, *ioh;
170
171         QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
172             if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
173                 ioh->fd_read(ioh->opaque);
174             }
175             if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
176                 ioh->fd_write(ioh->opaque);
177             }
178
179             /* Do this last in case read/write handlers marked it for deletion */
180             if (ioh->deleted) {
181                 QLIST_REMOVE(ioh, next);
182                 g_free(ioh);
183             }
184         }
185     }
186 }
187
188 /* reaping of zombies.  right now we're not passing the status to
189    anyone, but it would be possible to add a callback.  */
190 #ifndef _WIN32
191 typedef struct ChildProcessRecord {
192     int pid;
193     QLIST_ENTRY(ChildProcessRecord) next;
194 } ChildProcessRecord;
195
196 static QLIST_HEAD(, ChildProcessRecord) child_watches =
197     QLIST_HEAD_INITIALIZER(child_watches);
198
199 static QEMUBH *sigchld_bh;
200
201 static void sigchld_handler(int signal)
202 {
203     qemu_bh_schedule(sigchld_bh);
204 }
205
206 static void sigchld_bh_handler(void *opaque)
207 {
208     ChildProcessRecord *rec, *next;
209
210     QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
211         if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
212             QLIST_REMOVE(rec, next);
213             g_free(rec);
214         }
215     }
216 }
217
218 static void qemu_init_child_watch(void)
219 {
220     struct sigaction act;
221     sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
222
223     act.sa_handler = sigchld_handler;
224     act.sa_flags = SA_NOCLDSTOP;
225     sigaction(SIGCHLD, &act, NULL);
226 }
227
228 int qemu_add_child_watch(pid_t pid)
229 {
230     ChildProcessRecord *rec;
231
232     if (!sigchld_bh) {
233         qemu_init_child_watch();
234     }
235
236     QLIST_FOREACH(rec, &child_watches, next) {
237         if (rec->pid == pid) {
238             return 1;
239         }
240     }
241     rec = g_malloc0(sizeof(ChildProcessRecord));
242     rec->pid = pid;
243     QLIST_INSERT_HEAD(&child_watches, rec, next);
244     return 0;
245 }
246 #endif