]> git.proxmox.com Git - qemu.git/blame - iohandler.c
xen_disk: use bdrv_aio_flush instead of bdrv_flush
[qemu.git] / iohandler.c
CommitLineData
02981419
PB
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"
44a9b356 29#include "main-loop.h"
02981419 30
4d54ec78
PB
31#ifndef _WIN32
32#include <sys/wait.h>
33#endif
34
02981419 35typedef struct IOHandlerRecord {
02981419
PB
36 IOCanReadHandler *fd_read_poll;
37 IOHandler *fd_read;
38 IOHandler *fd_write;
02981419
PB
39 void *opaque;
40 QLIST_ENTRY(IOHandlerRecord) next;
c97feed1
SW
41 int fd;
42 bool deleted;
02981419
PB
43} IOHandlerRecord;
44
45static 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(). */
51int 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 }
7267c094 71 ioh = g_malloc0(sizeof(IOHandlerRecord));
02981419
PB
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
84int qemu_set_fd_handler(int fd,
85 IOHandler *fd_read,
86 IOHandler *fd_write,
87 void *opaque)
88{
be08e65e 89 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
02981419
PB
90}
91
92void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
93{
94 IOHandlerRecord *ioh;
95
96 QLIST_FOREACH(ioh, &io_handlers, next) {
97 if (ioh->deleted)
98 continue;
99 if (ioh->fd_read &&
100 (!ioh->fd_read_poll ||
101 ioh->fd_read_poll(ioh->opaque) != 0)) {
102 FD_SET(ioh->fd, readfds);
103 if (ioh->fd > *pnfds)
104 *pnfds = ioh->fd;
105 }
106 if (ioh->fd_write) {
107 FD_SET(ioh->fd, writefds);
108 if (ioh->fd > *pnfds)
109 *pnfds = ioh->fd;
110 }
111 }
112}
113
114void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
115{
116 if (ret > 0) {
117 IOHandlerRecord *pioh, *ioh;
118
119 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
120 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
121 ioh->fd_read(ioh->opaque);
122 }
123 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
124 ioh->fd_write(ioh->opaque);
125 }
126
127 /* Do this last in case read/write handlers marked it for deletion */
128 if (ioh->deleted) {
129 QLIST_REMOVE(ioh, next);
7267c094 130 g_free(ioh);
02981419
PB
131 }
132 }
133 }
134}
4d54ec78
PB
135
136/* reaping of zombies. right now we're not passing the status to
137 anyone, but it would be possible to add a callback. */
138#ifndef _WIN32
139typedef struct ChildProcessRecord {
140 int pid;
141 QLIST_ENTRY(ChildProcessRecord) next;
142} ChildProcessRecord;
143
144static QLIST_HEAD(, ChildProcessRecord) child_watches =
145 QLIST_HEAD_INITIALIZER(child_watches);
146
147static QEMUBH *sigchld_bh;
148
149static void sigchld_handler(int signal)
150{
151 qemu_bh_schedule(sigchld_bh);
152}
153
154static void sigchld_bh_handler(void *opaque)
155{
156 ChildProcessRecord *rec, *next;
157
158 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
159 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
160 QLIST_REMOVE(rec, next);
7267c094 161 g_free(rec);
4d54ec78
PB
162 }
163 }
164}
165
166static void qemu_init_child_watch(void)
167{
168 struct sigaction act;
169 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
170
171 act.sa_handler = sigchld_handler;
172 act.sa_flags = SA_NOCLDSTOP;
173 sigaction(SIGCHLD, &act, NULL);
174}
175
176int qemu_add_child_watch(pid_t pid)
177{
178 ChildProcessRecord *rec;
179
180 if (!sigchld_bh) {
181 qemu_init_child_watch();
182 }
183
184 QLIST_FOREACH(rec, &child_watches, next) {
185 if (rec->pid == pid) {
186 return 1;
187 }
188 }
7267c094 189 rec = g_malloc0(sizeof(ChildProcessRecord));
4d54ec78
PB
190 rec->pid = pid;
191 QLIST_INSERT_HEAD(&child_watches, rec, next);
192 return 0;
193}
194#endif