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