finish migrating old code to work with gdbus-codegen types, cleaned up config IO...
authorkremlin <ian@kremlin.cc>
Fri, 20 Jun 2014 02:38:50 +0000 (21:38 -0500)
committerkremlin <ian@kremlin.cc>
Fri, 20 Jun 2014 02:38:50 +0000 (21:38 -0500)
Makefile
src/config.c
src/interfaces/hostnamed/hostnamed.c
src/main.c
src/main.h

index 2940959434d110375ddabef754aaf475c541a547..342c3bf57249b4b65772aee556d408a03395effd 100644 (file)
--- 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
index 189eb235f507788493b2e5bce8a2be97980c9078..5d8534614cc0bad884eb3146b89d2cb60330a205 100644 (file)
@@ -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;
+       }
 }
index 95539fb36ece9d9c84f4e1449226a1dc093606bc..e75476ea6b0c0aff5c336dd29c82e89cdd470368 100644 (file)
@@ -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)) {
index d9a6ef960884a633bce6a958265dd1129bdf3da9..82d68f8da7f1f5397d05ae468d631dc3214bc88f 100644 (file)
@@ -4,40 +4,38 @@
 /* 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;
 }
-
index c8f97819dee7e4811a1715d80b25f3fafc71908d..a5fd502aa5dd181248f8594eea995e887ed4bb44 100644 (file)
@@ -1,5 +1,6 @@
 #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();