add get_file_sha256(path) to util.c
[systembsd.git] / src / interfaces / timedated / timedated.c
CommitLineData
3b82e3c1 1/*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
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.
7 *
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.
15 */
16
b6ad18ad 17#include <unistd.h>
18#include <limits.h>
c12c41f4 19#include <signal.h>
b6ad18ad 20
21#include <sys/param.h>
22#include <string.h>
23
24#include <glib/gprintf.h>
8caf1f61 25#include <glib-unix.h>
904d744d 26#include <polkit/polkit.h>
b6ad18ad 27
1e8c7c88 28#include "timedated-gen.h"
a33bcdfa 29#include "timedated.h"
1e8c7c88 30
ed4cf3c8 31#include "../../util.h"
483e90b7 32
b6ad18ad 33GPtrArray *timedated_freeable;
34Timedate1 *timedated_interf;
35
c12c41f4 36GMainLoop *timedated_loop;
37
38guint bus_descriptor;
39gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
40
1ce41045 41/* --- begin method/property/dbus signal code --- */
b6ad18ad 42
28b86015 43static gboolean
44on_handle_set_time(Timedate1 *hn1_passed_interf,
45 GDBusMethodInvocation *invoc,
46 const gchar *greet,
47 gpointer data) {
b6ad18ad 48 return FALSE;
28b86015 49}
b6ad18ad 50
28b86015 51static gboolean
52on_handle_set_timezone(Timedate1 *hn1_passed_interf,
53 GDBusMethodInvocation *invoc,
54 const gchar *greet,
55 gpointer data) {
56 return FALSE;
57}
58
59static gboolean
60on_handle_set_local_rtc(Timedate1 *hn1_passed_interf,
61 GDBusMethodInvocation *invoc,
62 const gchar *greet,
63 gpointer data) {
64 return FALSE;
65}
66
67static gboolean
68on_handle_set_ntp(Timedate1 *hn1_passed_interf,
69 GDBusMethodInvocation *invoc,
70 const gchar *greet,
71 gpointer data) {
72 return FALSE;
73}
74
75const gchar *
76our_get_timezone() {
77
78 return "";
79}
80
81gboolean
82our_get_local_rtc() {
83
84 gboolean ret = FALSE;
85
86 return ret;
87}
88
89gboolean
90our_get_can_ntp() {
91
92 const gboolean ret = FALSE;
93
94 return ret;
95}
96
97gboolean
98our_get_ntp() {
99
100 const gboolean ret = FALSE;
101
102 return ret;
103}
b6ad18ad 104
28b86015 105gboolean
106our_get_ntpsynchronized() {
107
108 const gboolean ret = FALSE;
b6ad18ad 109
28b86015 110 return ret;
111}
b6ad18ad 112
28b86015 113guint64
114our_get_time_usec() {
b6ad18ad 115
28b86015 116 guint64 ret = 0;
b6ad18ad 117
28b86015 118 return ret;
119}
120
121guint64
122our_get_rtc_time_usec() {
123
124 guint64 ret = 0;
125
126 return ret;
127}
b6ad18ad 128
1ce41045 129/* --- end method/property/dbus signal code, begin bus/name handlers --- */
b6ad18ad 130
131static void timedated_on_bus_acquired(GDBusConnection *conn,
132 const gchar *name,
133 gpointer user_data) {
134
254ceec0 135 g_printf("got bus/name, exporting %s's interface...\n", name);
b6ad18ad 136
137 timedated_interf = timedate1_skeleton_new();
138
28b86015 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());
152
b6ad18ad 153 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf),
154 conn,
155 "/org/freedesktop/timedate1",
156 NULL)) {
157
509599f0 158 g_printf("failed to export %s's interface!\n", name);
159 timedated_mem_clean();
160
161 } else {
162
163 dbus_interface_exported = TRUE;
164 g_printf("exported %s's interface on the system bus...\n", name);
b6ad18ad 165 }
509599f0 166}
167
168static void timedated_on_name_acquired(GDBusConnection *conn,
90f54407 169 const gchar *name,
509599f0 170 gpointer user_data) {
b6ad18ad 171
509599f0 172 g_printf("success!\n");
b6ad18ad 173}
174
b6ad18ad 175static void timedated_on_name_lost(GDBusConnection *conn,
176 const gchar *name,
177 gpointer user_data) {
178
509599f0 179 if(!conn) {
b6ad18ad 180
509599f0 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();
183 }
b6ad18ad 184
509599f0 185 g_print("lost name %s, exiting...\n", name);
186
187 timedated_mem_clean();
b6ad18ad 188}
189
b70beb08 190/* --- end bus/name handlers, begin misc unix functions --- */
191
509599f0 192/* safe call to clean and then exit
193 * this stops our GMainLoop safely before letting main() return */
b70beb08 194void timedated_mem_clean() {
195
341587db 196 g_printf("exiting...\n");
197
198 if(dbus_interface_exported)
199 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf));
200
201 if(g_main_loop_is_running(timedated_loop))
202 g_main_loop_quit(timedated_loop);
203
b70beb08 204}
205
712eb329 206/* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
207gboolean unix_sig_terminate_handler(gpointer data) {
208
209 g_printf("caught SIGINT/HUP/TERM, exiting\n");
210
211 timedated_mem_clean();
212 return G_SOURCE_REMOVE;
213}
214
2ef2cfe1 215void set_signal_handlers() {
216
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);
221}
222
1e8c7c88 223int main() {
b6ad18ad 224
9728ae1f 225 set_signal_handlers();
226
90f54407 227 timedated_loop = g_main_loop_new(NULL, TRUE);
228 timedated_freeable = g_ptr_array_new();
b6ad18ad 229
90f54407 230 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
b6ad18ad 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,
236 NULL,
237 NULL);
238
90f54407 239 g_main_loop_run(timedated_loop);
9728ae1f 240 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
90f54407 241 g_main_loop_unref(timedated_loop);
b6ad18ad 242
9728ae1f 243 /* guaranteed unownable */
90f54407 244 g_bus_unown_name(bus_descriptor);
b6ad18ad 245
9728ae1f 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);
b6ad18ad 248
90f54407 249 return 0;
1e8c7c88 250}