Support relative time in SetTime

This commit is contained in:
Eugen Wissner 2024-04-14 10:49:28 +02:00
parent 729e175e6b
commit 5f35df67c2
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 39 additions and 43 deletions

View File

@ -11,7 +11,7 @@ pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1)
find_program(SED sed) find_program(SED sed)
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service
COMMAND ${SED} -e s|@LIBEXECDIR@|${CMAKE_INSTALL_LIBEXECDIR}| ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.timedate1.service.in > ${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 MAIN_DEPENDENCY org.freedesktop.timedate1.service.in
VERBATIM) VERBATIM)
add_custom_target(service ALL DEPENDS ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service) add_custom_target(service ALL DEPENDS ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service)

View File

@ -16,7 +16,6 @@
* *
*/ */
#include <gio/gio.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -30,9 +29,6 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
gboolean user_interaction, relative, is_localtime, use_ntp; gboolean user_interaction, relative, is_localtime, use_ntp;
gint64 usec_utc; gint64 usec_utc;
FILE *fp = fopen("/tmp/settime", "w");
fprintf(fp, "Method %s\n", method_name);
// Set time zone // Set time zone
if (g_strcmp0 (method_name, "SetTimezone") == 0) { if (g_strcmp0 (method_name, "SetTimezone") == 0) {
g_variant_get (parameters, "(&sb)", &timezone, &user_interaction); g_variant_get (parameters, "(&sb)", &timezone, &user_interaction);
@ -48,16 +44,17 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
} else if (g_strcmp0 (method_name, "SetTime") == 0) { } else if (g_strcmp0 (method_name, "SetTime") == 0) {
g_variant_get (parameters, "(xbb)", &usec_utc, &relative, &user_interaction); g_variant_get (parameters, "(xbb)", &usec_utc, &relative, &user_interaction);
fprintf(fp, "Seconds since epoche %lld %llu\n", usec_utc, relative);
// Set time // Set time
//if (!slack_set_time (usec_utc, slack_get_is_localtime ())) { //if (!slack_set_time (usec_utc, slack_get_is_localtime ())) {
if (slack_set_time (usec_utc)) g_dbus_method_invocation_return_value (invocation, if (slack_set_time(usec_utc, relative))
NULL); {
else g_dbus_method_invocation_return_error (invocation, g_dbus_method_invocation_return_value(invocation, NULL);
G_IO_ERROR, }
G_IO_ERROR_FAILED, else
{
g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to set system clock"); "Failed to set system clock");
}
} else if (g_strcmp0 (method_name, "SetNTP") == 0) { } else if (g_strcmp0 (method_name, "SetNTP") == 0) {
g_variant_get (parameters, "(bb)", &use_ntp, &user_interaction); g_variant_get (parameters, "(bb)", &use_ntp, &user_interaction);
@ -69,7 +66,6 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
G_IO_ERROR_FAILED, G_IO_ERROR_FAILED,
"Error enabling NTP"); "Error enabling NTP");
} }
fclose(fp);
return; return;
} }
@ -194,33 +190,31 @@ gboolean slack_set_timezone (gchar *zone) {
return TRUE; return TRUE;
} }
gboolean slack_set_time (gint64 seconds_since_epoch) { gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative)
struct timespec ts; {
/* gint exit_status; timespec ts;
gboolean spawn_status;
gchar *cmd;*/
// Set system clock if (relative)
ts.tv_sec = (time_t) (seconds_since_epoch / USEC_PER_SEC); {
ts.tv_nsec = 0; if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
if (clock_settime (CLOCK_REALTIME, &ts)) { {
return FALSE; return FALSE;
} }
ts.tv_sec += static_cast<time_t>(seconds_since_epoch / USEC_PER_SEC);
ts.tv_nsec += (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC;
// Don't save the system time to the hardware clock. The saving takes much if (ts.tv_nsec < 0)
// time. Anyway it is saved automatically on shutdown. {
/* Set hardware clock --ts.tv_sec;
if (!g_file_test ("/sbin/hwclock", G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_EXECUTABLE)) { ts.tv_nsec += USEC_PER_SEC;
return FALSE;
} }
cmd = g_strdup_printf ("/sbin/hwclock %s --systohc", is_localtime ? "--localtime" : "-u"); }
spawn_status = g_spawn_command_line_sync (cmd, NULL, NULL, &exit_status, NULL); else
g_free (cmd); {
if ((WEXITSTATUS (exit_status) != 0) || !spawn_status) { ts.tv_sec = static_cast<time_t>(seconds_since_epoch / USEC_PER_SEC);
return FALSE; ts.tv_nsec = (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC;
}*/ }
return clock_settime (CLOCK_REALTIME, &ts) == 0;
return TRUE;
} }
gboolean slack_get_is_localtime () { gboolean slack_get_is_localtime () {

View File

@ -17,6 +17,7 @@
*/ */
#include <dbus/dbus.h> #include <dbus/dbus.h>
#include <gio/gio.h>
#define BUS_NAME "org.freedesktop.timedate1" #define BUS_NAME "org.freedesktop.timedate1"
#define BUS_PATH "/org/freedesktop/timedate1" #define BUS_PATH "/org/freedesktop/timedate1"
@ -50,7 +51,9 @@
"</node>\n" "</node>\n"
#define DEFAULT_EXIT_SEC 300 #define DEFAULT_EXIT_SEC 300
#define USEC_PER_SEC 1000000ULL
constexpr gint32 USEC_PER_SEC = 1000000ULL;
constexpr gint32 NSEC_PER_USEC = 1000ULL;
// Returns the system time zone // Returns the system time zone
gchar *slack_get_timezone (); gchar *slack_get_timezone ();
@ -62,8 +65,7 @@ gboolean slack_set_timezone (gchar *);
// Changes the date/time // Changes the date/time
// Takes the amount of seconds since UNIX epoche and // Takes the amount of seconds since UNIX epoche and
// Returns true on success, false otherwise // Returns true on success, false otherwise
gboolean slack_set_time (gint64); gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative);
//gboolean slack_set_time (gint64, gboolean);
// Returns if the hardware clock is set to local time or not // Returns if the hardware clock is set to local time or not
gboolean slack_get_is_localtime (); gboolean slack_get_is_localtime ();