blob: 3f23433b5a901efc26afa85d2e877dbd169e4215 [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
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Paolo Bonzini02981412011-03-09 18:21:09 +010027#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/queue.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010029#include "block/aio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/main-loop.h"
Paolo Bonzini02981412011-03-09 18:21:09 +010031
Paolo Bonzini4d54ec72011-03-09 18:21:10 +010032#ifndef _WIN32
33#include <sys/wait.h>
34#endif
35
Fam Zhengf3926942015-09-07 11:28:58 +080036/* This context runs on top of main loop. We can't reuse qemu_aio_context
37 * because iohandlers mustn't be polled by aio_poll(qemu_aio_context). */
38static AioContext *iohandler_ctx;
Paolo Bonzini02981412011-03-09 18:21:09 +010039
Fam Zhengf3926942015-09-07 11:28:58 +080040static void iohandler_init(void)
41{
42 if (!iohandler_ctx) {
43 iohandler_ctx = aio_context_new(&error_abort);
44 }
45}
46
47GSource *iohandler_get_g_source(void)
48{
49 iohandler_init();
50 return aio_get_g_source(iohandler_ctx);
51}
Paolo Bonzini02981412011-03-09 18:21:09 +010052
Fam Zhengf4d248b2015-06-04 14:45:24 +080053void qemu_set_fd_handler(int fd,
54 IOHandler *fd_read,
55 IOHandler *fd_write,
56 void *opaque)
Paolo Bonzini02981412011-03-09 18:21:09 +010057{
Fam Zhengf3926942015-09-07 11:28:58 +080058 iohandler_init();
Fam Zhengdca21ef2015-10-23 11:08:05 +080059 aio_set_fd_handler(iohandler_ctx, fd, false,
60 fd_read, fd_write, opaque);
Paolo Bonzini02981412011-03-09 18:21:09 +010061}
Paolo Bonzini4d54ec72011-03-09 18:21:10 +010062
63/* reaping of zombies. right now we're not passing the status to
64 anyone, but it would be possible to add a callback. */
65#ifndef _WIN32
66typedef struct ChildProcessRecord {
67 int pid;
68 QLIST_ENTRY(ChildProcessRecord) next;
69} ChildProcessRecord;
70
71static QLIST_HEAD(, ChildProcessRecord) child_watches =
72 QLIST_HEAD_INITIALIZER(child_watches);
73
74static QEMUBH *sigchld_bh;
75
76static void sigchld_handler(int signal)
77{
78 qemu_bh_schedule(sigchld_bh);
79}
80
81static void sigchld_bh_handler(void *opaque)
82{
83 ChildProcessRecord *rec, *next;
84
85 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
86 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
87 QLIST_REMOVE(rec, next);
Anthony Liguori7267c092011-08-20 22:09:37 -050088 g_free(rec);
Paolo Bonzini4d54ec72011-03-09 18:21:10 +010089 }
90 }
91}
92
93static void qemu_init_child_watch(void)
94{
95 struct sigaction act;
96 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
97
Peter Maydellaef553f2014-05-16 14:00:03 +010098 memset(&act, 0, sizeof(act));
Paolo Bonzini4d54ec72011-03-09 18:21:10 +010099 act.sa_handler = sigchld_handler;
100 act.sa_flags = SA_NOCLDSTOP;
101 sigaction(SIGCHLD, &act, NULL);
102}
103
104int qemu_add_child_watch(pid_t pid)
105{
106 ChildProcessRecord *rec;
107
108 if (!sigchld_bh) {
109 qemu_init_child_watch();
110 }
111
112 QLIST_FOREACH(rec, &child_watches, next) {
113 if (rec->pid == pid) {
114 return 1;
115 }
116 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500117 rec = g_malloc0(sizeof(ChildProcessRecord));
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100118 rec->pid = pid;
119 QLIST_INSERT_HEAD(&child_watches, rec, next);
120 return 0;
121}
122#endif