From: kremlin- Date: Fri, 20 Jun 2014 02:38:50 +0000 (-0500) Subject: finish migrating old code to work with gdbus-codegen types, cleaned up config IO... X-Git-Tag: hostnamed-copyable~20 X-Git-Url: https://uglyman.kremlin.cc/gitweb/gitweb.cgi?a=commitdiff_plain;h=c3b84b0abed4c48ab8ff036081f43a523009c8a9;p=systembsd.git finish migrating old code to work with gdbus-codegen types, cleaned up config IO/install, cleaned up code all-around to work with strict compiler flags --- diff --git a/Makefile b/Makefile index 2940959..342c3bf 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,15 @@ .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 diff --git a/src/config.c b/src/config.c index 189eb23..5d85346 100644 --- a/src/config.c +++ b/src/config.c @@ -34,45 +34,62 @@ gboolean set_option(gchar *key, gchar *value, gchar *group) { } /* 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; + } } diff --git a/src/interfaces/hostnamed/hostnamed.c b/src/interfaces/hostnamed/hostnamed.c index 95539fb..e75476e 100644 --- a/src/interfaces/hostnamed/hostnamed.c +++ b/src/interfaces/hostnamed/hostnamed.c @@ -18,11 +18,6 @@ static void on_bus_acquired(GDBusConnection *conn, 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, @@ -44,7 +39,7 @@ static void on_name_lost(GDBusConnection *conn, } /* safe call to try and start hostnamed */ -GError * hostnamed_init() { +GError *hostnamed_init() { guint bus_descriptor; GError *err = NULL; @@ -52,8 +47,9 @@ GError * hostnamed_init() { 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); @@ -69,24 +65,10 @@ GError * hostnamed_init() { 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() { @@ -102,7 +84,7 @@ 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. @@ -110,7 +92,7 @@ void hostnamed_mem_clean() { 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)) { diff --git a/src/main.c b/src/main.c index d9a6ef9..82d68f8 100644 --- a/src/main.c +++ b/src/main.c @@ -4,40 +4,38 @@ /* end debugging */ #include +#include +#include +#include #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; } - diff --git a/src/main.h b/src/main.h index c8f9781..a5fd502 100644 --- a/src/main.h +++ b/src/main.h @@ -1,5 +1,6 @@ #include +//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();