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>
25 #include <glib/gprintf.h>
26 #include <glib-unix.h>
27 #include <glib/gstdio.h>
28 #include <polkit/polkit.h>
30 #include "timedated-gen.h"
31 #include "timedated.h"
33 #include "../../util.h"
35 GPtrArray
*timedated_freeable
;
36 Timedate1
*timedated_interf
;
38 GMainLoop
*timedated_loop
;
41 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
43 const gchar
*OS_LOCALTIME
= "/etc/localtime"; /* current timezone file */
44 const gchar
*OS_TIMEZONE_PATH
= "/usr/share/zoneinfo"; /* path to system timezone files */
46 struct timezone_checksum_pair
{
54 static struct timezone_checksum_pair tz_table
[5000];
56 /* --- begin method/property/dbus signal code --- */
59 on_handle_set_time(Timedate1
*hn1_passed_interf
,
60 GDBusMethodInvocation
*invoc
,
67 on_handle_set_timezone(Timedate1
*hn1_passed_interf
,
68 GDBusMethodInvocation
*invoc
,
75 on_handle_set_local_rtc(Timedate1
*hn1_passed_interf
,
76 GDBusMethodInvocation
*invoc
,
83 on_handle_set_ntp(Timedate1
*hn1_passed_interf
,
84 GDBusMethodInvocation
*invoc
,
93 GStatBuf
*stat_zoneinfo
;
94 gchar
*find_cmd
, *readlink_path
, *ret
, *argvp
, *hash_to_match
;
97 struct timezone_checksum_pair tmp
;
99 find_cmd
= (gchar
*) g_malloc0(2048);
100 stat_zoneinfo
= (GStatBuf
*) g_malloc0(8192);
101 err
= (GError
*) g_malloc0(2048);
103 if(g_stat(OS_LOCALTIME
, stat_zoneinfo
)) {
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
);
108 } else if(g_file_test(OS_LOCALTIME
, G_FILE_TEST_IS_SYMLINK
)) {
110 readlink_path
= g_file_read_link(OS_LOCALTIME
, &err
);
112 gchar
*split
[2] = { readlink_path
, "" };
113 tmp
= parse_timezone_path(split
);
118 g_free(readlink_path
);
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
);
125 ret
= lookup_hash(hash_to_match
);
128 g_free(hash_to_match
);
135 our_get_local_rtc() {
137 gboolean ret
= FALSE
;
145 const gboolean ret
= FALSE
;
153 const gboolean ret
= FALSE
;
159 our_get_ntpsynchronized() {
161 const gboolean ret
= FALSE
;
167 our_get_time_usec() {
175 our_get_rtc_time_usec() {
182 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
184 static void timedated_on_bus_acquired(GDBusConnection
*conn
,
186 gpointer user_data
) {
188 g_printf("got bus/name, exporting %s's interface...\n", name
);
190 timedated_interf
= timedate1_skeleton_new();
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
);
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());
207 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf
),
209 "/org/freedesktop/timedate1",
212 g_printf("failed to export %s's interface!\n", name
);
213 timedated_mem_clean();
217 dbus_interface_exported
= TRUE
;
218 g_printf("exported %s's interface on the system bus...\n", name
);
222 static void timedated_on_name_acquired(GDBusConnection
*conn
,
224 gpointer user_data
) {
226 g_printf("success!\n");
229 static void timedated_on_name_lost(GDBusConnection
*conn
,
231 gpointer user_data
) {
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();
239 g_print("lost name %s, exiting...\n", name
);
241 timedated_mem_clean();
244 /* --- end bus/name handlers, begin misc unix functions --- */
246 /* safe call to clean and then exit
247 * this stops our GMainLoop safely before letting main() return */
248 void timedated_mem_clean() {
250 g_printf("exiting...\n");
252 if(dbus_interface_exported
)
253 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf
));
255 if(g_main_loop_is_running(timedated_loop
))
256 g_main_loop_quit(timedated_loop
);
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
) {
263 g_printf("caught SIGINT/HUP/TERM, exiting\n");
265 timedated_mem_clean();
266 return G_SOURCE_REMOVE
;
269 void set_signal_handlers() {
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
);
279 set_signal_handlers();
281 if(!build_lookup_table())
284 timedated_loop
= g_main_loop_new(NULL
, TRUE
);
285 timedated_freeable
= g_ptr_array_new();
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
,
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
);
300 /* guaranteed unownable */
301 g_bus_unown_name(bus_descriptor
);
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
);
309 static struct timezone_checksum_pair
parse_timezone_path(gchar
**pair
) {
311 gchar
*prefix_pattern
, *right_prefix_pattern
, *posix_prefix_pattern
, *lean_path
;
312 GRegex
*prefix
, *posix
, *right
;
314 struct timezone_checksum_pair ret
= { NULL
, NULL
, FALSE
, FALSE
};
319 prefix_pattern
= (gchar
*) g_malloc0(4096);
320 right_prefix_pattern
= (gchar
*) g_malloc0(4096);
321 posix_prefix_pattern
= (gchar
*) g_malloc0(4096);
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
);
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
);
331 if(g_regex_match_full(posix
, pair
[0], -1, 0, G_REGEX_MATCH_NOTEMPTY
, NULL
, NULL
)) {
334 lean_path
= g_regex_replace_literal(posix
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
336 } else if(g_regex_match_full(right
, pair
[0], -1, 0, G_REGEX_MATCH_NOTEMPTY
, NULL
, NULL
)) {
339 lean_path
= g_regex_replace_literal(right
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
342 lean_path
= g_regex_replace_literal(prefix
, pair
[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY
, NULL
);
344 ret
.path
= lean_path
;
346 ret
.sum
= g_malloc0(256);
347 g_strlcpy(ret
.sum
, pair
[1], 66);
349 g_regex_unref(prefix
);
350 g_regex_unref(right
);
351 g_regex_unref(posix
);
356 /* TODO need to deconstruct tz_table on exit */
357 static gboolean
build_lookup_table() {
359 gchar
*find_cmd
, **map_pairs
, *find_output
, *path_buf
, *sum_buf
, **entry_buf
;
368 find_cmd
= (gchar
*) g_malloc0(4096);
369 find_output
= (gchar
*) g_malloc0(1000000);
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
);
373 if(!g_spawn_command_line_sync(find_cmd
, &find_output
, NULL
, NULL
, &err
)) {
375 g_printf("error running `%s`\n", find_cmd
);
379 map_pairs
= g_strsplit(find_output
, "\n", INT_MAX
);
381 while(map_pairs
[i
] && (entry_buf
= g_strsplit(map_pairs
[i
], "=", INT_MAX
))) {
383 tz_table
[i
] = parse_timezone_path(entry_buf
);
385 g_strfreev(entry_buf
);
396 static gchar
*lookup_hash(gchar
*hash
) {
400 while(tz_table
[i
].sum
)
401 if(!g_strcmp0(tz_table
[i
].sum
, hash
))
402 return tz_table
[i
].path
;