diff options
author | Lennart Poettering <lennart@poettering.net> | 2008-09-03 18:30:27 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2008-09-03 18:30:27 +0200 |
commit | 99d5ec6c93c14a2b5892d9342e20c145d7915ae4 (patch) | |
tree | c572fd28806ef597641ec31088edb29cfcf36c46 | |
parent | 5f931138d2445691e7e97d237e1db183d0dabfb7 (diff) |
Rework pa_machine_id() a bit
Guarantee this function never fails, use POSIX gethostid as last resort. Add
comments.
-rw-r--r-- | src/pulsecore/core-util.c | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index c78e8fd7e..ad00f4f43 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -2391,18 +2391,29 @@ char *pa_machine_id(void) { FILE *f; size_t l; + /* The returned value is supposed be some kind of ascii identifier + * that is unique and stable across reboots. */ + + /* First we try the D-Bus UUID, which is the best option we have, + * since it fits perfectly our needs and is not as volatile as the + * hostname which might be set from dhcp. */ + if ((f = fopen(PA_MACHINE_ID, "r"))) { char ln[34] = "", *r; r = fgets(ln, sizeof(ln)-1, f); fclose(f); - if (r) - return pa_xstrdup(pa_strip_nl(ln)); + pa_strip_nl(ln); + + if (ln[0]) + return pa_xstrdup(ln); } - l = 100; + /* The we fall back to the host name. It supposed to be somewhat + * unique, at least in a network, but may change. */ + l = 100; for (;;) { char *c; @@ -2410,17 +2421,18 @@ char *pa_machine_id(void) { if (!pa_get_host_name(c, l)) { - if (errno == EINVAL || errno == ENAMETOOLONG) { + if (errno != EINVAL && errno != ENAMETOOLONG) + break; + + } else if (strlen(c) < l-1) { + + if (*c == 0) { pa_xfree(c); - l *= 2; - continue; + break; } - return NULL; - } - - if (strlen(c) < l-1) return c; + } /* Hmm, the hostname is as long the space we offered the * function, we cannot know if it fully fit in, so let's play @@ -2429,4 +2441,9 @@ char *pa_machine_id(void) { pa_xfree(c); l *= 2; } + + /* If no hostname was set we use the POSIX hostid. It's usually + * the IPv4 address. Mit not be that stable. */ + return pa_sprintf_malloc("%08lx", (unsigned long) gethostid); + } |