monitor: avoid potential dead-lock when cleaning up
[qemu/ar7.git] / util / iohandler.c
blob623b55b9ecea96057d396e7ca28289edf0267e14
1 /*
2 * QEMU System Emulator - managing I/O handler
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-common.h"
28 #include "qemu/queue.h"
29 #include "block/aio.h"
30 #include "qemu/main-loop.h"
32 #ifndef _WIN32
33 #include <sys/wait.h>
34 #endif
36 /* 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). */
38 static AioContext *iohandler_ctx;
40 static void iohandler_init(void)
42 if (!iohandler_ctx) {
43 iohandler_ctx = aio_context_new(&error_abort);
47 AioContext *iohandler_get_aio_context(void)
49 iohandler_init();
50 return iohandler_ctx;
53 GSource *iohandler_get_g_source(void)
55 iohandler_init();
56 return aio_get_g_source(iohandler_ctx);
59 void qemu_set_fd_handler(int fd,
60 IOHandler *fd_read,
61 IOHandler *fd_write,
62 void *opaque)
64 iohandler_init();
65 aio_set_fd_handler(iohandler_ctx, fd, false,
66 fd_read, fd_write, NULL, opaque);
69 void event_notifier_set_handler(EventNotifier *e,
70 EventNotifierHandler *handler)
72 iohandler_init();
73 aio_set_event_notifier(iohandler_ctx, e, false,
74 handler, NULL);
77 /* reaping of zombies. right now we're not passing the status to
78 anyone, but it would be possible to add a callback. */
79 #ifndef _WIN32
80 typedef struct ChildProcessRecord {
81 int pid;
82 QLIST_ENTRY(ChildProcessRecord) next;
83 } ChildProcessRecord;
85 static QLIST_HEAD(, ChildProcessRecord) child_watches =
86 QLIST_HEAD_INITIALIZER(child_watches);
88 static QEMUBH *sigchld_bh;
90 static void sigchld_handler(int signal)
92 qemu_bh_schedule(sigchld_bh);
95 static void sigchld_bh_handler(void *opaque)
97 ChildProcessRecord *rec, *next;
99 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
100 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
101 QLIST_REMOVE(rec, next);
102 g_free(rec);
107 static void qemu_init_child_watch(void)
109 struct sigaction act;
110 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
112 memset(&act, 0, sizeof(act));
113 act.sa_handler = sigchld_handler;
114 act.sa_flags = SA_NOCLDSTOP;
115 sigaction(SIGCHLD, &act, NULL);
118 int qemu_add_child_watch(pid_t pid)
120 ChildProcessRecord *rec;
122 if (!sigchld_bh) {
123 qemu_init_child_watch();
126 QLIST_FOREACH(rec, &child_watches, next) {
127 if (rec->pid == pid) {
128 return 1;
131 rec = g_malloc0(sizeof(ChildProcessRecord));
132 rec->pid = pid;
133 QLIST_INSERT_HEAD(&child_watches, rec, next);
134 return 0;
136 #endif