From 0eccfe3ef3ca602212667f339440d77c13d757b1 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 21 May 2024 18:58:15 +0200 Subject: Split the data and sources --- CMakeLists.txt | 27 +-- ChangeLog | 9 - README | 39 ----- README.md | 39 +++++ data/CMakeLists.txt | 16 ++ data/org.freedesktop.timedate1.conf | 27 +++ data/org.freedesktop.timedate1.policy | 41 +++++ data/org.freedesktop.timedate1.service.in | 4 + data/org.freedesktop.timedate1.xml | 27 +++ org.freedesktop.timedate1.conf | 27 --- org.freedesktop.timedate1.policy | 41 ----- org.freedesktop.timedate1.service.in | 4 - org.freedesktop.timedate1.xml | 27 --- slack-timedate.cpp | 274 ------------------------------ slack-timedate.h | 78 --------- src/CMakeLists.txt | 10 ++ src/slack-timedate.cpp | 274 ++++++++++++++++++++++++++++++ src/slack-timedate.h | 78 +++++++++ 18 files changed, 518 insertions(+), 524 deletions(-) delete mode 100644 ChangeLog delete mode 100644 README create mode 100644 README.md create mode 100644 data/CMakeLists.txt create mode 100644 data/org.freedesktop.timedate1.conf create mode 100644 data/org.freedesktop.timedate1.policy create mode 100644 data/org.freedesktop.timedate1.service.in create mode 100644 data/org.freedesktop.timedate1.xml delete mode 100644 org.freedesktop.timedate1.conf delete mode 100644 org.freedesktop.timedate1.policy delete mode 100644 org.freedesktop.timedate1.service.in delete mode 100644 org.freedesktop.timedate1.xml delete mode 100644 slack-timedate.cpp delete mode 100644 slack-timedate.h create mode 100644 src/CMakeLists.txt create mode 100644 src/slack-timedate.cpp create mode 100644 src/slack-timedate.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 304a366..ae5077d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,28 +6,5 @@ include(GNUInstallDirs) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_CXX_STANDARD 17) -find_package(PkgConfig) -pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4) -find_program(SED sed) - -add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service - COMMAND ${SED} -e s|@LIBEXECDIR@|${CMAKE_INSTALL_FULL_LIBEXECDIR}| ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.timedate1.service.in > ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service - MAIN_DEPENDENCY org.freedesktop.timedate1.service.in - VERBATIM) -add_custom_target(service ALL DEPENDS ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service) - -add_executable(slack-timedate - slack-timedate.cpp slack-timedate.h -) -target_include_directories(slack-timedate PRIVATE ${GDBUS_INCLUDE_DIRS}) -target_link_libraries(slack-timedate ${GDBUS_LIBRARIES}) - -install(TARGETS slack-timedate DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}) -install(FILES org.freedesktop.timedate1.xml - DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/interfaces) -install(FILES org.freedesktop.timedate1.policy - DESTINATION ${CMAKE_INSTALL_DATADIR}/polkit-1/actions) -install(FILES org.freedesktop.timedate1.conf - DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/dbus-1/system.d) -install(FILES ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service - DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/system-services) +add_subdirectory(data) +add_subdirectory(src) diff --git a/ChangeLog b/ChangeLog deleted file mode 100644 index a60d8ae..0000000 --- a/ChangeLog +++ /dev/null @@ -1,9 +0,0 @@ -2024-04-07 Eugen Wissner - - * Added PolicyKit configuration - -==================== 1.1 ==================== - -2012-01-22 Eugen Wissner - - * Added autotools scripts diff --git a/README b/README deleted file mode 100644 index bcfffae..0000000 --- a/README +++ /dev/null @@ -1,39 +0,0 @@ -# slack-timedate - -This program implements timedated1 interface from systemd what is -required by GNOME Control Center for now. -slack-timedate's purpose is to make it possible to enjoy full GNOME -functionality on Slackware Linux which doesn't use systemd as its -init-system. - -## Installation - -``` -cmake -B build \ - -DCMAKE_INSTALL_PREFIX=/usr \ - -DCMAKE_INSTALL_SYSCONFDIR=/etc -make -C build -make -C build install -``` - -The installation will create a file `build/install_manifest.txt`, which -contains a list of all files installed in the system. To uninstall the -application those should be removed, for example by doing: - -``` -xargs rm < build/install_manifest.txt -``` - -## Debugging - -Firstly an application is required that changes the time settings over the -timedate1 service. It can be `gnome-control-center` or `kcmshell5 kcm_clock` -for KDE. - -Then running `dbus-monitor --system` as root can be used to see the -communication between the control center and this service. - -## Dependencies - -- glib -- dbus diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f26492 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# slack-timedate + +This program implements timedated1 interface from systemd what is +required by GNOME Control Center for now. +slack-timedate's purpose is to make it possible to enjoy full GNOME +functionality on Slackware Linux which doesn't use systemd as its +init-system. + +## Installation + +```sh +cmake -B build \ + -DCMAKE_INSTALL_PREFIX=/usr \ + -DCMAKE_INSTALL_SYSCONFDIR=/etc +make -C build +make -C build install +``` + +The installation will create a file `build/install_manifest.txt`, which +contains a list of all files installed in the system. To uninstall the +application those should be removed, for example by doing: + +```sh +xargs rm < build/install_manifest.txt +``` + +## Debugging + +Firstly an application is required that changes the time settings over the +timedate1 service. It can be `gnome-control-center` or `kcmshell5 kcm_clock` +for KDE. + +Then running `dbus-monitor --system` as root can be used to see the +communication between the control center and this service. + +## Dependencies + +- glib +- dbus diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt new file mode 100644 index 0000000..93c1f77 --- /dev/null +++ b/data/CMakeLists.txt @@ -0,0 +1,16 @@ +find_program(SED sed) + +add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service + COMMAND ${SED} -e s|@LIBEXECDIR@|${CMAKE_INSTALL_FULL_LIBEXECDIR}| ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.timedate1.service.in > ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service + MAIN_DEPENDENCY org.freedesktop.timedate1.service.in + VERBATIM) +add_custom_target(service ALL DEPENDS ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service) + +install(FILES org.freedesktop.timedate1.xml + DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/interfaces) +install(FILES org.freedesktop.timedate1.policy + DESTINATION ${CMAKE_INSTALL_DATADIR}/polkit-1/actions) +install(FILES org.freedesktop.timedate1.conf + DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/dbus-1/system.d) +install(FILES ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service + DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/system-services) diff --git a/data/org.freedesktop.timedate1.conf b/data/org.freedesktop.timedate1.conf new file mode 100644 index 0000000..36557d5 --- /dev/null +++ b/data/org.freedesktop.timedate1.conf @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/data/org.freedesktop.timedate1.policy b/data/org.freedesktop.timedate1.policy new file mode 100644 index 0000000..a3b1dc5 --- /dev/null +++ b/data/org.freedesktop.timedate1.policy @@ -0,0 +1,41 @@ + + + + slack-timedate + + + Changes the system clock + + auth_admin_keep + auth_admin_keep + auth_admin_keep + + + + + Sets the system timezone + + auth_admin_keep + auth_admin_keep + auth_admin_keep + + + + + Controls whether the RTC is in local time or UTC + + auth_admin_keep + auth_admin_keep + auth_admin_keep + + + + + Controls whether the system clock is synchronized with the network + + auth_admin_keep + auth_admin_keep + auth_admin_keep + + + diff --git a/data/org.freedesktop.timedate1.service.in b/data/org.freedesktop.timedate1.service.in new file mode 100644 index 0000000..1849390 --- /dev/null +++ b/data/org.freedesktop.timedate1.service.in @@ -0,0 +1,4 @@ +[D-BUS Service] +Name=org.freedesktop.timedate1 +Exec=@LIBEXECDIR@/slack-timedate +User=root diff --git a/data/org.freedesktop.timedate1.xml b/data/org.freedesktop.timedate1.xml new file mode 100644 index 0000000..b035d10 --- /dev/null +++ b/data/org.freedesktop.timedate1.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.freedesktop.timedate1.conf b/org.freedesktop.timedate1.conf deleted file mode 100644 index 36557d5..0000000 --- a/org.freedesktop.timedate1.conf +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/org.freedesktop.timedate1.policy b/org.freedesktop.timedate1.policy deleted file mode 100644 index a3b1dc5..0000000 --- a/org.freedesktop.timedate1.policy +++ /dev/null @@ -1,41 +0,0 @@ - - - - slack-timedate - - - Changes the system clock - - auth_admin_keep - auth_admin_keep - auth_admin_keep - - - - - Sets the system timezone - - auth_admin_keep - auth_admin_keep - auth_admin_keep - - - - - Controls whether the RTC is in local time or UTC - - auth_admin_keep - auth_admin_keep - auth_admin_keep - - - - - Controls whether the system clock is synchronized with the network - - auth_admin_keep - auth_admin_keep - auth_admin_keep - - - diff --git a/org.freedesktop.timedate1.service.in b/org.freedesktop.timedate1.service.in deleted file mode 100644 index 1849390..0000000 --- a/org.freedesktop.timedate1.service.in +++ /dev/null @@ -1,4 +0,0 @@ -[D-BUS Service] -Name=org.freedesktop.timedate1 -Exec=@LIBEXECDIR@/slack-timedate -User=root diff --git a/org.freedesktop.timedate1.xml b/org.freedesktop.timedate1.xml deleted file mode 100644 index b035d10..0000000 --- a/org.freedesktop.timedate1.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/slack-timedate.cpp b/slack-timedate.cpp deleted file mode 100644 index 3f8b2d8..0000000 --- a/slack-timedate.cpp +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Copyright (C) 2013-2024 Eugen Wissner - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "slack-timedate.h" - -static void slack_method_call (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { - gchar *timezone, *response; - gboolean user_interaction, relative, is_localtime, use_ntp; - gint64 usec_utc; - - // Set time zone - if (g_strcmp0 (method_name, "SetTimezone") == 0) { - g_variant_get (parameters, "(&sb)", &timezone, &user_interaction); - - if (slack_set_timezone (timezone)) g_dbus_method_invocation_return_value (invocation, - NULL); - else g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_FAILED, - "Write operation failed"); - - g_free (timezone); - } else if (g_strcmp0 (method_name, "SetTime") == 0) { - g_variant_get (parameters, "(xbb)", &usec_utc, &relative, &user_interaction); - - // Set time - //if (!slack_set_time (usec_utc, slack_get_is_localtime ())) { - if (slack_set_time(usec_utc, relative)) - { - g_dbus_method_invocation_return_value(invocation, NULL); - } - else - { - g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED, - "Failed to set system clock"); - } - } else if (g_strcmp0 (method_name, "SetNTP") == 0) { - g_variant_get (parameters, "(bb)", &use_ntp, &user_interaction); - - // Enable NTP - if (slack_set_ntp (use_ntp)) g_dbus_method_invocation_return_value (invocation, - NULL); - else g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_FAILED, - "Error enabling NTP"); - } - return; -} - -static GVariant *slack_get_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *prop_name, GError **slack_err, gpointer user_data) { - if (g_strcmp0 ("Timezone", prop_name) == 0) { - return g_variant_new_string (slack_get_timezone ()); - } if (g_strcmp0 ("LocalRTC", prop_name) == 0) { - return g_variant_new_boolean (slack_get_is_localtime ()); - } if (g_strcmp0 ("NTP", prop_name) == 0) { - return g_variant_new_boolean (slack_get_ntp ()); - } - return NULL; -} - -static void on_timedate_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { - guint registration_id; - GDBusNodeInfo *introspection_data; - GError *slack_err; - const GDBusInterfaceVTable interface_vtable = { - slack_method_call, - slack_get_property, - NULL - }; - - slack_err = NULL; - introspection_data = g_dbus_node_info_new_for_xml (INTROSPECTION_XML, &slack_err); - if (introspection_data == NULL) - g_error ("Failed to parse D-Bus introspection XML: %s\n", slack_err->message); - - slack_err = NULL; - registration_id = g_dbus_connection_register_object (connection, - BUS_PATH, - introspection_data->interfaces[0], - &interface_vtable, - NULL, - NULL, - &slack_err); - - if (registration_id <= 0) { - g_critical ("Failed to register callbacks for the exported object with the D-Bus interface: %s\n", slack_err->message); - g_error_free (slack_err); - } - - g_dbus_node_info_unref (introspection_data); -} - -static void on_timedate_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) { - g_warning ("Failed to acquire the service %s.\n", name); - exit (1); -} - -gboolean timeout_callback (Glib::RefPtr loop2quit) { - loop2quit->quit(); - return false; -} - -int main(int argc, char **argv) -{ - guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, - BUS_NAME, - static_cast(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE), - on_timedate_acquired, - NULL, - on_timedate_lost, - NULL, - NULL); - Glib::RefPtr loop = Glib::MainLoop::create(false); - - Glib::signal_timeout().connect_seconds(sigc::bind(&timeout_callback, loop), DEFAULT_EXIT_SEC); - loop->run(); - - g_bus_unown_name (owner_id); - - return EXIT_SUCCESS; -} - - -gchar *slack_get_timezone () { - gchar *zone_copied_from, *zone; - - zone_copied_from = g_file_read_link ("/etc/localtime-copied-from", NULL); - if (zone_copied_from == NULL) return NULL; - - zone = g_strdup (zone_copied_from + strlen ("/usr/share/zoneinfo/") * sizeof (gchar)); - g_free (zone_copied_from); - - return zone; -} - -gboolean slack_set_timezone (gchar *zone) { - gchar *zone_file; - GFile *etc_localtime, *localtime_link, *g_zone_file; - - zone_file = g_strconcat ("/usr/share/zoneinfo/", zone, NULL); - if (Glib::file_test(zone_file, Glib::FILE_TEST_IS_REGULAR)) - { - etc_localtime = g_file_new_for_path ("/etc/localtime"); - localtime_link = g_file_new_for_path ("/etc/localtime-copied-from"); - g_zone_file = g_file_new_for_path (zone_file); - - if (!g_file_copy (g_zone_file, etc_localtime, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL)) { - return FALSE; - } - - if (!g_file_delete (localtime_link, NULL, NULL)) { - return FALSE; - } - if (!g_file_make_symbolic_link (localtime_link, zone_file, NULL, NULL)) { - return FALSE; - } - - g_free (zone_file); - g_object_unref (etc_localtime); - g_object_unref (localtime_link); - g_object_unref (g_zone_file); - - return true; - } - return false; -} - -gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) -{ - timespec ts; - - if (relative) - { - if (clock_gettime(CLOCK_REALTIME, &ts) == -1) - { - return FALSE; - } - ts.tv_sec += static_cast(seconds_since_epoch / USEC_PER_SEC); - ts.tv_nsec += (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC; - - if (ts.tv_nsec < 0) - { - --ts.tv_sec; - ts.tv_nsec += USEC_PER_SEC; - } - } - else - { - ts.tv_sec = static_cast(seconds_since_epoch / USEC_PER_SEC); - ts.tv_nsec = (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC; - } - return clock_settime (CLOCK_REALTIME, &ts) == 0; -} - -gboolean slack_get_is_localtime() -{ - std::ifstream fh{ "/etc/hardwareclock", std::ios::in }; - if (!fh.is_open()) - { - return false; - } - std::array time_str; // "localtime" is longer than "UTC" and has 9 letters + \0 - - while (fh.good()) - { - fh.getline(time_str.data(), time_str.size()); - fh.clear(fh.rdstate() & (~std::ios_base::failbit)); - - if (std::strncmp(time_str.data(), "localtime", time_str.size()) == 0) - { - return true; - } - } - return false; -} - -gboolean slack_get_ntp() -{ - return Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_EXECUTABLE); -} - -gboolean slack_set_ntp(gboolean xntp) -{ - std::filesystem::perms rc_mode = std::filesystem::perms::owner_read | std::filesystem::perms::owner_write - | std::filesystem::perms::group_read | std::filesystem::perms::others_read; - - if (xntp) - { - rc_mode |= std::filesystem::perms::owner_exec - | std::filesystem::perms::group_exec | std::filesystem::perms::others_exec; - g_message("Please don't forget to configure the NTP daemon"); - - /* It doesn't matter if fails. - * The ntpdate command is considered obsolete, but "orthodox" ntpd -g - * will fail if your system clock is off for more than half an hour. */ - g_spawn_command_line_async("/usr/sbin/ntpdate pool.ntp.org", NULL); - } - if (Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_REGULAR)) - { - std::error_code ec; - std::filesystem::permissions("/etc/rc.d/rc.ntpd", rc_mode, ec); - return !ec; - } - else - { - g_error("The NTP daemon isn't installed"); - return false; - } -} diff --git a/slack-timedate.h b/slack-timedate.h deleted file mode 100644 index 7154336..0000000 --- a/slack-timedate.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2013-2024 Eugen Wissner - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -#include -#include -#include - -#define BUS_NAME "org.freedesktop.timedate1" -#define BUS_PATH "/org/freedesktop/timedate1" -#define BUS_INTERFACE "org.freedesktop.timedate1" - -#define INTROSPECTION_XML DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ - "\n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - "\n" - -#define DEFAULT_EXIT_SEC 300 - -constexpr gint32 USEC_PER_SEC = 1000000ULL; -constexpr gint32 NSEC_PER_USEC = 1000ULL; - -// Returns the system time zone -gchar *slack_get_timezone (); - -// Sets the system time zone to the one passed by the argument -// Returns true on success, false otherwise -gboolean slack_set_timezone (gchar *); - -// Changes the date/time -// Takes the amount of seconds since UNIX epoche and -// Returns true on success, false otherwise -gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative); - -// Returns if the hardware clock is set to local time or not -gboolean slack_get_is_localtime (); - -// Returns if NTP is enabled -gboolean slack_get_ntp (); - -// Sets NTP -gboolean slack_set_ntp (gboolean); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..9dc53d4 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,10 @@ +find_package(PkgConfig) +pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4) + +add_executable(slack-timedate + slack-timedate.cpp slack-timedate.h +) +target_include_directories(slack-timedate PRIVATE ${GDBUS_INCLUDE_DIRS}) +target_link_libraries(slack-timedate ${GDBUS_LIBRARIES}) + +install(TARGETS slack-timedate DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}) diff --git a/src/slack-timedate.cpp b/src/slack-timedate.cpp new file mode 100644 index 0000000..3f8b2d8 --- /dev/null +++ b/src/slack-timedate.cpp @@ -0,0 +1,274 @@ +/* + * Copyright (C) 2013-2024 Eugen Wissner + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "slack-timedate.h" + +static void slack_method_call (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { + gchar *timezone, *response; + gboolean user_interaction, relative, is_localtime, use_ntp; + gint64 usec_utc; + + // Set time zone + if (g_strcmp0 (method_name, "SetTimezone") == 0) { + g_variant_get (parameters, "(&sb)", &timezone, &user_interaction); + + if (slack_set_timezone (timezone)) g_dbus_method_invocation_return_value (invocation, + NULL); + else g_dbus_method_invocation_return_error (invocation, + G_IO_ERROR, + G_IO_ERROR_FAILED, + "Write operation failed"); + + g_free (timezone); + } else if (g_strcmp0 (method_name, "SetTime") == 0) { + g_variant_get (parameters, "(xbb)", &usec_utc, &relative, &user_interaction); + + // Set time + //if (!slack_set_time (usec_utc, slack_get_is_localtime ())) { + if (slack_set_time(usec_utc, relative)) + { + g_dbus_method_invocation_return_value(invocation, NULL); + } + else + { + g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED, + "Failed to set system clock"); + } + } else if (g_strcmp0 (method_name, "SetNTP") == 0) { + g_variant_get (parameters, "(bb)", &use_ntp, &user_interaction); + + // Enable NTP + if (slack_set_ntp (use_ntp)) g_dbus_method_invocation_return_value (invocation, + NULL); + else g_dbus_method_invocation_return_error (invocation, + G_IO_ERROR, + G_IO_ERROR_FAILED, + "Error enabling NTP"); + } + return; +} + +static GVariant *slack_get_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *prop_name, GError **slack_err, gpointer user_data) { + if (g_strcmp0 ("Timezone", prop_name) == 0) { + return g_variant_new_string (slack_get_timezone ()); + } if (g_strcmp0 ("LocalRTC", prop_name) == 0) { + return g_variant_new_boolean (slack_get_is_localtime ()); + } if (g_strcmp0 ("NTP", prop_name) == 0) { + return g_variant_new_boolean (slack_get_ntp ()); + } + return NULL; +} + +static void on_timedate_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { + guint registration_id; + GDBusNodeInfo *introspection_data; + GError *slack_err; + const GDBusInterfaceVTable interface_vtable = { + slack_method_call, + slack_get_property, + NULL + }; + + slack_err = NULL; + introspection_data = g_dbus_node_info_new_for_xml (INTROSPECTION_XML, &slack_err); + if (introspection_data == NULL) + g_error ("Failed to parse D-Bus introspection XML: %s\n", slack_err->message); + + slack_err = NULL; + registration_id = g_dbus_connection_register_object (connection, + BUS_PATH, + introspection_data->interfaces[0], + &interface_vtable, + NULL, + NULL, + &slack_err); + + if (registration_id <= 0) { + g_critical ("Failed to register callbacks for the exported object with the D-Bus interface: %s\n", slack_err->message); + g_error_free (slack_err); + } + + g_dbus_node_info_unref (introspection_data); +} + +static void on_timedate_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) { + g_warning ("Failed to acquire the service %s.\n", name); + exit (1); +} + +gboolean timeout_callback (Glib::RefPtr loop2quit) { + loop2quit->quit(); + return false; +} + +int main(int argc, char **argv) +{ + guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, + BUS_NAME, + static_cast(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE), + on_timedate_acquired, + NULL, + on_timedate_lost, + NULL, + NULL); + Glib::RefPtr loop = Glib::MainLoop::create(false); + + Glib::signal_timeout().connect_seconds(sigc::bind(&timeout_callback, loop), DEFAULT_EXIT_SEC); + loop->run(); + + g_bus_unown_name (owner_id); + + return EXIT_SUCCESS; +} + + +gchar *slack_get_timezone () { + gchar *zone_copied_from, *zone; + + zone_copied_from = g_file_read_link ("/etc/localtime-copied-from", NULL); + if (zone_copied_from == NULL) return NULL; + + zone = g_strdup (zone_copied_from + strlen ("/usr/share/zoneinfo/") * sizeof (gchar)); + g_free (zone_copied_from); + + return zone; +} + +gboolean slack_set_timezone (gchar *zone) { + gchar *zone_file; + GFile *etc_localtime, *localtime_link, *g_zone_file; + + zone_file = g_strconcat ("/usr/share/zoneinfo/", zone, NULL); + if (Glib::file_test(zone_file, Glib::FILE_TEST_IS_REGULAR)) + { + etc_localtime = g_file_new_for_path ("/etc/localtime"); + localtime_link = g_file_new_for_path ("/etc/localtime-copied-from"); + g_zone_file = g_file_new_for_path (zone_file); + + if (!g_file_copy (g_zone_file, etc_localtime, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL)) { + return FALSE; + } + + if (!g_file_delete (localtime_link, NULL, NULL)) { + return FALSE; + } + if (!g_file_make_symbolic_link (localtime_link, zone_file, NULL, NULL)) { + return FALSE; + } + + g_free (zone_file); + g_object_unref (etc_localtime); + g_object_unref (localtime_link); + g_object_unref (g_zone_file); + + return true; + } + return false; +} + +gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) +{ + timespec ts; + + if (relative) + { + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) + { + return FALSE; + } + ts.tv_sec += static_cast(seconds_since_epoch / USEC_PER_SEC); + ts.tv_nsec += (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC; + + if (ts.tv_nsec < 0) + { + --ts.tv_sec; + ts.tv_nsec += USEC_PER_SEC; + } + } + else + { + ts.tv_sec = static_cast(seconds_since_epoch / USEC_PER_SEC); + ts.tv_nsec = (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC; + } + return clock_settime (CLOCK_REALTIME, &ts) == 0; +} + +gboolean slack_get_is_localtime() +{ + std::ifstream fh{ "/etc/hardwareclock", std::ios::in }; + if (!fh.is_open()) + { + return false; + } + std::array time_str; // "localtime" is longer than "UTC" and has 9 letters + \0 + + while (fh.good()) + { + fh.getline(time_str.data(), time_str.size()); + fh.clear(fh.rdstate() & (~std::ios_base::failbit)); + + if (std::strncmp(time_str.data(), "localtime", time_str.size()) == 0) + { + return true; + } + } + return false; +} + +gboolean slack_get_ntp() +{ + return Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_EXECUTABLE); +} + +gboolean slack_set_ntp(gboolean xntp) +{ + std::filesystem::perms rc_mode = std::filesystem::perms::owner_read | std::filesystem::perms::owner_write + | std::filesystem::perms::group_read | std::filesystem::perms::others_read; + + if (xntp) + { + rc_mode |= std::filesystem::perms::owner_exec + | std::filesystem::perms::group_exec | std::filesystem::perms::others_exec; + g_message("Please don't forget to configure the NTP daemon"); + + /* It doesn't matter if fails. + * The ntpdate command is considered obsolete, but "orthodox" ntpd -g + * will fail if your system clock is off for more than half an hour. */ + g_spawn_command_line_async("/usr/sbin/ntpdate pool.ntp.org", NULL); + } + if (Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_REGULAR)) + { + std::error_code ec; + std::filesystem::permissions("/etc/rc.d/rc.ntpd", rc_mode, ec); + return !ec; + } + else + { + g_error("The NTP daemon isn't installed"); + return false; + } +} diff --git a/src/slack-timedate.h b/src/slack-timedate.h new file mode 100644 index 0000000..7154336 --- /dev/null +++ b/src/slack-timedate.h @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2013-2024 Eugen Wissner + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include + +#define BUS_NAME "org.freedesktop.timedate1" +#define BUS_PATH "/org/freedesktop/timedate1" +#define BUS_INTERFACE "org.freedesktop.timedate1" + +#define INTROSPECTION_XML DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ + "\n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + "\n" + +#define DEFAULT_EXIT_SEC 300 + +constexpr gint32 USEC_PER_SEC = 1000000ULL; +constexpr gint32 NSEC_PER_USEC = 1000ULL; + +// Returns the system time zone +gchar *slack_get_timezone (); + +// Sets the system time zone to the one passed by the argument +// Returns true on success, false otherwise +gboolean slack_set_timezone (gchar *); + +// Changes the date/time +// Takes the amount of seconds since UNIX epoche and +// Returns true on success, false otherwise +gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative); + +// Returns if the hardware clock is set to local time or not +gboolean slack_get_is_localtime (); + +// Returns if NTP is enabled +gboolean slack_get_ntp (); + +// Sets NTP +gboolean slack_set_ntp (gboolean); -- cgit v1.2.3