blob: d361cf2c03689cf8b8906b354bb4c404f01acabc [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/queue.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/aio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/main-loop.h"
Paolo Bonzini02981412011-03-09 18:21:09 +010030
Paolo Bonzini4d54ec72011-03-09 18:21:10 +010031#ifndef _WIN32
32#include <sys/wait.h>
33#endif
34
Paolo Bonzini02981412011-03-09 18:21:09 +010035typedef struct IOHandlerRecord {
Paolo Bonzini02981412011-03-09 18:21:09 +010036 IOHandler *fd_read;
37 IOHandler *fd_write;
Paolo Bonzini02981412011-03-09 18:21:09 +010038 void *opaque;
39 QLIST_ENTRY(IOHandlerRecord) next;
Stefan Weilc97feed2012-04-29 19:08:46 +020040 int fd;
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010041 int pollfds_idx;
Stefan Weilc97feed2012-04-29 19:08:46 +020042 bool deleted;
Paolo Bonzini02981412011-03-09 18:21:09 +010043} IOHandlerRecord;
44
45static QLIST_HEAD(, IOHandlerRecord) io_handlers =
46 QLIST_HEAD_INITIALIZER(io_handlers);
47
Fam Zheng6484e422015-06-04 14:45:19 +080048int qemu_set_fd_handler(int fd,
49 IOHandler *fd_read,
50 IOHandler *fd_write,
51 void *opaque)
Paolo Bonzini02981412011-03-09 18:21:09 +010052{
53 IOHandlerRecord *ioh;
54
David Gibsonbbdd2ad2012-09-10 12:30:56 +100055 assert(fd >= 0);
56
Paolo Bonzini02981412011-03-09 18:21:09 +010057 if (!fd_read && !fd_write) {
58 QLIST_FOREACH(ioh, &io_handlers, next) {
59 if (ioh->fd == fd) {
60 ioh->deleted = 1;
61 break;
62 }
63 }
64 } else {
65 QLIST_FOREACH(ioh, &io_handlers, next) {
66 if (ioh->fd == fd)
67 goto found;
68 }
Anthony Liguori7267c092011-08-20 22:09:37 -050069 ioh = g_malloc0(sizeof(IOHandlerRecord));
Paolo Bonzini02981412011-03-09 18:21:09 +010070 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
71 found:
72 ioh->fd = fd;
Paolo Bonzini02981412011-03-09 18:21:09 +010073 ioh->fd_read = fd_read;
74 ioh->fd_write = fd_write;
75 ioh->opaque = opaque;
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010076 ioh->pollfds_idx = -1;
Paolo Bonzini02981412011-03-09 18:21:09 +010077 ioh->deleted = 0;
Alexey Kardashevskiy55ce75f2012-07-18 22:52:04 +100078 qemu_notify_event();
Paolo Bonzini02981412011-03-09 18:21:09 +010079 }
80 return 0;
81}
82
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010083void qemu_iohandler_fill(GArray *pollfds)
Paolo Bonzini02981412011-03-09 18:21:09 +010084{
85 IOHandlerRecord *ioh;
86
87 QLIST_FOREACH(ioh, &io_handlers, next) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010088 int events = 0;
89
Paolo Bonzini02981412011-03-09 18:21:09 +010090 if (ioh->deleted)
91 continue;
Fam Zheng6484e422015-06-04 14:45:19 +080092 if (ioh->fd_read) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010093 events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
Paolo Bonzini02981412011-03-09 18:21:09 +010094 }
95 if (ioh->fd_write) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010096 events |= G_IO_OUT | G_IO_ERR;
97 }
98 if (events) {
99 GPollFD pfd = {
100 .fd = ioh->fd,
101 .events = events,
102 };
103 ioh->pollfds_idx = pollfds->len;
104 g_array_append_val(pollfds, pfd);
105 } else {
106 ioh->pollfds_idx = -1;
Paolo Bonzini02981412011-03-09 18:21:09 +0100107 }
108 }
109}
110
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +0100111void qemu_iohandler_poll(GArray *pollfds, int ret)
Paolo Bonzini02981412011-03-09 18:21:09 +0100112{
113 if (ret > 0) {
114 IOHandlerRecord *pioh, *ioh;
115
116 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +0100117 int revents = 0;
118
119 if (!ioh->deleted && ioh->pollfds_idx != -1) {
120 GPollFD *pfd = &g_array_index(pollfds, GPollFD,
121 ioh->pollfds_idx);
122 revents = pfd->revents;
123 }
124
125 if (!ioh->deleted && ioh->fd_read &&
126 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
Paolo Bonzini02981412011-03-09 18:21:09 +0100127 ioh->fd_read(ioh->opaque);
128 }
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +0100129 if (!ioh->deleted && ioh->fd_write &&
130 (revents & (G_IO_OUT | G_IO_ERR))) {
Paolo Bonzini02981412011-03-09 18:21:09 +0100131 ioh->fd_write(ioh->opaque);
132 }
133
134 /* Do this last in case read/write handlers marked it for deletion */
135 if (ioh->deleted) {
136 QLIST_REMOVE(ioh, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500137 g_free(ioh);
Paolo Bonzini02981412011-03-09 18:21:09 +0100138 }
139 }
140 }
141}
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100142
143/* reaping of zombies. right now we're not passing the status to
144 anyone, but it would be possible to add a callback. */
145#ifndef _WIN32
146typedef struct ChildProcessRecord {
147 int pid;
148 QLIST_ENTRY(ChildProcessRecord) next;
149} ChildProcessRecord;
150
151static QLIST_HEAD(, ChildProcessRecord) child_watches =
152 QLIST_HEAD_INITIALIZER(child_watches);
153
154static QEMUBH *sigchld_bh;
155
156static void sigchld_handler(int signal)
157{
158 qemu_bh_schedule(sigchld_bh);
159}
160
161static void sigchld_bh_handler(void *opaque)
162{
163 ChildProcessRecord *rec, *next;
164
165 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
166 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
167 QLIST_REMOVE(rec, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500168 g_free(rec);
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100169 }
170 }
171}
172
173static void qemu_init_child_watch(void)
174{
175 struct sigaction act;
176 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
177
Peter Maydellaef553f2014-05-16 14:00:03 +0100178 memset(&act, 0, sizeof(act));
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100179 act.sa_handler = sigchld_handler;
180 act.sa_flags = SA_NOCLDSTOP;
181 sigaction(SIGCHLD, &act, NULL);
182}
183
184int qemu_add_child_watch(pid_t pid)
185{
186 ChildProcessRecord *rec;
187
188 if (!sigchld_bh) {
189 qemu_init_child_watch();
190 }
191
192 QLIST_FOREACH(rec, &child_watches, next) {
193 if (rec->pid == pid) {
194 return 1;
195 }
196 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500197 rec = g_malloc0(sizeof(ChildProcessRecord));
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100198 rec->pid = pid;
199 QLIST_INSERT_HEAD(&child_watches, rec, next);
200 return 0;
201}
202#endif