2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
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.
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.
21 static GKeyFile
*config
;
22 static int config_descr
;
24 static gchar
*data_dir
;
26 /*static int hostnamed_ispect_xml_descr, hostnamed_dbus_xml_descr;
27 static int localed_ispect_xml_descr, localed_dbus_xml_descr;
28 static int timedated_ispect_xml_descr, timedated_dbus_xml_descr;
29 static int logind_ispect_xml_descr, logind_dbus_xml_descr;
31 /*TODO depending on builtin xml flag, these should be matched to checksums */ /*
32 static gchar **hostnamed_ispect_xml, hostnamed_dbus_xml;
33 static gchar **localed_ispect_xml, localed_dbus_xml;
34 static gchar **timedated_ispect_xml, timedated_dbus_xml;
35 static gchar **logind_ispect_xml, logind_dbus_xml; */
37 static void set_xml_descriptors();
39 static const gchar
*CONFIG_KEYS
[] = {
45 /* NULL if key doesn't exist */
46 gchar
*get_option(gchar
*key
, gchar
*group
) {
51 return g_key_file_get_string(config
, group
, key
, NULL
);
54 /* false if key isn't already defined or value is invalid */
55 gboolean
set_option(gchar
*key
, gchar
*value
, gchar
*group
) {
60 if(!g_key_file_get_string(config
, group
, key
, NULL
))
63 //TODO safteycheck value
64 g_key_file_set_string(config
, group
, key
, value
);
68 /* initial load/check */
69 gboolean
config_init() {
72 return TRUE
; //already init'd
74 config
= g_key_file_new();
76 const gchar
*config_path
;
77 GStatBuf
*config_lstat
;
79 config_path
= "/etc/systemd_compat.conf";
81 /* does conf exist? */
82 if(g_lstat(config_path
, config_lstat
)) {
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..");
91 config_descr
= g_open(config_path
, O_CREAT
, 644);
93 gchar
*posix_hostname
;
94 posix_hostname
= g_malloc(HOST_NAME_MAX
);
96 gethostname(posix_hostname
, HOST_NAME_MAX
);
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
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
);
109 g_printf("wrote config to %s\n", config_path
);
111 g_free(posix_hostname
);
115 /* it does exist, read it */
118 if(g_access(config_path
, W_OK
)) {
119 g_printf("%s\n", "no write permissions for /etc/! exiting..");
121 } else if(g_key_file_load_from_file(config
, config_path
, G_KEY_FILE_KEEP_COMMENTS
, NULL
))
124 g_printf("could not read config at %s! exiting..", config_path
);
129 gboolean
init_xml() {
131 const gchar
* const *data_dir_prefix
;
133 data_dir_prefix
= g_get_system_data_dirs();
134 data_dir
= g_strconcat(data_dir_prefix
[0], "systemd_compat", NULL
);
138 /* does xml dir exist? */
139 if(g_lstat(data_dir
, xml_lstat
)) {
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]);
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
);
153 set_xml_descriptors();
155 return TRUE
; /* kill me! */
160 /* gchar *posix_hostname;
161 posix_hostname = g_malloc(255);
163 gethostname(posix_hostname, 255);
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
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);
176 g_printf("wrote config to %s\n", config_path);
178 g_free(posix_hostname);
182 /* it does exist, read it */ /*
185 if(!g_access(config_path, W_OK)) {
186 g_printf("%s\n", "no write permissions for /etc/! exiting..");
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);
193 g_printf("could not read config at %s! exiting..", config_path);
201 static void set_xml_descriptors() {
205 void clean_config() {
207 //TODO g_ptr_array all of this
210 g_close(config_descr
, NULL
);