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 /* --- begin method/property/dbus signal code --- */
49 on_handle_set_time(Timedate1
*hn1_passed_interf
,
50 GDBusMethodInvocation
*invoc
,
57 on_handle_set_timezone(Timedate1
*hn1_passed_interf
,
58 GDBusMethodInvocation
*invoc
,
65 on_handle_set_local_rtc(Timedate1
*hn1_passed_interf
,
66 GDBusMethodInvocation
*invoc
,
73 on_handle_set_ntp(Timedate1
*hn1_passed_interf
,
74 GDBusMethodInvocation
*invoc
,
83 GStatBuf
*stat_zoneinfo
;
84 gchar
*find_cmd
, *readlink_path
, *ret
;
87 find_cmd
= (gchar
*) g_malloc0(2048);
88 stat_zoneinfo
= (GStatBuf
*) g_malloc0(8192);
90 if(g_stat(OS_LOCALTIME
, stat_zoneinfo
)) {
92 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
);
95 } else if(g_file_test(OS_LOCALTIME
, G_FILE_TEST_IS_SYMLINK
)) {
97 readlink_path
= g_file_read_link(OS_LOCALTIME
, &err
);
98 ret
= parse_timezone_path(readlink_path
);
101 g_free(readlink_path
);
105 g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME
, OS_TIMEZONE_PATH
);
106 g_sprintf(find_cmd
, "find %s -type f", OS_TIMEZONE_PATH
);
114 our_get_local_rtc() {
116 gboolean ret
= FALSE
;
124 const gboolean ret
= FALSE
;
132 const gboolean ret
= FALSE
;
138 our_get_ntpsynchronized() {
140 const gboolean ret
= FALSE
;
146 our_get_time_usec() {
154 our_get_rtc_time_usec() {
161 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
163 static void timedated_on_bus_acquired(GDBusConnection
*conn
,
165 gpointer user_data
) {
167 g_printf("got bus/name, exporting %s's interface...\n", name
);
169 timedated_interf
= timedate1_skeleton_new();
171 /* attach function pointers to generated struct's method handlers */
172 g_signal_connect(timedated_interf
, "handle-set-time", G_CALLBACK(on_handle_set_time
), NULL
);
173 g_signal_connect(timedated_interf
, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone
), NULL
);
174 g_signal_connect(timedated_interf
, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc
), NULL
);
175 g_signal_connect(timedated_interf
, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp
), NULL
);
176 /* set our properties before export */
177 timedate1_set_timezone(timedated_interf
, our_get_timezone());
178 timedate1_set_local_rtc(timedated_interf
, our_get_local_rtc());
179 timedate1_set_can_ntp(timedated_interf
, our_get_can_ntp());
180 timedate1_set_ntp(timedated_interf
, our_get_ntp());
181 timedate1_set_ntpsynchronized(timedated_interf
, our_get_ntpsynchronized());
182 timedate1_set_time_usec(timedated_interf
, our_get_time_usec());
183 timedate1_set_rtctime_usec(timedated_interf
, our_get_rtc_time_usec());
185 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf
),
187 "/org/freedesktop/timedate1",
190 g_printf("failed to export %s's interface!\n", name
);
191 timedated_mem_clean();
195 dbus_interface_exported
= TRUE
;
196 g_printf("exported %s's interface on the system bus...\n", name
);
200 static void timedated_on_name_acquired(GDBusConnection
*conn
,
202 gpointer user_data
) {
204 g_printf("success!\n");
207 static void timedated_on_name_lost(GDBusConnection
*conn
,
209 gpointer user_data
) {
213 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
);
214 timedated_mem_clean();
217 g_print("lost name %s, exiting...\n", name
);
219 timedated_mem_clean();
222 /* --- end bus/name handlers, begin misc unix functions --- */
224 /* safe call to clean and then exit
225 * this stops our GMainLoop safely before letting main() return */
226 void timedated_mem_clean() {
228 g_printf("exiting...\n");
230 if(dbus_interface_exported
)
231 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf
));
233 if(g_main_loop_is_running(timedated_loop
))
234 g_main_loop_quit(timedated_loop
);
238 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
239 gboolean
unix_sig_terminate_handler(gpointer data
) {
241 g_printf("caught SIGINT/HUP/TERM, exiting\n");
243 timedated_mem_clean();
244 return G_SOURCE_REMOVE
;
247 void set_signal_handlers() {
249 /* we don't care about its descriptor, we never need to unregister these */
250 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
251 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
252 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
257 set_signal_handlers();
259 timedated_loop
= g_main_loop_new(NULL
, TRUE
);
260 timedated_freeable
= g_ptr_array_new();
262 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
263 "org.freedesktop.timedate1",
264 G_BUS_NAME_OWNER_FLAGS_NONE
,
265 timedated_on_bus_acquired
,
266 timedated_on_name_acquired
,
267 timedated_on_name_lost
,
271 g_main_loop_run(timedated_loop
);
272 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
273 g_main_loop_unref(timedated_loop
);
275 /* guaranteed unownable */
276 g_bus_unown_name(bus_descriptor
);
278 /* at this point no operations can occur with our data, it is safe to free it + its container */
279 g_ptr_array_free(timedated_freeable
, TRUE
);
284 static gchar
*parse_timezone_path(gchar
*full_path
) {
286 gchar
*prefix_pattern
;
287 GRegex
*prefix
, *posix
, *right
;
293 prefix_pattern
= (gchar
*) g_malloc0(4096);
294 g_sprintf(prefix_pattern
, "^%s/$", OS_TIMEZONE_PATH
);
296 prefix
= g_regex_new(prefix_pattern
, 0, 0, &err
);
297 posix
= g_regex_new("^posix/$", 0, 0, &err
);
298 right
= g_regex_new("^right/$", 0, 0, &err
);
300 g_regex_unref(prefix
);
301 g_regex_unref(right
);
302 g_regex_unref(posix
);
304 return NULL
; /* TODO temp */