]>
git.proxmox.com Git - qemu.git/blob - iohandler.c
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"
30 typedef struct IOHandlerRecord
{
32 IOCanReadHandler
*fd_read_poll
;
37 QLIST_ENTRY(IOHandlerRecord
) next
;
40 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
41 QLIST_HEAD_INITIALIZER(io_handlers
);
44 /* XXX: fd_read_poll should be suppressed, but an API change is
45 necessary in the character devices to suppress fd_can_read(). */
46 int qemu_set_fd_handler2(int fd
,
47 IOCanReadHandler
*fd_read_poll
,
54 if (!fd_read
&& !fd_write
) {
55 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
62 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
66 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
67 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
70 ioh
->fd_read_poll
= fd_read_poll
;
71 ioh
->fd_read
= fd_read
;
72 ioh
->fd_write
= fd_write
;
79 int qemu_set_fd_handler(int fd
,
84 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
87 void qemu_iohandler_fill(int *pnfds
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
91 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
95 (!ioh
->fd_read_poll
||
96 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
97 FD_SET(ioh
->fd
, readfds
);
102 FD_SET(ioh
->fd
, writefds
);
103 if (ioh
->fd
> *pnfds
)
109 void qemu_iohandler_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
, int ret
)
112 IOHandlerRecord
*pioh
, *ioh
;
114 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
115 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, readfds
)) {
116 ioh
->fd_read(ioh
->opaque
);
118 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, writefds
)) {
119 ioh
->fd_write(ioh
->opaque
);
122 /* Do this last in case read/write handlers marked it for deletion */
124 QLIST_REMOVE(ioh
, next
);