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 | |
a3d2e50f |
21 | #include <sys/types.h> |
22 | #include <time.h> |
b6ad18ad |
23 | #include <string.h> |
24 | |
25 | #include <glib/gprintf.h> |
8caf1f61 |
26 | #include <glib-unix.h> |
904d744d |
27 | #include <polkit/polkit.h> |
b6ad18ad |
28 | |
1e8c7c88 |
29 | #include "timedated-gen.h" |
a33bcdfa |
30 | #include "timedated.h" |
1e8c7c88 |
31 | |
ed4cf3c8 |
32 | #include "../../util.h" |
483e90b7 |
33 | |
b6ad18ad |
34 | GPtrArray *timedated_freeable; |
35 | Timedate1 *timedated_interf; |
36 | |
c12c41f4 |
37 | GMainLoop *timedated_loop; |
38 | |
39 | guint bus_descriptor; |
40 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
41 | |
1ce41045 |
42 | /* --- begin method/property/dbus signal code --- */ |
b6ad18ad |
43 | |
28b86015 |
44 | static gboolean |
45 | on_handle_set_time(Timedate1 *hn1_passed_interf, |
46 | GDBusMethodInvocation *invoc, |
47 | const gchar *greet, |
48 | gpointer data) { |
b6ad18ad |
49 | return FALSE; |
28b86015 |
50 | } |
b6ad18ad |
51 | |
28b86015 |
52 | static gboolean |
53 | on_handle_set_timezone(Timedate1 *hn1_passed_interf, |
54 | GDBusMethodInvocation *invoc, |
55 | const gchar *greet, |
56 | gpointer data) { |
57 | return FALSE; |
58 | } |
59 | |
60 | static gboolean |
61 | on_handle_set_local_rtc(Timedate1 *hn1_passed_interf, |
62 | GDBusMethodInvocation *invoc, |
63 | const gchar *greet, |
64 | gpointer data) { |
65 | return FALSE; |
66 | } |
67 | |
68 | static gboolean |
69 | on_handle_set_ntp(Timedate1 *hn1_passed_interf, |
70 | GDBusMethodInvocation *invoc, |
71 | const gchar *greet, |
72 | gpointer data) { |
73 | return FALSE; |
74 | } |
75 | |
76 | const gchar * |
77 | our_get_timezone() { |
78 | |
79 | return ""; |
80 | } |
81 | |
82 | gboolean |
83 | our_get_local_rtc() { |
84 | |
85 | gboolean ret = FALSE; |
86 | |
87 | return ret; |
88 | } |
89 | |
90 | gboolean |
91 | our_get_can_ntp() { |
92 | |
93 | const gboolean ret = FALSE; |
94 | |
95 | return ret; |
96 | } |
97 | |
98 | gboolean |
99 | our_get_ntp() { |
100 | |
101 | const gboolean ret = FALSE; |
102 | |
103 | return ret; |
104 | } |
b6ad18ad |
105 | |
28b86015 |
106 | gboolean |
107 | our_get_ntpsynchronized() { |
108 | |
109 | const gboolean ret = FALSE; |
b6ad18ad |
110 | |
28b86015 |
111 | return ret; |
112 | } |
b6ad18ad |
113 | |
28b86015 |
114 | guint64 |
115 | our_get_time_usec() { |
b6ad18ad |
116 | |
28b86015 |
117 | guint64 ret = 0; |
b6ad18ad |
118 | |
28b86015 |
119 | return ret; |
120 | } |
121 | |
122 | guint64 |
123 | our_get_rtc_time_usec() { |
124 | |
125 | guint64 ret = 0; |
126 | |
127 | return ret; |
128 | } |
b6ad18ad |
129 | |
1ce41045 |
130 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
b6ad18ad |
131 | |
132 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
133 | const gchar *name, |
134 | gpointer user_data) { |
135 | |
254ceec0 |
136 | g_printf("got bus/name, exporting %s's interface...\n", name); |
b6ad18ad |
137 | |
138 | timedated_interf = timedate1_skeleton_new(); |
139 | |
28b86015 |
140 | /* attach function pointers to generated struct's method handlers */ |
141 | g_signal_connect(timedated_interf, "handle-set-time", G_CALLBACK(on_handle_set_time), NULL); |
142 | g_signal_connect(timedated_interf, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone), NULL); |
143 | g_signal_connect(timedated_interf, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc), NULL); |
144 | g_signal_connect(timedated_interf, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp), NULL); |
145 | /* set our properties before export */ |
146 | timedate1_set_timezone(timedated_interf, our_get_timezone()); |
147 | timedate1_set_local_rtc(timedated_interf, our_get_local_rtc()); |
148 | timedate1_set_can_ntp(timedated_interf, our_get_can_ntp()); |
149 | timedate1_set_ntp(timedated_interf, our_get_ntp()); |
150 | timedate1_set_ntpsynchronized(timedated_interf, our_get_ntpsynchronized()); |
151 | timedate1_set_time_usec(timedated_interf, our_get_time_usec()); |
152 | timedate1_set_rtctime_usec(timedated_interf, our_get_rtc_time_usec()); |
153 | |
b6ad18ad |
154 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
155 | conn, |
156 | "/org/freedesktop/timedate1", |
157 | NULL)) { |
158 | |
509599f0 |
159 | g_printf("failed to export %s's interface!\n", name); |
160 | timedated_mem_clean(); |
161 | |
162 | } else { |
163 | |
164 | dbus_interface_exported = TRUE; |
165 | g_printf("exported %s's interface on the system bus...\n", name); |
b6ad18ad |
166 | } |
509599f0 |
167 | } |
168 | |
169 | static void timedated_on_name_acquired(GDBusConnection *conn, |
90f54407 |
170 | const gchar *name, |
509599f0 |
171 | gpointer user_data) { |
b6ad18ad |
172 | |
509599f0 |
173 | g_printf("success!\n"); |
b6ad18ad |
174 | } |
175 | |
b6ad18ad |
176 | static void timedated_on_name_lost(GDBusConnection *conn, |
177 | const gchar *name, |
178 | gpointer user_data) { |
179 | |
509599f0 |
180 | if(!conn) { |
b6ad18ad |
181 | |
509599f0 |
182 | 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); |
183 | timedated_mem_clean(); |
184 | } |
b6ad18ad |
185 | |
509599f0 |
186 | g_print("lost name %s, exiting...\n", name); |
187 | |
188 | timedated_mem_clean(); |
b6ad18ad |
189 | } |
190 | |
b70beb08 |
191 | /* --- end bus/name handlers, begin misc unix functions --- */ |
192 | |
509599f0 |
193 | /* safe call to clean and then exit |
194 | * this stops our GMainLoop safely before letting main() return */ |
b70beb08 |
195 | void timedated_mem_clean() { |
196 | |
341587db |
197 | g_printf("exiting...\n"); |
198 | |
199 | if(dbus_interface_exported) |
200 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf)); |
201 | |
202 | if(g_main_loop_is_running(timedated_loop)) |
203 | g_main_loop_quit(timedated_loop); |
204 | |
b70beb08 |
205 | } |
206 | |
712eb329 |
207 | /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */ |
208 | gboolean unix_sig_terminate_handler(gpointer data) { |
209 | |
210 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
211 | |
212 | timedated_mem_clean(); |
213 | return G_SOURCE_REMOVE; |
214 | } |
215 | |
2ef2cfe1 |
216 | void set_signal_handlers() { |
217 | |
218 | /* we don't care about its descriptor, we never need to unregister these */ |
219 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
220 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
221 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
222 | } |
223 | |
1e8c7c88 |
224 | int main() { |
b6ad18ad |
225 | |
9728ae1f |
226 | set_signal_handlers(); |
227 | |
90f54407 |
228 | timedated_loop = g_main_loop_new(NULL, TRUE); |
229 | timedated_freeable = g_ptr_array_new(); |
b6ad18ad |
230 | |
90f54407 |
231 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
b6ad18ad |
232 | "org.freedesktop.timedate1", |
233 | G_BUS_NAME_OWNER_FLAGS_NONE, |
234 | timedated_on_bus_acquired, |
235 | timedated_on_name_acquired, |
236 | timedated_on_name_lost, |
237 | NULL, |
238 | NULL); |
239 | |
90f54407 |
240 | g_main_loop_run(timedated_loop); |
9728ae1f |
241 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
90f54407 |
242 | g_main_loop_unref(timedated_loop); |
b6ad18ad |
243 | |
9728ae1f |
244 | /* guaranteed unownable */ |
90f54407 |
245 | g_bus_unown_name(bus_descriptor); |
b6ad18ad |
246 | |
9728ae1f |
247 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
248 | g_ptr_array_free(timedated_freeable, TRUE); |
b6ad18ad |
249 | |
90f54407 |
250 | return 0; |
1e8c7c88 |
251 | } |