diff options
author | kraxel <kraxel> | 2006-02-14 18:09:00 +0000 |
---|---|---|
committer | kraxel <kraxel> | 2006-02-14 18:09:00 +0000 |
commit | 0594e0e1c7d386c1308da9ddb934d123875ad20b (patch) | |
tree | dd781c87ba31d7e9459e961fba50cc25cfe5493b /mdns-publish-xendom.c | |
parent | a1119721e93162b221bcbc1ac00353f69447cec1 (diff) | |
download | xenwatch-0594e0e1c7d386c1308da9ddb934d123875ad20b.tar.gz |
more daemon stuff.
Diffstat (limited to 'mdns-publish-xendom.c')
-rw-r--r-- | mdns-publish-xendom.c | 59 |
1 files changed, 53 insertions, 6 deletions
diff --git a/mdns-publish-xendom.c b/mdns-publish-xendom.c index 1a16384..430bfd0 100644 --- a/mdns-publish-xendom.c +++ b/mdns-publish-xendom.c @@ -1,3 +1,5 @@ +#define _GNU_SOURCE + #include <time.h> #include <stdio.h> #include <stdlib.h> @@ -9,6 +11,7 @@ #include <sys/utsname.h> #include <sys/stat.h> #include <sys/select.h> +#include <sys/signal.h> #include <xs.h> @@ -32,6 +35,7 @@ static int have_tty = 1; static int have_syslog = 0; static char *appname = "mdns-publish-xendom"; static int app_quit = 0; +static int termsig = 0; static char *name; static char *service = "_xendom._tcp"; @@ -190,8 +194,6 @@ static int xen_init(void) return -1; } - xs_watch(xenstore, "vm", "token"); - xs_watch(xenstore, "domid", "token"); return 0; } @@ -230,7 +232,20 @@ static int xen_get_info(void) return -1; } -static int xen_watch(void) +static int xen_watch_add(char *path) +{ + int ret = 0; + + /* Hmm, not working ... */ + if (!xs_watch(xenstore, path, "token")) { + log_printf(LOG_ERR, "%s: xs_watch for \"%s\" failed\n", + __FUNCTION__, path); + ret = -1; + } + return ret; +} + +static int xen_watch_data(void) { char **vec = NULL; unsigned int count; @@ -272,11 +287,22 @@ static int wait_fd(int fd, int secs) tv.tv_sec = secs; tv.tv_usec = 0; rc = select(fd+1, &rd, NULL, NULL, &tv); + if (-1 == rc) { + if (EINTR == errno) + return 0; + } return rc; } +static void catchsig(int signal) +{ + termsig = signal; + app_quit = 1; +} + int main(int argc, char*argv[]) { + struct sigaction act,old; AvahiClient *client = NULL; char buf[128]; struct utsname uts; @@ -329,6 +355,18 @@ int main(int argc, char*argv[]) goto fail; } + /* setup signal handler */ + memset(&act,0,sizeof(act)); + sigemptyset(&act.sa_mask); + act.sa_handler = SIG_IGN; + sigaction(SIGPIPE,&act,&old); + act.sa_handler = catchsig; + sigaction(SIGHUP,&act,&old); + sigaction(SIGUSR1,&act,&old); + sigaction(SIGTERM,&act,&old); + if (debug) + sigaction(SIGINT,&act,&old); + /* damonize */ if (!debug) { switch (fork()) { @@ -352,26 +390,35 @@ int main(int argc, char*argv[]) ret = 0; avahi_threaded_poll_start(thread_poll); +#if 0 /* hmm, not working */ + if (0 != xen_watch_add("memory/target")) + goto fail; + xen_watch_add("vm"); + xen_watch_add("domid"); +#endif + for (;;) { if (app_quit) break; switch (wait_fd(xs_fileno(xenstore),1)) { case -1: - log_printf(LOG_ERR, "select: %s", strerror(errno)); + log_printf(LOG_ERR, "select: %s\n", strerror(errno)); app_quit = 1; case 0: /* timeout */ break; default: /* data to read */ - xen_watch(); + xen_watch_data(); } } avahi_threaded_poll_stop(thread_poll); fail: - log_printf(ret ? LOG_ERR : LOG_INFO, "exiting (%d)\n", ret); + log_printf(ret ? LOG_ERR : LOG_INFO, "exiting (%d)%s%s\n", ret, + termsig ? ", on signal " : "", + termsig ? strsignal(termsig) : ""); /* Cleanup things */ if (client) |