aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkraxel <kraxel>2006-02-14 17:43:07 +0000
committerkraxel <kraxel>2006-02-14 17:43:07 +0000
commita1119721e93162b221bcbc1ac00353f69447cec1 (patch)
tree8b7f425b428aff2208ad1fe5cff4ca20beae76bd
parentfa1a0eb5c02c9bb68c9ad6f651b9b7145b35ff9d (diff)
downloadxenwatch-a1119721e93162b221bcbc1ac00353f69447cec1.tar.gz
- add xenstore watch.
-rw-r--r--mdns-publish-xendom.c99
1 files changed, 80 insertions, 19 deletions
diff --git a/mdns-publish-xendom.c b/mdns-publish-xendom.c
index 48bf2bd..1a16384 100644
--- a/mdns-publish-xendom.c
+++ b/mdns-publish-xendom.c
@@ -8,6 +8,7 @@
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/stat.h>
+#include <sys/select.h>
#include <xs.h>
@@ -15,19 +16,22 @@
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
-#include <avahi-common/simple-watch.h>
+#include <avahi-common/thread-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
/* --------------------------------------------------------------------- */
-static AvahiEntryGroup *group = NULL;
-static AvahiSimplePoll *simple_poll = NULL;
+static AvahiEntryGroup *group = NULL;
+static AvahiThreadedPoll *thread_poll = NULL;
+
+static struct xs_handle *xenstore;
static int debug = 0;
static int have_tty = 1;
static int have_syslog = 0;
static char *appname = "mdns-publish-xendom";
+static int app_quit = 0;
static char *name;
static char *service = "_xendom._tcp";
@@ -78,7 +82,7 @@ static void entry_group_callback(AvahiEntryGroup *g,
create_services(avahi_entry_group_get_client(g));
break;
case AVAHI_ENTRY_GROUP_FAILURE:
- avahi_simple_poll_quit(simple_poll);
+ app_quit = 1;
break;
default:
break;
@@ -115,7 +119,7 @@ static void create_services(AvahiClient *c)
return;
fail:
- avahi_simple_poll_quit(simple_poll);
+ app_quit = 1;
}
static void client_callback(AvahiClient *c,
@@ -144,7 +148,7 @@ static void client_callback(AvahiClient *c,
group = NULL;
log_printf(LOG_NOTICE, "disconnected from avahi daemon, reconnecting ...\n");
- c = avahi_client_new(avahi_simple_poll_get(simple_poll),
+ c = avahi_client_new(avahi_threaded_poll_get(thread_poll),
AVAHI_CLIENT_NO_FAIL,
client_callback, NULL, &error);
if (!c) {
@@ -166,14 +170,13 @@ static void client_callback(AvahiClient *c,
return;
fail:
- avahi_simple_poll_quit(simple_poll);
+ app_quit = 1;
}
-static int update_xendom_info(void)
+/* --------------------------------------------------------------------- */
+
+static int xen_init(void)
{
- struct xs_handle *xenstore;
- xs_transaction_t xst;
- char *xs_value;
struct stat st;
if (-1 == stat("/proc/xen", &st)) {
@@ -187,6 +190,16 @@ static int update_xendom_info(void)
return -1;
}
+ xs_watch(xenstore, "vm", "token");
+ xs_watch(xenstore, "domid", "token");
+ return 0;
+}
+
+static int xen_get_info(void)
+{
+ xs_transaction_t xst;
+ char *xs_value;
+
if (!(xst = xs_transaction_start(xenstore))) {
log_printf(LOG_ERR, "can't start xenstore transaction\n");
goto out;
@@ -217,6 +230,21 @@ static int update_xendom_info(void)
return -1;
}
+static int xen_watch(void)
+{
+ char **vec = NULL;
+ unsigned int count;
+
+ vec = xs_read_watch(xenstore, &count);
+ log_printf(LOG_DEBUG, "%s: \"%s\"\n", __FUNCTION__, vec[XS_WATCH_PATH]);
+
+ if (vec)
+ free(vec);
+ return 0;
+}
+
+/* --------------------------------------------------------------------- */
+
static void usage(FILE *fp)
{
fprintf(fp,
@@ -233,6 +261,20 @@ static void usage(FILE *fp)
appname);
}
+static int wait_fd(int fd, int secs)
+{
+ struct timeval tv;
+ fd_set rd;
+ int rc;
+
+ FD_ZERO(&rd);
+ FD_SET(fd,&rd);
+ tv.tv_sec = secs;
+ tv.tv_usec = 0;
+ rc = select(fd+1, &rd, NULL, NULL, &tv);
+ return rc;
+}
+
int main(int argc, char*argv[])
{
AvahiClient *client = NULL;
@@ -268,17 +310,18 @@ int main(int argc, char*argv[])
name = avahi_strdup(buf);
/* figure domain info */
- if (0 != update_xendom_info())
+ if (0 != xen_init())
goto fail;
+ xen_get_info();
/* prepare nDNS bits */
- simple_poll = avahi_simple_poll_new();
- if (!simple_poll) {
+ thread_poll = avahi_threaded_poll_new();
+ if (!thread_poll) {
log_printf(LOG_ERR, "failed to create simple poll object\n");
goto fail;
}
- client = avahi_client_new(avahi_simple_poll_get(simple_poll),
+ client = avahi_client_new(avahi_threaded_poll_get(thread_poll),
AVAHI_CLIENT_NO_FAIL,
client_callback, NULL, &error);
if (!client) {
@@ -305,9 +348,27 @@ int main(int argc, char*argv[])
}
}
- /* go ahead */
- avahi_simple_poll_loop(simple_poll);
+ /* enter main loop */
ret = 0;
+ avahi_threaded_poll_start(thread_poll);
+
+ for (;;) {
+ if (app_quit)
+ break;
+ switch (wait_fd(xs_fileno(xenstore),1)) {
+ case -1:
+ log_printf(LOG_ERR, "select: %s", strerror(errno));
+ app_quit = 1;
+ case 0:
+ /* timeout */
+ break;
+ default:
+ /* data to read */
+ xen_watch();
+ }
+ }
+
+ avahi_threaded_poll_stop(thread_poll);
fail:
log_printf(ret ? LOG_ERR : LOG_INFO, "exiting (%d)\n", ret);
@@ -316,8 +377,8 @@ fail:
if (client)
avahi_client_free(client);
- if (simple_poll)
- avahi_simple_poll_free(simple_poll);
+ if (thread_poll)
+ avahi_threaded_poll_free(thread_poll);
avahi_free(name);
return ret;