]> git.meshlink.io Git - catta/blobdiff - avahi-daemon/main.c
avahi-daemon: add option "host-name-from-machine-id="
[catta] / avahi-daemon / main.c
index 18e8518b5e43c103bb741357a5efcca019c408f5..8c28fd6ec8d786122d1f83cec0e4bb1a507412b5 100644 (file)
@@ -65,6 +65,7 @@
 #include <avahi-core/publish.h>
 #include <avahi-core/dns-srv-rr.h>
 #include <avahi-core/log.h>
+#include <avahi-core/util.h>
 
 #ifdef ENABLE_CHROOT
 #include "chroot.h"
@@ -576,6 +577,29 @@ static int parse_usec(const char *s, AvahiUsec *u) {
     return 0;
 }
 
+static char *get_machine_id(void) {
+    int fd;
+    char buf[32];
+
+    fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+    if (fd == -1 && errno == ENOENT)
+        fd = open("/var/lib/dbus/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+    if (fd == -1)
+        return NULL;
+
+    /* File is on a filesystem so we never get EINTR or partial reads */
+    if (read(fd, buf, sizeof buf) != sizeof buf) {
+        close(fd);
+        return NULL;
+    }
+    close(fd);
+
+    /* Contents can be lower, upper and even mixed case so normalize */
+    avahi_strdown(buf);
+
+    return avahi_strndup(buf, sizeof buf);
+}
+
 static int load_config_file(DaemonConfig *c) {
     int r = -1;
     AvahiIniFile *f;
@@ -631,6 +655,15 @@ static int load_config_file(DaemonConfig *c) {
                     c->server_config.use_iff_running = is_yes(p->value);
                 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
                     c->server_config.disallow_other_stacks = is_yes(p->value);
+                else if (strcasecmp(p->key, "host-name-from-machine-id") == 0) {
+                    if (*(p->value) == 'y' || *(p->value) == 'Y') {
+                        char *machine_id = get_machine_id();
+                        if (machine_id != NULL) {
+                            avahi_free(c->server_config.host_name);
+                            c->server_config.host_name = machine_id;
+                        }
+                    }
+                }
 #ifdef HAVE_DBUS
                 else if (strcasecmp(p->key, "enable-dbus") == 0) {
 
@@ -1046,12 +1079,10 @@ static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GC
 
     switch (sig) {
         case SIGINT:
-        case SIGQUIT:
         case SIGTERM:
             avahi_log_info(
                     "Got %s, quitting.",
-                    sig == SIGINT ? "SIGINT" :
-                    (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
+                    sig == SIGINT ? "SIGINT" : "SIGTERM");
             avahi_simple_poll_quit(simple_poll_api);
             break;
 
@@ -1113,7 +1144,7 @@ static int run_server(DaemonConfig *c) {
 
     poll_api = avahi_simple_poll_get(simple_poll_api);
 
-    if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
+    if (daemon_signal_init(SIGINT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
         avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
         goto finish;
     }
@@ -1587,6 +1618,7 @@ int main(int argc, char *argv[]) {
                 avahi_log_warn("Failed to close all remaining file descriptors: %s", strerror(errno));
 
         daemon_reset_sigs(-1);
+        daemon_unblock_sigs(-1);
 
         if (make_runtime_dir() < 0)
             goto finish;
@@ -1638,7 +1670,7 @@ int main(int argc, char *argv[]) {
 
         avahi_log_info("%s "PACKAGE_VERSION" exiting.", argv0);
         sd_notifyf(0, "STATUS=%s "PACKAGE_VERSION" exiting.", argv0);
-}
+    }
 
 finish: