blob: 2e30fe3bcb7384bb13ce466939f7777d481defb1 [file] [log] [blame]
Paolo Bonzini02981412011-03-09 18:21:09 +01001/*
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
30typedef struct IOHandlerRecord {
31 int fd;
32 IOCanReadHandler *fd_read_poll;
33 IOHandler *fd_read;
34 IOHandler *fd_write;
35 int deleted;
36 void *opaque;
37 QLIST_ENTRY(IOHandlerRecord) next;
38} IOHandlerRecord;
39
40static QLIST_HEAD(, IOHandlerRecord) io_handlers =
41 QLIST_HEAD_INITIALIZER(io_handlers);
42
43
44/* XXX: fd_read_poll should be suppressed, but an API change is
45 necessary in the character devices to suppress fd_can_read(). */
46int qemu_set_fd_handler2(int fd,
47 IOCanReadHandler *fd_read_poll,
48 IOHandler *fd_read,
49 IOHandler *fd_write,
50 void *opaque)
51{
52 IOHandlerRecord *ioh;
53
54 if (!fd_read && !fd_write) {
55 QLIST_FOREACH(ioh, &io_handlers, next) {
56 if (ioh->fd == fd) {
57 ioh->deleted = 1;
58 break;
59 }
60 }
61 } else {
62 QLIST_FOREACH(ioh, &io_handlers, next) {
63 if (ioh->fd == fd)
64 goto found;
65 }
66 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
67 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
68 found:
69 ioh->fd = fd;
70 ioh->fd_read_poll = fd_read_poll;
71 ioh->fd_read = fd_read;
72 ioh->fd_write = fd_write;
73 ioh->opaque = opaque;
74 ioh->deleted = 0;
75 }
76 return 0;
77}
78
79int qemu_set_fd_handler(int fd,
80 IOHandler *fd_read,
81 IOHandler *fd_write,
82 void *opaque)
83{
84 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
85}
86
87void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
88{
89 IOHandlerRecord *ioh;
90
91 QLIST_FOREACH(ioh, &io_handlers, next) {
92 if (ioh->deleted)
93 continue;
94 if (ioh->fd_read &&
95 (!ioh->fd_read_poll ||
96 ioh->fd_read_poll(ioh->opaque) != 0)) {
97 FD_SET(ioh->fd, readfds);
98 if (ioh->fd > *pnfds)
99 *pnfds = ioh->fd;
100 }
101 if (ioh->fd_write) {
102 FD_SET(ioh->fd, writefds);
103 if (ioh->fd > *pnfds)
104 *pnfds = ioh->fd;
105 }
106 }
107}
108
109void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
110{
111 if (ret > 0) {
112 IOHandlerRecord *pioh, *ioh;
113
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);
117 }
118 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
119 ioh->fd_write(ioh->opaque);
120 }
121
122 /* Do this last in case read/write handlers marked it for deletion */
123 if (ioh->deleted) {
124 QLIST_REMOVE(ioh, next);
125 qemu_free(ioh);
126 }
127 }
128 }
129}