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/param.h>
24 #include <glib/gprintf.h>
25 #include <glib-unix.h>
26 #include <polkit/polkit.h>
28 #include "timedated-gen.h"
29 #include "timedated.h"
31 #include "../../util.h"
33 GPtrArray
*timedated_freeable
;
34 Timedate1
*timedated_interf
;
36 GMainLoop
*timedated_loop
;
39 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
41 /* --- begin method/property/dbus signal code --- */
44 on_handle_set_time(Timedate1
*hn1_passed_interf
,
45 GDBusMethodInvocation
*invoc
,
52 on_handle_set_timezone(Timedate1
*hn1_passed_interf
,
53 GDBusMethodInvocation
*invoc
,
60 on_handle_set_local_rtc(Timedate1
*hn1_passed_interf
,
61 GDBusMethodInvocation
*invoc
,
68 on_handle_set_ntp(Timedate1
*hn1_passed_interf
,
69 GDBusMethodInvocation
*invoc
,
92 const gboolean ret
= FALSE
;
100 const gboolean ret
= FALSE
;
106 our_get_ntpsynchronized() {
108 const gboolean ret
= FALSE
;
114 our_get_time_usec() {
122 our_get_rtc_time_usec() {
129 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
131 static void timedated_on_bus_acquired(GDBusConnection
*conn
,
133 gpointer user_data
) {
135 g_printf("got bus/name, exporting %s's interface...\n", name
);
137 timedated_interf
= timedate1_skeleton_new();
139 /* attach function pointers to generated struct's method handlers */
140 g_signal_connect(timedated_interf
, "handle-set-time", G_CALLBACK(on_handle_set_time
), NULL
);
141 g_signal_connect(timedated_interf
, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone
), NULL
);
142 g_signal_connect(timedated_interf
, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc
), NULL
);
143 g_signal_connect(timedated_interf
, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp
), NULL
);
144 /* set our properties before export */
145 timedate1_set_timezone(timedated_interf
, our_get_timezone());
146 timedate1_set_local_rtc(timedated_interf
, our_get_local_rtc());
147 timedate1_set_can_ntp(timedated_interf
, our_get_can_ntp());
148 timedate1_set_ntp(timedated_interf
, our_get_ntp());
149 timedate1_set_ntpsynchronized(timedated_interf
, our_get_ntpsynchronized());
150 timedate1_set_time_usec(timedated_interf
, our_get_time_usec());
151 timedate1_set_rtctime_usec(timedated_interf
, our_get_rtc_time_usec());
153 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf
),
155 "/org/freedesktop/timedate1",
158 g_printf("failed to export %s's interface!\n", name
);
159 timedated_mem_clean();
163 dbus_interface_exported
= TRUE
;
164 g_printf("exported %s's interface on the system bus...\n", name
);
168 static void timedated_on_name_acquired(GDBusConnection
*conn
,
170 gpointer user_data
) {
172 g_printf("success!\n");
175 static void timedated_on_name_lost(GDBusConnection
*conn
,
177 gpointer user_data
) {
181 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
);
182 timedated_mem_clean();
185 g_print("lost name %s, exiting...\n", name
);
187 timedated_mem_clean();
190 /* --- end bus/name handlers, begin misc unix functions --- */
192 /* safe call to clean and then exit
193 * this stops our GMainLoop safely before letting main() return */
194 void timedated_mem_clean() {
196 g_printf("exiting...\n");
198 if(dbus_interface_exported
)
199 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf
));
201 if(g_main_loop_is_running(timedated_loop
))
202 g_main_loop_quit(timedated_loop
);
206 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
207 gboolean
unix_sig_terminate_handler(gpointer data
) {
209 g_printf("caught SIGINT/HUP/TERM, exiting\n");
211 timedated_mem_clean();
212 return G_SOURCE_REMOVE
;
215 void set_signal_handlers() {
217 /* we don't care about its descriptor, we never need to unregister these */
218 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
219 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
220 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
225 set_signal_handlers();
227 timedated_loop
= g_main_loop_new(NULL
, TRUE
);
228 timedated_freeable
= g_ptr_array_new();
230 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
231 "org.freedesktop.timedate1",
232 G_BUS_NAME_OWNER_FLAGS_NONE
,
233 timedated_on_bus_acquired
,
234 timedated_on_name_acquired
,
235 timedated_on_name_lost
,
239 g_main_loop_run(timedated_loop
);
240 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
241 g_main_loop_unref(timedated_loop
);
243 /* guaranteed unownable */
244 g_bus_unown_name(bus_descriptor
);
246 /* at this point no operations can occur with our data, it is safe to free it + its container */
247 g_ptr_array_free(timedated_freeable
, TRUE
);