add localtime determination system for timezone property
[systembsd.git] / src / interfaces / timedated / timedated.c
CommitLineData
3b82e3c1 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
b6ad18ad 17#include <unistd.h>
18#include <limits.h>
c12c41f4 19#include <signal.h>
b6ad18ad 20
a3d2e50f 21#include <sys/types.h>
22#include <time.h>
b6ad18ad 23#include <string.h>
24
25#include <glib/gprintf.h>
8caf1f61 26#include <glib-unix.h>
1066bd36 27#include <glib/gstdio.h>
904d744d 28#include <polkit/polkit.h>
b6ad18ad 29
1e8c7c88 30#include "timedated-gen.h"
a33bcdfa 31#include "timedated.h"
1e8c7c88 32
ed4cf3c8 33#include "../../util.h"
483e90b7 34
b6ad18ad 35GPtrArray *timedated_freeable;
36Timedate1 *timedated_interf;
37
c12c41f4 38GMainLoop *timedated_loop;
39
40guint bus_descriptor;
41gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
42
03f12ac7 43const gchar *OS_LOCALTIME = "/etc/localtime"; /* current timezone file */
44const gchar *OS_TIMEZONE_PATH = "/usr/share/zoneinfo"; /* path to system timezone files */
45
46struct timezone_checksum_pair {
47
48 gchar *path;
49 gchar *sum;
50 gboolean posix;
51 gboolean right;
52};
53
54static struct timezone_checksum_pair tz_table[5000];
1066bd36 55
1ce41045 56/* --- begin method/property/dbus signal code --- */
b6ad18ad 57
28b86015 58static gboolean
59on_handle_set_time(Timedate1 *hn1_passed_interf,
60 GDBusMethodInvocation *invoc,
61 const gchar *greet,
62 gpointer data) {
b6ad18ad 63 return FALSE;
28b86015 64}
b6ad18ad 65
28b86015 66static gboolean
67on_handle_set_timezone(Timedate1 *hn1_passed_interf,
68 GDBusMethodInvocation *invoc,
69 const gchar *greet,
70 gpointer data) {
71 return FALSE;
72}
73
74static gboolean
75on_handle_set_local_rtc(Timedate1 *hn1_passed_interf,
76 GDBusMethodInvocation *invoc,
77 const gchar *greet,
78 gpointer data) {
79 return FALSE;
80}
81
82static gboolean
83on_handle_set_ntp(Timedate1 *hn1_passed_interf,
84 GDBusMethodInvocation *invoc,
85 const gchar *greet,
86 gpointer data) {
87 return FALSE;
88}
89
90const gchar *
91our_get_timezone() {
92
1066bd36 93 GStatBuf *stat_zoneinfo;
03f12ac7 94 gchar *find_cmd, *readlink_path, *ret, *argvp, *hash_to_match;
95 gint argcp;
96 GError *err;
97 struct timezone_checksum_pair tmp;
1066bd36 98
03f12ac7 99 find_cmd = (gchar *) g_malloc0(2048);
100 stat_zoneinfo = (GStatBuf *) g_malloc0(8192);
101 err = (GError *) g_malloc0(2048);
1066bd36 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);
03f12ac7 111
112 gchar *split[2] = { readlink_path, "" };
113 tmp = parse_timezone_path(split);
114
115 ret = tmp.path;
1066bd36 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);
03f12ac7 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);
1066bd36 129 }
130
131 return ret;
28b86015 132}
133
134gboolean
135our_get_local_rtc() {
136
137 gboolean ret = FALSE;
138
139 return ret;
140}
141
142gboolean
143our_get_can_ntp() {
144
145 const gboolean ret = FALSE;
146
147 return ret;
148}
149
150gboolean
151our_get_ntp() {
152
153 const gboolean ret = FALSE;
154
155 return ret;
156}
b6ad18ad 157
28b86015 158gboolean
159our_get_ntpsynchronized() {
160
161 const gboolean ret = FALSE;
b6ad18ad 162
28b86015 163 return ret;
164}
b6ad18ad 165
28b86015 166guint64
167our_get_time_usec() {
b6ad18ad 168
28b86015 169 guint64 ret = 0;
b6ad18ad 170
28b86015 171 return ret;
172}
173
174guint64
175our_get_rtc_time_usec() {
176
177 guint64 ret = 0;
178
179 return ret;
180}
b6ad18ad 181
1ce41045 182/* --- end method/property/dbus signal code, begin bus/name handlers --- */
b6ad18ad 183
184static void timedated_on_bus_acquired(GDBusConnection *conn,
185 const gchar *name,
186 gpointer user_data) {
187
254ceec0 188 g_printf("got bus/name, exporting %s's interface...\n", name);
b6ad18ad 189
190 timedated_interf = timedate1_skeleton_new();
191
28b86015 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);
03f12ac7 197
28b86015 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
b6ad18ad 207 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf),
208 conn,
209 "/org/freedesktop/timedate1",
210 NULL)) {
211
509599f0 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);
b6ad18ad 219 }
509599f0 220}
221
222static void timedated_on_name_acquired(GDBusConnection *conn,
90f54407 223 const gchar *name,
509599f0 224 gpointer user_data) {
b6ad18ad 225
509599f0 226 g_printf("success!\n");
b6ad18ad 227}
228
b6ad18ad 229static void timedated_on_name_lost(GDBusConnection *conn,
230 const gchar *name,
231 gpointer user_data) {
232
509599f0 233 if(!conn) {
b6ad18ad 234
509599f0 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 }
b6ad18ad 238
509599f0 239 g_print("lost name %s, exiting...\n", name);
240
241 timedated_mem_clean();
b6ad18ad 242}
243
b70beb08 244/* --- end bus/name handlers, begin misc unix functions --- */
245
509599f0 246/* safe call to clean and then exit
247 * this stops our GMainLoop safely before letting main() return */
b70beb08 248void timedated_mem_clean() {
249
341587db 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
b70beb08 258}
259
712eb329 260/* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
261gboolean 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
2ef2cfe1 269void 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
1e8c7c88 277int main() {
b6ad18ad 278
9728ae1f 279 set_signal_handlers();
280
03f12ac7 281 if(!build_lookup_table())
282 return 1;
283
90f54407 284 timedated_loop = g_main_loop_new(NULL, TRUE);
285 timedated_freeable = g_ptr_array_new();
b6ad18ad 286
90f54407 287 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
b6ad18ad 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
90f54407 296 g_main_loop_run(timedated_loop);
9728ae1f 297 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
90f54407 298 g_main_loop_unref(timedated_loop);
b6ad18ad 299
9728ae1f 300 /* guaranteed unownable */
90f54407 301 g_bus_unown_name(bus_descriptor);
b6ad18ad 302
9728ae1f 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);
b6ad18ad 305
90f54407 306 return 0;
1e8c7c88 307}
1066bd36 308
03f12ac7 309static struct timezone_checksum_pair parse_timezone_path(gchar **pair) {
1066bd36 310
03f12ac7 311 gchar *prefix_pattern, *right_prefix_pattern, *posix_prefix_pattern, *lean_path;
1066bd36 312 GRegex *prefix, *posix, *right;
313 GError *err = NULL;
03f12ac7 314 struct timezone_checksum_pair ret = { NULL, NULL, FALSE, FALSE };
1066bd36 315
03f12ac7 316 if(!pair[0])
317 return ret;
1066bd36 318
319 prefix_pattern = (gchar *) g_malloc0(4096);
03f12ac7 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);
1066bd36 326
327 prefix = g_regex_new(prefix_pattern, 0, 0, &err);
03f12ac7 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);
1066bd36 348
349 g_regex_unref(prefix);
350 g_regex_unref(right);
351 g_regex_unref(posix);
352
03f12ac7 353 return ret;
354}
355
356/* TODO need to deconstruct tz_table on exit */
357static 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
396static 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;
1066bd36 407}