Report errors from clock_gettime and clock_settime
All checks were successful
Build / build (push) Successful in 16s

This commit is contained in:
Eugen Wissner 2024-06-15 11:34:37 +02:00
parent ad19cb19d1
commit bbb4efde18
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 37 additions and 31 deletions

View File

@ -53,18 +53,20 @@ static void slack_method_call(const Glib::RefPtr<Gio::DBus::Connection>& connect
{ {
Glib::Variant<gint64> usec_utc; Glib::Variant<gint64> usec_utc;
Glib::Variant<bool> relative; Glib::Variant<bool> relative;
Glib::Variant<bool> user_interaction;
parameters.get_child(usec_utc, 0); parameters.get_child(usec_utc, 0);
parameters.get_child(relative, 1); parameters.get_child(relative, 1);
parameters.get_child(user_interaction, 2);
if (slack_set_time(usec_utc.get(), relative.get())) try
{ {
dlackware::timedate::set_time(usec_utc.get(), relative.get(), user_interaction.get());
invocation->return_value(Glib::VariantContainerBase()); invocation->return_value(Glib::VariantContainerBase());
} }
else catch (const std::system_error& error)
{ {
Gio::DBus::Error error{ Gio::DBus::Error::FAILED, "Failed to set system clock" }; invocation->return_error(Gio::DBus::Error{ Gio::DBus::Error::FAILED, error.what() });
invocation->return_error(error);
} }
} }
else if (method_name == "SetNTP") else if (method_name == "SetNTP")
@ -293,31 +295,34 @@ namespace dlackware::timedate
{ {
return Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_EXECUTABLE); return Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_EXECUTABLE);
} }
}
gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) void set_time(gint64 seconds_since_epoch, bool relative, bool)
{
timespec ts;
if (relative)
{ {
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) timespec ts;
{
return FALSE;
}
ts.tv_sec += static_cast<time_t>(seconds_since_epoch / dlackware::timedate::usec_per_sec);
ts.tv_nsec += (seconds_since_epoch % dlackware::timedate::usec_per_sec) * dlackware::timedate::nsec_per_usec;
if (ts.tv_nsec < 0) if (relative)
{ {
--ts.tv_sec; if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
ts.tv_nsec += dlackware::timedate::usec_per_sec; {
throw std::system_error(errno, std::generic_category());
}
ts.tv_sec += static_cast<time_t>(seconds_since_epoch / dlackware::timedate::usec_per_sec);
ts.tv_nsec += (seconds_since_epoch % dlackware::timedate::usec_per_sec) * dlackware::timedate::nsec_per_usec;
if (ts.tv_nsec < 0)
{
--ts.tv_sec;
ts.tv_nsec += dlackware::timedate::usec_per_sec;
}
}
else
{
ts.tv_sec = static_cast<time_t>(seconds_since_epoch / dlackware::timedate::usec_per_sec);
ts.tv_nsec = (seconds_since_epoch % dlackware::timedate::usec_per_sec) * dlackware::timedate::nsec_per_usec;
}
if (clock_settime(CLOCK_REALTIME, &ts) == -1)
{
throw std::system_error(errno, std::generic_category());
} }
} }
else
{
ts.tv_sec = static_cast<time_t>(seconds_since_epoch / dlackware::timedate::usec_per_sec);
ts.tv_nsec = (seconds_since_epoch % dlackware::timedate::usec_per_sec) * dlackware::timedate::nsec_per_usec;
}
return clock_settime (CLOCK_REALTIME, &ts) == 0;
} }

View File

@ -47,12 +47,18 @@ namespace dlackware::timedate
std::vector<Glib::ustring> list_timezones(); std::vector<Glib::ustring> list_timezones();
// Sets the system time zone to the one passed by the argument // Sets the system time zone to the one passed by the argument
// Returns true on success, false otherwise // Throws std::filesystem::filesystem_error.
void set_timezone(const Glib::ustring& zone, bool user_interaction); void set_timezone(const Glib::ustring& zone, bool user_interaction);
// Sets NTP // Sets NTP
// Throws std::filesystem::filesystem_error.
void set_ntp(bool use_ntp, bool user_interaction); void set_ntp(bool use_ntp, bool user_interaction);
// Changes the date/time
// Takes the amount of seconds since UNIX epoche and
// Throws std::system_error.
void set_time(gint64 seconds_since_epoch, bool relative, bool user_interaction);
class timedate1 class timedate1
{ {
const Gio::DBus::InterfaceVTable interface_vtable; const Gio::DBus::InterfaceVTable interface_vtable;
@ -64,8 +70,3 @@ namespace dlackware::timedate
void on_name_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name); void on_name_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name);
}; };
} }
// 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);