5 static GKeyFile
*config
;
6 static int config_descr
;
8 static gchar
*data_dir
;
10 static int hostnamed_ispect_xml_descr
, hostnamed_dbus_xml_descr
;
11 static int localed_ispect_xml_descr
, localed_dbus_xml_descr
;
12 static int timedated_ispect_xml_descr
, timedated_dbus_xml_descr
;
13 static int logind_ispect_xml_descr
, logind_dbus_xml_descr
;
15 /*TODO depending on builtin xml flag, these should be matched to checksums */
16 static gchar
**hostnamed_ispect_xml
, hostnamed_dbus_xml
;
17 static gchar
**localed_ispect_xml
, localed_dbus_xml
;
18 static gchar
**timedated_ispect_xml
, timedated_dbus_xml
;
19 static gchar
**logind_ispect_xml
, logind_dbus_xml
;
21 static const gchar
*CONFIG_KEYS
[] = {
27 /* NULL if key doesn't exist */
28 gchar
*get_option(gchar
*key
, gchar
*group
) {
33 return g_key_file_get_string(config
, group
, key
, NULL
);
36 /* false if key isn't already defined or value is invalid */
37 gboolean
set_option(gchar
*key
, gchar
*value
, gchar
*group
) {
42 if(!g_key_file_get_string(config
, group
, key
, NULL
))
45 //TODO safteycheck value
46 g_key_file_set_string(config
, group
, key
, value
);
50 /* initial load/check */
51 gboolean
config_init() {
54 return TRUE
; //already init'd
56 config
= g_key_file_new();
58 const gchar
*config_path
;
59 GStatBuf
*config_lstat
;
61 config_path
= "/etc/systemd_compat.conf";
63 /* does conf exist? */
64 if(g_lstat(config_path
, config_lstat
)) {
66 /* if not, can we write it? */
67 if(g_access("/etc/", W_OK
)) {
68 g_printf("%s\n", "no write permissions for /etc/! exiting..");
72 config_descr
= g_open(config_path
, O_CREAT
, 644);
74 gchar
*posix_hostname
;
75 posix_hostname
= g_malloc(255);
77 gethostname(posix_hostname
, 255);
79 g_key_file_set_string(config
, "hostnamed", "Hostname", posix_hostname
);
80 g_key_file_set_string(config
, "hostnamed", "PrettyHostname", "");
81 g_key_file_set_string(config
, "hostnamed", "IconName", "Computer");
82 g_key_file_set_string(config
, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
84 if(!g_key_file_save_to_file(config
, config_path
, NULL
)) {
85 g_printf("failed to write config to %s!\n", config_path
);
86 g_free(posix_hostname
);
90 g_printf("wrote config to %s\n", config_path
);
92 g_free(posix_hostname
);
96 /* it does exist, read it */
99 if(!g_access(config_path
, W_OK
)) {
100 g_printf("%s\n", "no write permissions for /etc/! exiting..");
102 } else if(g_key_file_load_from_file(config
, config_path
, G_KEY_FILE_KEEP_COMMENTS
, NULL
)) {
103 config_descr
= g_open(config_path
, O_RDWR
, 644);
107 g_printf("could not read config at %s! exiting..", config_path
);
112 gboolean
init_xml() {
114 gchar
**data_dir_prefix
;
116 data_dir_prefix
= g_get_system_data_dirs();
117 data_dir
= g_strconcat(data_dir_prefix
[0], "systemd_compat", NULL
);
121 /* does xml dir exist? */
122 if(g_lstat(data_dir
, xml_lstat
)) {
124 /* if not, can we write it? */
125 if(g_access(data_dir_prefix
[0], W_OK
)) {
126 g_printf("no write permissions for %s! exiting...\n", data_dir_prefix
[0]);
130 g_printf("creating xml data directory %s...\n", data_dir
);
131 if(g_mkdir(data_dir
, 644) {
132 g_printf("failed to create dir %s...\n", data_dir
);
136 set_xml_descriptors();
139 gchar
*posix_hostname
;
140 posix_hostname
= g_malloc(255);
142 gethostname(posix_hostname
, 255);
144 g_key_file_set_string(config
, "hostnamed", "Hostname", posix_hostname
);
145 g_key_file_set_string(config
, "hostnamed", "PrettyHostname", "");
146 g_key_file_set_string(config
, "hostnamed", "IconName", "Computer");
147 g_key_file_set_string(config
, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
149 if(!g_key_file_save_to_file(config
, config_path
, NULL
)) {
150 g_printf("failed to write config to %s!\n", config_path
);
151 g_free(posix_hostname
);
155 g_printf("wrote config to %s\n", config_path
);
157 g_free(posix_hostname
);
161 /* it does exist, read it */
164 if(!g_access(config_path
, W_OK
)) {
165 g_printf("%s\n", "no write permissions for /etc/! exiting..");
167 } else if(g_key_file_load_from_file(config
, config_path
, G_KEY_FILE_KEEP_COMMENTS
, NULL
)) {
168 config_descr
= g_open(config_path
, O_RDWR
, 644);
172 g_printf("could not read config at %s! exiting..", config_path
);
180 static void set_xml_descriptors() {
184 void clean_config() {
186 //TODO g_ptr_array all of this
189 g_close(config_descr
, NULL
);