diff options
author | Eamon Walsh <ewalsh@tycho.nsa.gov> | 2011-06-01 15:50:54 -0400 |
---|---|---|
committer | Eamon Walsh <ewalsh@tycho.nsa.gov> | 2011-06-01 15:50:54 -0400 |
commit | 71bc57fdd1c239fff1be3ede4e4e7a2a94878cda (patch) | |
tree | 2b6b9e6d6d10d9f69bc4e45c61952b5bacef3d38 /src | |
parent | a77dbaf7a204f96eb52bf32246be51ca39890dda (diff) |
Factor out fd-handling code into a common file.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/fd.c | 96 | ||||
-rw-r--r-- | src/fd.h | 15 | ||||
-rw-r--r-- | src/input.c | 10 | ||||
-rw-r--r-- | src/local.c | 4 | ||||
-rw-r--r-- | src/monitor.c | 107 | ||||
-rw-r--r-- | src/server.c | 107 | ||||
-rw-r--r-- | src/server.h | 8 | ||||
-rw-r--r-- | src/xen_backend.c | 8 | ||||
-rw-r--r-- | src/xen_linpicker.h | 2 | ||||
-rw-r--r-- | src/xen_server.c | 4 |
11 files changed, 135 insertions, 230 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 80cc4c2..c66bb73 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,12 +3,12 @@ bin_PROGRAMS = linpicker_server linpicker_monitor linpicker_track AM_CFLAGS = -I../libvchan $(DFB_CFLAGS) $(UDEV_CFLAGS) $(XCB_CFLAGS) \ -DPKGDATADIR="\"$(pkgdatadir)\"" -linpicker_monitor_SOURCES = monitor.c \ +linpicker_monitor_SOURCES = monitor.c fd.c \ xen_backend.c xen_monitor.c xenfb_monitor.c linpicker_monitor_LDADD = -lxenlight -lxenguest -lxenctrl -lxenstore \ -luuid -lblktapctl -lutil -linpicker_server_SOURCES = server.c local.c input.c comm.c \ +linpicker_server_SOURCES = server.c fd.c local.c input.c comm.c \ view.c buffer.c client.c display.c \ sclient.c dclient.c bclient.c \ xen_backend.c xen_server.c xenfb_server.c diff --git a/src/fd.c b/src/fd.c new file mode 100644 index 0000000..841a6c9 --- /dev/null +++ b/src/fd.c @@ -0,0 +1,96 @@ +#include <sys/types.h> +#include <sys/select.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include "sys-queue.h" +#include "fd.h" + +struct listener { + LIST_ENTRY(listener) next; + int fd; + int deleted; + fd_handler callback; + void *closure; +}; + +LIST_HEAD(listener_list, listener); + +static struct listener_list listeners; +static fd_set fdset, readset; +static int maxfd; + +int +fd_set_handler(int fd, fd_handler callback, void *closure) +{ + struct listener *l; + + if (callback == NULL) { + LIST_FOREACH(l, &listeners, next) + if (l->fd == fd) { + l->deleted = 1; + FD_CLR(fd, &fdset); + } + return 0; + } + + l = calloc(1, sizeof(struct listener)); + if (!l) + return -1; + + l->fd = fd; + l->callback = callback; + l->closure = closure; + + LIST_INSERT_HEAD(&listeners, l, next); + FD_SET(fd, &fdset); + maxfd = (fd + 1 > maxfd) ? fd + 1: maxfd; + + return 0; +} + +int +fd_run_select(void) +{ + struct listener *l; + int rc; + + memcpy(&readset, &fdset, sizeof(fd_set)); + rc = select(maxfd, &readset, NULL, NULL, NULL); + if (rc < 0) { + if (errno == EAGAIN || errno == EINTR) + return 0; + perror("select"); + return -1; + } + +loop: + LIST_FOREACH(l, &listeners, next) { + if (l->deleted) { + LIST_REMOVE(l, next); + free(l); + goto loop; + } + if (FD_ISSET(l->fd, &readset)) { + l->callback(l->closure); + FD_CLR(l->fd, &readset); + } + } + + return 0; +} + +void +fd_free_handlers(void) +{ + struct listener *l; + + while (!LIST_EMPTY(&listeners)) { + l = LIST_FIRST(&listeners); + LIST_REMOVE(l, next); + free(l); + } +} + diff --git a/src/fd.h b/src/fd.h new file mode 100644 index 0000000..64da71d --- /dev/null +++ b/src/fd.h @@ -0,0 +1,15 @@ +#ifndef _LINPICKER_FD_H_ +#define _LINPICKER_FD_H_ + +typedef void (*fd_handler)(void *closure); + +int +fd_set_handler(int fd, fd_handler callback, void *closure); + +int +fd_run_select(void); + +void +fd_free_handlers(void); + +#endif /* _LINPICKER_FD_H_ */ diff --git a/src/input.c b/src/input.c index 1d3f1f6..7d7a3b8 100644 --- a/src/input.c +++ b/src/input.c @@ -13,7 +13,7 @@ #include "client.h" #include "display.h" #include "input.h" -#include "server.h" +#include "fd.h" #define MAX_DEVICES 16 #define DEVICE_PATH_LEN 32 @@ -246,7 +246,7 @@ open_device(const char *path) if ((bits & (1 << EV_ABS)) && configure_absolute_device(fd) < 0) goto err; - if (server_set_fd_handler(fd, input_event, d) < 0) + if (fd_set_handler(fd, input_event, d) < 0) goto err; TAILQ_INSERT_TAIL(&devices, d, next); @@ -265,7 +265,7 @@ static void close_device(struct device *d) { ioctl(d->fd, EVIOCGRAB, 0); - server_set_fd_handler(d->fd, NULL, NULL); + fd_set_handler(d->fd, NULL, NULL); close(d->fd); TAILQ_REMOVE(&devices, d, next); free(d); @@ -381,7 +381,7 @@ input_initialize(int argc, char *argv[]) udev_enumerate_unref(enumerate); fd = udev_monitor_get_fd(udev_monitor); - if (server_set_fd_handler(fd, udev_event, NULL) < 0) + if (fd_set_handler(fd, udev_event, NULL) < 0) goto err; absx = display_get_width() / 2; @@ -401,7 +401,7 @@ input_shutdown(void) udev = udev_monitor_get_udev(udev_monitor); fd = udev_monitor_get_fd(udev_monitor); - server_set_fd_handler(fd, NULL, NULL); + fd_set_handler(fd, NULL, NULL); udev_monitor_unref(udev_monitor); udev_unref(udev); diff --git a/src/local.c b/src/local.c index 58d42a2..a32b955 100644 --- a/src/local.c +++ b/src/local.c @@ -9,7 +9,7 @@ #include "config.h" #include "local.h" -#include "server.h" +#include "fd.h" #include "view.h" #ifdef LOCALSOCK @@ -63,7 +63,7 @@ local_init(int argc, char **argv) return -1; } - return server_set_fd_handler(sd, local_process, NULL); + return fd_set_handler(sd, local_process, NULL); } #else diff --git a/src/monitor.c b/src/monitor.c index f5875fd..17253ac 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -3,33 +3,18 @@ #include <stdio.h> #include <string.h> #include <unistd.h> -#include <string.h> #include <fcntl.h> #include <signal.h> #include <errno.h> #include "xen_backend.h" #include "sys-queue.h" -#include "server.h" +#include "fd.h" extern int xenfb_monitor_init(void); static int timetodie; -struct listener { - LIST_ENTRY(listener) next; - int fd; - int deleted; - void (*callback)(void *); - void *closure; -}; - -LIST_HEAD(listener_list, listener); - -static struct listener_list listeners; -static fd_set fdset; -static int maxfd; - /* * Is called in case of process signals. */ @@ -39,85 +24,12 @@ server_sighandler(int sig) timetodie = 1; } -/* - * FD handling - */ -static void -server_remove_fd_handler(int fd) -{ - struct listener *l; - - LIST_FOREACH(l, &listeners, next) - if (l->fd == fd) { - l->deleted = 1; - FD_CLR(fd, &fdset); - } -} - -int -server_set_fd_handler(int fd, void (*callback)(void *), void *closure) -{ - struct listener *l; - - if (callback == NULL) { - server_remove_fd_handler(fd); - return 0; - } - - l = calloc(1, sizeof(struct listener)); - if (!l) - return -1; - - l->fd = fd; - l->callback = callback; - l->closure = closure; - - LIST_INSERT_HEAD(&listeners, l, next); - FD_SET(fd, &fdset); - maxfd = (fd + 1 > maxfd) ? fd + 1: maxfd; - - return 0; -} - -static void -server_call_fd_handlers(fd_set *readset) -{ - struct listener *l; - -top: - LIST_FOREACH(l, &listeners, next) { - if (l->deleted) { - LIST_REMOVE(l, next); - free(l); - goto top; - } - if (FD_ISSET(l->fd, readset)) { - l->callback(l->closure); - FD_CLR(l->fd, readset); - } - } -} - -static void -server_free_fd_handlers(void) -{ - struct listener *l; - - while (!LIST_EMPTY(&listeners)) { - l = LIST_FIRST(&listeners); - LIST_REMOVE(l, next); - free(l); - } -} - /* * Main */ int main (int argc, char *argv[]) { struct sigaction sa = { .sa_handler = server_sighandler }; - fd_set readset; - int rc; /* Set up sighandlers */ sigaction(SIGINT, &sa, NULL); @@ -130,20 +42,9 @@ int main (int argc, char *argv[]) return 2; /* Enter select loop */ - while (!timetodie) - { - memcpy(&readset, &fdset, sizeof(fd_set)); - rc = select(maxfd, &readset, NULL, NULL, NULL); - if (rc < 0) { - if (errno == EAGAIN || errno == EINTR) - continue; - perror("select"); - break; - } - - server_call_fd_handlers(&readset); - } + while (!timetodie && fd_run_select() == 0); - server_free_fd_handlers(); + /* Shut down */ + fd_free_handlers(); return 0; } diff --git a/src/server.c b/src/server.c index af64934..1ba43be 100644 --- a/src/server.c +++ b/src/server.c @@ -13,7 +13,6 @@ #include <stdio.h> #include <string.h> #include <unistd.h> -#include <string.h> #include <fcntl.h> #include <signal.h> #include <errno.h> @@ -22,25 +21,11 @@ #include "sys-queue.h" #include "display.h" #include "input.h" -#include "server.h" #include "local.h" +#include "fd.h" static int timetodie; -struct listener { - LIST_ENTRY(listener) next; - int fd; - int deleted; - void (*callback)(void *); - void *closure; -}; - -LIST_HEAD(listener_list, listener); - -static struct listener_list listeners; -static fd_set fdset; -static int maxfd; - /* * Is called in case of process signals. */ @@ -50,85 +35,12 @@ server_sighandler(int sig) timetodie = 1; } -/* - * FD handling - */ -static void -server_remove_fd_handler(int fd) -{ - struct listener *l; - - LIST_FOREACH(l, &listeners, next) - if (l->fd == fd) { - l->deleted = 1; - FD_CLR(fd, &fdset); - } -} - -int -server_set_fd_handler(int fd, void (*callback)(void *), void *closure) -{ - struct listener *l; - - if (callback == NULL) { - server_remove_fd_handler(fd); - return 0; - } - - l = calloc(1, sizeof(struct listener)); - if (!l) - return -1; - - l->fd = fd; - l->callback = callback; - l->closure = closure; - - LIST_INSERT_HEAD(&listeners, l, next); - FD_SET(fd, &fdset); - maxfd = (fd + 1 > maxfd) ? fd + 1: maxfd; - - return 0; -} - -static void -server_call_fd_handlers(fd_set *readset) -{ - struct listener *l; - -top: - LIST_FOREACH(l, &listeners, next) { - if (l->deleted) { - LIST_REMOVE(l, next); - free(l); - goto top; - } - if (FD_ISSET(l->fd, readset)) { - l->callback(l->closure); - FD_CLR(l->fd, readset); - } - } -} - -static void -server_free_fd_handlers(void) -{ - struct listener *l; - - while (!LIST_EMPTY(&listeners)) { - l = LIST_FIRST(&listeners); - LIST_REMOVE(l, next); - free(l); - } -} - /* * Main */ int main (int argc, char *argv[]) { struct sigaction sa = { .sa_handler = server_sighandler }; - fd_set readset; - int rc; /* Set up sighandlers */ sigaction(SIGINT, &sa, NULL); @@ -145,22 +57,11 @@ int main (int argc, char *argv[]) return 4; /* Enter select loop */ - while (!timetodie) - { - memcpy(&readset, &fdset, sizeof(fd_set)); - rc = select(maxfd, &readset, NULL, NULL, NULL); - if (rc < 0) { - if (errno == EAGAIN || errno == EINTR) - continue; - perror("select"); - break; - } - - server_call_fd_handlers(&readset); - } + while (!timetodie && fd_run_select() == 0); + /* Shut down */ input_shutdown(); display_shutdown(); - server_free_fd_handlers(); + fd_free_handlers(); return 0; } diff --git a/src/server.h b/src/server.h deleted file mode 100644 index c3735de..0000000 --- a/src/server.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _LINPICKER_SERVER_H_ -#define _LINPICKER_SERVER_H_ - -int -server_set_fd_handler(int fd, void (*callback)(void *), void *closure); - - -#endif /* _LINPICKER_SERVER_H_ */ diff --git a/src/xen_backend.c b/src/xen_backend.c index 7cec45a..d6fca41 100644 --- a/src/xen_backend.c +++ b/src/xen_backend.c @@ -641,7 +641,7 @@ int xen_be_init(void) return -1; } - if (server_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL) < 0) + if (fd_set_handler(xs_fileno(xenstore), xenstore_update, NULL) < 0) goto err; xen_xc = xc_interface_open(0,0,0); @@ -660,7 +660,7 @@ int xen_be_init(void) err2: xc_interface_close(xen_xc); err: - server_set_fd_handler(xs_fileno(xenstore), NULL, NULL); + fd_set_handler(xs_fileno(xenstore), NULL, NULL); xs_daemon_close(xenstore); xenstore = NULL; @@ -683,7 +683,7 @@ int xen_be_bind_evtchn(struct XenDevice *xendev) return -1; } xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port); - server_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), + fd_set_handler(xc_evtchn_fd(xendev->evtchndev), xen_be_evtchn_event, xendev); return 0; } @@ -692,7 +692,7 @@ void xen_be_unbind_evtchn(struct XenDevice *xendev) { if (xendev->local_port == -1) return; - server_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL); + fd_set_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL); xc_evtchn_unbind(xendev->evtchndev, xendev->local_port); xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port); xendev->local_port = -1; diff --git a/src/xen_linpicker.h b/src/xen_linpicker.h index 76c2b6c..a5e1d18 100644 --- a/src/xen_linpicker.h +++ b/src/xen_linpicker.h @@ -7,7 +7,7 @@ #include "common.h" #include "xen_backend.h" -#include "server.h" +#include "fd.h" #include "buffer.h" #include "display.h" #include "input.h" diff --git a/src/xen_server.c b/src/xen_server.c index 049d94c..2cff419 100644 --- a/src/xen_server.c +++ b/src/xen_server.c @@ -43,7 +43,7 @@ static void vchan_shutdown(struct dominfo *d) { TAILQ_REMOVE(&vchans, d, next); - server_set_fd_handler(d->fd, NULL, NULL); + fd_set_handler(d->fd, NULL, NULL); libvchan_close(d->ctrl); fprintf(stderr, "Tore down vchan for domain %d\n", d->domid); free(d); @@ -122,7 +122,7 @@ vchan_setup(struct xs_handle *xenstore, xc_interface* xen_xc, const char *path) // Note: failure to write is ignored for the moment, as this means we // already have an update request enqueued - if (server_set_fd_handler(d->fd, vchan_update, d) < 0) + if (fd_set_handler(d->fd, vchan_update, d) < 0) goto err; vchan_queue(d); |