blob: 826f713e9f3f7cd820549b08be323eb8a7441281 [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 Zhengf4d248b2015-06-04 14:45:24 +080048void 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 }
Paolo Bonzini02981412011-03-09 18:21:09 +010080}
81
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010082void qemu_iohandler_fill(GArray *pollfds)
Paolo Bonzini02981412011-03-09 18:21:09 +010083{
84 IOHandlerRecord *ioh;
85
86 QLIST_FOREACH(ioh, &io_handlers, next) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010087 int events = 0;
88
Paolo Bonzini02981412011-03-09 18:21:09 +010089 if (ioh->deleted)
90 continue;
Fam Zheng6484e422015-06-04 14:45:19 +080091 if (ioh->fd_read) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010092 events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
Paolo Bonzini02981412011-03-09 18:21:09 +010093 }
94 if (ioh->fd_write) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +010095 events |= G_IO_OUT | G_IO_ERR;
96 }
97 if (events) {
98 GPollFD pfd = {
99 .fd = ioh->fd,
100 .events = events,
101 };
102 ioh->pollfds_idx = pollfds->len;
103 g_array_append_val(pollfds, pfd);
104 } else {
105 ioh->pollfds_idx = -1;
Paolo Bonzini02981412011-03-09 18:21:09 +0100106 }
107 }
108}
109
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +0100110void qemu_iohandler_poll(GArray *pollfds, int ret)
Paolo Bonzini02981412011-03-09 18:21:09 +0100111{
112 if (ret > 0) {
113 IOHandlerRecord *pioh, *ioh;
114
115 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +0100116 int revents = 0;
117
118 if (!ioh->deleted && ioh->pollfds_idx != -1) {
119 GPollFD *pfd = &g_array_index(pollfds, GPollFD,
120 ioh->pollfds_idx);
121 revents = pfd->revents;
122 }
123
124 if (!ioh->deleted && ioh->fd_read &&
125 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
Paolo Bonzini02981412011-03-09 18:21:09 +0100126 ioh->fd_read(ioh->opaque);
127 }
Stefan Hajnoczia3e4b4a2013-02-20 11:28:29 +0100128 if (!ioh->deleted && ioh->fd_write &&
129 (revents & (G_IO_OUT | G_IO_ERR))) {
Paolo Bonzini02981412011-03-09 18:21:09 +0100130 ioh->fd_write(ioh->opaque);
131 }
132
133 /* Do this last in case read/write handlers marked it for deletion */
134 if (ioh->deleted) {
135 QLIST_REMOVE(ioh, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500136 g_free(ioh);
Paolo Bonzini02981412011-03-09 18:21:09 +0100137 }
138 }
139 }
140}
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100141
142/* reaping of zombies. right now we're not passing the status to
143 anyone, but it would be possible to add a callback. */
144#ifndef _WIN32
145typedef struct ChildProcessRecord {
146 int pid;
147 QLIST_ENTRY(ChildProcessRecord) next;
148} ChildProcessRecord;
149
150static QLIST_HEAD(, ChildProcessRecord) child_watches =
151 QLIST_HEAD_INITIALIZER(child_watches);
152
153static QEMUBH *sigchld_bh;
154
155static void sigchld_handler(int signal)
156{
157 qemu_bh_schedule(sigchld_bh);
158}
159
160static void sigchld_bh_handler(void *opaque)
161{
162 ChildProcessRecord *rec, *next;
163
164 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
165 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
166 QLIST_REMOVE(rec, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500167 g_free(rec);
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100168 }
169 }
170}
171
172static void qemu_init_child_watch(void)
173{
174 struct sigaction act;
175 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
176
Peter Maydellaef553f2014-05-16 14:00:03 +0100177 memset(&act, 0, sizeof(act));
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100178 act.sa_handler = sigchld_handler;
179 act.sa_flags = SA_NOCLDSTOP;
180 sigaction(SIGCHLD, &act, NULL);
181}
182
183int qemu_add_child_watch(pid_t pid)
184{
185 ChildProcessRecord *rec;
186
187 if (!sigchld_bh) {
188 qemu_init_child_watch();
189 }
190
191 QLIST_FOREACH(rec, &child_watches, next) {
192 if (rec->pid == pid) {
193 return 1;
194 }
195 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500196 rec = g_malloc0(sizeof(ChildProcessRecord));
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100197 rec->pid = pid;
198 QLIST_INSERT_HEAD(&child_watches, rec, next);
199 return 0;
200}
201#endif