migration: fix detached migration with fd
[qemu/qmp-unstable.git] / main-loop.c
blob60e97483242fc20ae1614b214681471241efb3a5
1 /*
2 * QEMU System Emulator
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-common.h"
26 #include "qemu-timer.h"
27 #include "slirp/slirp.h"
28 #include "main-loop.h"
30 #ifndef _WIN32
32 #include "compatfd.h"
34 static int io_thread_fd = -1;
36 void qemu_notify_event(void)
38 /* Write 8 bytes to be compatible with eventfd. */
39 static const uint64_t val = 1;
40 ssize_t ret;
42 if (io_thread_fd == -1) {
43 return;
45 do {
46 ret = write(io_thread_fd, &val, sizeof(val));
47 } while (ret < 0 && errno == EINTR);
49 /* EAGAIN is fine, a read must be pending. */
50 if (ret < 0 && errno != EAGAIN) {
51 fprintf(stderr, "qemu_notify_event: write() failed: %s\n",
52 strerror(errno));
53 exit(1);
57 static void qemu_event_read(void *opaque)
59 int fd = (intptr_t)opaque;
60 ssize_t len;
61 char buffer[512];
63 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
64 do {
65 len = read(fd, buffer, sizeof(buffer));
66 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
69 static int qemu_event_init(void)
71 int err;
72 int fds[2];
74 err = qemu_eventfd(fds);
75 if (err == -1) {
76 return -errno;
78 err = fcntl_setfl(fds[0], O_NONBLOCK);
79 if (err < 0) {
80 goto fail;
82 err = fcntl_setfl(fds[1], O_NONBLOCK);
83 if (err < 0) {
84 goto fail;
86 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
87 (void *)(intptr_t)fds[0]);
89 io_thread_fd = fds[1];
90 return 0;
92 fail:
93 close(fds[0]);
94 close(fds[1]);
95 return err;
98 /* If we have signalfd, we mask out the signals we want to handle and then
99 * use signalfd to listen for them. We rely on whatever the current signal
100 * handler is to dispatch the signals when we receive them.
102 static void sigfd_handler(void *opaque)
104 int fd = (intptr_t)opaque;
105 struct qemu_signalfd_siginfo info;
106 struct sigaction action;
107 ssize_t len;
109 while (1) {
110 do {
111 len = read(fd, &info, sizeof(info));
112 } while (len == -1 && errno == EINTR);
114 if (len == -1 && errno == EAGAIN) {
115 break;
118 if (len != sizeof(info)) {
119 printf("read from sigfd returned %zd: %m\n", len);
120 return;
123 sigaction(info.ssi_signo, NULL, &action);
124 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
125 action.sa_sigaction(info.ssi_signo,
126 (siginfo_t *)&info, NULL);
127 } else if (action.sa_handler) {
128 action.sa_handler(info.ssi_signo);
133 static int qemu_signal_init(void)
135 int sigfd;
136 sigset_t set;
139 * SIG_IPI must be blocked in the main thread and must not be caught
140 * by sigwait() in the signal thread. Otherwise, the cpu thread will
141 * not catch it reliably.
143 sigemptyset(&set);
144 sigaddset(&set, SIG_IPI);
145 pthread_sigmask(SIG_BLOCK, &set, NULL);
147 sigemptyset(&set);
148 sigaddset(&set, SIGIO);
149 sigaddset(&set, SIGALRM);
150 sigaddset(&set, SIGBUS);
151 pthread_sigmask(SIG_BLOCK, &set, NULL);
153 sigfd = qemu_signalfd(&set);
154 if (sigfd == -1) {
155 fprintf(stderr, "failed to create signalfd\n");
156 return -errno;
159 fcntl_setfl(sigfd, O_NONBLOCK);
161 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
162 (void *)(intptr_t)sigfd);
164 return 0;
167 #else /* _WIN32 */
169 HANDLE qemu_event_handle;
171 static void dummy_event_handler(void *opaque)
175 static int qemu_event_init(void)
177 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
178 if (!qemu_event_handle) {
179 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
180 return -1;
182 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
183 return 0;
186 void qemu_notify_event(void)
188 if (!SetEvent(qemu_event_handle)) {
189 fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n",
190 GetLastError());
191 exit(1);
195 static int qemu_signal_init(void)
197 return 0;
199 #endif
201 int qemu_init_main_loop(void)
203 int ret;
205 qemu_mutex_lock_iothread();
206 ret = qemu_signal_init();
207 if (ret) {
208 return ret;
211 /* Note eventfd must be drained before signalfd handlers run */
212 ret = qemu_event_init();
213 if (ret) {
214 return ret;
217 return 0;
221 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
222 static int n_poll_fds;
223 static int max_priority;
225 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
226 fd_set *xfds, struct timeval *tv)
228 GMainContext *context = g_main_context_default();
229 int i;
230 int timeout = 0, cur_timeout;
232 g_main_context_prepare(context, &max_priority);
234 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
235 poll_fds, ARRAY_SIZE(poll_fds));
236 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
238 for (i = 0; i < n_poll_fds; i++) {
239 GPollFD *p = &poll_fds[i];
241 if ((p->events & G_IO_IN)) {
242 FD_SET(p->fd, rfds);
243 *max_fd = MAX(*max_fd, p->fd);
245 if ((p->events & G_IO_OUT)) {
246 FD_SET(p->fd, wfds);
247 *max_fd = MAX(*max_fd, p->fd);
249 if ((p->events & G_IO_ERR)) {
250 FD_SET(p->fd, xfds);
251 *max_fd = MAX(*max_fd, p->fd);
255 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
256 if (timeout >= 0 && timeout < cur_timeout) {
257 tv->tv_sec = timeout / 1000;
258 tv->tv_usec = (timeout % 1000) * 1000;
262 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
263 bool err)
265 GMainContext *context = g_main_context_default();
267 if (!err) {
268 int i;
270 for (i = 0; i < n_poll_fds; i++) {
271 GPollFD *p = &poll_fds[i];
273 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
274 p->revents |= G_IO_IN;
276 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
277 p->revents |= G_IO_OUT;
279 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
280 p->revents |= G_IO_ERR;
285 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
286 g_main_context_dispatch(context);
290 #ifdef _WIN32
291 /***********************************************************/
292 /* Polling handling */
294 typedef struct PollingEntry {
295 PollingFunc *func;
296 void *opaque;
297 struct PollingEntry *next;
298 } PollingEntry;
300 static PollingEntry *first_polling_entry;
302 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
304 PollingEntry **ppe, *pe;
305 pe = g_malloc0(sizeof(PollingEntry));
306 pe->func = func;
307 pe->opaque = opaque;
308 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
309 *ppe = pe;
310 return 0;
313 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
315 PollingEntry **ppe, *pe;
316 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
317 pe = *ppe;
318 if (pe->func == func && pe->opaque == opaque) {
319 *ppe = pe->next;
320 g_free(pe);
321 break;
326 /***********************************************************/
327 /* Wait objects support */
328 typedef struct WaitObjects {
329 int num;
330 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
331 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
332 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
333 } WaitObjects;
335 static WaitObjects wait_objects = {0};
337 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
339 WaitObjects *w = &wait_objects;
340 if (w->num >= MAXIMUM_WAIT_OBJECTS) {
341 return -1;
343 w->events[w->num] = handle;
344 w->func[w->num] = func;
345 w->opaque[w->num] = opaque;
346 w->num++;
347 return 0;
350 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
352 int i, found;
353 WaitObjects *w = &wait_objects;
355 found = 0;
356 for (i = 0; i < w->num; i++) {
357 if (w->events[i] == handle) {
358 found = 1;
360 if (found) {
361 w->events[i] = w->events[i + 1];
362 w->func[i] = w->func[i + 1];
363 w->opaque[i] = w->opaque[i + 1];
366 if (found) {
367 w->num--;
371 static void os_host_main_loop_wait(int *timeout)
373 int ret, ret2, i;
374 PollingEntry *pe;
376 /* XXX: need to suppress polling by better using win32 events */
377 ret = 0;
378 for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
379 ret |= pe->func(pe->opaque);
381 if (ret == 0) {
382 int err;
383 WaitObjects *w = &wait_objects;
385 qemu_mutex_unlock_iothread();
386 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
387 qemu_mutex_lock_iothread();
388 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
389 if (w->func[ret - WAIT_OBJECT_0]) {
390 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
393 /* Check for additional signaled events */
394 for (i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
395 /* Check if event is signaled */
396 ret2 = WaitForSingleObject(w->events[i], 0);
397 if (ret2 == WAIT_OBJECT_0) {
398 if (w->func[i]) {
399 w->func[i](w->opaque[i]);
401 } else if (ret2 != WAIT_TIMEOUT) {
402 err = GetLastError();
403 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
406 } else if (ret != WAIT_TIMEOUT) {
407 err = GetLastError();
408 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
412 *timeout = 0;
414 #else
415 static inline void os_host_main_loop_wait(int *timeout)
418 #endif
420 int main_loop_wait(int nonblocking)
422 fd_set rfds, wfds, xfds;
423 int ret, nfds;
424 struct timeval tv;
425 int timeout;
427 if (nonblocking) {
428 timeout = 0;
429 } else {
430 timeout = qemu_calculate_timeout();
431 qemu_bh_update_timeout(&timeout);
434 os_host_main_loop_wait(&timeout);
436 tv.tv_sec = timeout / 1000;
437 tv.tv_usec = (timeout % 1000) * 1000;
439 /* poll any events */
440 /* XXX: separate device handlers from system ones */
441 nfds = -1;
442 FD_ZERO(&rfds);
443 FD_ZERO(&wfds);
444 FD_ZERO(&xfds);
446 #ifdef CONFIG_SLIRP
447 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
448 #endif
449 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
450 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
452 if (timeout > 0) {
453 qemu_mutex_unlock_iothread();
456 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
458 if (timeout > 0) {
459 qemu_mutex_lock_iothread();
462 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
463 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
464 #ifdef CONFIG_SLIRP
465 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
466 #endif
468 qemu_run_all_timers();
470 /* Check bottom-halves last in case any of the earlier events triggered
471 them. */
472 qemu_bh_poll();
474 return ret;