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 new_time
= (struct timespec
*) g_malloc0(sizeof(struct timespec
));
112 cur_time
= g_get_real_time();
114 if(proposed_time
< 0 && cur_time
+ proposed_time
> proposed_time
) {
116 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
119 } else if(cur_time
+ proposed_time
< proposed_time
) {
121 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
125 new_time
= (struct timespec
*) g_malloc0(sizeof(struct timespec
));
126 new_time
->tv_sec
= proposed_time
;
127 new_time
->tv_nsec
= 0;
128 g_ptr_array_add(timedated_freeable
, new_time
);
130 if(!clock_settime(CLOCK_REALTIME
, new_time
)) {
132 timedate1_complete_set_time(td1_passed_interf
, invoc
);
137 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
141 } else if(proposed_time
>= 0) {
143 new_time
= (struct timespec
*) g_malloc0(sizeof(struct timespec
));
144 new_time
->tv_sec
= proposed_time
;
145 new_time
->tv_nsec
= 0;
146 g_ptr_array_add(timedated_freeable
, new_time
);
148 if(!clock_settime(CLOCK_REALTIME
, new_time
)) {
150 timedate1_complete_set_time(td1_passed_interf
, invoc
);
155 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons.");
161 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds.");
167 on_handle_set_timezone(Timedate1
*td1_passed_interf
,
168 GDBusMethodInvocation
*invoc
,
175 on_handle_set_local_rtc(Timedate1
*td1_passed_interf
,
176 GDBusMethodInvocation
*invoc
,
183 on_handle_set_ntp(Timedate1
*td1_passed_interf
,
184 GDBusMethodInvocation
*invoc
,
193 GStatBuf
*stat_zoneinfo
;
194 gchar
*find_cmd
, *readlink_path
, *ret
, *argvp
, *hash_to_match
;
197 struct timezone_checksum_pair tmp
;
199 find_cmd
= (gchar
*) g_malloc0(2048);
200 stat_zoneinfo
= (GStatBuf
*) g_malloc0(8192);
201 err
= (GError
*) g_malloc0(2048);
203 if(g_stat(OS_LOCALTIME
, stat_zoneinfo
)) {
205 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
);
208 } else if(g_file_test(OS_LOCALTIME
, G_FILE_TEST_IS_SYMLINK
)) {
210 readlink_path
= g_file_read_link(OS_LOCALTIME
, &err
);
212 gchar
*split
[2] = { readlink_path
, "" };
213 tmp
= parse_timezone_path(split
);
218 g_free(readlink_path
);
222 g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME
, OS_TIMEZONE_PATH
);
223 hash_to_match
= get_file_sha256(OS_LOCALTIME
);
225 ret
= lookup_hash(hash_to_match
);
228 g_free(hash_to_match
);
235 our_get_local_rtc() {
237 gboolean ret
= FALSE
;
245 const gboolean ret
= FALSE
;
253 const gboolean ret
= FALSE
;
259 our_get_ntpsynchronized() {
261 const gboolean ret
= FALSE
;
267 our_get_time_usec() {
275 our_get_rtc_time_usec() {
282 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
284 static void timedated_on_bus_acquired(GDBusConnection
*conn
,
286 gpointer user_data
) {
288 g_printf("got bus/name, exporting %s's interface...\n", name
);
290 timedated_interf
= timedate1_skeleton_new();
292 /* attach function pointers to generated struct's method handlers */
293 g_signal_connect(timedated_interf
, "handle-set-time", G_CALLBACK(on_handle_set_time
), NULL
);
294 g_signal_connect(timedated_interf
, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone
), NULL
);
295 g_signal_connect(timedated_interf
, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc
), NULL
);
296 g_signal_connect(timedated_interf
, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp
), NULL
);
298 /* set our properties before export */
299 timedate1_set_timezone(timedated_interf
, our_get_timezone());
300 timedate1_set_local_rtc(timedated_interf
, our_get_local_rtc());
301 timedate1_set_can_ntp(timedated_interf
, our_get_can_ntp());
302 timedate1_set_ntp(timedated_interf
, our_get_ntp());
303 timedate1_set_ntpsynchronized(timedated_interf
, our_get_ntpsynchronized());
304 timedate1_set_time_usec(timedated_interf
, our_get_time_usec());
305 timedate1_set_rtctime_usec(timedated_interf
, our_get_rtc_time_usec());
307 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf
),
309 "/org/freedesktop/timedate1",
312 g_printf("failed to export %s's interface!\n", name
);
313 timedated_mem_clean();
317 dbus_interface_exported
= TRUE
;
318 g_printf("exported %s's interface on the system bus...\n", name
);
322 static void timedated_on_name_acquired(GDBusConnection
*conn
,
324 gpointer user_data
) {
326 g_printf("success!\n");
329 static void timedated_on_name_lost(GDBusConnection
*conn
,
331 gpointer user_data
) {
335 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
);
336 timedated_mem_clean();
339 g_print("lost name %s, exiting...\n", name
);
341 timedated_mem_clean();
344 /* --- end bus/name handlers, begin misc unix functions --- */
346 /* safe call to clean and then exit
347 * this stops our GMainLoop safely before letting main() return */
348 void timedated_mem_clean() {
350 g_printf("exiting...\n");
352 if(dbus_interface_exported
)
353 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf
));
355 if(g_main_loop_is_running(timedated_loop
))
356 g_main_loop_quit(timedated_loop
);
360 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
361 gboolean
unix_sig_terminate_handler(gpointer data
) {
363 g_printf("caught SIGINT/HUP/TERM, exiting\n");
365 timedated_mem_clean();
366 return G_SOURCE_REMOVE
;
369 void set_signal_handlers() {
371 /* we don't care about its descriptor, we never need to unregister these */
372 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
373 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
374 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
379 set_signal_handlers();
381 if(!build_lookup_table())
384 timedated_loop
= g_main_loop_new(NULL
, TRUE
);
385 timedated_freeable
= g_ptr_array_new();
387 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
388 "org.freedesktop.timedate1",
389 G_BUS_NAME_OWNER_FLAGS_NONE
,
390 timedated_on_bus_acquired
,
391 timedated_on_name_acquired
,
392 timedated_on_name_lost
,
396 g_main_loop_run(timedated_loop
);
397 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
398 g_main_loop_unref(timedated_loop
);
400 /* guaranteed unownable */
401 g_bus_unown_name(bus_descriptor
);
403 /* at this point no operations can occur with our data, it is safe to free it + its container */
404 g_ptr_array_free(timedated_freeable
, TRUE
);
409 static struct timezone_checksum_pair
parse_timezone_path(gchar
**pair
) {
411 gchar
*prefix_pattern
, *right_prefix_pattern
, *posix_prefix_pattern
, *lean_path
;
412 GRegex
*prefix
, *posix
, *right
;
414 struct timezone_checksum_pair ret
= { NULL
, NULL
, FALSE
, FALSE
};
419 prefix_pattern
= (gchar
*) g_malloc0(4096);
420 right_prefix_pattern
= (gchar
*) g_malloc0(4096);
421 posix_prefix_pattern
= (gchar
*) g_malloc0(4096);
423 g_sprintf(prefix_pattern
, "%s/", OS_TIMEZONE_PATH
);
424 g_sprintf(posix_prefix_pattern
, "%s/posix/", OS_TIMEZONE_PATH
);
425 g_sprintf(right_prefix_pattern
, "%s/right/", OS_TIMEZONE_PATH
);
427 prefix
= g_regex_new(prefix_pattern
, 0, 0, &err
);
428 posix
= g_regex_new(posix_prefix_pattern
, 0, 0, &err
);
429 right
= g_regex_new(right_prefix_pattern
, 0, 0, &err
);
431 if(g_regex_match_full(posix
, pair
[0], -1, 0, G_REGEX_MATCH_NOTEMPTY
, NULL
, NULL
)) {
434 lean_path
= g_regex_replace_literal(posix
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
436 } else if(g_regex_match_full(right
, pair
[0], -1, 0, G_REGEX_MATCH_NOTEMPTY
, NULL
, NULL
)) {
439 lean_path
= g_regex_replace_literal(right
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
442 lean_path
= g_regex_replace_literal(prefix
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
444 ret
.path
= lean_path
;
446 ret
.sum
= g_malloc0(256);
447 g_strlcpy(ret
.sum
, pair
[1], 66);
449 g_regex_unref(prefix
);
450 g_regex_unref(right
);
451 g_regex_unref(posix
);
456 /* TODO need to deconstruct tz_table on exit */
457 static gboolean
build_lookup_table() {
459 gchar
*find_cmd
, **map_pairs
, *find_output
, *path_buf
, *sum_buf
, **entry_buf
;
468 find_cmd
= (gchar
*) g_malloc0(4096);
469 find_output
= (gchar
*) g_malloc0(1000000);
471 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
);
473 if(!g_spawn_command_line_sync(find_cmd
, &find_output
, NULL
, NULL
, &err
)) {
475 g_printf("error running `%s`\n", find_cmd
);
479 map_pairs
= g_strsplit(find_output
, "\n", INT_MAX
);
481 while(map_pairs
[i
] && (entry_buf
= g_strsplit(map_pairs
[i
], "=", INT_MAX
))) {
483 tz_table
[i
] = parse_timezone_path(entry_buf
);
485 g_strfreev(entry_buf
);
496 static gchar
*lookup_hash(gchar
*hash
) {
500 while(tz_table
[i
].sum
)
501 if(!g_strcmp0(tz_table
[i
].sum
, hash
))
502 return tz_table
[i
].path
;