add localtime determination system for timezone property
[systembsd.git] / src / interfaces / timedated / timedated.c
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
17 #include <unistd.h>
18 #include <limits.h>
19 #include <signal.h>
20
21 #include <sys/types.h>
22 #include <time.h>
23 #include <string.h>
24
25 #include <glib/gprintf.h>
26 #include <glib-unix.h>
27 #include <glib/gstdio.h>
28 #include <polkit/polkit.h>
29
30 #include "timedated-gen.h"
31 #include "timedated.h"
32
33 #include "../../util.h"
34
35 GPtrArray *timedated_freeable;
36 Timedate1 *timedated_interf;
37
38 GMainLoop *timedated_loop;
39
40 guint bus_descriptor;
41 gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
42
43 const gchar *OS_LOCALTIME = "/etc/localtime"; /* current timezone file */
44 const gchar *OS_TIMEZONE_PATH = "/usr/share/zoneinfo"; /* path to system timezone files */
45
46 struct timezone_checksum_pair {
47
48 gchar *path;
49 gchar *sum;
50 gboolean posix;
51 gboolean right;
52 };
53
54 static struct timezone_checksum_pair tz_table[5000];
55
56 /* --- begin method/property/dbus signal code --- */
57
58 static gboolean
59 on_handle_set_time(Timedate1 *hn1_passed_interf,
60 GDBusMethodInvocation *invoc,
61 const gchar *greet,
62 gpointer data) {
63 return FALSE;
64 }
65
66 static gboolean
67 on_handle_set_timezone(Timedate1 *hn1_passed_interf,
68 GDBusMethodInvocation *invoc,
69 const gchar *greet,
70 gpointer data) {
71 return FALSE;
72 }
73
74 static gboolean
75 on_handle_set_local_rtc(Timedate1 *hn1_passed_interf,
76 GDBusMethodInvocation *invoc,
77 const gchar *greet,
78 gpointer data) {
79 return FALSE;
80 }
81
82 static gboolean
83 on_handle_set_ntp(Timedate1 *hn1_passed_interf,
84 GDBusMethodInvocation *invoc,
85 const gchar *greet,
86 gpointer data) {
87 return FALSE;
88 }
89
90 const gchar *
91 our_get_timezone() {
92
93 GStatBuf *stat_zoneinfo;
94 gchar *find_cmd, *readlink_path, *ret, *argvp, *hash_to_match;
95 gint argcp;
96 GError *err;
97 struct timezone_checksum_pair tmp;
98
99 find_cmd = (gchar *) g_malloc0(2048);
100 stat_zoneinfo = (GStatBuf *) g_malloc0(8192);
101 err = (GError *) g_malloc0(2048);
102
103 if(g_stat(OS_LOCALTIME, stat_zoneinfo)) {
104
105 g_printf("could not read from %s! please symlink or copy a timezone file from %s to %s!\n", OS_LOCALTIME, OS_TIMEZONE_PATH, OS_LOCALTIME);
106 ret = NULL;
107
108 } else if(g_file_test(OS_LOCALTIME, G_FILE_TEST_IS_SYMLINK)) {
109
110 readlink_path = g_file_read_link(OS_LOCALTIME, &err);
111
112 gchar *split[2] = { readlink_path, "" };
113 tmp = parse_timezone_path(split);
114
115 ret = tmp.path;
116
117 if(readlink_path)
118 g_free(readlink_path);
119
120 } else {
121
122 g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME, OS_TIMEZONE_PATH);
123 hash_to_match = get_file_sha256(OS_LOCALTIME);
124
125 ret = lookup_hash(hash_to_match);
126
127 if(hash_to_match)
128 g_free(hash_to_match);
129 }
130
131 return ret;
132 }
133
134 gboolean
135 our_get_local_rtc() {
136
137 gboolean ret = FALSE;
138
139 return ret;
140 }
141
142 gboolean
143 our_get_can_ntp() {
144
145 const gboolean ret = FALSE;
146
147 return ret;
148 }
149
150 gboolean
151 our_get_ntp() {
152
153 const gboolean ret = FALSE;
154
155 return ret;
156 }
157
158 gboolean
159 our_get_ntpsynchronized() {
160
161 const gboolean ret = FALSE;
162
163 return ret;
164 }
165
166 guint64
167 our_get_time_usec() {
168
169 guint64 ret = 0;
170
171 return ret;
172 }
173
174 guint64
175 our_get_rtc_time_usec() {
176
177 guint64 ret = 0;
178
179 return ret;
180 }
181
182 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
183
184 static void timedated_on_bus_acquired(GDBusConnection *conn,
185 const gchar *name,
186 gpointer user_data) {
187
188 g_printf("got bus/name, exporting %s's interface...\n", name);
189
190 timedated_interf = timedate1_skeleton_new();
191
192 /* attach function pointers to generated struct's method handlers */
193 g_signal_connect(timedated_interf, "handle-set-time", G_CALLBACK(on_handle_set_time), NULL);
194 g_signal_connect(timedated_interf, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone), NULL);
195 g_signal_connect(timedated_interf, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc), NULL);
196 g_signal_connect(timedated_interf, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp), NULL);
197
198 /* set our properties before export */
199 timedate1_set_timezone(timedated_interf, our_get_timezone());
200 timedate1_set_local_rtc(timedated_interf, our_get_local_rtc());
201 timedate1_set_can_ntp(timedated_interf, our_get_can_ntp());
202 timedate1_set_ntp(timedated_interf, our_get_ntp());
203 timedate1_set_ntpsynchronized(timedated_interf, our_get_ntpsynchronized());
204 timedate1_set_time_usec(timedated_interf, our_get_time_usec());
205 timedate1_set_rtctime_usec(timedated_interf, our_get_rtc_time_usec());
206
207 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf),
208 conn,
209 "/org/freedesktop/timedate1",
210 NULL)) {
211
212 g_printf("failed to export %s's interface!\n", name);
213 timedated_mem_clean();
214
215 } else {
216
217 dbus_interface_exported = TRUE;
218 g_printf("exported %s's interface on the system bus...\n", name);
219 }
220 }
221
222 static void timedated_on_name_acquired(GDBusConnection *conn,
223 const gchar *name,
224 gpointer user_data) {
225
226 g_printf("success!\n");
227 }
228
229 static void timedated_on_name_lost(GDBusConnection *conn,
230 const gchar *name,
231 gpointer user_data) {
232
233 if(!conn) {
234
235 g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name);
236 timedated_mem_clean();
237 }
238
239 g_print("lost name %s, exiting...\n", name);
240
241 timedated_mem_clean();
242 }
243
244 /* --- end bus/name handlers, begin misc unix functions --- */
245
246 /* safe call to clean and then exit
247 * this stops our GMainLoop safely before letting main() return */
248 void timedated_mem_clean() {
249
250 g_printf("exiting...\n");
251
252 if(dbus_interface_exported)
253 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf));
254
255 if(g_main_loop_is_running(timedated_loop))
256 g_main_loop_quit(timedated_loop);
257
258 }
259
260 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
261 gboolean unix_sig_terminate_handler(gpointer data) {
262
263 g_printf("caught SIGINT/HUP/TERM, exiting\n");
264
265 timedated_mem_clean();
266 return G_SOURCE_REMOVE;
267 }
268
269 void set_signal_handlers() {
270
271 /* we don't care about its descriptor, we never need to unregister these */
272 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
273 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
274 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
275 }
276
277 int main() {
278
279 set_signal_handlers();
280
281 if(!build_lookup_table())
282 return 1;
283
284 timedated_loop = g_main_loop_new(NULL, TRUE);
285 timedated_freeable = g_ptr_array_new();
286
287 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
288 "org.freedesktop.timedate1",
289 G_BUS_NAME_OWNER_FLAGS_NONE,
290 timedated_on_bus_acquired,
291 timedated_on_name_acquired,
292 timedated_on_name_lost,
293 NULL,
294 NULL);
295
296 g_main_loop_run(timedated_loop);
297 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
298 g_main_loop_unref(timedated_loop);
299
300 /* guaranteed unownable */
301 g_bus_unown_name(bus_descriptor);
302
303 /* at this point no operations can occur with our data, it is safe to free it + its container */
304 g_ptr_array_free(timedated_freeable, TRUE);
305
306 return 0;
307 }
308
309 static struct timezone_checksum_pair parse_timezone_path(gchar **pair) {
310
311 gchar *prefix_pattern, *right_prefix_pattern, *posix_prefix_pattern, *lean_path;
312 GRegex *prefix, *posix, *right;
313 GError *err = NULL;
314 struct timezone_checksum_pair ret = { NULL, NULL, FALSE, FALSE };
315
316 if(!pair[0])
317 return ret;
318
319 prefix_pattern = (gchar *) g_malloc0(4096);
320 right_prefix_pattern = (gchar *) g_malloc0(4096);
321 posix_prefix_pattern = (gchar *) g_malloc0(4096);
322
323 g_sprintf(prefix_pattern, "%s/", OS_TIMEZONE_PATH);
324 g_sprintf(posix_prefix_pattern, "%s/posix/", OS_TIMEZONE_PATH);
325 g_sprintf(right_prefix_pattern, "%s/right/", OS_TIMEZONE_PATH);
326
327 prefix = g_regex_new(prefix_pattern, 0, 0, &err);
328 posix = g_regex_new(posix_prefix_pattern, 0, 0, &err);
329 right = g_regex_new(right_prefix_pattern, 0, 0, &err);
330
331 if(g_regex_match_full(posix, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) {
332
333 ret.posix = TRUE;
334 lean_path = g_regex_replace_literal(posix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
335
336 } else if(g_regex_match_full(right, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) {
337
338 ret.right = TRUE;
339 lean_path = g_regex_replace_literal(right, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
340
341 } else
342 lean_path = g_regex_replace_literal(prefix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL);
343
344 ret.path = lean_path;
345
346 ret.sum = g_malloc0(256);
347 g_strlcpy(ret.sum, pair[1], 66);
348
349 g_regex_unref(prefix);
350 g_regex_unref(right);
351 g_regex_unref(posix);
352
353 return ret;
354 }
355
356 /* TODO need to deconstruct tz_table on exit */
357 static gboolean build_lookup_table() {
358
359 gchar *find_cmd, **map_pairs, *find_output, *path_buf, *sum_buf, **entry_buf;
360 GError *err;
361 gboolean ret;
362 gint i;
363
364 i = 0;
365 err = NULL;
366 ret = TRUE;
367
368 find_cmd = (gchar *) g_malloc0(4096);
369 find_output = (gchar *) g_malloc0(1000000);
370
371 g_sprintf(find_cmd, "/bin/sh -c \"find %s -type f -exec cksum -a sha256 {} \\; | sed -E 's/SHA256 \\(//g' | sed -E 's/\\) = /=/g'\"", OS_TIMEZONE_PATH);
372
373 if(!g_spawn_command_line_sync(find_cmd, &find_output, NULL, NULL, &err)) {
374
375 g_printf("error running `%s`\n", find_cmd);
376 ret = FALSE;
377 }
378
379 map_pairs = g_strsplit(find_output, "\n", INT_MAX);
380
381 while(map_pairs[i] && (entry_buf = g_strsplit(map_pairs[i], "=", INT_MAX))) {
382
383 tz_table[i] = parse_timezone_path(entry_buf);
384
385 g_strfreev(entry_buf);
386 i++;
387 }
388
389 g_free(find_output);
390 g_free(find_cmd);
391 g_free(map_pairs);
392
393 return ret;
394 }
395
396 static gchar *lookup_hash(gchar *hash) {
397
398 gint i = 0;
399
400 while(tz_table[i].sum)
401 if(!g_strcmp0(tz_table[i].sum, hash))
402 return tz_table[i].path;
403 else
404 i++;
405
406 return NULL;
407 }