Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 27 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/queue.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 29 | #include "block/aio.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/main-loop.h" |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 31 | |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 32 | #ifndef _WIN32 |
| 33 | #include <sys/wait.h> |
| 34 | #endif |
| 35 | |
Fam Zheng | f392694 | 2015-09-07 11:28:58 +0800 | [diff] [blame] | 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; |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 39 | |
Fam Zheng | f392694 | 2015-09-07 11:28:58 +0800 | [diff] [blame] | 40 | static void iohandler_init(void) |
| 41 | { |
| 42 | if (!iohandler_ctx) { |
| 43 | iohandler_ctx = aio_context_new(&error_abort); |
| 44 | } |
| 45 | } |
| 46 | |
Fam Zheng | bcd82a9 | 2016-04-22 21:53:52 +0800 | [diff] [blame] | 47 | AioContext *iohandler_get_aio_context(void) |
| 48 | { |
| 49 | iohandler_init(); |
| 50 | return iohandler_ctx; |
| 51 | } |
| 52 | |
Fam Zheng | f392694 | 2015-09-07 11:28:58 +0800 | [diff] [blame] | 53 | GSource *iohandler_get_g_source(void) |
| 54 | { |
| 55 | iohandler_init(); |
| 56 | return aio_get_g_source(iohandler_ctx); |
| 57 | } |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 58 | |
Fam Zheng | f4d248b | 2015-06-04 14:45:24 +0800 | [diff] [blame] | 59 | void qemu_set_fd_handler(int fd, |
| 60 | IOHandler *fd_read, |
| 61 | IOHandler *fd_write, |
| 62 | void *opaque) |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 63 | { |
Fam Zheng | f392694 | 2015-09-07 11:28:58 +0800 | [diff] [blame] | 64 | iohandler_init(); |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 65 | aio_set_fd_handler(iohandler_ctx, fd, false, |
| 66 | fd_read, fd_write, opaque); |
Paolo Bonzini | 0298141 | 2011-03-09 18:21:09 +0100 | [diff] [blame] | 67 | } |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 68 | |
| 69 | /* reaping of zombies. right now we're not passing the status to |
| 70 | anyone, but it would be possible to add a callback. */ |
| 71 | #ifndef _WIN32 |
| 72 | typedef struct ChildProcessRecord { |
| 73 | int pid; |
| 74 | QLIST_ENTRY(ChildProcessRecord) next; |
| 75 | } ChildProcessRecord; |
| 76 | |
| 77 | static QLIST_HEAD(, ChildProcessRecord) child_watches = |
| 78 | QLIST_HEAD_INITIALIZER(child_watches); |
| 79 | |
| 80 | static QEMUBH *sigchld_bh; |
| 81 | |
| 82 | static void sigchld_handler(int signal) |
| 83 | { |
| 84 | qemu_bh_schedule(sigchld_bh); |
| 85 | } |
| 86 | |
| 87 | static void sigchld_bh_handler(void *opaque) |
| 88 | { |
| 89 | ChildProcessRecord *rec, *next; |
| 90 | |
| 91 | QLIST_FOREACH_SAFE(rec, &child_watches, next, next) { |
| 92 | if (waitpid(rec->pid, NULL, WNOHANG) == rec->pid) { |
| 93 | QLIST_REMOVE(rec, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 94 | g_free(rec); |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static void qemu_init_child_watch(void) |
| 100 | { |
| 101 | struct sigaction act; |
| 102 | sigchld_bh = qemu_bh_new(sigchld_bh_handler, NULL); |
| 103 | |
Peter Maydell | aef553f | 2014-05-16 14:00:03 +0100 | [diff] [blame] | 104 | memset(&act, 0, sizeof(act)); |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 105 | act.sa_handler = sigchld_handler; |
| 106 | act.sa_flags = SA_NOCLDSTOP; |
| 107 | sigaction(SIGCHLD, &act, NULL); |
| 108 | } |
| 109 | |
| 110 | int qemu_add_child_watch(pid_t pid) |
| 111 | { |
| 112 | ChildProcessRecord *rec; |
| 113 | |
| 114 | if (!sigchld_bh) { |
| 115 | qemu_init_child_watch(); |
| 116 | } |
| 117 | |
| 118 | QLIST_FOREACH(rec, &child_watches, next) { |
| 119 | if (rec->pid == pid) { |
| 120 | return 1; |
| 121 | } |
| 122 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 123 | rec = g_malloc0(sizeof(ChildProcessRecord)); |
Paolo Bonzini | 4d54ec7 | 2011-03-09 18:21:10 +0100 | [diff] [blame] | 124 | rec->pid = pid; |
| 125 | QLIST_INSERT_HEAD(&child_watches, rec, next); |
| 126 | return 0; |
| 127 | } |
| 128 | #endif |