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 "config-host.h"
26#include "qemu-common.h"
27#include "qemu/queue.h"
28#include "block/aio.h"
29#include "qemu/main-loop.h"
30
31#ifndef _WIN32
32#include <sys/wait.h>
33#endif
34
35typedef struct IOHandlerRecord {
36 IOCanReadHandler *fd_read_poll;
37 IOHandler *fd_read;
38 IOHandler *fd_write;
39 void *opaque;
40 QLIST_ENTRY(IOHandlerRecord) next;
41 int fd;
42 bool deleted;
43} IOHandlerRecord;
44
45static QLIST_HEAD(, IOHandlerRecord) io_handlers =
46 QLIST_HEAD_INITIALIZER(io_handlers);
47
48
49
50
51int qemu_set_fd_handler2(int fd,
52 IOCanReadHandler *fd_read_poll,
53 IOHandler *fd_read,
54 IOHandler *fd_write,
55 void *opaque)
56{
57 IOHandlerRecord *ioh;
58
59 assert(fd >= 0);
60
61 if (!fd_read && !fd_write) {
62 QLIST_FOREACH(ioh, &io_handlers, next) {
63 if (ioh->fd == fd) {
64 ioh->deleted = 1;
65 break;
66 }
67 }
68 } else {
69 QLIST_FOREACH(ioh, &io_handlers, next) {
70 if (ioh->fd == fd)
71 goto found;
72 }
73 ioh = g_malloc0(sizeof(IOHandlerRecord));
74 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
75 found:
76 ioh->fd = fd;
77 ioh->fd_read_poll = fd_read_poll;
78 ioh->fd_read = fd_read;
79 ioh->fd_write = fd_write;
80 ioh->opaque = opaque;
81 ioh->deleted = 0;
82 qemu_notify_event();
83 }
84 return 0;
85}
86
87int qemu_set_fd_handler(int fd,
88 IOHandler *fd_read,
89 IOHandler *fd_write,
90 void *opaque)
91{
92 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
93}
94
95void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
96{
97 IOHandlerRecord *ioh;
98
99 QLIST_FOREACH(ioh, &io_handlers, next) {
100 if (ioh->deleted)
101 continue;
102 if (ioh->fd_read &&
103 (!ioh->fd_read_poll ||
104 ioh->fd_read_poll(ioh->opaque) != 0)) {
105 FD_SET(ioh->fd, readfds);
106 if (ioh->fd > *pnfds)
107 *pnfds = ioh->fd;
108 }
109 if (ioh->fd_write) {
110 FD_SET(ioh->fd, writefds);
111 if (ioh->fd > *pnfds)
112 *pnfds = ioh->fd;
113 }
114 }
115}
116
117void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int ret)
118{
119 if (ret > 0) {
120 IOHandlerRecord *pioh, *ioh;
121
122 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
123 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, readfds)) {
124 ioh->fd_read(ioh->opaque);
125 }
126 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, writefds)) {
127 ioh->fd_write(ioh->opaque);
128 }
129
130
131 if (ioh->deleted) {
132 QLIST_REMOVE(ioh, next);
133 g_free(ioh);
134 }
135 }
136 }
137}
138
139
140
141#ifndef _WIN32
142typedef struct ChildProcessRecord {
143 int pid;
144 QLIST_ENTRY(ChildProcessRecord) next;
145} ChildProcessRecord;
146
147static QLIST_HEAD(, ChildProcessRecord) child_watches =
148 QLIST_HEAD_INITIALIZER(child_watches);
149
150static QEMUBH *sigchld_bh;
151
152static void sigchld_handler(int signal)
153{
154 qemu_bh_schedule(sigchld_bh);
155}
156
157static void sigchld_bh_handler(void *opaque)
158{
159 ChildProcessRecord *rec, *next;
160
161 QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
162 if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
163 QLIST_REMOVE(rec, next);
164 g_free(rec);
165 }
166 }
167}
168
169static void qemu_init_child_watch(void)
170{
171 struct sigaction act;
172 sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
173
174 act.sa_handler = sigchld_handler;
175 act.sa_flags = SA_NOCLDSTOP;
176 sigaction(SIGCHLD, &act, NULL);
177}
178
179int qemu_add_child_watch(pid_t pid)
180{
181 ChildProcessRecord *rec;
182
183 if (!sigchld_bh) {
184 qemu_init_child_watch();
185 }
186
187 QLIST_FOREACH(rec, &child_watches, next) {
188 if (rec->pid == pid) {
189 return 1;
190 }
191 }
192 rec = g_malloc0(sizeof(ChildProcessRecord));
193 rec->pid = pid;
194 QLIST_INSERT_HEAD(&child_watches, rec, next);
195 return 0;
196}
197#endif
198