| 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 | |
| 17 | #include <unistd.h> |
| 18 | #include <limits.h> |
| 19 | #include <signal.h> |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <time.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include <glib/gprintf.h> |
| 26 | #include <glib-unix.h> |
| 27 | #include <glib/gstdio.h> |
| 28 | #include <polkit/polkit.h> |
| 29 | |
| 30 | #include "timedated-gen.h" |
| 31 | #include "timedated.h" |
| 32 | |
| 33 | #include "../../util.h" |
| 34 | |
| 35 | GPtrArray *timedated_freeable; |
| 36 | Timedate1 *timedated_interf; |
| 37 | |
| 38 | GMainLoop *timedated_loop; |
| 39 | |
| 40 | guint bus_descriptor; |
| 41 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
| 42 | |
| 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 | |
| 46 | /* --- begin method/property/dbus signal code --- */ |
| 47 | |
| 48 | static gboolean |
| 49 | on_handle_set_time(Timedate1 *hn1_passed_interf, |
| 50 | GDBusMethodInvocation *invoc, |
| 51 | const gchar *greet, |
| 52 | gpointer data) { |
| 53 | return FALSE; |
| 54 | } |
| 55 | |
| 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 | |
| 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; |
| 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 | } |
| 136 | |
| 137 | gboolean |
| 138 | our_get_ntpsynchronized() { |
| 139 | |
| 140 | const gboolean ret = FALSE; |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | guint64 |
| 146 | our_get_time_usec() { |
| 147 | |
| 148 | guint64 ret = 0; |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | guint64 |
| 154 | our_get_rtc_time_usec() { |
| 155 | |
| 156 | guint64 ret = 0; |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
| 162 | |
| 163 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
| 164 | const gchar *name, |
| 165 | gpointer user_data) { |
| 166 | |
| 167 | g_printf("got bus/name, exporting %s's interface...\n", name); |
| 168 | |
| 169 | timedated_interf = timedate1_skeleton_new(); |
| 170 | |
| 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 | |
| 185 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
| 186 | conn, |
| 187 | "/org/freedesktop/timedate1", |
| 188 | NULL)) { |
| 189 | |
| 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); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static void timedated_on_name_acquired(GDBusConnection *conn, |
| 201 | const gchar *name, |
| 202 | gpointer user_data) { |
| 203 | |
| 204 | g_printf("success!\n"); |
| 205 | } |
| 206 | |
| 207 | static void timedated_on_name_lost(GDBusConnection *conn, |
| 208 | const gchar *name, |
| 209 | gpointer user_data) { |
| 210 | |
| 211 | if(!conn) { |
| 212 | |
| 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 | } |
| 216 | |
| 217 | g_print("lost name %s, exiting...\n", name); |
| 218 | |
| 219 | timedated_mem_clean(); |
| 220 | } |
| 221 | |
| 222 | /* --- end bus/name handlers, begin misc unix functions --- */ |
| 223 | |
| 224 | /* safe call to clean and then exit |
| 225 | * this stops our GMainLoop safely before letting main() return */ |
| 226 | void timedated_mem_clean() { |
| 227 | |
| 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 | |
| 236 | } |
| 237 | |
| 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 | |
| 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 | |
| 255 | int main() { |
| 256 | |
| 257 | set_signal_handlers(); |
| 258 | |
| 259 | timedated_loop = g_main_loop_new(NULL, TRUE); |
| 260 | timedated_freeable = g_ptr_array_new(); |
| 261 | |
| 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, |
| 268 | NULL, |
| 269 | NULL); |
| 270 | |
| 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); |
| 274 | |
| 275 | /* guaranteed unownable */ |
| 276 | g_bus_unown_name(bus_descriptor); |
| 277 | |
| 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); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 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 | } |