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-char.h"
28 #include "qemu-queue.h"
34 typedef struct IOHandlerRecord
{
36 IOCanReadHandler
*fd_read_poll
;
41 QLIST_ENTRY(IOHandlerRecord
) next
;
44 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
45 QLIST_HEAD_INITIALIZER(io_handlers
);
48 /* XXX: fd_read_poll should be suppressed, but an API change is
49 necessary in the character devices to suppress fd_can_read(). */
50 int qemu_set_fd_handler2(int fd
,
51 IOCanReadHandler
*fd_read_poll
,
58 if (!fd_read
&& !fd_write
) {
59 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
66 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
70 ioh
= g_malloc0(sizeof(IOHandlerRecord
));
71 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
74 ioh
->fd_read_poll
= fd_read_poll
;
75 ioh
->fd_read
= fd_read
;
76 ioh
->fd_write
= fd_write
;
83 typedef struct IOTrampoline
92 static gboolean
fd_trampoline(GIOChannel
*chan
, GIOCondition cond
, gpointer opaque
)
94 IOTrampoline
*tramp
= opaque
;
96 if ((cond
& G_IO_IN
) && tramp
->fd_read
) {
97 tramp
->fd_read(tramp
->opaque
);
100 if ((cond
& G_IO_OUT
) && tramp
->fd_write
) {
101 tramp
->fd_write(tramp
->opaque
);
107 int qemu_set_fd_handler(int fd
,
112 static IOTrampoline fd_trampolines
[FD_SETSIZE
];
113 IOTrampoline
*tramp
= &fd_trampolines
[fd
];
115 if (tramp
->tag
!= 0) {
116 g_io_channel_unref(tramp
->chan
);
117 g_source_remove(tramp
->tag
);
121 if (fd_read
|| fd_write
|| opaque
) {
122 GIOCondition cond
= 0;
124 tramp
->fd_read
= fd_read
;
125 tramp
->fd_write
= fd_write
;
126 tramp
->opaque
= opaque
;
129 cond
|= G_IO_IN
| G_IO_ERR
;
133 cond
|= G_IO_OUT
| G_IO_ERR
;
136 tramp
->chan
= g_io_channel_unix_new(fd
);
137 tramp
->tag
= g_io_add_watch(tramp
->chan
, cond
, fd_trampoline
, tramp
);
143 void qemu_iohandler_fill(int *pnfds
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
145 IOHandlerRecord
*ioh
;
147 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
151 (!ioh
->fd_read_poll
||
152 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
153 FD_SET(ioh
->fd
, readfds
);
154 if (ioh
->fd
> *pnfds
)
158 FD_SET(ioh
->fd
, writefds
);
159 if (ioh
->fd
> *pnfds
)
165 void qemu_iohandler_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
, int ret
)
168 IOHandlerRecord
*pioh
, *ioh
;
170 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
171 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, readfds
)) {
172 ioh
->fd_read(ioh
->opaque
);
174 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, writefds
)) {
175 ioh
->fd_write(ioh
->opaque
);
178 /* Do this last in case read/write handlers marked it for deletion */
180 QLIST_REMOVE(ioh
, next
);
187 /* reaping of zombies. right now we're not passing the status to
188 anyone, but it would be possible to add a callback. */
190 typedef struct ChildProcessRecord
{
192 QLIST_ENTRY(ChildProcessRecord
) next
;
193 } ChildProcessRecord
;
195 static QLIST_HEAD(, ChildProcessRecord
) child_watches
=
196 QLIST_HEAD_INITIALIZER(child_watches
);
198 static QEMUBH
*sigchld_bh
;
200 static void sigchld_handler(int signal
)
202 qemu_bh_schedule(sigchld_bh
);
205 static void sigchld_bh_handler(void *opaque
)
207 ChildProcessRecord
*rec
, *next
;
209 QLIST_FOREACH_SAFE(rec
, &child_watches
, next
, next
) {
210 if (waitpid(rec
->pid
, NULL
, WNOHANG
) == rec
->pid
) {
211 QLIST_REMOVE(rec
, next
);
217 static void qemu_init_child_watch(void)
219 struct sigaction act
;
220 sigchld_bh
= qemu_bh_new(sigchld_bh_handler
, NULL
);
222 act
.sa_handler
= sigchld_handler
;
223 act
.sa_flags
= SA_NOCLDSTOP
;
224 sigaction(SIGCHLD
, &act
, NULL
);
227 int qemu_add_child_watch(pid_t pid
)
229 ChildProcessRecord
*rec
;
232 qemu_init_child_watch();
235 QLIST_FOREACH(rec
, &child_watches
, next
) {
236 if (rec
->pid
== pid
) {
240 rec
= g_malloc0(sizeof(ChildProcessRecord
));
242 QLIST_INSERT_HEAD(&child_watches
, rec
, next
);