| 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <gio/gio.h> |
| 4 | |
| 5 | static GKeyFile *config; |
| 6 | static int config_descr; |
| 7 | |
| 8 | static const gchar *CONFIG_KEYS[] = { |
| 9 | "PrettyHostname", |
| 10 | "IconName", |
| 11 | "ChassisType" |
| 12 | }; |
| 13 | |
| 14 | /* NULL if key doesn't exist */ |
| 15 | gchar *get_option(gchar *key, gchar *group) { |
| 16 | |
| 17 | if(!group) |
| 18 | group = "default"; |
| 19 | |
| 20 | return g_key_file_get_string(config, group, key, NULL); |
| 21 | } |
| 22 | |
| 23 | /* false if key isn't already defined or value is invalid */ |
| 24 | gboolean set_option(gchar *key, gchar *value, gchar *group) { |
| 25 | |
| 26 | if(!group) |
| 27 | group = "default"; |
| 28 | |
| 29 | if(!g_key_file_get_string(config, group, key, NULL)) |
| 30 | return FALSE; |
| 31 | |
| 32 | //TODO safteycheck value |
| 33 | g_key_file_set_string(config, group, key, value); |
| 34 | return TRUE; |
| 35 | } |
| 36 | |
| 37 | /* initial load/check */ |
| 38 | gboolean config_init() { |
| 39 | |
| 40 | if(config) |
| 41 | return TRUE; //already init'd |
| 42 | |
| 43 | config = g_key_file_new(); |
| 44 | |
| 45 | const gchar *config_path; |
| 46 | GStatBuf *config_lstat; |
| 47 | |
| 48 | config_path = "/etc/systemd_compat.conf"; |
| 49 | |
| 50 | /* does conf exist? */ |
| 51 | if(g_lstat(config_path, config_lstat)) { |
| 52 | |
| 53 | /* if not, can we write it */ |
| 54 | if(g_access("/etc/", W_OK)) { |
| 55 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
| 56 | return FALSE; |
| 57 | } |
| 58 | |
| 59 | config_descr = g_open(config_path, O_CREAT, 644); |
| 60 | |
| 61 | gchar *posix_hostname; |
| 62 | posix_hostname = g_malloc(255); |
| 63 | |
| 64 | gethostname(posix_hostname, 255); |
| 65 | |
| 66 | g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname); |
| 67 | g_key_file_set_string(config, "hostnamed", "PrettyHostname", ""); |
| 68 | g_key_file_set_string(config, "hostnamed", "IconName", "Computer"); |
| 69 | g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly |
| 70 | |
| 71 | if(!g_key_file_save_to_file(config, config_path, NULL)) { |
| 72 | g_printf("failed to write config to %s!\n", config_path); |
| 73 | g_free(posix_hostname); |
| 74 | return FALSE; |
| 75 | } |
| 76 | |
| 77 | g_printf("wrote config to %s\n", config_path); |
| 78 | |
| 79 | g_free(posix_hostname); |
| 80 | |
| 81 | return TRUE; |
| 82 | |
| 83 | /* it does exist, read it */ |
| 84 | } else { |
| 85 | |
| 86 | if(!g_access(config_path, W_OK)) { |
| 87 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
| 88 | return FALSE; |
| 89 | } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) { |
| 90 | config_descr = g_open(config_path, O_RDWR, 644); |
| 91 | return TRUE; |
| 92 | } |
| 93 | |
| 94 | g_printf("could not read config at %s! exiting..", config_path); |
| 95 | return FALSE; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void clean_config() { |
| 100 | |
| 101 | if(config) |
| 102 | g_free(config); |
| 103 | |
| 104 | if(config_descr) |
| 105 | g_close(config_descr, NULL); |
| 106 | |
| 107 | } |