see previous commit, forgot 'git add'..
[systembsd.git] / src / config.c
CommitLineData
baa40e28 1/*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
1c38000a 17#include <unistd.h>
18#include <fcntl.h>
19#include <gio/gio.h>
20
21static GKeyFile *config;
7d10ea85 22static int config_descr;
1c38000a 23
4241079f 24static gchar *data_dir;
25
94304e78 26/*static int hostnamed_ispect_xml_descr, hostnamed_dbus_xml_descr;
4241079f 27static int localed_ispect_xml_descr, localed_dbus_xml_descr;
28static int timedated_ispect_xml_descr, timedated_dbus_xml_descr;
29static int logind_ispect_xml_descr, logind_dbus_xml_descr;
30
94304e78 31/*TODO depending on builtin xml flag, these should be matched to checksums */ /*
4241079f 32static gchar **hostnamed_ispect_xml, hostnamed_dbus_xml;
33static gchar **localed_ispect_xml, localed_dbus_xml;
34static gchar **timedated_ispect_xml, timedated_dbus_xml;
94304e78 35static gchar **logind_ispect_xml, logind_dbus_xml; */
4241079f 36
53f83ba5 37static void set_xml_descriptors();
38
1c38000a 39static const gchar *CONFIG_KEYS[] = {
fbc0295f 40 "PrettyHostname",
41 "IconName",
42 "ChassisType"
1c38000a 43};
44
45/* NULL if key doesn't exist */
46gchar *get_option(gchar *key, gchar *group) {
47
fbc0295f 48 if(!group)
49 group = "default";
1c38000a 50
fbc0295f 51 return g_key_file_get_string(config, group, key, NULL);
1c38000a 52}
53
54/* false if key isn't already defined or value is invalid */
55gboolean set_option(gchar *key, gchar *value, gchar *group) {
56
fbc0295f 57 if(!group)
58 group = "default";
1c38000a 59
fbc0295f 60 if(!g_key_file_get_string(config, group, key, NULL))
61 return FALSE;
1c38000a 62
fbc0295f 63 //TODO safteycheck value
64 g_key_file_set_string(config, group, key, value);
65 return TRUE;
1c38000a 66}
67
68/* initial load/check */
1c38000a 69gboolean config_init() {
70
fbc0295f 71 if(config)
72 return TRUE; //already init'd
c3b84b0a 73
fbc0295f 74 config = g_key_file_new();
75
76 const gchar *config_path;
77 GStatBuf *config_lstat;
c3b84b0a 78
fbc0295f 79 config_path = "/etc/systemd_compat.conf";
c3b84b0a 80
fbc0295f 81 /* does conf exist? */
82 if(g_lstat(config_path, config_lstat)) {
c3b84b0a 83
fbc0295f 84 /* if not, can we write it */
85 if(g_access("/etc/", W_OK)) {
86 g_printf("%s\n", "no write permissions for /etc/! exiting..");
87 return FALSE;
88 }
c3b84b0a 89
fbc0295f 90 int config_descr;
91 config_descr = g_open(config_path, O_CREAT, 644);
c3b84b0a 92
fbc0295f 93 gchar *posix_hostname;
94 posix_hostname = g_malloc(HOST_NAME_MAX);
c3b84b0a 95
fbc0295f 96 gethostname(posix_hostname, HOST_NAME_MAX);
c3b84b0a 97
fbc0295f 98 g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname);
99 g_key_file_set_string(config, "hostnamed", "PrettyHostname", "");
100 g_key_file_set_string(config, "hostnamed", "IconName", "Computer");
101 g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
c3b84b0a 102
fbc0295f 103 if(!g_key_file_save_to_file(config, config_path, NULL)) {
104 g_printf("failed to write config to %s!\n", config_path);
105 g_free(posix_hostname);
106 return FALSE;
107 }
c3b84b0a 108
fbc0295f 109 g_printf("wrote config to %s\n", config_path);
c3b84b0a 110
fbc0295f 111 g_free(posix_hostname);
c3b84b0a 112
fbc0295f 113 return TRUE;
1c38000a 114
fbc0295f 115 /* it does exist, read it */
116 } else {
1c38000a 117
fbc0295f 118 if(!g_access(config_path, W_OK)) {
119 g_printf("%s\n", "no write permissions for /etc/! exiting..");
120 return FALSE;
121 } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL))
122 return TRUE;
1c38000a 123
fbc0295f 124 g_printf("could not read config at %s! exiting..", config_path);
125 return FALSE;
126 }
e3756876 127}
94304e78 128
129gboolean init_xml() {
130
131 const gchar * const *data_dir_prefix;
132
133 data_dir_prefix = g_get_system_data_dirs();
134 data_dir = g_strconcat(data_dir_prefix[0], "systemd_compat", NULL);
135
136 GStatBuf *xml_lstat;
137
138 /* does xml dir exist? */
139 if(g_lstat(data_dir, xml_lstat)) {
140
141 /* if not, can we write it? */
142 if(g_access(data_dir_prefix[0], W_OK)) {
143 g_printf("no write permissions for %s! exiting...\n", data_dir_prefix[0]);
144 return FALSE;
145 }
146
147 g_printf("creating xml data directory %s...\n", data_dir);
148 if(g_mkdir(data_dir, 644)) {
149 g_printf("failed to create dir %s...\n", data_dir);
150 return FALSE;
151 }
152
53f83ba5 153 set_xml_descriptors();
94304e78 154 }
53f83ba5 155 return TRUE; /* kill me! */
156} /* kill me too!*/
e3756876 157
94304e78 158//LEFTOFF
159
160/* gchar *posix_hostname;
161 posix_hostname = g_malloc(255);
162
163 gethostname(posix_hostname, 255);
164
165 g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname);
166 g_key_file_set_string(config, "hostnamed", "PrettyHostname", "");
167 g_key_file_set_string(config, "hostnamed", "IconName", "Computer");
168 g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
169
170 if(!g_key_file_save_to_file(config, config_path, NULL)) {
171 g_printf("failed to write config to %s!\n", config_path);
172 g_free(posix_hostname);
173 return FALSE;
174 }
175
176 g_printf("wrote config to %s\n", config_path);
177
178 g_free(posix_hostname);
179
180 return TRUE;
181
182 /* it does exist, read it */ /*
183 } else {
184
185 if(!g_access(config_path, W_OK)) {
186 g_printf("%s\n", "no write permissions for /etc/! exiting..");
187 return FALSE;
188 } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) {
189 config_descr = g_open(config_path, O_RDWR, 644);
190 return TRUE;
191 }
192
193 g_printf("could not read config at %s! exiting..", config_path);
194 return FALSE;
195 }
196
197
198 return TRUE;
199} */
200
e3756876 201static void set_xml_descriptors() {
94304e78 202
203}
204
205void clean_config() {
206
207 //TODO g_ptr_array all of this
208 g_free(config);
209 g_free(data_dir);
210 g_close(config_descr, NULL);
e3756876 211}