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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <assert.h>
extern int init_blktap(void);
extern void qemu_aio_init(void);
extern void qemu_aio_poll(void);
extern void bdrv_init(void);
extern void *qemu_mallocz(size_t size);
extern void qemu_free(void *ptr);
extern void *fd_start;
int domid = 0;
FILE* logfile;
void term_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
void term_print_filename(const char *filename)
{
term_printf(filename);
}
typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
typedef int IOCanRWHandler(void *opaque);
typedef void IOHandler(void *opaque);
typedef struct IOHandlerRecord {
int fd;
IOCanRWHandler *fd_read_poll;
IOHandler *fd_read;
IOHandler *fd_write;
int deleted;
void *opaque;
/* temporary data */
struct pollfd *ufd;
struct IOHandlerRecord *next;
} IOHandlerRecord;
static IOHandlerRecord *first_io_handler;
int qemu_set_fd_handler2(int fd,
IOCanRWHandler *fd_read_poll,
IOHandler *fd_read,
IOHandler *fd_write,
void *opaque)
{
IOHandlerRecord *ioh;
/* This is a stripped down version of fd handling */
assert(fd_read_poll == NULL);
assert(fd_write == NULL);
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next)
if (ioh->fd == fd)
goto found;
if (!fd_read && !fd_write)
return 0;
ioh = qemu_mallocz(sizeof(IOHandlerRecord));
if (!ioh)
return -1;
ioh->next = first_io_handler;
first_io_handler = ioh;
found:
if (!fd_read && !fd_write) {
ioh->deleted = 1;
} else {
ioh->fd = fd;
ioh->fd_read = fd_read;
ioh->opaque = opaque;
ioh->deleted = 0;
}
return 0;
}
int main(void)
{
IOHandlerRecord *ioh, **pioh;
int max_fd;
fd_set rfds;
struct timeval tv;
void *old_fd_start = NULL;
logfile = stderr;
bdrv_init();
qemu_aio_init();
init_blktap();
/* Daemonize */
if (fork() != 0)
exit(0);
/*
* Main loop: Pass events to the corrsponding handlers and check for
* completed aio operations.
*/
while (1) {
qemu_aio_poll();
max_fd = -1;
FD_ZERO(&rfds);
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next)
if (!ioh->deleted) {
FD_SET(ioh->fd, &rfds);
max_fd = max_fd > ioh->fd ? max_fd : ioh->fd;
}
tv.tv_sec = 0;
tv.tv_usec = 10000;
if (select(max_fd + 1, &rfds, NULL, NULL, &tv) <= 0)
continue;
/* Call handlers */
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next)
if (FD_ISSET(ioh->fd, &rfds))
ioh->fd_read(ioh->opaque);
/* Remove deleted IO handlers */
pioh = &first_io_handler;
while (*pioh) {
ioh = *pioh;
if (ioh->deleted) {
*pioh = ioh->next;
qemu_free(ioh);
} else
pioh = &ioh->next;
}
/* Exit when the last image has been closed */
if (old_fd_start != NULL && fd_start == NULL)
exit(0);
old_fd_start = fd_start;
}
return 0;
}
|