| 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 <sys/time.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <errno.h> |
| 25 | #include <time.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #include <glib/gprintf.h> |
| 29 | #include <glib-unix.h> |
| 30 | #include <glib/gstdio.h> |
| 31 | #include <polkit/polkit.h> |
| 32 | |
| 33 | #include "timedated-gen.h" |
| 34 | #include "timedated.h" |
| 35 | |
| 36 | #include "../../util.h" |
| 37 | |
| 38 | #define TZNAME_MAX PATH_MAX |
| 39 | |
| 40 | GPtrArray *timedated_freeable; |
| 41 | Timedate1 *timedated_interf; |
| 42 | |
| 43 | GMainLoop *timedated_loop; |
| 44 | |
| 45 | guint bus_descriptor; |
| 46 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
| 47 | |
| 48 | const gchar *OS_LOCALTIME = "/etc/localtime"; /* current timezone file */ |
| 49 | const gchar *OS_TIMEZONE_PATH = "/usr/share/zoneinfo"; /* path to system timezone files */ |
| 50 | |
| 51 | struct timezone_checksum_pair { |
| 52 | |
| 53 | gchar *path; |
| 54 | gchar *sum; |
| 55 | gboolean posix; |
| 56 | gboolean right; |
| 57 | }; |
| 58 | |
| 59 | static struct timezone_checksum_pair tz_table[5000]; |
| 60 | |
| 61 | /* --- begin method/property/dbus signal code --- */ |
| 62 | |
| 63 | static gboolean |
| 64 | on_handle_set_time(Timedate1 *td1_passed_interf, |
| 65 | GDBusMethodInvocation *invoc, |
| 66 | const gchar *greet, |
| 67 | gpointer data) { |
| 68 | |
| 69 | GVariant *params; |
| 70 | gint64 proposed_time, cur_time; |
| 71 | const gchar *bus_name; |
| 72 | gboolean policykit_auth; |
| 73 | check_auth_result is_authed; |
| 74 | gboolean relative; /* relative if passed time_t is meant to be added to current time */ |
| 75 | struct timespec *new_time; |
| 76 | |
| 77 | params = g_dbus_method_invocation_get_parameters(invoc); |
| 78 | g_variant_get(params, "(xbb)", &proposed_time, &relative, &policykit_auth); |
| 79 | bus_name = g_dbus_method_invocation_get_sender(invoc); |
| 80 | |
| 81 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.timedate1.set-time", policykit_auth); |
| 82 | |
| 83 | switch(is_authed) { |
| 84 | |
| 85 | case AUTHORIZED_NATIVELY: |
| 86 | case AUTHORIZED_BY_PROMPT: |
| 87 | break; |
| 88 | |
| 89 | case UNAUTHORIZED_NATIVELY: |
| 90 | case UNAUTHORIZED_FAILED_PROMPT: |
| 91 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EACCES", "Insufficient permissions to set system time."); |
| 92 | return FALSE; |
| 93 | |
| 94 | case ERROR_BADBUS: |
| 95 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided bus name is invalid."); |
| 96 | return FALSE; |
| 97 | |
| 98 | case ERROR_BADACTION: |
| 99 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided action ID is invalid."); |
| 100 | return FALSE; |
| 101 | |
| 102 | case ERROR_GENERIC: |
| 103 | default: |
| 104 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons."); |
| 105 | return FALSE; |
| 106 | } |
| 107 | |
| 108 | if(!proposed_time) { |
| 109 | |
| 110 | timedate1_complete_set_time(td1_passed_interf, invoc); |
| 111 | return TRUE; |
| 112 | |
| 113 | } else if(relative) { |
| 114 | |
| 115 | cur_time = g_get_real_time(); |
| 116 | |
| 117 | if(proposed_time < 0 && cur_time + proposed_time > cur_time) { |
| 118 | |
| 119 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds."); |
| 120 | return FALSE; |
| 121 | |
| 122 | } else if(proposed_time > 0 && cur_time + proposed_time < cur_time) { |
| 123 | |
| 124 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds."); |
| 125 | return FALSE; |
| 126 | } |
| 127 | |
| 128 | new_time = mktimespec(proposed_time); |
| 129 | |
| 130 | if(!clock_settime(CLOCK_REALTIME, new_time)) { |
| 131 | |
| 132 | timedate1_complete_set_time(td1_passed_interf, invoc); |
| 133 | return TRUE; |
| 134 | |
| 135 | } else { |
| 136 | |
| 137 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons."); |
| 138 | return FALSE; |
| 139 | } |
| 140 | |
| 141 | } else if(proposed_time > 0) { |
| 142 | |
| 143 | |
| 144 | new_time = mktimespec(proposed_time); |
| 145 | |
| 146 | if(!clock_settime(CLOCK_REALTIME, new_time)) { |
| 147 | |
| 148 | timedate1_complete_set_time(td1_passed_interf, invoc); |
| 149 | return TRUE; |
| 150 | |
| 151 | } else { |
| 152 | |
| 153 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set system time for unknown reasons."); |
| 154 | return FALSE; |
| 155 | } |
| 156 | |
| 157 | } else { |
| 158 | |
| 159 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EINVAL", "Resultant time out of bounds."); |
| 160 | return FALSE; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static gboolean |
| 165 | on_handle_set_timezone(Timedate1 *td1_passed_interf, |
| 166 | GDBusMethodInvocation *invoc, |
| 167 | const gchar *greet, |
| 168 | gpointer data) { |
| 169 | |
| 170 | GVariant *params; |
| 171 | gchar *proposed_tz; |
| 172 | const gchar *bus_name; |
| 173 | gboolean policykit_auth; |
| 174 | check_auth_result is_authed; |
| 175 | |
| 176 | gchar *tz_target_path; |
| 177 | struct stat *statbuf; |
| 178 | extern int errno; |
| 179 | |
| 180 | params = g_dbus_method_invocation_get_parameters(invoc); |
| 181 | g_variant_get(params, "(sb)", &proposed_tz, &policykit_auth); |
| 182 | bus_name = g_dbus_method_invocation_get_sender(invoc); |
| 183 | |
| 184 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.timedate1.set-timezone", policykit_auth); |
| 185 | |
| 186 | switch(is_authed) { |
| 187 | |
| 188 | case AUTHORIZED_NATIVELY: |
| 189 | case AUTHORIZED_BY_PROMPT: |
| 190 | break; |
| 191 | |
| 192 | case UNAUTHORIZED_NATIVELY: |
| 193 | case UNAUTHORIZED_FAILED_PROMPT: |
| 194 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EACCES", "Insufficient permissions to set timezone."); |
| 195 | return FALSE; |
| 196 | |
| 197 | case ERROR_BADBUS: |
| 198 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided bus name is invalid."); |
| 199 | return FALSE; |
| 200 | |
| 201 | case ERROR_BADACTION: |
| 202 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided action ID is invalid."); |
| 203 | return FALSE; |
| 204 | |
| 205 | case ERROR_GENERIC: |
| 206 | default: |
| 207 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to set timezone for unknown reasons."); |
| 208 | return FALSE; |
| 209 | } |
| 210 | |
| 211 | statbuf = (struct stat*) calloc(1, sizeof(struct stat)); |
| 212 | tz_target_path = (gchar *) calloc(1, TZNAME_MAX); |
| 213 | |
| 214 | g_ptr_array_add(timedated_freeable, statbuf); |
| 215 | g_ptr_array_add(timedated_freeable, tz_target_path); |
| 216 | |
| 217 | strlcat(tz_target_path, OS_TIMEZONE_PATH, TZNAME_MAX); |
| 218 | strlcat(tz_target_path, "/", TZNAME_MAX); |
| 219 | strlcat(tz_target_path, proposed_tz, TZNAME_MAX); |
| 220 | |
| 221 | if(strstr(tz_target_path, "../")) { |
| 222 | |
| 223 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EBADF", "Provided timezone is invalid."); |
| 224 | return FALSE; |
| 225 | } |
| 226 | |
| 227 | if(!statbuf) |
| 228 | return FALSE; |
| 229 | |
| 230 | if(lstat(tz_target_path, statbuf)) { |
| 231 | |
| 232 | switch(errno) { |
| 233 | |
| 234 | case ENOENT: |
| 235 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ENOENT", "Specified timezone does not exist."); |
| 236 | break; |
| 237 | |
| 238 | default: |
| 239 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EBADF", "Specified timezone is invalid."); |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | return FALSE; |
| 244 | } |
| 245 | |
| 246 | if(!S_ISREG(statbuf->st_mode)) { |
| 247 | |
| 248 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EBADF", "Specified path is of an inappropriate type."); |
| 249 | return FALSE; |
| 250 | } |
| 251 | |
| 252 | memset(statbuf, 0, sizeof statbuf); |
| 253 | |
| 254 | if(!lstat(OS_LOCALTIME, statbuf)) |
| 255 | if(remove(OS_LOCALTIME)) |
| 256 | return FALSE; |
| 257 | |
| 258 | if(symlink(tz_target_path, OS_LOCALTIME)) |
| 259 | return FALSE; |
| 260 | |
| 261 | |
| 262 | timedate1_complete_set_timezone(td1_passed_interf, invoc); |
| 263 | |
| 264 | return TRUE; |
| 265 | } |
| 266 | |
| 267 | static gboolean |
| 268 | on_handle_set_local_rtc(Timedate1 *td1_passed_interf, |
| 269 | GDBusMethodInvocation *invoc, |
| 270 | const gchar *greet, |
| 271 | gpointer data) { |
| 272 | |
| 273 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ENODEV", "Unix RTC must be in UTC."); |
| 274 | return TRUE; |
| 275 | } |
| 276 | |
| 277 | static gboolean |
| 278 | on_handle_set_ntp(Timedate1 *td1_passed_interf, |
| 279 | GDBusMethodInvocation *invoc, |
| 280 | const gchar *greet, |
| 281 | gpointer data) { |
| 282 | |
| 283 | GVariant *params; |
| 284 | const gchar *bus_name; |
| 285 | gboolean policykit_auth; |
| 286 | check_auth_result is_authed; |
| 287 | |
| 288 | /* revert to rcctl when 5.7 rolls around */ |
| 289 | gint ntpd_notrunning, ntpd_notenabled; /* this logic flip is due to rcctl returning 0 on success, |
| 290 | * in this case an error means ntpd is not running or not enabled */ |
| 291 | gboolean proposed_ntpstate; |
| 292 | GError *sh_errors; |
| 293 | |
| 294 | extern int errno; |
| 295 | |
| 296 | params = g_dbus_method_invocation_get_parameters(invoc); |
| 297 | g_variant_get(params, "(bb)", &proposed_ntpstate, &policykit_auth); |
| 298 | bus_name = g_dbus_method_invocation_get_sender(invoc); |
| 299 | |
| 300 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.timedate1.set-ntp", policykit_auth); |
| 301 | |
| 302 | switch(is_authed) { |
| 303 | |
| 304 | case AUTHORIZED_NATIVELY: |
| 305 | case AUTHORIZED_BY_PROMPT: |
| 306 | break; |
| 307 | |
| 308 | case UNAUTHORIZED_NATIVELY: |
| 309 | case UNAUTHORIZED_FAILED_PROMPT: |
| 310 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EACCES", "Insufficient permissions to toggle the NTP daemon."); |
| 311 | return FALSE; |
| 312 | |
| 313 | case ERROR_BADBUS: |
| 314 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided bus name is invalid."); |
| 315 | return FALSE; |
| 316 | |
| 317 | case ERROR_BADACTION: |
| 318 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.EFAULT", "Provided action ID is invalid."); |
| 319 | return FALSE; |
| 320 | |
| 321 | case ERROR_GENERIC: |
| 322 | default: |
| 323 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.timedate1.Error.ECANCELED", "Failed to toggle the NTP daemon for unknown reasons."); |
| 324 | return FALSE; |
| 325 | } |
| 326 | |
| 327 | ntpd_notrunning = 0; /* GLib does not bother asserting the passed return value int to zero */ |
| 328 | ntpd_notenabled = 0; /* if the program's exit status is also zero, hence this decl. */ |
| 329 | |
| 330 | if((ntpd_notrunning = system("/etc/rc.d/ntpd check > /dev/null 2>&1")) == -1) |
| 331 | return FALSE; |
| 332 | |
| 333 | if((ntpd_notenabled = system("/etc/rc.d/ntpd status > /dev/null 2>&1")) == -1) |
| 334 | return FALSE; |
| 335 | |
| 336 | if(proposed_ntpstate) { |
| 337 | |
| 338 | if(ntpd_notrunning) |
| 339 | system("/etc/rc.d/ntpd -f start > /dev/null 2>&1"); |
| 340 | |
| 341 | if(ntpd_notenabled) |
| 342 | system("/etc/rc.d/ntpd enable > /dev/null 2>&1"); |
| 343 | |
| 344 | } else { |
| 345 | |
| 346 | if(!ntpd_notrunning) |
| 347 | system("/etc/rc.d/ntpd stop > /dev/null 2>&1"); |
| 348 | |
| 349 | if(!ntpd_notenabled) |
| 350 | system("/etc/rc.d/ntpd disable > /dev/null 2>&1"); |
| 351 | } |
| 352 | |
| 353 | timedate1_complete_set_ntp(td1_passed_interf, invoc); |
| 354 | |
| 355 | return TRUE; |
| 356 | } |
| 357 | /* NOTE: you should be using gobject->set_property() for these ! */ |
| 358 | const gchar * |
| 359 | our_get_timezone() { |
| 360 | |
| 361 | GStatBuf *stat_zoneinfo; |
| 362 | gchar *find_cmd, *readlink_path, *ret, *argvp, *hash_to_match; |
| 363 | gint argcp; |
| 364 | GError *err; |
| 365 | struct timezone_checksum_pair tmp; |
| 366 | |
| 367 | find_cmd = (gchar *) g_malloc0(2048); |
| 368 | stat_zoneinfo = (GStatBuf *) g_malloc0(8192); |
| 369 | err = (GError *) g_malloc0(2048); |
| 370 | |
| 371 | if(g_stat(OS_LOCALTIME, stat_zoneinfo)) { |
| 372 | |
| 373 | 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); |
| 374 | ret = NULL; |
| 375 | |
| 376 | } else if(g_file_test(OS_LOCALTIME, G_FILE_TEST_IS_SYMLINK)) { |
| 377 | |
| 378 | readlink_path = g_file_read_link(OS_LOCALTIME, &err); |
| 379 | |
| 380 | gchar *split[2] = { readlink_path, "" }; |
| 381 | tmp = parse_timezone_path(split); |
| 382 | |
| 383 | ret = tmp.path; |
| 384 | |
| 385 | if(readlink_path) |
| 386 | g_free(readlink_path); |
| 387 | |
| 388 | } else { |
| 389 | |
| 390 | g_printf("%s is not a symlink! attempting to match checksums in %s...\n", OS_LOCALTIME, OS_TIMEZONE_PATH); |
| 391 | hash_to_match = get_file_sha256(OS_LOCALTIME); |
| 392 | |
| 393 | /* ret = lookup_hash(hash_to_match); */ |
| 394 | return FALSE; /* TODO fix me for real */ |
| 395 | |
| 396 | if(hash_to_match) |
| 397 | g_free(hash_to_match); |
| 398 | } |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | /* Unix time is in UTC. */ |
| 404 | gboolean |
| 405 | our_get_local_rtc() { |
| 406 | |
| 407 | return FALSE; |
| 408 | } |
| 409 | |
| 410 | gboolean |
| 411 | our_get_can_ntp() { |
| 412 | |
| 413 | /* ntpd is part of the default install */ |
| 414 | |
| 415 | return TRUE; |
| 416 | } |
| 417 | |
| 418 | gboolean |
| 419 | our_get_ntp() { |
| 420 | |
| 421 | int system_ret; |
| 422 | |
| 423 | system_ret = system("/etc/rc.d/ntpd check > /dev/null 2>&1"); |
| 424 | |
| 425 | if(system_ret) |
| 426 | return FALSE; |
| 427 | |
| 428 | return TRUE; |
| 429 | } |
| 430 | |
| 431 | /* undocumented feature present in systemd */ |
| 432 | gboolean |
| 433 | our_get_ntpsynchronized() { |
| 434 | |
| 435 | gboolean ntp; |
| 436 | ntp = our_get_ntp(); |
| 437 | |
| 438 | return ntp; |
| 439 | } |
| 440 | |
| 441 | /* undocumented feature present in systemd */ |
| 442 | guint64 |
| 443 | our_get_time_usec() { |
| 444 | |
| 445 | guint64 ret = 0; |
| 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | /* undocumented feature present in systemd */ |
| 451 | guint64 |
| 452 | our_get_rtc_time_usec() { |
| 453 | |
| 454 | guint64 ret = 0; |
| 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
| 460 | |
| 461 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
| 462 | const gchar *name, |
| 463 | gpointer user_data) { |
| 464 | |
| 465 | g_printf("got bus/name, exporting %s's interface...\n", name); |
| 466 | |
| 467 | timedated_interf = timedate1_skeleton_new(); |
| 468 | |
| 469 | /* attach function pointers to generated struct's method handlers */ |
| 470 | g_signal_connect(timedated_interf, "handle-set-time", G_CALLBACK(on_handle_set_time), NULL); |
| 471 | g_signal_connect(timedated_interf, "handle-set-timezone", G_CALLBACK(on_handle_set_timezone), NULL); |
| 472 | g_signal_connect(timedated_interf, "handle-set-local-rtc", G_CALLBACK(on_handle_set_local_rtc), NULL); |
| 473 | g_signal_connect(timedated_interf, "handle-set-ntp", G_CALLBACK(on_handle_set_ntp), NULL); |
| 474 | |
| 475 | /* set our properties before export */ |
| 476 | |
| 477 | timedate1_set_timezone(timedated_interf, our_get_timezone()); |
| 478 | timedate1_set_local_rtc(timedated_interf, our_get_local_rtc()); |
| 479 | timedate1_set_can_ntp(timedated_interf, our_get_can_ntp()); |
| 480 | timedate1_set_ntp(timedated_interf, our_get_ntp()); |
| 481 | timedate1_set_ntpsynchronized(timedated_interf, our_get_ntpsynchronized()); |
| 482 | timedate1_set_time_usec(timedated_interf, our_get_time_usec()); |
| 483 | timedate1_set_rtctime_usec(timedated_interf, our_get_rtc_time_usec()); |
| 484 | |
| 485 | /* WIP |
| 486 | |
| 487 | timedated_interf->get_timezone = our_get_timezone(); |
| 488 | timedated_interf->get_local_rtc = our_get_local_rtc(); |
| 489 | timedated_interf->get_can_ntp = our_get_can_ntp(); |
| 490 | timedated_interf->get_ntp = our_get_ntp(); |
| 491 | timedated_interf->get_ntpsynchronized = our_get_ntpsynchronized(); |
| 492 | timedated_interf->get_time_usec = our_get_time_usec(); |
| 493 | timedated_interf->get_rtctime_usec = our_get_rtc_time_usec(); */ |
| 494 | |
| 495 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
| 496 | conn, |
| 497 | "/org/freedesktop/timedate1", |
| 498 | NULL)) { |
| 499 | |
| 500 | g_printf("failed to export %s's interface!\n", name); |
| 501 | timedated_mem_clean(); |
| 502 | |
| 503 | } else { |
| 504 | |
| 505 | dbus_interface_exported = TRUE; |
| 506 | g_printf("exported %s's interface on the system bus...\n", name); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static void timedated_on_name_acquired(GDBusConnection *conn, |
| 511 | const gchar *name, |
| 512 | gpointer user_data) { |
| 513 | |
| 514 | g_printf("success!\n"); |
| 515 | } |
| 516 | |
| 517 | static void timedated_on_name_lost(GDBusConnection *conn, |
| 518 | const gchar *name, |
| 519 | gpointer user_data) { |
| 520 | |
| 521 | if(!conn) { |
| 522 | |
| 523 | 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); |
| 524 | timedated_mem_clean(); |
| 525 | } |
| 526 | |
| 527 | g_print("lost name %s, exiting...\n", name); |
| 528 | |
| 529 | timedated_mem_clean(); |
| 530 | } |
| 531 | |
| 532 | /* --- end bus/name handlers, begin misc unix functions --- */ |
| 533 | |
| 534 | /* safe call to clean and then exit |
| 535 | * this stops our GMainLoop safely before letting main() return */ |
| 536 | void timedated_mem_clean() { |
| 537 | |
| 538 | g_printf("exiting...\n"); |
| 539 | |
| 540 | if(dbus_interface_exported) |
| 541 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf)); |
| 542 | |
| 543 | if(g_main_loop_is_running(timedated_loop)) |
| 544 | g_main_loop_quit(timedated_loop); |
| 545 | |
| 546 | } |
| 547 | |
| 548 | /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */ |
| 549 | gboolean unix_sig_terminate_handler(gpointer data) { |
| 550 | |
| 551 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
| 552 | |
| 553 | timedated_mem_clean(); |
| 554 | return G_SOURCE_REMOVE; |
| 555 | } |
| 556 | |
| 557 | void set_signal_handlers() { |
| 558 | |
| 559 | /* we don't care about its descriptor, we never need to unregister these */ |
| 560 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
| 561 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
| 562 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
| 563 | } |
| 564 | |
| 565 | int main() { |
| 566 | |
| 567 | set_signal_handlers(); |
| 568 | |
| 569 | /*if(!build_lookup_table()) |
| 570 | return 1; */ |
| 571 | |
| 572 | timedated_loop = g_main_loop_new(NULL, TRUE); |
| 573 | timedated_freeable = g_ptr_array_new(); |
| 574 | |
| 575 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
| 576 | "org.freedesktop.timedate1", |
| 577 | G_BUS_NAME_OWNER_FLAGS_NONE, |
| 578 | timedated_on_bus_acquired, |
| 579 | timedated_on_name_acquired, |
| 580 | timedated_on_name_lost, |
| 581 | NULL, |
| 582 | NULL); |
| 583 | |
| 584 | g_main_loop_run(timedated_loop); |
| 585 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
| 586 | g_main_loop_unref(timedated_loop); |
| 587 | |
| 588 | /* guaranteed unownable */ |
| 589 | g_bus_unown_name(bus_descriptor); |
| 590 | |
| 591 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
| 592 | g_ptr_array_free(timedated_freeable, TRUE); |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static struct timezone_checksum_pair parse_timezone_path(gchar **pair) { |
| 598 | |
| 599 | gchar *prefix_pattern, *right_prefix_pattern, *posix_prefix_pattern, *lean_path; |
| 600 | GRegex *prefix, *posix, *right; |
| 601 | GError *err = NULL; |
| 602 | struct timezone_checksum_pair ret = { NULL, NULL, FALSE, FALSE }; |
| 603 | |
| 604 | if(!pair[0]) |
| 605 | return ret; |
| 606 | |
| 607 | prefix_pattern = (gchar *) g_malloc0(4096); |
| 608 | right_prefix_pattern = (gchar *) g_malloc0(4096); |
| 609 | posix_prefix_pattern = (gchar *) g_malloc0(4096); |
| 610 | |
| 611 | g_sprintf(prefix_pattern, "%s/", OS_TIMEZONE_PATH); |
| 612 | g_sprintf(posix_prefix_pattern, "%s/posix/", OS_TIMEZONE_PATH); |
| 613 | g_sprintf(right_prefix_pattern, "%s/right/", OS_TIMEZONE_PATH); |
| 614 | |
| 615 | prefix = g_regex_new(prefix_pattern, 0, 0, &err); |
| 616 | posix = g_regex_new(posix_prefix_pattern, 0, 0, &err); |
| 617 | right = g_regex_new(right_prefix_pattern, 0, 0, &err); |
| 618 | |
| 619 | if(g_regex_match_full(posix, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) { |
| 620 | |
| 621 | ret.posix = TRUE; |
| 622 | lean_path = g_regex_replace_literal(posix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
| 623 | |
| 624 | } else if(g_regex_match_full(right, pair[0], -1, 0, G_REGEX_MATCH_NOTEMPTY, NULL, NULL)) { |
| 625 | |
| 626 | ret.right = TRUE; |
| 627 | lean_path = g_regex_replace_literal(right, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
| 628 | |
| 629 | } else |
| 630 | lean_path = g_regex_replace_literal(prefix, pair[0], -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); |
| 631 | |
| 632 | ret.path = lean_path; |
| 633 | |
| 634 | ret.sum = g_malloc0(256); |
| 635 | g_strlcpy(ret.sum, pair[1], 66); |
| 636 | |
| 637 | g_regex_unref(prefix); |
| 638 | g_regex_unref(right); |
| 639 | g_regex_unref(posix); |
| 640 | |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | /* TODO need to deconstruct tz_table on exit |
| 645 | static gboolean build_lookup_table() { |
| 646 | |
| 647 | gchar *find_cmd, **map_pairs, *find_output, *path_buf, *sum_buf, **entry_buf; |
| 648 | GError *err; |
| 649 | gboolean ret; |
| 650 | gint i; |
| 651 | |
| 652 | i = 0; |
| 653 | err = NULL; |
| 654 | ret = TRUE; |
| 655 | |
| 656 | find_cmd = (gchar *) g_malloc0(4096); |
| 657 | find_output = (gchar *) g_malloc0(1000000); |
| 658 | |
| 659 | g_sprintf(find_cmd, "/bin/sh -c \"find %s -type f -exec cksum -a sha256 {} \\; | sed -E 's/SHA256 \\(//g' | sed -E 's/\\) = /=/g'\"", OS_TIMEZONE_PATH); |
| 660 | |
| 661 | if(!g_spawn_command_line_sync(find_cmd, &find_output, NULL, NULL, &err)) { |
| 662 | |
| 663 | g_printf("error running `%s`\n", find_cmd); |
| 664 | ret = FALSE; |
| 665 | } |
| 666 | |
| 667 | map_pairs = g_strsplit(find_output, "\n", INT_MAX); |
| 668 | |
| 669 | while(map_pairs[i] && (entry_buf = g_strsplit(map_pairs[i], "=", INT_MAX))) { |
| 670 | |
| 671 | tz_table[i] = parse_timezone_path(entry_buf); |
| 672 | |
| 673 | g_strfreev(entry_buf); |
| 674 | i++; |
| 675 | } |
| 676 | |
| 677 | g_free(find_output); |
| 678 | g_free(find_cmd); |
| 679 | g_free(map_pairs); |
| 680 | |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | static gchar *lookup_hash(gchar *hash) { |
| 685 | |
| 686 | gint i = 0; |
| 687 | |
| 688 | while(tz_table[i].sum) |
| 689 | if(!g_strcmp0(tz_table[i].sum, hash)) |
| 690 | return tz_table[i].path; |
| 691 | else |
| 692 | i++; |
| 693 | |
| 694 | return NULL; |
| 695 | }*/ |
| 696 | |
| 697 | /* takes number of microseconds since epoch and returns a |
| 698 | * ptr to a timespec suitable to be passed to clock_settime(3) |
| 699 | */ |
| 700 | static struct timespec* mktimespec(gint64 us) { |
| 701 | |
| 702 | long nanoseconds; |
| 703 | time_t seconds; |
| 704 | |
| 705 | gint64 div_buf_remainder, div_buf_s, div_buf_ns; |
| 706 | struct timespec *ret; |
| 707 | |
| 708 | div_buf_s = (us / 1000000); /* us / 10^6 = s */ |
| 709 | div_buf_remainder = (us % 1000000); /* fraction of second lost from prev. line */ |
| 710 | div_buf_ns = div_buf_remainder * 1000; /* us * 10^3 = ns */ |
| 711 | |
| 712 | seconds = (time_t) div_buf_s; /* porting note: most systems use 32 bit time, adjust accordingly */ |
| 713 | nanoseconds = (long) div_buf_ns; |
| 714 | |
| 715 | ret = (struct timespec *) calloc(1, sizeof(struct timespec)); |
| 716 | |
| 717 | ret->tv_sec = seconds; |
| 718 | ret->tv_nsec = nanoseconds; |
| 719 | |
| 720 | g_ptr_array_add(timedated_freeable, ret); |
| 721 | |
| 722 | return ret; |
| 723 | } |