+ if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
+ return 1;
+
+ hostnamed_loop = g_main_loop_new(NULL, TRUE);
+
+ bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+ "org.freedesktop.hostname1",
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ hostnamed_on_bus_acquired,
+ hostnamed_on_name_acquired,
+ hostnamed_on_name_lost,
+ NULL,
+ NULL);
+
+ g_main_loop_run(hostnamed_loop);
+ /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
+ g_main_loop_unref(hostnamed_loop);
+
+ /* guaranteed unownable */
+ g_bus_unown_name(bus_descriptor);
+
+ /* at this point no operations can occur with our data, it is safe to free it + its container */
+ g_ptr_array_free(hostnamed_freeable, TRUE);
+
+ return 0;
+}
+
+gboolean set_names() {
+
+ /* (1) set up */
+ gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf;
+ GKeyFile *config;
+ size_t hostname_divider;
+
+ hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
+ static_hostname_buf = (gchar*) g_malloc0(4096);
+ pretty_hostname_buf = (gchar*) g_malloc0(4096);
+
+ config = g_key_file_new();
+
+ g_ptr_array_add(hostnamed_freeable, hostname_buf);
+ g_ptr_array_add(hostnamed_freeable, static_hostname_buf);
+ g_ptr_array_add(hostnamed_freeable, pretty_hostname_buf);
+
+ /* (2) set HOSTNAME */
+ if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, ""))
+ HOSTNAME = "localhost";
+
+ HOSTNAME = hostname_buf;
+
+ /* this bit gets you the /etc/myname style hostname
+ hostname_divider = strcspn(hostname_buf, ".");
+ strncpy(ret, hostname_buf, hostname_divider); */
+
+ /* (3) set PRETTY_HOSTNAME */
+ if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
+ && (pretty_hostname_buf = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL)))
+ PRETTY_HOSTNAME = pretty_hostname_buf;
+ else
+ PRETTY_HOSTNAME = "";
+
+ if(config)
+ g_key_file_unref(config);
+
+ /* (4) set STATIC_HOSTNAME */
+ if(!g_strcmp0(PRETTY_HOSTNAME, ""))
+ STATIC_HOSTNAME = HOSTNAME;
+
+ else if((static_hostname_buf = g_hostname_to_ascii(PRETTY_HOSTNAME)))
+ STATIC_HOSTNAME = static_hostname_buf;
+
+ return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE;
+
+}
+
+gboolean set_uname_properties() {
+
+ struct utsname un;
+
+ if(-1 == uname(&un))
+ return FALSE;
+
+ KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname));
+ g_ptr_array_add(hostnamed_freeable, KERN_NAME);
+ g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname));
+
+ KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release));
+ g_ptr_array_add(hostnamed_freeable, KERN_RELEASE);
+ g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release));
+
+ KERN_VERS = (gchar*)g_malloc0(sizeof(un.version));
+ g_ptr_array_add(hostnamed_freeable, KERN_VERS);
+ g_strlcpy(KERN_VERS, un.version, sizeof(un.version));
+
+ return TRUE;
+}
+
+gboolean determine_chassis_and_icon() {
+
+ const size_t bufsize = 4096;
+
+ char *hwproduct, *hwmodel, *hwvendor, *hwmachine;
+ size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size;
+ int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2];
+ unsigned int i;
+ gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
+
+ hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
+ UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
+ i = 0;
+
+ hwproduct = (char*)g_malloc0(4096);
+ hwmodel = (char*)g_malloc0(4096);
+ hwvendor = (char*)g_malloc0(4096);
+ hwmachine = (char*)g_malloc0(4096);
+
+ g_ptr_array_add(hostnamed_freeable, hwproduct);
+ g_ptr_array_add(hostnamed_freeable, hwmodel);
+ g_ptr_array_add(hostnamed_freeable, hwvendor);
+ g_ptr_array_add(hostnamed_freeable, hwmachine);
+
+ hwproduct_name[0] = CTL_HW;
+ hwproduct_name[1] = HW_PRODUCT;
+
+ hwmodel_name[0] = CTL_HW;
+ hwmodel_name[1] = HW_MODEL;
+
+ hwvendor_name[0] = CTL_HW;
+ hwvendor_name[1] = HW_VENDOR;
+
+ hwmachine_name[0] = CTL_HW;
+ hwmachine_name[1] = HW_MACHINE;
+
+ /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
+ if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0))
+ return FALSE;
+
+ if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0))
+ return FALSE;
+
+ if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0))
+ return FALSE;