From 22f7a6378b21e15d6d916b97430cdb9e84e507d7 Mon Sep 17 00:00:00 2001 From: kraxel Date: Thu, 26 Jan 2006 17:00:38 +0000 Subject: - mdns server first cut done. --- mdns-publish-xendom.c | 121 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 96 insertions(+), 25 deletions(-) (limited to 'mdns-publish-xendom.c') diff --git a/mdns-publish-xendom.c b/mdns-publish-xendom.c index 3d69ab6..f2be81f 100644 --- a/mdns-publish-xendom.c +++ b/mdns-publish-xendom.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include @@ -16,9 +18,12 @@ static AvahiEntryGroup *group = NULL; static AvahiSimplePoll *simple_poll = NULL; +static int debug = 0; static char *name; static char *service = "_xendom._tcp"; static int port = 9; +static char vm_uuid[256]; +static char dom_id[256]; static void create_services(AvahiClient *c); @@ -30,13 +35,15 @@ static void entry_group_callback(AvahiEntryGroup *g, switch (state) { case AVAHI_ENTRY_GROUP_ESTABLISHED: - fprintf(stderr, "Service '%s' successfully established.\n", name); + if (debug) + fprintf(stderr, "Service '%s' successfully established.\n", name); break; case AVAHI_ENTRY_GROUP_COLLISION: n = avahi_alternative_service_name(name); avahi_free(name); name = n; - fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); + if (debug) + fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); create_services(avahi_entry_group_get_client(g)); break; case AVAHI_ENTRY_GROUP_FAILURE : @@ -49,35 +56,32 @@ static void entry_group_callback(AvahiEntryGroup *g, static void create_services(AvahiClient *c) { - char r[128]; int ret; /* If this is the first time we're called, let's create a new entry group */ if (!group) { if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { - fprintf(stderr, "avahi_entry_group_new() failed: %s\n", - avahi_strerror(avahi_client_errno(c))); + if (debug) + fprintf(stderr, "avahi_entry_group_new() failed: %s\n", + avahi_strerror(avahi_client_errno(c))); goto fail; } } - fprintf(stderr, "Adding service '%s'\n", name); - - /* Create some random TXT data */ - snprintf(r, sizeof(r), "random=%i", rand()); - - /* Add the service for IPP */ + /* Add the service */ ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, service, NULL, NULL, port, - "test=blah", r, NULL); + vm_uuid, dom_id, NULL); if (ret < 0) { - fprintf(stderr, "Failed to add %s service: %s\n", service, avahi_strerror(ret)); + if (debug) + fprintf(stderr, "Failed to add %s service: %s\n", service, avahi_strerror(ret)); goto fail; } /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { - fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret)); + if (debug) + fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret)); goto fail; } return; @@ -100,7 +104,8 @@ static void client_callback(AvahiClient *c, avahi_entry_group_reset(group); break; case AVAHI_CLIENT_FAILURE: - fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c))); + if (debug) + fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c))); avahi_simple_poll_quit(simple_poll); break; default: @@ -108,30 +113,61 @@ static void client_callback(AvahiClient *c, } } -static void xendom_info(void) +static int xendom_info(void) { struct xs_handle *xenstore; - struct xs_transaction_handle *xst; + xs_transaction_t xst; char *xs_value; xenstore = xs_domain_open(); if (NULL == xenstore) { fprintf(stderr,"%s: can't connect to %s\n", __FUNCTION__, xs_domain_dev()); - return; + return -1; } - if (NULL == (xst = xs_transaction_start(xenstore))) + if (!(xst = xs_transaction_start(xenstore))) { + fprintf(stderr,"%s: can't start transaction\n", __FUNCTION__); goto out; + } xs_value = xs_read(xenstore, xst, "vm", NULL); xs_transaction_end(xenstore, xst, 0); - if (!xs_value) + if (!xs_value) { + fprintf(stderr,"%s: can't read \"vm\" value\n", __FUNCTION__); + goto out; + } + snprintf(vm_uuid, sizeof(vm_uuid), "vm-uuid=%s", xs_value+4); + + if (!(xst = xs_transaction_start(xenstore))) { + fprintf(stderr,"%s: can't start transaction\n", __FUNCTION__); + goto out; + } + xs_value = xs_read(xenstore, xst, "domid", NULL); + xs_transaction_end(xenstore, xst, 0); + if (!xs_value) { + fprintf(stderr,"%s: can't read \"domid\" value\n", __FUNCTION__); goto out; + } + snprintf(dom_id, sizeof(dom_id), "dom-id=%s", xs_value); - fprintf(stderr, "%s", xs_value); + return 0; out: - return; + return -1; +} + +static void usage(FILE *fp) +{ + fprintf(fp, + "This little daemon publishes xen domain info via mDNS.\n" + "\n" + "usage: mdns-publish-xendom [options]\n" + "options:\n" + " -h print this text\n" + " -d enable debug mode\n" + "\n" + "-- \n" + "(c) 2006 Gerd Hoffmann \n"); } int main(int argc, char*argv[]) @@ -139,18 +175,36 @@ int main(int argc, char*argv[]) AvahiClient *client = NULL; char buf[128]; struct utsname uts; - int error; + int error,c; int ret = 1; + /* parse options */ + for (;;) { + if (-1 == (c = getopt(argc, argv, "hd"))) + break; + switch (c) { + case 'd': + debug = 1; + break; + case 'h': + usage(stdout); + exit(0); + default: + usage(stderr); + exit(1); + } + } + /* figure name */ uname(&uts); snprintf(buf, sizeof(buf), "Xen domain %s", uts.nodename); name = avahi_strdup(buf); /* figure domain info */ - xendom_info(); + if (0 != xendom_info()) + exit(1); - /* go announce info */ + /* prepare nDNS bits */ simple_poll = avahi_simple_poll_new(); if (!simple_poll) { fprintf(stderr, "Failed to create simple poll object.\n"); @@ -164,6 +218,23 @@ int main(int argc, char*argv[]) goto fail; } + /* damonize */ + if (!debug) { + switch (fork()) { + case -1: + perror("fork"); + exit(1); + case 0: + close(0); close(1); close(2); + setsid(); + open("/dev/null", O_RDWR); dup(0); dup(0); + break; + default: + exit(0); + } + } + + /* go ahead */ avahi_simple_poll_loop(simple_poll); ret = 0; -- cgit