.PHONY: all
-CFLAGS=-Wall -Wextra -Werror -pedantic
+CFLAGS=-Wall -Wextra -Werror
DEBUGF=-O0 -v -g
SRCDIR=src
INTFDIR=$(SRCDIR)/interfaces
+SANITY=-Wno-unused-variable -Wno-unused-parameter
GLIBF=`pkg-config --cflags --libs glib-2.0 gobject-2.0 gio-2.0 gio-unix-2.0`
-hostnamed:
- gcc -o bin/out.bin $(DEBUGF) $(GLIBF) $(SRCDIR)/main.c
+main:
+ gcc -o bin/out.bin $(DEBUGF) $(GLIBF) $(SANITY) $(SRCDIR)/main.c
+
+main-publish:
+ gcc -o bin/out.bin $(CFLAGS) $(GLIBF) $(SANITY) $(SRCDIR)/main.c
}
/* initial load/check */
-
gboolean config_init() {
- static gchar *config_path;
- int tryopen = 0;
- /* config is already good to go */
if(config)
+ return TRUE; //already init'd
+
+ config = g_key_file_new();
+
+ const gchar *config_path;
+ GStatBuf *config_lstat;
+
+ config_path = "/etc/systemd_compat.conf";
+
+ /* does conf exist? */
+ if(g_lstat(config_path, config_lstat)) {
+
+ /* if not, can we write it */
+ if(g_access("/etc/", W_OK)) {
+ g_printf("%s\n", "no write permissions for /etc/! exiting..");
+ return FALSE;
+ }
+
+ int config_descr;
+ config_descr = g_open(config_path, O_CREAT, 644);
+
+ gchar *posix_hostname;
+ posix_hostname = g_malloc(255);
+
+ gethostname(posix_hostname, 255);
+
+ g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname);
+ g_key_file_set_string(config, "hostnamed", "PrettyHostname", "");
+ g_key_file_set_string(config, "hostnamed", "IconName", "Computer");
+ g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
+
+ if(!g_key_file_save_to_file(config, config_path, NULL)) {
+ g_printf("failed to write config to %s!\n", config_path);
+ g_free(posix_hostname);
+ return FALSE;
+ }
+
+ g_printf("wrote config to %s\n", config_path);
+
+ g_free(posix_hostname);
+
return TRUE;
- /* does config file exist? if not, write one */
- else if(!g_key_file_load_from_data(config, "systemd_compat.conf", &config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) {
-
- tryopen = g_open("/etc/systemd_compat.conf", O_CREAT, 644);
-
- //TODO clean this up, use g_data_dirs and g_exit
- /* can we open it rw? */
- if(!g_access("/etc/systemd_compat.conf", W_OK) && !tryopen) {
- g_printf("%s\n", "ERROR: cannot open systemd_compat.conf as read/write!");
- return FALSE;
- }
-
- if(tryopen) {
- config_path = "/etc/systemd_compat.conf";
- g_close(tryopen, NULL);
- }
-
- //TODO set these properly
- config = g_key_file_new();
-
- g_key_file_set_string(config, "hostnamed", "PrettyHostname", "");
- g_key_file_set_string(config, "hostnamed", "IconName", "Computer");
- g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop");
-
- if(!g_key_file_save_to_file(config, config_path, NULL))
- return FALSE;
+ /* it does exist, read it */
+ } else {
+ if(!g_access(config_path, W_OK)) {
+ g_printf("%s\n", "no write permissions for /etc/! exiting..");
+ return FALSE;
+ } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL))
return TRUE;
- /* it does it exist and was written to config var */
- } else
- return TRUE;
+ g_printf("could not read config at %s! exiting..", config_path);
+ return FALSE;
+ }
}
g_print("got bus, name: %s\n", name);
- /*g_dbus_connection_register_object(conn,
- "/org/freedesktop/hostname1",
- spect_data->interfaces[0],
- &interface_vtable,
- NULL, NULL, NULL);*/
}
static void on_name_acquired(GDBusConnection *conn,
}
/* safe call to try and start hostnamed */
-GError * hostnamed_init() {
+GError *hostnamed_init() {
guint bus_descriptor;
GError *err = NULL;
gchar *hostnamed_joined_xml;
hostnamed_freeable = g_ptr_array_new();
+ hostnamed_ispect_xml = g_malloc(3000);
- g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, GUINT_TO_POINTER(3000), &err);
+ g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
NULL,
NULL);
- //TODO: malloc and return reference as if a main() closed
+ //TODO: malloc and return reference as if a main() closed
return err;
}
-//POSIX, for future ports try_hostname should be checked for null-termination
-/*
-gboolean init_hostname() {
-
- gchar try_hostname[HOST_NAME_MAX];
-
- if(!gethostname(try_hostname, HOST_NAME_MAX)) {
- hostname = try_hostname;
- return TRUE;
- }
-
- return FALSE;
-}*/
-
/* free()'s */
void hostnamed_mem_clean() {
//TODO vm check
#if defined(__i386__) || defined(__x86_64__)
- /*
+
Taken with a few minor changes from systemd's hostnamed.c,
copyright 2011 Lennart Poettering.
details about the values listed here:
http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
- */ /*
+
if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
switch (g_ascii_strtoull (filebuf, NULL, 10)) {
/* end debugging */
#include <gio/gio.h>
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <glib/gstdio.h>
#include "config.c"
#include "interfaces/hostnamed/hostnamed.c"
-//#include "main.h"
+//#include "main/h"
-#ifdef INSTALL
-static gboolean install_conf() {
-
-}
-#endif
-
-int main() {
- //TODO cleanup
+gboolean systemd_utils_init() {
#ifdef INSTALL
if(!config_init()) {
g_printf("%s\n", "FAILED to install configs in /etc/!");
- return 1;
+ return FALSE;
}
#endif
+ return TRUE;
+}
- //TODO cleanup
- #if (defined NO_BUILTIN_XML && defined INSTALL)
- if(!config_init()) {
- g_printf("%s\n", "FAILED to install xml configs!");
- return 1;
- }
- #else
- #endif
+int main() {
- GMainLoop *mloop = NULL;
+ GMainLoop *mloop;
- mloop = g_main_loop_new(NULL, FALSE);
+ if(!systemd_utils_init()) {
+ g_printf("failed to init, are you root?\n");
+ return 1; //TODO errno properly. grep for all "return 1;"s, not TODO'ing each one
+ }
+
hostnamed_init();
+
+ mloop = g_main_loop_new(NULL, TRUE);
+
g_main_loop_run(mloop);
+ g_main_loop_unref(mloop);
return 0;
}
-
#include <gio/gio.h>
+//TODO change this to config dir, not data dir
gboolean install_conf() {
gchar *our_conf_uri = "systemd-utl/xml-conf/";
gchar **data_dir = g_get_system_data_dirs();