diff options
author | Tom Gundersen <teg@jklm.no> | 2016-02-22 17:38:34 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2016-02-22 17:38:34 +0100 |
commit | 7c7c0cbe640e33e5f57b24d0281564724b3eaeeb (patch) | |
tree | 478c558c880ef1082eda62b6ee3208b29fe98832 /src/basic/hostname-util.c | |
parent | a4ae7f1d739cc011b5312a8bfba99971bce0e7c3 (diff) | |
parent | da6c766d539dc38dbabf401be31180ca4691215f (diff) |
Merge pull request #2685 from poettering/lldp-fixes2
lldp fixes, second iteration
Diffstat (limited to 'src/basic/hostname-util.c')
-rw-r--r-- | src/basic/hostname-util.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 57031b645c..f900c509a3 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -48,6 +48,9 @@ bool hostname_is_set(void) { char* gethostname_malloc(void) { struct utsname u; + /* This call tries to return something useful, either the actual hostname or it makes something up. The only + * reason it might mail is OOM. It might even return "localhost" if that's set. */ + assert_se(uname(&u) >= 0); if (isempty(u.nodename) || streq(u.nodename, "(none)")) @@ -56,6 +59,31 @@ char* gethostname_malloc(void) { return strdup(u.nodename); } +int gethostname_strict(char **ret) { + struct utsname u; + char *k; + + /* This call will rather fail than make up a name. It will not return "localhost" either. */ + + assert_se(uname(&u) >= 0); + + if (isempty(u.nodename)) + return -ENXIO; + + if (streq(u.nodename, "(none)")) + return -ENXIO; + + if (is_localhost(u.nodename)) + return -ENXIO; + + k = strdup(u.nodename); + if (!k) + return -ENOMEM; + + *ret = k; + return 0; +} + static bool hostname_valid_char(char c) { return (c >= 'a' && c <= 'z') || |