aboutsummaryrefslogtreecommitdiffstats
path: root/mdns-publish-xendom.c
diff options
context:
space:
mode:
authorkraxel <kraxel>2006-01-30 13:21:13 +0000
committerkraxel <kraxel>2006-01-30 13:21:13 +0000
commit0738deb0a0dcfb6256538b577e7d47d11cdfea3c (patch)
tree39730196856d8a072a9a6853b4fb1b6f10b474f7 /mdns-publish-xendom.c
parent2eb29a6f65cdc025fba80803b68a57f64091bbe6 (diff)
downloadxenwatch-0738deb0a0dcfb6256538b577e7d47d11cdfea3c.tar.gz
- add xenstuff to mdns bits.
Diffstat (limited to 'mdns-publish-xendom.c')
-rw-r--r--mdns-publish-xendom.c115
1 files changed, 86 insertions, 29 deletions
diff --git a/mdns-publish-xendom.c b/mdns-publish-xendom.c
index f2be81f..69eccb4 100644
--- a/mdns-publish-xendom.c
+++ b/mdns-publish-xendom.c
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
+#include <syslog.h>
+#include <string.h>
#include <sys/utsname.h>
#include <xs.h>
@@ -18,13 +20,38 @@
static AvahiEntryGroup *group = NULL;
static AvahiSimplePoll *simple_poll = NULL;
-static int debug = 0;
+static int debug = 0;
+static int have_tty = 1;
+static int have_syslog = 0;
+
static char *name;
-static char *service = "_xendom._tcp";
-static int port = 9;
+static char *service = "_xendom._tcp";
+static int port = 9;
static char vm_uuid[256];
static char dom_id[256];
+/* --------------------------------------------------------------------- */
+
+static int __attribute__ ((format (printf, 2, 0)))
+log_printf(int priority, char *fmt, ...)
+{
+ va_list args;
+ char msgbuf[1024];
+ int rc;
+
+ va_start(args, fmt);
+ rc = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
+ va_end(args);
+
+ if (have_tty)
+ fprintf(stderr, "%s", msgbuf);
+ if (have_syslog)
+ syslog(priority, "%s", msgbuf);
+ return rc;
+}
+
+/* --------------------------------------------------------------------- */
+
static void create_services(AvahiClient *c);
static void entry_group_callback(AvahiEntryGroup *g,
@@ -35,15 +62,14 @@ static void entry_group_callback(AvahiEntryGroup *g,
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED:
- if (debug)
- fprintf(stderr, "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",
+ name, n);
avahi_free(name);
name = n;
- 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 :
@@ -61,9 +87,8 @@ static void create_services(AvahiClient *c)
/* 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))) {
- if (debug)
- fprintf(stderr, "avahi_entry_group_new() failed: %s\n",
- avahi_strerror(avahi_client_errno(c)));
+ log_printf(LOG_ERR, "avahi_entry_group_new() failed: %s\n",
+ avahi_strerror(avahi_client_errno(c)));
goto fail;
}
}
@@ -73,15 +98,13 @@ static void create_services(AvahiClient *c)
name, service, NULL, NULL, port,
vm_uuid, dom_id, NULL);
if (ret < 0) {
- if (debug)
- fprintf(stderr, "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) {
- if (debug)
- fprintf(stderr, "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;
@@ -94,7 +117,12 @@ static void client_callback(AvahiClient *c,
AvahiClientState state,
void * userdata)
{
+ int error;
+
switch (state) {
+ case AVAHI_CLIENT_CONNECTING:
+ log_printf(LOG_NOTICE, "avahi not running (yet), I'll keep trying ...\n");
+ break;
case AVAHI_CLIENT_S_RUNNING:
if (!group)
create_services(c);
@@ -104,16 +132,39 @@ static void client_callback(AvahiClient *c,
avahi_entry_group_reset(group);
break;
case AVAHI_CLIENT_FAILURE:
- if (debug)
- fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
- avahi_simple_poll_quit(simple_poll);
+ switch (avahi_client_errno(c)) {
+ case AVAHI_ERR_DISCONNECTED:
+ avahi_entry_group_free(group);
+ avahi_client_free(c);
+ group = NULL;
+
+ log_printf(LOG_NOTICE, "disconnected from avahi, 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",
+ avahi_strerror(error));
+ goto fail;
+ }
+ break;
+ default:
+ log_printf(LOG_ERR, "Client failure: %s\n",
+ avahi_strerror(avahi_client_errno(c)));
+ goto fail;
+ break;
+ }
break;
default:
break;
}
+ return;
+
+ fail:
+ avahi_simple_poll_quit(simple_poll);
}
-static int xendom_info(void)
+static int update_xendom_info(void)
{
struct xs_handle *xenstore;
xs_transaction_t xst;
@@ -121,31 +172,31 @@ static int xendom_info(void)
xenstore = xs_domain_open();
if (NULL == xenstore) {
- fprintf(stderr,"%s: can't connect to %s\n", __FUNCTION__,
- xs_domain_dev());
+ log_printf(LOG_ERR, "%s: can't connect to %s\n", __FUNCTION__,
+ xs_domain_dev());
return -1;
}
if (!(xst = xs_transaction_start(xenstore))) {
- fprintf(stderr,"%s: can't start transaction\n", __FUNCTION__);
+ log_printf(LOG_ERR, "%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) {
- fprintf(stderr,"%s: can't read \"vm\" value\n", __FUNCTION__);
+ log_printf(LOG_ERR, "%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__);
+ log_printf(LOG_ERR, "%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__);
+ log_printf(LOG_ERR, "%s: can't read \"domid\" value\n", __FUNCTION__);
goto out;
}
snprintf(dom_id, sizeof(dom_id), "dom-id=%s", xs_value);
@@ -195,26 +246,31 @@ int main(int argc, char*argv[])
}
}
+ /* open syslog */
+ openlog("mdns-publish-xendom", 0, LOG_LOCAL0);
+ have_syslog = 1;
+
/* figure name */
uname(&uts);
snprintf(buf, sizeof(buf), "Xen domain %s", uts.nodename);
name = avahi_strdup(buf);
/* figure domain info */
- if (0 != xendom_info())
+ if (0 != update_xendom_info())
exit(1);
/* prepare nDNS bits */
simple_poll = avahi_simple_poll_new();
if (!simple_poll) {
- fprintf(stderr, "Failed to create simple poll object.\n");
+ log_printf(LOG_ERR, "Failed to create simple poll object.\n");
goto fail;
}
- client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0,
+ client = avahi_client_new(avahi_simple_poll_get(simple_poll),
+ AVAHI_CLIENT_NO_FAIL,
client_callback, NULL, &error);
if (!client) {
- fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
+ log_printf(LOG_ERR, "Failed to create client: %s\n", avahi_strerror(error));
goto fail;
}
@@ -222,12 +278,13 @@ int main(int argc, char*argv[])
if (!debug) {
switch (fork()) {
case -1:
- perror("fork");
+ log_printf(LOG_ERR, "fork: %s", strerror(errno));
exit(1);
case 0:
close(0); close(1); close(2);
setsid();
open("/dev/null", O_RDWR); dup(0); dup(0);
+ have_tty = 0;
break;
default:
exit(0);