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 const gchar
*CONFIG_KEYS
[] = {
43 /* NULL if key doesn't exist */
44 gchar
*get_option(gchar
*key
, gchar
*group
) {
49 return g_key_file_get_string(config
, group
, key
, NULL
);
52 /* false if key isn't already defined or value is invalid */
53 gboolean
set_option(gchar
*key
, gchar
*value
, gchar
*group
) {
58 if(!g_key_file_get_string(config
, group
, key
, NULL
))
61 //TODO safteycheck value
62 g_key_file_set_string(config
, group
, key
, value
);
66 /* initial load/check */
67 gboolean
config_init() {
70 return TRUE
; //already init'd
72 config
= g_key_file_new();
74 const gchar
*config_path
;
75 GStatBuf
*config_lstat
;
77 config_path
= "/etc/systemd_compat.conf";
79 /* does conf exist? */
80 if(g_lstat(config_path
, config_lstat
)) {
82 /* if not, can we write it */
83 if(g_access("/etc/", W_OK
)) {
84 g_printf("%s\n", "no write permissions for /etc/! exiting..");
89 config_descr
= g_open(config_path
, O_CREAT
, 644);
91 gchar
*posix_hostname
;
92 posix_hostname
= g_malloc(HOST_NAME_MAX
);
94 gethostname(posix_hostname
, HOST_NAME_MAX
);
96 g_key_file_set_string(config
, "hostnamed", "Hostname", posix_hostname
);
97 g_key_file_set_string(config
, "hostnamed", "PrettyHostname", "");
98 g_key_file_set_string(config
, "hostnamed", "IconName", "Computer");
99 g_key_file_set_string(config
, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
101 if(!g_key_file_save_to_file(config
, config_path
, NULL
)) {
102 g_printf("failed to write config to %s!\n", config_path
);
103 g_free(posix_hostname
);
107 g_printf("wrote config to %s\n", config_path
);
109 g_free(posix_hostname
);
113 /* it does exist, read it */
116 if(!g_access(config_path
, W_OK
)) {
117 g_printf("%s\n", "no write permissions for /etc/! exiting..");
119 } else if(g_key_file_load_from_file(config
, config_path
, G_KEY_FILE_KEEP_COMMENTS
, NULL
))
122 g_printf("could not read config at %s! exiting..", config_path
);