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> |
a8f71719 |
22 | #include <sys/time.h> |
c8a3e18e |
23 | #include <sys/stat.h> |
24 | #include <errno.h> |
a3d2e50f |
25 | #include <time.h> |
b6ad18ad |
26 | #include <string.h> |
c8a3e18e |
27 | #include <tzfile.h> |
b6ad18ad |
28 | |
29 | #include <glib/gprintf.h> |
8caf1f61 |
30 | #include <glib-unix.h> |
1066bd36 |
31 | #include <glib/gstdio.h> |
904d744d |
32 | #include <polkit/polkit.h> |
b6ad18ad |
33 | |
1e8c7c88 |
34 | #include "timedated-gen.h" |
a33bcdfa |
35 | #include "timedated.h" |
1e8c7c88 |
36 | |
ed4cf3c8 |
37 | #include "../../util.h" |
483e90b7 |
38 | |
c8a3e18e |
39 | #define TZNAME_MAX PATH_MAX |
40 | |
b6ad18ad |
41 | GPtrArray *timedated_freeable; |
42 | Timedate1 *timedated_interf; |
43 | |
c12c41f4 |
44 | GMainLoop *timedated_loop; |
45 | |
46 | guint bus_descriptor; |
47 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
48 | |
03f12ac7 |
49 | const gchar *OS_LOCALTIME = "/etc/localtime"; /* current timezone file */ |
50 | const gchar *OS_TIMEZONE_PATH = "/usr/share/zoneinfo"; /* path to system timezone files */ |
51 | |
52 | struct timezone_checksum_pair { |
53 | |
54 | gchar *path; |
55 | gchar *sum; |
56 | gboolean posix; |
57 | gboolean right; |
58 | }; |
59 | |
60 | static struct timezone_checksum_pair tz_table[5000]; |
1066bd36 |
61 | |
1ce41045 |
62 | /* --- begin method/property/dbus signal code --- */ |
b6ad18ad |
63 | |
28b86015 |
64 | static gboolean |
a8f71719 |
65 | on_handle_set_time(Timedate1 *td1_passed_interf, |
28b86015 |
66 | GDBusMethodInvocation *invoc, |
67 | const gchar *greet, |
68 | gpointer data) { |
a8f71719 |
69 | |
70 | GVariant *params; |
71 | gint64 proposed_time, cur_time; |
72 | const gchar *bus_name; |
73 | gboolean policykit_auth; |
74 | check_auth_result is_authed; |
75 | gboolean relative; /* relative if passed time_t is meant to be added to current time */ |
644bae40 |
76 | struct timespec *new_time; |
a8f71719 |
77 | |
78 | params = g_dbus_method_invocation_get_parameters(invoc); |
79 | g_variant_get(params, "(xbb)", &proposed_time, &relative, &policykit_auth); |
80 | bus_name = g_dbus_method_invocation_get_sender(invoc); |
81 | |
82 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.timedate1.set-time", policykit_auth); |
83 | |
84 | switch(is_authed) { |
85 | |
86 | case AUTHORIZED_NATIVELY: |
87 | case AUTHORIZED_BY_PROMPT: |
88 | break; |
89 | |
90 | case UNAUTHORIZED_NATIVELY: |
91 | case UNAUTHORIZED_FAILED_PROMPT: |
92 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EACCES", "Insufficient permissions to set system time."); |
93 | return FALSE; |
94 | |
95 | case ERROR_BADBUS: |
96 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided bus name is invalid."); |
97 | return FALSE; |
98 | |
99 | case ERROR_BADACTION: |
100 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided action ID is invalid."); |
101 | return FALSE; |
102 | |
103 | case ERROR_GENERIC: |
104 | default: |
105 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons."); |
106 | return FALSE; |
107 | } |
108 | |
fe959043 |
109 | if(!proposed_time) { |
110 | |
111 | timedate1_complete_set_time(td1_passed_interf, invoc); |
112 | return TRUE; |
a8f71719 |
113 | |
fe959043 |
114 | } else if(relative) { |
a8f71719 |
115 | |
116 | cur_time = g_get_real_time(); |
fe959043 |
117 | |
10ddceeb |
118 | if(proposed_time < 0 && cur_time + proposed_time > cur_time) { |
fe959043 |
119 | |
120 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds."); |
121 | return FALSE; |
122 | |
10ddceeb |
123 | } else if(proposed_time > 0 && cur_time + proposed_time < cur_time) { |
fe959043 |
124 | |
125 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds."); |
126 | return FALSE; |
127 | } |
128 | |
644bae40 |
129 | new_time = mktimespec(proposed_time); |
fe959043 |
130 | |
644bae40 |
131 | if(!clock_settime(CLOCK_REALTIME, new_time)) { |
fe959043 |
132 | |
133 | timedate1_complete_set_time(td1_passed_interf, invoc); |
134 | return TRUE; |
135 | |
136 | } else { |
137 | |
138 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons."); |
139 | return FALSE; |
140 | } |
141 | |
10ddceeb |
142 | } else if(proposed_time > 0) { |
a8f71719 |
143 | |
a8f71719 |
144 | |
644bae40 |
145 | new_time = mktimespec(proposed_time); |
146 | |
147 | if(!clock_settime(CLOCK_REALTIME, new_time)) { |
a8f71719 |
148 | |
149 | timedate1_complete_set_time(td1_passed_interf, invoc); |
150 | return TRUE; |
151 | |
152 | } else { |
153 | |
154 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons."); |
155 | return FALSE; |
156 | } |
fe959043 |
157 | |
a8f71719 |
158 | } else { |
159 | |
fe959043 |
160 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds."); |
a8f71719 |
161 | return FALSE; |
162 | } |
28b86015 |
163 | } |
b6ad18ad |
164 | |
28b86015 |
165 | static gboolean |
a8f71719 |
166 | on_handle_set_timezone(Timedate1 *td1_passed_interf, |
28b86015 |
167 | GDBusMethodInvocation *invoc, |
168 | const gchar *greet, |
169 | gpointer data) { |
c8a3e18e |
170 | |
171 | GVariant *params; |
172 | gchar *proposed_tz; |
173 | const gchar *bus_name; |
174 | gboolean policykit_auth; |
175 | check_auth_result is_authed; |
176 | |
177 | gchar *tz_target_path; |
178 | struct stat *statbuf; |
179 | extern int errno; |
180 | |
181 | params = g_dbus_method_invocation_get_parameters(invoc); |
182 | g_variant_get(params, "(sb)", &proposed_tz, &policykit_auth); |
183 | bus_name = g_dbus_method_invocation_get_sender(invoc); |
184 | |
185 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.timedate1.set-timezone", policykit_auth); |
186 | |
187 | switch(is_authed) { |
188 | |
189 | case AUTHORIZED_NATIVELY: |
190 | case AUTHORIZED_BY_PROMPT: |
191 | break; |
192 | |
193 | case UNAUTHORIZED_NATIVELY: |
194 | case UNAUTHORIZED_FAILED_PROMPT: |
195 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EACCES", "Insufficient permissions to set timezone."); |
196 | return FALSE; |
197 | |
198 | case ERROR_BADBUS: |
199 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided bus name is invalid."); |
200 | return FALSE; |
201 | |
202 | case ERROR_BADACTION: |
203 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided action ID is invalid."); |
204 | return FALSE; |
205 | |
206 | case ERROR_GENERIC: |
207 | default: |
208 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set timezone for unknown reasons."); |
209 | return FALSE; |
210 | } |
211 | |
212 | statbuf = (struct stat*) calloc(1, sizeof(struct stat)); |
213 | tz_target_path = (gchar *) calloc(1, TZNAME_MAX); |
214 | |
215 | g_ptr_array_add(timedated_freeable, statbuf); |
216 | g_ptr_array_add(timedated_freeable, tz_target_path); |
217 | |
218 | strlcat(tz_target_path, TZDIR, TZNAME_MAX); |
219 | strlcat(tz_target_path, "/", TZNAME_MAX); |
220 | strlcat(tz_target_path, proposed_tz, TZNAME_MAX); |
221 | |
222 | g_printf("%s\n", tz_target_path); |
223 | |
224 | if(!statbuf) |
225 | return FALSE; |
226 | |
227 | if(lstat(tz_target_path, statbuf)) { |
228 | |
229 | switch(errno) { |
230 | |
231 | case ENOENT: |
232 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ENOENT", "Specified timezone does not exist."); |
233 | break; |
234 | |
235 | default: |
236 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EBADF", "Specified timezone is invalid."); |
237 | break; |
238 | } |
239 | |
240 | return FALSE; |
241 | } |
242 | |
243 | if(!S_ISREG(statbuf->st_mode)) { |
244 | |
245 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EBADF", "Specified path is of an inappropriate type."); |
246 | return FALSE; |
247 | } |
248 | |
249 | memset(statbuf, 0, sizeof statbuf); |
250 | |
251 | if(!lstat(TZDEFAULT, statbuf)) |
252 | if(remove(TZDEFAULT)) |
253 | return FALSE; |
254 | |
255 | if(symlink(tz_target_path, TZDEFAULT)) |
256 | return FALSE; |
257 | |
258 | |
259 | timedate1_complete_set_timezone(td1_passed_interf, invoc); |
260 | |
261 | return TRUE; |
28b86015 |
262 | } |
263 | |
264 | static gboolean |
a8f71719 |
265 | on_handle_set_local_rtc(Timedate1 *td1_passed_interf, |
28b86015 |
266 | GDBusMethodInvocation *invoc, |
267 | const gchar *greet, |
268 | gpointer data) { |
269 | return FALSE; |
270 | } |
271 | |
272 | static gboolean |
a8f71719 |
273 | on_handle_set_ntp(Timedate1 *td1_passed_interf, |
28b86015 |
274 | GDBusMethodInvocation *invoc, |
275 | const gchar *greet, |
276 | gpointer data) { |
277 | return FALSE; |
278 | } |
279 | |
280 | const gchar * |
281 | our_get_timezone() { |
282 | |
1066bd36 |
283 | GStatBuf *stat_zoneinfo; |
03f12ac7 |
284 | gchar *find_cmd, *readlink_path, *ret, *argvp, *hash_to_match; |
285 | gint argcp; |
286 | GError *err; |
287 | struct timezone_checksum_pair tmp; |
1066bd36 |
288 | |
03f12ac7 |
289 | find_cmd = (gchar *) g_malloc0(2048); |
290 | stat_zoneinfo = (GStatBuf *) g_malloc0(8192); |
291 | err = (GError *) g_malloc0(2048); |
1066bd36 |
292 | |
293 | if(g_stat(OS_LOCALTIME, stat_zoneinfo)) { |
294 | |
295 | 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); |
296 | ret = NULL; |
297 | |
298 | } else if(g_file_test(OS_LOCALTIME, G_FILE_TEST_IS_SYMLINK)) { |
299 | |
300 | readlink_path = g_file_read_link(OS_LOCALTIME, &err); |
03f12ac7 |
301 | |
302 | gchar *split[2] = { readlink_path, "" }; |
303 | tmp = parse_timezone_path(split); |
304 | |
305 | ret = tmp.path; |
1066bd36 |
306 | |
307 | if(readlink_path) |
308 | g_free(readlink_path); |
309 | |
310 | } else { |
311 | |
312 | g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME, OS_TIMEZONE_PATH); |
03f12ac7 |
313 | hash_to_match = get_file_sha256(OS_LOCALTIME); |
314 | |
644bae40 |
315 | /* ret = lookup_hash(hash_to_match); */ |
316 | return FALSE; /* TODO fix me for real */ |
03f12ac7 |
317 | |
318 | if(hash_to_match) |
319 | g_free(hash_to_match); |
1066bd36 |
320 | } |
321 | |
c8a3e18e |
322 | |
323 | |
1066bd36 |
324 | return ret; |
28b86015 |
325 | } |
326 | |
327 | gboolean |
328 | our_get_local_rtc() { |
329 | |
330 | gboolean ret = FALSE; |
331 | |
332 | return ret; |
333 | } |
334 | |
335 | gboolean |
336 | our_get_can_ntp() { |
337 | |
338 | const gboolean ret = FALSE; |
339 | |
340 | return ret; |
341 | } |
342 | |
343 | gboolean |
344 | our_get_ntp() { |
345 | |
346 | const gboolean ret = FALSE; |
347 | |
348 | return ret; |
349 | } |
b6ad18ad |
350 | |
28b86015 |
351 | gboolean |
352 | our_get_ntpsynchronized() { |
353 | |
354 | const gboolean ret = FALSE; |
b6ad18ad |
355 | |
28b86015 |
356 | return ret; |
357 | } |
b6ad18ad |
358 | |
28b86015 |
359 | guint64 |
360 | our_get_time_usec() { |
b6ad18ad |
361 | |
28b86015 |
362 | guint64 ret = 0; |
b6ad18ad |
363 | |
28b86015 |
364 | return ret; |
365 | } |
366 | |
367 | guint64 |
368 | our_get_rtc_time_usec() { |
369 | |
370 | guint64 ret = 0; |
371 | |
372 | return ret; |
373 | } |
b6ad18ad |
374 | |
1ce41045 |
375 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
b6ad18ad |
376 | |
377 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
378 | const gchar *name, |
379 | gpointer user_data) { |
380 | |
254ceec0 |
381 | g_printf("got bus/name, exporting %s's interface...\n", name); |
b6ad18ad |
382 | |
383 | timedated_interf = timedate1_skeleton_new(); |
384 | |
28b86015 |
385 | /* attach function pointers to generated struct's method handlers */ |
386 | g_signal_connect(timedated_interf, "handle-set-time", G_CALLBACK(on_handle_set_time), NULL); |
387 | g_signal_connect(timedated_interf, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone), NULL); |
388 | g_signal_connect(timedated_interf, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc), NULL); |
389 | g_signal_connect(timedated_interf, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp), NULL); |
03f12ac7 |
390 | |
28b86015 |
391 | /* set our properties before export */ |
392 | timedate1_set_timezone(timedated_interf, our_get_timezone()); |
393 | timedate1_set_local_rtc(timedated_interf, our_get_local_rtc()); |
394 | timedate1_set_can_ntp(timedated_interf, our_get_can_ntp()); |
395 | timedate1_set_ntp(timedated_interf, our_get_ntp()); |
396 | timedate1_set_ntpsynchronized(timedated_interf, our_get_ntpsynchronized()); |
397 | timedate1_set_time_usec(timedated_interf, our_get_time_usec()); |
398 | timedate1_set_rtctime_usec(timedated_interf, our_get_rtc_time_usec()); |
399 | |
b6ad18ad |
400 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
401 | conn, |
402 | "/org/freedesktop/timedate1", |
403 | NULL)) { |
404 | |
509599f0 |
405 | g_printf("failed to export %s's interface!\n", name); |
406 | timedated_mem_clean(); |
407 | |
408 | } else { |
409 | |
410 | dbus_interface_exported = TRUE; |
411 | g_printf("exported %s's interface on the system bus...\n", name); |
b6ad18ad |
412 | } |
509599f0 |
413 | } |
414 | |
415 | static void timedated_on_name_acquired(GDBusConnection *conn, |
90f54407 |
416 | const gchar *name, |
509599f0 |
417 | gpointer user_data) { |
b6ad18ad |
418 | |
509599f0 |
419 | g_printf("success!\n"); |
b6ad18ad |
420 | } |
421 | |
b6ad18ad |
422 | static void timedated_on_name_lost(GDBusConnection *conn, |
423 | const gchar *name, |
424 | gpointer user_data) { |
425 | |
509599f0 |
426 | if(!conn) { |
b6ad18ad |
427 | |
509599f0 |
428 | 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); |
429 | timedated_mem_clean(); |
430 | } |
b6ad18ad |
431 | |
509599f0 |
432 | g_print("lost name %s, exiting...\n", name); |
433 | |
434 | timedated_mem_clean(); |
b6ad18ad |
435 | } |
436 | |
b70beb08 |
437 | /* --- end bus/name handlers, begin misc unix functions --- */ |
438 | |
509599f0 |
439 | /* safe call to clean and then exit |
440 | * this stops our GMainLoop safely before letting main() return */ |
b70beb08 |
441 | void timedated_mem_clean() { |
442 | |
341587db |
443 | g_printf("exiting...\n"); |
444 | |
445 | if(dbus_interface_exported) |
446 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf)); |
447 | |
448 | if(g_main_loop_is_running(timedated_loop)) |
449 | g_main_loop_quit(timedated_loop); |
450 | |
b70beb08 |
451 | } |
452 | |
712eb329 |
453 | /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */ |
454 | gboolean unix_sig_terminate_handler(gpointer data) { |
455 | |
456 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
457 | |
458 | timedated_mem_clean(); |
459 | return G_SOURCE_REMOVE; |
460 | } |
461 | |
2ef2cfe1 |
462 | void set_signal_handlers() { |
463 | |
464 | /* we don't care about its descriptor, we never need to unregister these */ |
465 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
466 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
467 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
468 | } |
469 | |
1e8c7c88 |
470 | int main() { |
b6ad18ad |
471 | |
9728ae1f |
472 | set_signal_handlers(); |
473 | |
644bae40 |
474 | /*if(!build_lookup_table()) |
475 | return 1; */ |
03f12ac7 |
476 | |
90f54407 |
477 | timedated_loop = g_main_loop_new(NULL, TRUE); |
478 | timedated_freeable = g_ptr_array_new(); |
b6ad18ad |
479 | |
90f54407 |
480 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
b6ad18ad |
481 | "org.freedesktop.timedate1", |
482 | G_BUS_NAME_OWNER_FLAGS_NONE, |
483 | timedated_on_bus_acquired, |
484 | timedated_on_name_acquired, |
485 | timedated_on_name_lost, |
486 | NULL, |
487 | NULL); |
488 | |
90f54407 |
489 | g_main_loop_run(timedated_loop); |
9728ae1f |
490 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
90f54407 |
491 | g_main_loop_unref(timedated_loop); |
b6ad18ad |
492 | |
9728ae1f |
493 | /* guaranteed unownable */ |
90f54407 |
494 | g_bus_unown_name(bus_descriptor); |
b6ad18ad |
495 | |
9728ae1f |
496 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
497 | g_ptr_array_free(timedated_freeable, TRUE); |
b6ad18ad |
498 | |
90f54407 |
499 | return 0; |
1e8c7c88 |
500 | } |
1066bd36 |
501 | |
03f12ac7 |
502 | static struct timezone_checksum_pair parse_timezone_path(gchar **pair) { |
1066bd36 |
503 | |
03f12ac7 |
504 | gchar *prefix_pattern, *right_prefix_pattern, *posix_prefix_pattern, *lean_path; |
1066bd36 |
505 | GRegex *prefix, *posix, *right; |
506 | GError *err = NULL; |
03f12ac7 |
507 | struct timezone_checksum_pair ret = { NULL, NULL, FALSE, FALSE }; |
1066bd36 |
508 | |
03f12ac7 |
509 | if(!pair[0]) |
510 | return ret; |
1066bd36 |
511 | |
512 | prefix_pattern = (gchar *) g_malloc0(4096); |
03f12ac7 |
513 | right_prefix_pattern = (gchar *) g_malloc0(4096); |
514 | posix_prefix_pattern = (gchar *) g_malloc0(4096); |
515 | |
516 | g_sprintf(prefix_pattern, "%s/", OS_TIMEZONE_PATH); |
517 | g_sprintf(posix_prefix_pattern, "%s/posix/", OS_TIMEZONE_PATH); |
518 | g_sprintf(right_prefix_pattern, "%s/right/", OS_TIMEZONE_PATH); |
1066bd36 |
519 | |
520 | prefix = g_regex_new(prefix_pattern, 0, 0, &err); |
03f12ac7 |
521 | posix = g_regex_new(posix_prefix_pattern, 0, 0, &err); |
522 | right = g_regex_new(right_prefix_pattern, 0, 0, &err); |
523 | |
524 | if(g_regex_match_full(posix, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) { |
525 | |
526 | ret.posix = TRUE; |
527 | lean_path = g_regex_replace_literal(posix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
528 | |
529 | } else if(g_regex_match_full(right, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) { |
530 | |
531 | ret.right = TRUE; |
532 | lean_path = g_regex_replace_literal(right, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
533 | |
534 | } else |
535 | lean_path = g_regex_replace_literal(prefix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
536 | |
537 | ret.path = lean_path; |
538 | |
539 | ret.sum = g_malloc0(256); |
540 | g_strlcpy(ret.sum, pair[1], 66); |
1066bd36 |
541 | |
542 | g_regex_unref(prefix); |
543 | g_regex_unref(right); |
544 | g_regex_unref(posix); |
545 | |
03f12ac7 |
546 | return ret; |
547 | } |
548 | |
644bae40 |
549 | /* TODO need to deconstruct tz_table on exit |
03f12ac7 |
550 | static gboolean build_lookup_table() { |
551 | |
552 | gchar *find_cmd, **map_pairs, *find_output, *path_buf, *sum_buf, **entry_buf; |
553 | GError *err; |
554 | gboolean ret; |
555 | gint i; |
556 | |
557 | i = 0; |
558 | err = NULL; |
559 | ret = TRUE; |
560 | |
561 | find_cmd = (gchar *) g_malloc0(4096); |
562 | find_output = (gchar *) g_malloc0(1000000); |
563 | |
564 | 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); |
565 | |
566 | if(!g_spawn_command_line_sync(find_cmd, &find_output, NULL, NULL, &err)) { |
567 | |
568 | g_printf("error running `%s`\n", find_cmd); |
569 | ret = FALSE; |
570 | } |
571 | |
572 | map_pairs = g_strsplit(find_output, "\n", INT_MAX); |
573 | |
574 | while(map_pairs[i] && (entry_buf = g_strsplit(map_pairs[i], "=", INT_MAX))) { |
575 | |
576 | tz_table[i] = parse_timezone_path(entry_buf); |
577 | |
578 | g_strfreev(entry_buf); |
579 | i++; |
580 | } |
581 | |
582 | g_free(find_output); |
583 | g_free(find_cmd); |
584 | g_free(map_pairs); |
585 | |
586 | return ret; |
587 | } |
588 | |
589 | static gchar *lookup_hash(gchar *hash) { |
590 | |
591 | gint i = 0; |
592 | |
593 | while(tz_table[i].sum) |
594 | if(!g_strcmp0(tz_table[i].sum, hash)) |
595 | return tz_table[i].path; |
596 | else |
597 | i++; |
598 | |
599 | return NULL; |
644bae40 |
600 | }*/ |
601 | |
602 | /* takes number of microseconds since epoch and returns a |
603 | * ptr to a timespec suitable to be passed to clock_settime(3) |
604 | */ |
605 | static struct timespec* mktimespec(gint64 us) { |
606 | |
607 | long nanoseconds; |
608 | time_t seconds; |
609 | |
610 | gint64 div_buf_remainder, div_buf_s, div_buf_ns; |
611 | struct timespec *ret; |
612 | |
613 | div_buf_s = (us / 1000000); /* us / 10^6 = s */ |
614 | div_buf_remainder = (us % 1000000); /* fraction of second lost from prev. line */ |
615 | div_buf_ns = div_buf_remainder * 1000; /* us * 10^3 = ns */ |
616 | |
617 | seconds = (time_t) div_buf_s; /* porting note: most systems use 32 bit time, adjust accordingly */ |
618 | nanoseconds = (long) div_buf_ns; |
619 | |
620 | ret = (struct timespec *) calloc(1, sizeof(struct timespec)); |
621 | |
622 | ret->tv_sec = seconds; |
623 | ret->tv_nsec = nanoseconds; |
624 | |
625 | g_ptr_array_add(timedated_freeable, ret); |
626 | |
627 | return ret; |
1066bd36 |
628 | } |