diff options
Diffstat (limited to 'mdns-publish-xendom.c')
-rw-r--r-- | mdns-publish-xendom.c | 66 |
1 files changed, 41 insertions, 25 deletions
diff --git a/mdns-publish-xendom.c b/mdns-publish-xendom.c index 69eccb4..3cc8119 100644 --- a/mdns-publish-xendom.c +++ b/mdns-publish-xendom.c @@ -5,7 +5,9 @@ #include <fcntl.h> #include <syslog.h> #include <string.h> +#include <sys/types.h> #include <sys/utsname.h> +#include <sys/stat.h> #include <xs.h> @@ -17,12 +19,15 @@ #include <avahi-common/malloc.h> #include <avahi-common/error.h> +/* --------------------------------------------------------------------- */ + static AvahiEntryGroup *group = NULL; static AvahiSimplePoll *simple_poll = NULL; static int debug = 0; static int have_tty = 1; static int have_syslog = 0; +static char *appname = "mdns-publish-xendom"; static char *name; static char *service = "_xendom._tcp"; @@ -44,7 +49,7 @@ log_printf(int priority, char *fmt, ...) va_end(args); if (have_tty) - fprintf(stderr, "%s", msgbuf); + fprintf(stderr, "%s: %s", appname, msgbuf); if (have_syslog) syslog(priority, "%s", msgbuf); return rc; @@ -62,17 +67,17 @@ static void entry_group_callback(AvahiEntryGroup *g, switch (state) { case AVAHI_ENTRY_GROUP_ESTABLISHED: - log_printf(LOG_INFO, "Service '%s' successfully established.\n", name); + log_printf(LOG_INFO, "service '%s' successfully established\n", name); break; case AVAHI_ENTRY_GROUP_COLLISION: n = avahi_alternative_service_name(name); - log_printf(LOG_NOTICE, "Service name collision, renaming '%s' to '%s'\n", + log_printf(LOG_NOTICE, "service name collision, renaming '%s' to '%s'\n", name, n); avahi_free(name); name = n; create_services(avahi_entry_group_get_client(g)); break; - case AVAHI_ENTRY_GROUP_FAILURE : + case AVAHI_ENTRY_GROUP_FAILURE: avahi_simple_poll_quit(simple_poll); break; default: @@ -98,13 +103,13 @@ static void create_services(AvahiClient *c) name, service, NULL, NULL, port, vm_uuid, dom_id, NULL); if (ret < 0) { - log_printf(LOG_ERR, "Failed to add '%s' service: %s\n", service, avahi_strerror(ret)); + log_printf(LOG_ERR, "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) { - log_printf(LOG_ERR, "Failed to commit entry_group: %s\n", avahi_strerror(ret)); + log_printf(LOG_ERR, "failed to commit entry_group: %s\n", avahi_strerror(ret)); goto fail; } return; @@ -121,7 +126,7 @@ static void client_callback(AvahiClient *c, switch (state) { case AVAHI_CLIENT_CONNECTING: - log_printf(LOG_NOTICE, "avahi not running (yet), I'll keep trying ...\n"); + log_printf(LOG_NOTICE, "avahi daemon not running (yet), I'll keep trying ...\n"); break; case AVAHI_CLIENT_S_RUNNING: if (!group) @@ -138,18 +143,18 @@ static void client_callback(AvahiClient *c, avahi_client_free(c); group = NULL; - log_printf(LOG_NOTICE, "disconnected from avahi, reconnecting ...\n"); + log_printf(LOG_NOTICE, "disconnected from avahi daemon, reconnecting ...\n"); c = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error); if (!c) { - log_printf(LOG_ERR, "Failed to create client: %s\n", + log_printf(LOG_ERR, "failed to create client: %s\n", avahi_strerror(error)); goto fail; } break; default: - log_printf(LOG_ERR, "Client failure: %s\n", + log_printf(LOG_ERR, "client failure: %s\n", avahi_strerror(avahi_client_errno(c))); goto fail; break; @@ -169,34 +174,39 @@ static int update_xendom_info(void) struct xs_handle *xenstore; xs_transaction_t xst; char *xs_value; + struct stat st; + + if (-1 == stat("/proc/xen", &st)) { + log_printf(LOG_ERR, "/proc/xen not found, running on bare metal?\n"); + return -1; + } xenstore = xs_domain_open(); if (NULL == xenstore) { - log_printf(LOG_ERR, "%s: can't connect to %s\n", __FUNCTION__, - xs_domain_dev()); + log_printf(LOG_ERR, "can't connect to xenstore (%s)\n", xs_domain_dev()); return -1; } if (!(xst = xs_transaction_start(xenstore))) { - log_printf(LOG_ERR, "%s: can't start transaction\n", __FUNCTION__); + log_printf(LOG_ERR, "can't start xenstore transaction\n"); goto out; } xs_value = xs_read(xenstore, xst, "vm", NULL); xs_transaction_end(xenstore, xst, 0); if (!xs_value) { - log_printf(LOG_ERR, "%s: can't read \"vm\" value\n", __FUNCTION__); + log_printf(LOG_ERR, "can't read 'vm' value from xenstore\n"); goto out; } snprintf(vm_uuid, sizeof(vm_uuid), "vm-uuid=%s", xs_value+4); if (!(xst = xs_transaction_start(xenstore))) { - log_printf(LOG_ERR, "%s: can't start transaction\n", __FUNCTION__); + log_printf(LOG_ERR, "can't start xenstore transaction\n"); goto out; } xs_value = xs_read(xenstore, xst, "domid", NULL); xs_transaction_end(xenstore, xst, 0); if (!xs_value) { - log_printf(LOG_ERR, "%s: can't read \"domid\" value\n", __FUNCTION__); + log_printf(LOG_ERR, "can't read 'domid' value from xenstore\n"); goto out; } snprintf(dom_id, sizeof(dom_id), "dom-id=%s", xs_value); @@ -210,15 +220,17 @@ static int update_xendom_info(void) static void usage(FILE *fp) { fprintf(fp, - "This little daemon publishes xen domain info via mDNS.\n" + "This little daemon publishes xen domain info,\n" + "via mDNS (using avahi), as service '_xendom._tcp'.\n" "\n" - "usage: mdns-publish-xendom [options]\n" + "usage: %s [options]\n" "options:\n" " -h print this text\n" " -d enable debug mode\n" "\n" "-- \n" - "(c) 2006 Gerd Hoffmann <kraxel@suse.de>\n"); + "(c) 2006 Gerd Hoffmann <kraxel@suse.de>\n", + appname); } int main(int argc, char*argv[]) @@ -247,22 +259,22 @@ int main(int argc, char*argv[]) } /* open syslog */ - openlog("mdns-publish-xendom", 0, LOG_LOCAL0); + openlog(appname, 0, LOG_LOCAL0); have_syslog = 1; /* figure name */ uname(&uts); - snprintf(buf, sizeof(buf), "Xen domain %s", uts.nodename); + snprintf(buf, sizeof(buf), "xendom %s", uts.nodename); name = avahi_strdup(buf); /* figure domain info */ if (0 != update_xendom_info()) - exit(1); + goto fail; /* prepare nDNS bits */ simple_poll = avahi_simple_poll_new(); if (!simple_poll) { - log_printf(LOG_ERR, "Failed to create simple poll object.\n"); + log_printf(LOG_ERR, "failed to create simple poll object\n"); goto fail; } @@ -270,7 +282,7 @@ int main(int argc, char*argv[]) AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error); if (!client) { - log_printf(LOG_ERR, "Failed to create client: %s\n", avahi_strerror(error)); + log_printf(LOG_ERR, "failed to create client: %s\n", avahi_strerror(error)); goto fail; } @@ -279,14 +291,16 @@ int main(int argc, char*argv[]) switch (fork()) { case -1: log_printf(LOG_ERR, "fork: %s", strerror(errno)); - exit(1); + goto fail; case 0: + /* child */ close(0); close(1); close(2); setsid(); open("/dev/null", O_RDWR); dup(0); dup(0); have_tty = 0; break; default: + /* parent */ exit(0); } } @@ -296,6 +310,8 @@ int main(int argc, char*argv[]) ret = 0; fail: + log_printf(ret ? LOG_ERR : LOG_INFO, "exiting (%d)\n", ret); + /* Cleanup things */ if (client) avahi_client_free(client); |