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 #include <sys/types.h>
26 #include <glib/gprintf.h>
27 #include <glib-unix.h>
28 #include <glib/gstdio.h>
29 #include <polkit/polkit.h>
31 #include "timedated-gen.h"
32 #include "timedated.h"
34 #include "../../util.h"
36 GPtrArray
*timedated_freeable
;
37 Timedate1
*timedated_interf
;
39 GMainLoop
*timedated_loop
;
42 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
44 const gchar
*OS_LOCALTIME
= "/etc/localtime"; /* current timezone file */
45 const gchar
*OS_TIMEZONE_PATH
= "/usr/share/zoneinfo"; /* path to system timezone files */
47 struct timezone_checksum_pair
{
55 static struct timezone_checksum_pair tz_table
[5000];
57 /* --- begin method/property/dbus signal code --- */
60 on_handle_set_time(Timedate1
*td1_passed_interf
,
61 GDBusMethodInvocation
*invoc
,
66 gint64 proposed_time
, cur_time
;
67 const gchar
*bus_name
;
68 gboolean policykit_auth
;
69 check_auth_result is_authed
;
70 gboolean relative
; /* relative if passed time_t is meant to be added to current time */
71 struct timespec new_time
;
73 params
= g_dbus_method_invocation_get_parameters(invoc
);
74 g_variant_get(params
, "(xbb)", &proposed_time
, &relative
, &policykit_auth
);
75 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
77 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.timedate1.set-time", policykit_auth
);
81 case AUTHORIZED_NATIVELY
:
82 case AUTHORIZED_BY_PROMPT
:
85 case UNAUTHORIZED_NATIVELY
:
86 case UNAUTHORIZED_FAILED_PROMPT
:
87 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EACCES", "Insufficient permissions to set system time.");
91 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EFAULT", "Provided bus name is invalid.");
95 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EFAULT", "Provided action ID is invalid.");
100 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
106 timedate1_complete_set_time(td1_passed_interf
, invoc
);
109 } else if(relative
) {
111 cur_time
= g_get_real_time();
113 if(proposed_time
< 0 && cur_time
+ proposed_time
> cur_time
) {
115 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
118 } else if(proposed_time
> 0 && cur_time
+ proposed_time
< cur_time
) {
120 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
124 new_time
.tv_sec
= (cur_time
+ proposed_time
) / 1000000;
125 new_time
.tv_nsec
= CLAMP((((cur_time
+ proposed_time
) % 1000000) * 1000), 0, 1000000000);
127 if(!clock_settime(CLOCK_REALTIME
, &new_time
)) {
129 timedate1_complete_set_time(td1_passed_interf
, invoc
);
134 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
138 } else if(proposed_time
> 0) {
140 new_time
.tv_sec
= (cur_time
+ proposed_time
) / 1000000;
141 new_time
.tv_nsec
= CLAMP((((cur_time
+ proposed_time
) % 1000000) * 1000), 0, 1000000000);
143 if(!clock_settime(CLOCK_REALTIME
, &new_time
)) {
145 timedate1_complete_set_time(td1_passed_interf
, invoc
);
150 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
156 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
162 on_handle_set_timezone(Timedate1
*td1_passed_interf
,
163 GDBusMethodInvocation
*invoc
,
170 on_handle_set_local_rtc(Timedate1
*td1_passed_interf
,
171 GDBusMethodInvocation
*invoc
,
178 on_handle_set_ntp(Timedate1
*td1_passed_interf
,
179 GDBusMethodInvocation
*invoc
,
188 GStatBuf
*stat_zoneinfo
;
189 gchar
*find_cmd
, *readlink_path
, *ret
, *argvp
, *hash_to_match
;
192 struct timezone_checksum_pair tmp
;
194 find_cmd
= (gchar
*) g_malloc0(2048);
195 stat_zoneinfo
= (GStatBuf
*) g_malloc0(8192);
196 err
= (GError
*) g_malloc0(2048);
198 if(g_stat(OS_LOCALTIME
, stat_zoneinfo
)) {
200 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
);
203 } else if(g_file_test(OS_LOCALTIME
, G_FILE_TEST_IS_SYMLINK
)) {
205 readlink_path
= g_file_read_link(OS_LOCALTIME
, &err
);
207 gchar
*split
[2] = { readlink_path
, "" };
208 tmp
= parse_timezone_path(split
);
213 g_free(readlink_path
);
217 g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME
, OS_TIMEZONE_PATH
);
218 hash_to_match
= get_file_sha256(OS_LOCALTIME
);
220 ret
= lookup_hash(hash_to_match
);
223 g_free(hash_to_match
);
230 our_get_local_rtc() {
232 gboolean ret
= FALSE
;
240 const gboolean ret
= FALSE
;
248 const gboolean ret
= FALSE
;
254 our_get_ntpsynchronized() {
256 const gboolean ret
= FALSE
;
262 our_get_time_usec() {
270 our_get_rtc_time_usec() {
277 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
279 static void timedated_on_bus_acquired(GDBusConnection
*conn
,
281 gpointer user_data
) {
283 g_printf("got bus/name, exporting %s's interface...\n", name
);
285 timedated_interf
= timedate1_skeleton_new();
287 /* attach function pointers to generated struct's method handlers */
288 g_signal_connect(timedated_interf
, "handle-set-time", G_CALLBACK(on_handle_set_time
), NULL
);
289 g_signal_connect(timedated_interf
, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone
), NULL
);
290 g_signal_connect(timedated_interf
, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc
), NULL
);
291 g_signal_connect(timedated_interf
, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp
), NULL
);
293 /* set our properties before export */
294 timedate1_set_timezone(timedated_interf
, our_get_timezone());
295 timedate1_set_local_rtc(timedated_interf
, our_get_local_rtc());
296 timedate1_set_can_ntp(timedated_interf
, our_get_can_ntp());
297 timedate1_set_ntp(timedated_interf
, our_get_ntp());
298 timedate1_set_ntpsynchronized(timedated_interf
, our_get_ntpsynchronized());
299 timedate1_set_time_usec(timedated_interf
, our_get_time_usec());
300 timedate1_set_rtctime_usec(timedated_interf
, our_get_rtc_time_usec());
302 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf
),
304 "/org/freedesktop/timedate1",
307 g_printf("failed to export %s's interface!\n", name
);
308 timedated_mem_clean();
312 dbus_interface_exported
= TRUE
;
313 g_printf("exported %s's interface on the system bus...\n", name
);
317 static void timedated_on_name_acquired(GDBusConnection
*conn
,
319 gpointer user_data
) {
321 g_printf("success!\n");
324 static void timedated_on_name_lost(GDBusConnection
*conn
,
326 gpointer user_data
) {
330 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
);
331 timedated_mem_clean();
334 g_print("lost name %s, exiting...\n", name
);
336 timedated_mem_clean();
339 /* --- end bus/name handlers, begin misc unix functions --- */
341 /* safe call to clean and then exit
342 * this stops our GMainLoop safely before letting main() return */
343 void timedated_mem_clean() {
345 g_printf("exiting...\n");
347 if(dbus_interface_exported
)
348 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf
));
350 if(g_main_loop_is_running(timedated_loop
))
351 g_main_loop_quit(timedated_loop
);
355 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
356 gboolean
unix_sig_terminate_handler(gpointer data
) {
358 g_printf("caught SIGINT/HUP/TERM, exiting\n");
360 timedated_mem_clean();
361 return G_SOURCE_REMOVE
;
364 void set_signal_handlers() {
366 /* we don't care about its descriptor, we never need to unregister these */
367 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
368 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
369 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
374 set_signal_handlers();
376 if(!build_lookup_table())
379 timedated_loop
= g_main_loop_new(NULL
, TRUE
);
380 timedated_freeable
= g_ptr_array_new();
382 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
383 "org.freedesktop.timedate1",
384 G_BUS_NAME_OWNER_FLAGS_NONE
,
385 timedated_on_bus_acquired
,
386 timedated_on_name_acquired
,
387 timedated_on_name_lost
,
391 g_main_loop_run(timedated_loop
);
392 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
393 g_main_loop_unref(timedated_loop
);
395 /* guaranteed unownable */
396 g_bus_unown_name(bus_descriptor
);
398 /* at this point no operations can occur with our data, it is safe to free it + its container */
399 g_ptr_array_free(timedated_freeable
, TRUE
);
404 static struct timezone_checksum_pair
parse_timezone_path(gchar
**pair
) {
406 gchar
*prefix_pattern
, *right_prefix_pattern
, *posix_prefix_pattern
, *lean_path
;
407 GRegex
*prefix
, *posix
, *right
;
409 struct timezone_checksum_pair ret
= { NULL
, NULL
, FALSE
, FALSE
};
414 prefix_pattern
= (gchar
*) g_malloc0(4096);
415 right_prefix_pattern
= (gchar
*) g_malloc0(4096);
416 posix_prefix_pattern
= (gchar
*) g_malloc0(4096);
418 g_sprintf(prefix_pattern
, "%s/", OS_TIMEZONE_PATH
);
419 g_sprintf(posix_prefix_pattern
, "%s/posix/", OS_TIMEZONE_PATH
);
420 g_sprintf(right_prefix_pattern
, "%s/right/", OS_TIMEZONE_PATH
);
422 prefix
= g_regex_new(prefix_pattern
, 0, 0, &err
);
423 posix
= g_regex_new(posix_prefix_pattern
, 0, 0, &err
);
424 right
= g_regex_new(right_prefix_pattern
, 0, 0, &err
);
426 if(g_regex_match_full(posix
, pair
[0], -1, 0, G_REGEX_MATCH_NOTEMPTY
, NULL
, NULL
)) {
429 lean_path
= g_regex_replace_literal(posix
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
431 } else if(g_regex_match_full(right
, pair
[0], -1, 0, G_REGEX_MATCH_NOTEMPTY
, NULL
, NULL
)) {
434 lean_path
= g_regex_replace_literal(right
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
437 lean_path
= g_regex_replace_literal(prefix
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
439 ret
.path
= lean_path
;
441 ret
.sum
= g_malloc0(256);
442 g_strlcpy(ret
.sum
, pair
[1], 66);
444 g_regex_unref(prefix
);
445 g_regex_unref(right
);
446 g_regex_unref(posix
);
451 /* TODO need to deconstruct tz_table on exit */
452 static gboolean
build_lookup_table() {
454 gchar
*find_cmd
, **map_pairs
, *find_output
, *path_buf
, *sum_buf
, **entry_buf
;
463 find_cmd
= (gchar
*) g_malloc0(4096);
464 find_output
= (gchar
*) g_malloc0(1000000);
466 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
);
468 if(!g_spawn_command_line_sync(find_cmd
, &find_output
, NULL
, NULL
, &err
)) {
470 g_printf("error running `%s`\n", find_cmd
);
474 map_pairs
= g_strsplit(find_output
, "\n", INT_MAX
);
476 while(map_pairs
[i
] && (entry_buf
= g_strsplit(map_pairs
[i
], "=", INT_MAX
))) {
478 tz_table
[i
] = parse_timezone_path(entry_buf
);
480 g_strfreev(entry_buf
);
491 static gchar
*lookup_hash(gchar
*hash
) {
495 while(tz_table
[i
].sum
)
496 if(!g_strcmp0(tz_table
[i
].sum
, hash
))
497 return tz_table
[i
].path
;