sensor: add pedometer sensor device
[sdk/emulator/qemu.git] / iohandler.c
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
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
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). */
37 static AioContext *iohandler_ctx;
38
39 static void iohandler_init(void)
40 {
41     if (!iohandler_ctx) {
42         iohandler_ctx = aio_context_new(&error_abort);
43     }
44 }
45
46 GSource *iohandler_get_g_source(void)
47 {
48     iohandler_init();
49     return aio_get_g_source(iohandler_ctx);
50 }
51
52 void qemu_set_fd_handler(int fd,
53                          IOHandler *fd_read,
54                          IOHandler *fd_write,
55                          void *opaque)
56 {
57     iohandler_init();
58     aio_set_fd_handler(iohandler_ctx, fd, false,
59                        fd_read, fd_write, opaque);
60 }
61
62 /* reaping of zombies.  right now we're not passing the status to
63    anyone, but it would be possible to add a callback.  */
64 #ifndef _WIN32
65 typedef struct ChildProcessRecord {
66     int pid;
67     QLIST_ENTRY(ChildProcessRecord) next;
68 } ChildProcessRecord;
69
70 static QLIST_HEAD(, ChildProcessRecord) child_watches =
71     QLIST_HEAD_INITIALIZER(child_watches);
72
73 static QEMUBH *sigchld_bh;
74
75 static void sigchld_handler(int signal)
76 {
77     qemu_bh_schedule(sigchld_bh);
78 }
79
80 static void sigchld_bh_handler(void *opaque)
81 {
82     ChildProcessRecord *rec, *next;
83
84     QLIST_FOREACH_SAFE(rec, &child_watches, next, next) {
85         if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) {
86             QLIST_REMOVE(rec, next);
87             g_free(rec);
88         }
89     }
90 }
91
92 static void qemu_init_child_watch(void)
93 {
94     struct sigaction act;
95     sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL);
96
97     memset(&act, 0, sizeof(act));
98     act.sa_handler = sigchld_handler;
99     act.sa_flags = SA_NOCLDSTOP;
100     sigaction(SIGCHLD, &act, NULL);
101 }
102
103 int qemu_add_child_watch(pid_t pid)
104 {
105     ChildProcessRecord *rec;
106
107     if (!sigchld_bh) {
108         qemu_init_child_watch();
109     }
110
111     QLIST_FOREACH(rec, &child_watches, next) {
112         if (rec->pid == pid) {
113             return 1;
114         }
115     }
116     rec = g_malloc0(sizeof(ChildProcessRecord));
117     rec->pid = pid;
118     QLIST_INSERT_HEAD(&child_watches, rec, next);
119     return 0;
120 }
121 #endif