]> git.proxmox.com Git - qemu.git/blame - iohandler.c
add basic backup support to block driver
[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"
1de7afc9 27#include "qemu/queue.h"
737e150e 28#include "block/aio.h"
1de7afc9 29#include "qemu/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
bbdd2ad0
DG
59 assert(fd >= 0);
60
02981419
PB
61 if (!fd_read && !fd_write) {
62 QLIST_FOREACH(ioh, &io_handlers, next) {
63 if (ioh->fd == fd) {
64 ioh->deleted = 1;
65 break;
66 }
67 }
68 } else {
69 QLIST_FOREACH(ioh, &io_handlers, next) {
70 if (ioh->fd == fd)
71 goto found;
72 }
7267c094 73 ioh = g_malloc0(sizeof(IOHandlerRecord));
02981419
PB
74 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
75 found:
76 ioh->fd = fd;
77 ioh->fd_read_poll = fd_read_poll;
78 ioh->fd_read = fd_read;
79 ioh->fd_write = fd_write;
80 ioh->opaque = opaque;
81 ioh->deleted = 0;
55ce75fa 82 qemu_notify_event();
02981419
PB
83 }
84 return 0;
85}
86
87int qemu_set_fd_handler(int fd,
88 IOHandler *fd_read,
89 IOHandler *fd_write,
90 void *opaque)
91{
be08e65e 92 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
02981419
PB
93}
94
95void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
96{
97 IOHandlerRecord *ioh;
98
99 QLIST_FOREACH(ioh, &io_handlers, next) {
100 if (ioh->deleted)
101 continue;
102 if (ioh->fd_read &&
103 (!ioh->fd_read_poll ||
104 ioh->fd_read_poll(ioh->opaque) != 0)) {
105 FD_SET(ioh->fd, readfds);
106 if (ioh->fd > *pnfds)
107 *pnfds = ioh->fd;
108 }
109 if (ioh->fd_write) {
110 FD_SET(ioh->fd, writefds);
111 if (ioh->fd > *pnfds)
112 *pnfds = ioh->fd;
113 }
114 }
115}
116
117void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
118{
119 if (ret > 0) {
120 IOHandlerRecord *pioh, *ioh;
121
122 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
123 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
124 ioh->fd_read(ioh->opaque);
125 }
126 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
127 ioh->fd_write(ioh->opaque);
128 }
129
130 /* Do this last in case read/write handlers marked it for deletion */
131 if (ioh->deleted) {
132 QLIST_REMOVE(ioh, next);
7267c094 133 g_free(ioh);
02981419
PB
134 }
135 }
136 }
137}
4d54ec78
PB
138
139/* reaping of zombies. right now we're not passing the status to
140 anyone, but it would be possible to add a callback. */
141#ifndef _WIN32
142typedef struct ChildProcessRecord {
143 int pid;
144 QLIST_ENTRY(ChildProcessRecord) next;
145} ChildProcessRecord;
146
147static QLIST_HEAD(, ChildProcessRecord) child_watches =
148 QLIST_HEAD_INITIALIZER(child_watches);
149
150static QEMUBH *sigchld_bh;
151
152static void sigchld_handler(int signal)
153{
154 qemu_bh_schedule(sigchld_bh);
155}
156
157static void sigchld_bh_handler(void *opaque)
158{
159 ChildProcessRecord *rec, *next;
160
161 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
162 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
163 QLIST_REMOVE(rec, next);
7267c094 164 g_free(rec);
4d54ec78
PB
165 }
166 }
167}
168
169static void qemu_init_child_watch(void)
170{
171 struct sigaction act;
172 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
173
174 act.sa_handler = sigchld_handler;
175 act.sa_flags = SA_NOCLDSTOP;
176 sigaction(SIGCHLD, &act, NULL);
177}
178
179int qemu_add_child_watch(pid_t pid)
180{
181 ChildProcessRecord *rec;
182
183 if (!sigchld_bh) {
184 qemu_init_child_watch();
185 }
186
187 QLIST_FOREACH(rec, &child_watches, next) {
188 if (rec->pid == pid) {
189 return 1;
190 }
191 }
7267c094 192 rec = g_malloc0(sizeof(ChildProcessRecord));
4d54ec78
PB
193 rec->pid = pid;
194 QLIST_INSERT_HEAD(&child_watches, rec, next);
195 return 0;
196}
197#endif