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> |
1066bd36 |
27 | #include <glib/gstdio.h> |
904d744d |
28 | #include <polkit/polkit.h> |
b6ad18ad |
29 | |
1e8c7c88 |
30 | #include "timedated-gen.h" |
a33bcdfa |
31 | #include "timedated.h" |
1e8c7c88 |
32 | |
ed4cf3c8 |
33 | #include "../../util.h" |
483e90b7 |
34 | |
b6ad18ad |
35 | GPtrArray *timedated_freeable; |
36 | Timedate1 *timedated_interf; |
37 | |
c12c41f4 |
38 | GMainLoop *timedated_loop; |
39 | |
40 | guint bus_descriptor; |
41 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
42 | |
1066bd36 |
43 | const gchar *OS_LOCALTIME = "/etc/localtime"; /* current timezone file */ |
44 | const gchar *OS_TIMEZONE_PATH = "/usr/share/zoneinfo"; /* path to system timezone files */ |
45 | |
1ce41045 |
46 | /* --- begin method/property/dbus signal code --- */ |
b6ad18ad |
47 | |
28b86015 |
48 | static gboolean |
49 | on_handle_set_time(Timedate1 *hn1_passed_interf, |
50 | GDBusMethodInvocation *invoc, |
51 | const gchar *greet, |
52 | gpointer data) { |
b6ad18ad |
53 | return FALSE; |
28b86015 |
54 | } |
b6ad18ad |
55 | |
28b86015 |
56 | static gboolean |
57 | on_handle_set_timezone(Timedate1 *hn1_passed_interf, |
58 | GDBusMethodInvocation *invoc, |
59 | const gchar *greet, |
60 | gpointer data) { |
61 | return FALSE; |
62 | } |
63 | |
64 | static gboolean |
65 | on_handle_set_local_rtc(Timedate1 *hn1_passed_interf, |
66 | GDBusMethodInvocation *invoc, |
67 | const gchar *greet, |
68 | gpointer data) { |
69 | return FALSE; |
70 | } |
71 | |
72 | static gboolean |
73 | on_handle_set_ntp(Timedate1 *hn1_passed_interf, |
74 | GDBusMethodInvocation *invoc, |
75 | const gchar *greet, |
76 | gpointer data) { |
77 | return FALSE; |
78 | } |
79 | |
80 | const gchar * |
81 | our_get_timezone() { |
82 | |
1066bd36 |
83 | GStatBuf *stat_zoneinfo; |
84 | gchar *find_cmd, *readlink_path, *ret; |
85 | GError *err = NULL; |
86 | |
87 | find_cmd = (gchar *) g_malloc0(2048); |
88 | stat_zoneinfo = (GStatBuf*) g_malloc0(8192); |
89 | |
90 | if(g_stat(OS_LOCALTIME, stat_zoneinfo)) { |
91 | |
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); |
93 | ret = NULL; |
94 | |
95 | } else if(g_file_test(OS_LOCALTIME, G_FILE_TEST_IS_SYMLINK)) { |
96 | |
97 | readlink_path = g_file_read_link(OS_LOCALTIME, &err); |
98 | ret = parse_timezone_path(readlink_path); |
99 | |
100 | if(readlink_path) |
101 | g_free(readlink_path); |
102 | |
103 | } else { |
104 | |
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); |
107 | ret = NULL; |
108 | } |
109 | |
110 | return ret; |
28b86015 |
111 | } |
112 | |
113 | gboolean |
114 | our_get_local_rtc() { |
115 | |
116 | gboolean ret = FALSE; |
117 | |
118 | return ret; |
119 | } |
120 | |
121 | gboolean |
122 | our_get_can_ntp() { |
123 | |
124 | const gboolean ret = FALSE; |
125 | |
126 | return ret; |
127 | } |
128 | |
129 | gboolean |
130 | our_get_ntp() { |
131 | |
132 | const gboolean ret = FALSE; |
133 | |
134 | return ret; |
135 | } |
b6ad18ad |
136 | |
28b86015 |
137 | gboolean |
138 | our_get_ntpsynchronized() { |
139 | |
140 | const gboolean ret = FALSE; |
b6ad18ad |
141 | |
28b86015 |
142 | return ret; |
143 | } |
b6ad18ad |
144 | |
28b86015 |
145 | guint64 |
146 | our_get_time_usec() { |
b6ad18ad |
147 | |
28b86015 |
148 | guint64 ret = 0; |
b6ad18ad |
149 | |
28b86015 |
150 | return ret; |
151 | } |
152 | |
153 | guint64 |
154 | our_get_rtc_time_usec() { |
155 | |
156 | guint64 ret = 0; |
157 | |
158 | return ret; |
159 | } |
b6ad18ad |
160 | |
1ce41045 |
161 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
b6ad18ad |
162 | |
163 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
164 | const gchar *name, |
165 | gpointer user_data) { |
166 | |
254ceec0 |
167 | g_printf("got bus/name, exporting %s's interface...\n", name); |
b6ad18ad |
168 | |
169 | timedated_interf = timedate1_skeleton_new(); |
170 | |
28b86015 |
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()); |
184 | |
b6ad18ad |
185 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
186 | conn, |
187 | "/org/freedesktop/timedate1", |
188 | NULL)) { |
189 | |
509599f0 |
190 | g_printf("failed to export %s's interface!\n", name); |
191 | timedated_mem_clean(); |
192 | |
193 | } else { |
194 | |
195 | dbus_interface_exported = TRUE; |
196 | g_printf("exported %s's interface on the system bus...\n", name); |
b6ad18ad |
197 | } |
509599f0 |
198 | } |
199 | |
200 | static void timedated_on_name_acquired(GDBusConnection *conn, |
90f54407 |
201 | const gchar *name, |
509599f0 |
202 | gpointer user_data) { |
b6ad18ad |
203 | |
509599f0 |
204 | g_printf("success!\n"); |
b6ad18ad |
205 | } |
206 | |
b6ad18ad |
207 | static void timedated_on_name_lost(GDBusConnection *conn, |
208 | const gchar *name, |
209 | gpointer user_data) { |
210 | |
509599f0 |
211 | if(!conn) { |
b6ad18ad |
212 | |
509599f0 |
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(); |
215 | } |
b6ad18ad |
216 | |
509599f0 |
217 | g_print("lost name %s, exiting...\n", name); |
218 | |
219 | timedated_mem_clean(); |
b6ad18ad |
220 | } |
221 | |
b70beb08 |
222 | /* --- end bus/name handlers, begin misc unix functions --- */ |
223 | |
509599f0 |
224 | /* safe call to clean and then exit |
225 | * this stops our GMainLoop safely before letting main() return */ |
b70beb08 |
226 | void timedated_mem_clean() { |
227 | |
341587db |
228 | g_printf("exiting...\n"); |
229 | |
230 | if(dbus_interface_exported) |
231 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf)); |
232 | |
233 | if(g_main_loop_is_running(timedated_loop)) |
234 | g_main_loop_quit(timedated_loop); |
235 | |
b70beb08 |
236 | } |
237 | |
712eb329 |
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) { |
240 | |
241 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
242 | |
243 | timedated_mem_clean(); |
244 | return G_SOURCE_REMOVE; |
245 | } |
246 | |
2ef2cfe1 |
247 | void set_signal_handlers() { |
248 | |
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); |
253 | } |
254 | |
1e8c7c88 |
255 | int main() { |
b6ad18ad |
256 | |
9728ae1f |
257 | set_signal_handlers(); |
258 | |
90f54407 |
259 | timedated_loop = g_main_loop_new(NULL, TRUE); |
260 | timedated_freeable = g_ptr_array_new(); |
b6ad18ad |
261 | |
90f54407 |
262 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
b6ad18ad |
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, |
268 | NULL, |
269 | NULL); |
270 | |
90f54407 |
271 | g_main_loop_run(timedated_loop); |
9728ae1f |
272 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
90f54407 |
273 | g_main_loop_unref(timedated_loop); |
b6ad18ad |
274 | |
9728ae1f |
275 | /* guaranteed unownable */ |
90f54407 |
276 | g_bus_unown_name(bus_descriptor); |
b6ad18ad |
277 | |
9728ae1f |
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); |
b6ad18ad |
280 | |
90f54407 |
281 | return 0; |
1e8c7c88 |
282 | } |
1066bd36 |
283 | |
284 | static gchar *parse_timezone_path(gchar *full_path) { |
285 | |
286 | gchar *prefix_pattern; |
287 | GRegex *prefix, *posix, *right; |
288 | GError *err = NULL; |
289 | |
290 | if(!full_path) |
291 | return NULL; |
292 | |
293 | prefix_pattern = (gchar *) g_malloc0(4096); |
294 | g_sprintf(prefix_pattern, "^%s/$", OS_TIMEZONE_PATH); |
295 | |
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); |
299 | |
300 | g_regex_unref(prefix); |
301 | g_regex_unref(right); |
302 | g_regex_unref(posix); |
303 | |
304 | return NULL; /* TODO temp */ |
305 | } |