virtagent: various bits to build QEMU with virtagent
[qemu/mdroth.git] / qemu-ioh.c
blobcc714704d7d9f6586d3d80baefd2d07afbad9bdc
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.
24 #include "qemu-ioh.h"
25 #include "qlist.h"
27 /* XXX: fd_read_poll should be suppressed, but an API change is
28 necessary in the character devices to suppress fd_can_read(). */
29 int qemu_set_fd_handler3(void *ioh_record_list,
30 int fd,
31 IOCanReadHandler *fd_read_poll,
32 IOHandler *fd_read,
33 IOHandler *fd_write,
34 void *opaque)
36 QLIST_HEAD(, IOHandlerRecord) *io_handlers_ptr = ioh_record_list;
37 IOHandlerRecord *ioh;
39 if (!fd_read && !fd_write) {
40 QLIST_FOREACH(ioh, io_handlers_ptr, next) {
41 if (ioh->fd == fd) {
42 ioh->deleted = 1;
43 break;
46 } else {
47 QLIST_FOREACH(ioh, io_handlers_ptr, next) {
48 if (ioh->fd == fd)
49 goto found;
51 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
52 QLIST_INSERT_HEAD(io_handlers_ptr, ioh, next);
53 found:
54 ioh->fd = fd;
55 ioh->fd_read_poll = fd_read_poll;
56 ioh->fd_read = fd_read;
57 ioh->fd_write = fd_write;
58 ioh->opaque = opaque;
59 ioh->deleted = 0;
61 return 0;
64 /* add entries from ioh record list to fd sets. nfds and fd sets
65 * should be cleared/reset by caller if desired. set a particular
66 * fdset to NULL to ignore fd events of that type
68 void qemu_get_fdset2(void *ioh_record_list, int *nfds, fd_set *rfds,
69 fd_set *wfds, fd_set *xfds)
71 QLIST_HEAD(, IOHandlerRecord) *io_handlers = ioh_record_list;
72 IOHandlerRecord *ioh;
74 QLIST_FOREACH(ioh, io_handlers, next) {
75 if (ioh->deleted)
76 continue;
77 if ((rfds != NULL && ioh->fd_read) &&
78 (!ioh->fd_read_poll ||
79 ioh->fd_read_poll(ioh->opaque) != 0)) {
80 FD_SET(ioh->fd, rfds);
81 if (ioh->fd > *nfds)
82 *nfds = ioh->fd;
84 if (wfds != NULL && ioh->fd_write) {
85 FD_SET(ioh->fd, wfds);
86 if (ioh->fd > *nfds)
87 *nfds = ioh->fd;
92 /* execute registered handlers for r/w events in the provided fdsets. unset
93 * handlers are cleaned up here as well
95 void qemu_process_fd_handlers2(void *ioh_record_list, const fd_set *rfds,
96 const fd_set *wfds, const fd_set *xfds)
98 QLIST_HEAD(, IOHandlerRecord) *io_handlers = ioh_record_list;
99 IOHandlerRecord *ioh, *pioh;
101 QLIST_FOREACH_SAFE(ioh, io_handlers, next, pioh) {
102 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, rfds)) {
103 ioh->fd_read(ioh->opaque);
105 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, wfds)) {
106 ioh->fd_write(ioh->opaque);
109 /* Do this last in case read/write handlers marked it for deletion */
110 if (ioh->deleted) {
111 QLIST_REMOVE(ioh, next);
112 qemu_free(ioh);