]> git.proxmox.com Git - mirror_qemu.git/blame - util/iohandler.c
Merge remote-tracking branch 'remotes/stefanberger/tags/pull-tpm-2019-07-08-1' into...
[mirror_qemu.git] / util / 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
d38ea87a 25#include "qemu/osdep.h"
da34e65c 26#include "qapi/error.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
f3926945
FZ
35/* This context runs on top of main loop. We can't reuse qemu_aio_context
36 * because iohandlers mustn't be polled by aio_poll(qemu_aio_context). */
37static AioContext *iohandler_ctx;
02981419 38
f3926945 39static void iohandler_init(void)
02981419 40{
f3926945
FZ
41 if (!iohandler_ctx) {
42 iohandler_ctx = aio_context_new(&error_abort);
02981419 43 }
02981419
PB
44}
45
bcd82a96
FZ
46AioContext *iohandler_get_aio_context(void)
47{
48 iohandler_init();
49 return iohandler_ctx;
50}
51
f3926945 52GSource *iohandler_get_g_source(void)
02981419 53{
f3926945
FZ
54 iohandler_init();
55 return aio_get_g_source(iohandler_ctx);
02981419
PB
56}
57
f3926945
FZ
58void qemu_set_fd_handler(int fd,
59 IOHandler *fd_read,
60 IOHandler *fd_write,
61 void *opaque)
02981419 62{
f3926945 63 iohandler_init();
dca21ef2 64 aio_set_fd_handler(iohandler_ctx, fd, false,
f6a51c84 65 fd_read, fd_write, NULL, opaque);
02981419 66}
4d54ec78 67
d6da1e9e
PB
68void event_notifier_set_handler(EventNotifier *e,
69 EventNotifierHandler *handler)
70{
71 iohandler_init();
72 aio_set_event_notifier(iohandler_ctx, e, false,
73 handler, NULL);
74}
75
4d54ec78
PB
76/* reaping of zombies. right now we're not passing the status to
77 anyone, but it would be possible to add a callback. */
78#ifndef _WIN32
79typedef struct ChildProcessRecord {
80 int pid;
81 QLIST_ENTRY(ChildProcessRecord) next;
82} ChildProcessRecord;
83
84static QLIST_HEAD(, ChildProcessRecord) child_watches =
85 QLIST_HEAD_INITIALIZER(child_watches);
86
87static QEMUBH *sigchld_bh;
88
89static void sigchld_handler(int signal)
90{
91 qemu_bh_schedule(sigchld_bh);
92}
93
94static void sigchld_bh_handler(void *opaque)
95{
96 ChildProcessRecord *rec, *next;
97
98 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
99 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
100 QLIST_REMOVE(rec, next);
7267c094 101 g_free(rec);
4d54ec78
PB
102 }
103 }
104}
105
106static void qemu_init_child_watch(void)
107{
108 struct sigaction act;
109 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
110
aef553fd 111 memset(&act, 0, sizeof(act));
4d54ec78
PB
112 act.sa_handler = sigchld_handler;
113 act.sa_flags = SA_NOCLDSTOP;
114 sigaction(SIGCHLD, &act, NULL);
115}
116
117int qemu_add_child_watch(pid_t pid)
118{
119 ChildProcessRecord *rec;
120
121 if (!sigchld_bh) {
122 qemu_init_child_watch();
123 }
124
125 QLIST_FOREACH(rec, &child_watches, next) {
126 if (rec->pid == pid) {
127 return 1;
128 }
129 }
7267c094 130 rec = g_malloc0(sizeof(ChildProcessRecord));
4d54ec78
PB
131 rec->pid = pid;
132 QLIST_INSERT_HEAD(&child_watches, rec, next);
133 return 0;
134}
135#endif