debuggers.hg

view tools/ioemu/tapdisk-ioemu.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdint.h>
6 #include <signal.h>
7 #include <unistd.h>
8 #include <sys/time.h>
10 #include <assert.h>
12 extern int init_blktap(void);
13 extern void qemu_aio_init(void);
14 extern void qemu_aio_poll(void);
15 extern void bdrv_init(void);
17 extern void *qemu_mallocz(size_t size);
18 extern void qemu_free(void *ptr);
20 extern void *fd_start;
22 int domid = 0;
23 FILE* logfile;
25 void term_printf(const char *fmt, ...)
26 {
27 va_list ap;
28 va_start(ap, fmt);
29 vprintf(fmt, ap);
30 va_end(ap);
31 }
33 void term_print_filename(const char *filename)
34 {
35 term_printf(filename);
36 }
39 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
40 typedef int IOCanRWHandler(void *opaque);
41 typedef void IOHandler(void *opaque);
43 typedef struct IOHandlerRecord {
44 int fd;
45 IOCanRWHandler *fd_read_poll;
46 IOHandler *fd_read;
47 IOHandler *fd_write;
48 int deleted;
49 void *opaque;
50 /* temporary data */
51 struct pollfd *ufd;
52 struct IOHandlerRecord *next;
53 } IOHandlerRecord;
55 static IOHandlerRecord *first_io_handler;
57 int qemu_set_fd_handler2(int fd,
58 IOCanRWHandler *fd_read_poll,
59 IOHandler *fd_read,
60 IOHandler *fd_write,
61 void *opaque)
62 {
63 IOHandlerRecord *ioh;
65 /* This is a stripped down version of fd handling */
66 assert(fd_read_poll == NULL);
67 assert(fd_write == NULL);
69 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next)
70 if (ioh->fd == fd)
71 goto found;
73 if (!fd_read && !fd_write)
74 return 0;
76 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
77 if (!ioh)
78 return -1;
79 ioh->next = first_io_handler;
80 first_io_handler = ioh;
82 found:
83 if (!fd_read && !fd_write) {
84 ioh->deleted = 1;
85 } else {
86 ioh->fd = fd;
87 ioh->fd_read = fd_read;
88 ioh->opaque = opaque;
89 ioh->deleted = 0;
90 }
92 return 0;
93 }
95 int main(void)
96 {
97 IOHandlerRecord *ioh, **pioh;
98 int max_fd;
99 fd_set rfds;
100 struct timeval tv;
101 void *old_fd_start = NULL;
103 logfile = stderr;
105 bdrv_init();
106 qemu_aio_init();
107 init_blktap();
109 /* Daemonize */
110 if (fork() != 0)
111 exit(0);
113 /*
114 * Main loop: Pass events to the corrsponding handlers and check for
115 * completed aio operations.
116 */
117 while (1) {
118 qemu_aio_poll();
120 max_fd = -1;
121 FD_ZERO(&rfds);
122 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next)
123 if (!ioh->deleted) {
124 FD_SET(ioh->fd, &rfds);
125 max_fd = max_fd > ioh->fd ? max_fd : ioh->fd;
126 }
128 tv.tv_sec = 0;
129 tv.tv_usec = 10000;
130 if (select(max_fd + 1, &rfds, NULL, NULL, &tv) <= 0)
131 continue;
133 /* Call handlers */
134 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next)
135 if (FD_ISSET(ioh->fd, &rfds))
136 ioh->fd_read(ioh->opaque);
138 /* Remove deleted IO handlers */
139 pioh = &first_io_handler;
140 while (*pioh) {
141 ioh = *pioh;
142 if (ioh->deleted) {
143 *pioh = ioh->next;
144 qemu_free(ioh);
145 } else
146 pioh = &ioh->next;
147 }
149 /* Exit when the last image has been closed */
150 if (old_fd_start != NULL && fd_start == NULL)
151 exit(0);
153 old_fd_start = fd_start;
154 }
155 return 0;
156 }