X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fbasic%2Fhostname-util.c;h=5aebd89e34af753b414391616f6595cf79d17ea5;hb=6414babd8b4554e19f9ac7826cb7e82ef6b3435c;hp=3bd3a4d7b3634a4d4175600a6a639a828aed5265;hpb=bc983c987eb3f92eceb373ba4e1c2076b0b3ca88;p=elogind.git diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 3bd3a4d7b..5aebd89e3 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -17,9 +17,6 @@ along with systemd; If not, see . ***/ -#if defined(__GLIBC__) -# include -#endif // defined(__GLIBC__) #include #include #include @@ -48,10 +45,15 @@ bool hostname_is_set(void) { return true; } +#endif // 0 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 fail is OOM. + * It might even return "localhost" if that's set. */ + assert_se(uname(&u) >= 0); if (isempty(u.nodename) || streq(u.nodename, "(none)")) @@ -59,6 +61,32 @@ char* gethostname_malloc(void) { return strdup(u.nodename); } + +#if 0 /// UNNEEDED by elogind +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; +} #endif // 0 static bool hostname_valid_char(char c) { @@ -127,6 +155,8 @@ char* hostname_cleanup(char *s) { assert(s); + strshorten(s, HOST_NAME_MAX); + for (p = s, d = s, dot = true; *p; p++) { if (*p == '.') { if (dot) @@ -138,7 +168,6 @@ char* hostname_cleanup(char *s) { *(d++) = *p; dot = false; } - } if (dot && d > s) @@ -146,8 +175,6 @@ char* hostname_cleanup(char *s) { else *d = 0; - strshorten(s, HOST_NAME_MAX); - return s; } #endif // 0 @@ -156,16 +183,16 @@ bool is_localhost(const char *hostname) { assert(hostname); /* This tries to identify local host and domain names - * described in RFC6761 plus the redhatism of .localdomain */ + * described in RFC6761 plus the redhatism of localdomain */ return strcaseeq(hostname, "localhost") || strcaseeq(hostname, "localhost.") || - strcaseeq(hostname, "localdomain.") || - strcaseeq(hostname, "localdomain") || + strcaseeq(hostname, "localhost.localdomain") || + strcaseeq(hostname, "localhost.localdomain.") || endswith_no_case(hostname, ".localhost") || endswith_no_case(hostname, ".localhost.") || - endswith_no_case(hostname, ".localdomain") || - endswith_no_case(hostname, ".localdomain."); + endswith_no_case(hostname, ".localhost.localdomain") || + endswith_no_case(hostname, ".localhost.localdomain."); } #if 0 /// UNNEEDED by elogind