aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkremlin <ian@kremlin.cc>2014-08-06 14:57:12 +0800
committerkremlin <ian@kremlin.cc>2014-08-06 14:57:12 +0800
commita2fffc07173bab908d10ac2d66f73d33f085c601 (patch)
treea96970cbed1ad95e18646493884fcf28a8e9fadb
parent90f544079ab170636e65ed7815317559f2a7924b (diff)
downloadsystembsd-a2fffc07173bab908d10ac2d66f73d33f085c601.tar
systembsd-a2fffc07173bab908d10ac2d66f73d33f085c601.tar.gz
systembsd-a2fffc07173bab908d10ac2d66f73d33f085c601.tar.bz2
systembsd-a2fffc07173bab908d10ac2d66f73d33f085c601.tar.lz
systembsd-a2fffc07173bab908d10ac2d66f73d33f085c601.tar.xz
systembsd-a2fffc07173bab908d10ac2d66f73d33f085c601.tar.zst
systembsd-a2fffc07173bab908d10ac2d66f73d33f085c601.zip
add get_[static/dynamic/pretty]hostname functionality
dynamic hostname (sometimes just hostname) is always whatever the gethostname(3) returns. static hostname is derived from prettyhostname, which attempts to convert it to a 7 bit ascii hostname (no bookend -'s, etc.) if this fails or prettyhostname has not yet been set, defaults to gethostname(3). prettyhostname is whatever is in /etc/systemd_compat.conf, if reading that fails it is just an empty string. prettyhostname is blank until manually set.
-rw-r--r--src/interfaces/hostnamed/hostnamed.c38
1 files changed, 33 insertions, 5 deletions
diff --git a/src/interfaces/hostnamed/hostnamed.c b/src/interfaces/hostnamed/hostnamed.c
index 6d83852..38d811c 100644
--- a/src/interfaces/hostnamed/hostnamed.c
+++ b/src/interfaces/hostnamed/hostnamed.c
@@ -89,13 +89,14 @@ our_get_hostname() {
gchar *hostname_buf, *ret;
size_t hostname_divider;
- hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); /* todo check & free */
+ hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
+
g_ptr_array_add(hostnamed_freeable, hostname_buf);
g_ptr_array_add(hostnamed_freeable, ret);
- if(gethostname(hostname_buf, MAXHOSTNAMELEN))
- return "";
+ if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0)
+ return "localhost";
hostname_divider = strcspn(hostname_buf, ".");
@@ -105,13 +106,40 @@ our_get_hostname() {
const gchar *
our_get_static_hostname() {
- return "TODO";
+ gchar *pretty_hostname;
+ gchar *ret;
+
+ pretty_hostname = our_get_pretty_hostname();
+
+ if(g_strcmp0(pretty_hostname, "") == 0)
+ ret = our_get_hostname();
+
+ else if(ret = g_hostname_to_ascii(pretty_hostname)) {
+
+ g_ptr_array_add(hostnamed_freeable, ret);
+ return ret;
+ }
+
+ return ret;
}
const gchar *
our_get_pretty_hostname() {
- return "TODO";
+ GKeyFile *config;
+ gchar *ret;
+
+ if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
+ && ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL)) { /* ret might need to be freed, docs dont specify but i am suspicious */
+
+ g_free(config);
+ return ret;
+ }
+
+ if(config)
+ g_free(config);
+
+ return "";
}
const gchar *