36575bff |
1 | #include <unistd.h> |
2 | #include <fcntl.h> |
3 | #include <gio/gio.h> |
4 | |
5 | static GKeyFile *config; |
d099276b |
6 | static int config_descr; |
36575bff |
7 | |
5b435dd1 |
8 | static gchar *data_dir; |
9 | |
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; |
14 | |
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; |
20 | |
36575bff |
21 | static const gchar *CONFIG_KEYS[] = { |
22 | "PrettyHostname", |
23 | "IconName", |
24 | "ChassisType" |
25 | }; |
26 | |
27 | /* NULL if key doesn't exist */ |
28 | gchar *get_option(gchar *key, gchar *group) { |
29 | |
30 | if(!group) |
31 | group = "default"; |
32 | |
33 | return g_key_file_get_string(config, group, key, NULL); |
34 | } |
35 | |
36 | /* false if key isn't already defined or value is invalid */ |
37 | gboolean set_option(gchar *key, gchar *value, gchar *group) { |
38 | |
39 | if(!group) |
40 | group = "default"; |
41 | |
42 | if(!g_key_file_get_string(config, group, key, NULL)) |
43 | return FALSE; |
44 | |
45 | //TODO safteycheck value |
46 | g_key_file_set_string(config, group, key, value); |
47 | return TRUE; |
48 | } |
49 | |
50 | /* initial load/check */ |
36575bff |
51 | gboolean config_init() { |
52 | |
36575bff |
53 | if(config) |
39df6847 |
54 | return TRUE; //already init'd |
55 | |
56 | config = g_key_file_new(); |
57 | |
58 | const gchar *config_path; |
59 | GStatBuf *config_lstat; |
60 | |
61 | config_path = "/etc/systemd_compat.conf"; |
62 | |
63 | /* does conf exist? */ |
64 | if(g_lstat(config_path, config_lstat)) { |
65 | |
5b435dd1 |
66 | /* if not, can we write it? */ |
39df6847 |
67 | if(g_access("/etc/", W_OK)) { |
68 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
69 | return FALSE; |
70 | } |
71 | |
39df6847 |
72 | config_descr = g_open(config_path, O_CREAT, 644); |
73 | |
74 | gchar *posix_hostname; |
75 | posix_hostname = g_malloc(255); |
76 | |
77 | gethostname(posix_hostname, 255); |
78 | |
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 |
83 | |
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); |
87 | return FALSE; |
88 | } |
89 | |
90 | g_printf("wrote config to %s\n", config_path); |
91 | |
92 | g_free(posix_hostname); |
93 | |
36575bff |
94 | return TRUE; |
95 | |
39df6847 |
96 | /* it does exist, read it */ |
97 | } else { |
36575bff |
98 | |
39df6847 |
99 | if(!g_access(config_path, W_OK)) { |
100 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
101 | return FALSE; |
d099276b |
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); |
36575bff |
104 | return TRUE; |
d099276b |
105 | } |
36575bff |
106 | |
39df6847 |
107 | g_printf("could not read config at %s! exiting..", config_path); |
108 | return FALSE; |
109 | } |
36575bff |
110 | } |
d099276b |
111 | |
5b435dd1 |
112 | gboolean init_xml() { |
d099276b |
113 | |
5b435dd1 |
114 | gchar **data_dir_prefix; |
115 | |
116 | data_dir_prefix = g_get_system_data_dirs(); |
117 | data_dir = g_strconcat(data_dir_prefix[0], "systemd_compat", NULL); |
118 | |
119 | GStatBuf *xml_lstat; |
120 | |
121 | /* does xml dir exist? */ |
122 | if(g_lstat(data_dir, xml_lstat)) { |
123 | |
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]); |
127 | return FALSE; |
128 | } |
129 | |
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); |
133 | return FALSE; |
134 | } |
135 | |
136 | set_xml_descriptors(); |
137 | //LEFTOFF |
138 | |
139 | gchar *posix_hostname; |
140 | posix_hostname = g_malloc(255); |
141 | |
142 | gethostname(posix_hostname, 255); |
143 | |
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 |
148 | |
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); |
152 | return FALSE; |
153 | } |
d099276b |
154 | |
5b435dd1 |
155 | g_printf("wrote config to %s\n", config_path); |
156 | |
157 | g_free(posix_hostname); |
158 | |
159 | return TRUE; |
160 | |
161 | /* it does exist, read it */ |
162 | } else { |
163 | |
164 | if(!g_access(config_path, W_OK)) { |
165 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
166 | return FALSE; |
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); |
169 | return TRUE; |
170 | } |
171 | |
172 | g_printf("could not read config at %s! exiting..", config_path); |
173 | return FALSE; |
174 | } |
175 | |
176 | |
177 | return TRUE; |
178 | } |
179 | |
180 | static void set_xml_descriptors() { |
181 | |
182 | } |
183 | |
184 | void clean_config() { |
d099276b |
185 | |
5b435dd1 |
186 | //TODO g_ptr_array all of this |
187 | g_free(config); |
188 | g_free(data_dir); |
189 | g_close(config_descr, NULL); |
d099276b |
190 | } |