1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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"
31
32#ifndef _WIN32
33#include <sys/wait.h>
34#endif
35
36
37
38static AioContext *iohandler_ctx;
39
40static void iohandler_init(void)
41{
42 if (!iohandler_ctx) {
43 iohandler_ctx = aio_context_new(&error_abort);
44 }
45}
46
47AioContext *iohandler_get_aio_context(void)
48{
49 iohandler_init();
50 return iohandler_ctx;
51}
52
53GSource *iohandler_get_g_source(void)
54{
55 iohandler_init();
56 return aio_get_g_source(iohandler_ctx);
57}
58
59void qemu_set_fd_handler(int fd,
60 IOHandler *fd_read,
61 IOHandler *fd_write,
62 void *opaque)
63{
64 iohandler_init();
65 aio_set_fd_handler(iohandler_ctx, fd, false,
66 fd_read, fd_write, NULL, opaque);
67}
68
69void event_notifier_set_handler(EventNotifier *e,
70 EventNotifierHandler *handler)
71{
72 iohandler_init();
73 aio_set_event_notifier(iohandler_ctx, e, false,
74 handler, NULL);
75}
76
77
78
79#ifndef _WIN32
80typedef struct ChildProcessRecord {
81 int pid;
82 QLIST_ENTRY(ChildProcessRecord) next;
83} ChildProcessRecord;
84
85static QLIST_HEAD(, ChildProcessRecord) child_watches =
86 QLIST_HEAD_INITIALIZER(child_watches);
87
88static QEMUBH *sigchld_bh;
89
90static void sigchld_handler(int signal)
91{
92 qemu_bh_schedule(sigchld_bh);
93}
94
95static void sigchld_bh_handler(void *opaque)
96{
97 ChildProcessRecord *rec, *next;
98
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);
103 }
104 }
105}
106
107static void qemu_init_child_watch(void)
108{
109 struct sigaction act;
110 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
111
112 memset(&act, 0, sizeof(act));
113 act.sa_handler = sigchld_handler;
114 act.sa_flags = SA_NOCLDSTOP;
115 sigaction(SIGCHLD, &act, NULL);
116}
117
118int qemu_add_child_watch(pid_t pid)
119{
120 ChildProcessRecord *rec;
121
122 if (!sigchld_bh) {
123 qemu_init_child_watch();
124 }
125
126 QLIST_FOREACH(rec, &child_watches, next) {
127 if (rec->pid == pid) {
128 return 1;
129 }
130 }
131 rec = g_malloc0(sizeof(ChildProcessRecord));
132 rec->pid = pid;
133 QLIST_INSERT_HEAD(&child_watches, rec, next);
134 return 0;
135}
136#endif
137