Initial commit
This commit is contained in:
commit
4b59879690
3
compile.bash
Executable file
3
compile.bash
Executable file
@ -0,0 +1,3 @@
|
||||
#/bin/bash
|
||||
|
||||
cc `pkg-config --cflags --libs gio-unix-2.0 dbus-1` -g -o slack-timedate slack-timedate.c
|
27
org.freedesktop.timedate1.conf
Normal file
27
org.freedesktop.timedate1.conf
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0"?> <!--*-nxml-*-->
|
||||
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
|
||||
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
|
||||
|
||||
<!--
|
||||
This file is part of systemd.
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
-->
|
||||
|
||||
<busconfig>
|
||||
|
||||
<policy user="root">
|
||||
<allow own="org.freedesktop.timedate1"/>
|
||||
<allow send_destination="org.freedesktop.timedate1"/>
|
||||
<allow receive_sender="org.freedesktop.timedate1"/>
|
||||
</policy>
|
||||
|
||||
<policy context="default">
|
||||
<allow send_destination="org.freedesktop.timedate1"/>
|
||||
<allow receive_sender="org.freedesktop.timedate1"/>
|
||||
</policy>
|
||||
|
||||
</busconfig>
|
4
org.freedesktop.timedate1.service
Normal file
4
org.freedesktop.timedate1.service
Normal file
@ -0,0 +1,4 @@
|
||||
[D-BUS Service]
|
||||
Name=org.freedesktop.timedate1
|
||||
Exec=/usr/libexec/slack-timedate
|
||||
User=root
|
27
org.freedesktop.timedate1.xml
Normal file
27
org.freedesktop.timedate1.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
|
||||
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
||||
<node>
|
||||
<interface name="org.freedesktop.timedate1">
|
||||
<property name="Timezone" type="s" access="read"/>
|
||||
<property name="LocalRTC" type="b" access="read"/>
|
||||
<property name="NTP" type="b" access="read"/>
|
||||
<method name="SetTime">
|
||||
<arg name="usec_utc" type="x" direction="in"/>
|
||||
<arg name="relative" type="b" direction="in"/>
|
||||
<arg name="user_interaction" type="b" direction="in"/>
|
||||
</method>
|
||||
<method name="SetTimezone">
|
||||
<arg name="timezone" type="s" direction="in"/>
|
||||
<arg name="user_interaction" type="b" direction="in"/>
|
||||
</method>
|
||||
<method name="SetLocalRTC">
|
||||
<arg name="local_rtc" type="b" direction="in"/>
|
||||
<arg name="fix_system" type="b" direction="in"/>
|
||||
<arg name="user_interaction" type="b" direction="in"/>
|
||||
</method>
|
||||
<method name="SetNTP">
|
||||
<arg name="use_ntp" type="b" direction="in"/>
|
||||
<arg name="user_interaction" type="b" direction="in"/>
|
||||
</method>
|
||||
</interface>
|
||||
</node>
|
269
slack-timedate.c
Normal file
269
slack-timedate.c
Normal file
@ -0,0 +1,269 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Eugene Wissner <belka.ew@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#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)) 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 (gpointer loop2quit) {
|
||||
g_main_loop_quit ((GMainLoop *)loop2quit);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
guint owner_id;
|
||||
GMainLoop *loop;
|
||||
|
||||
g_type_init ();
|
||||
|
||||
owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
|
||||
BUS_NAME,
|
||||
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
||||
G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
||||
on_timedate_acquired,
|
||||
NULL,
|
||||
on_timedate_lost,
|
||||
NULL,
|
||||
NULL);
|
||||
|
||||
loop = g_main_loop_new (NULL, FALSE);
|
||||
g_timeout_add_seconds (DEFAULT_EXIT_SEC, timeout_callback , loop);
|
||||
g_main_loop_run (loop);
|
||||
|
||||
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 (g_file_test (zone_file, G_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);
|
||||
} else {
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean slack_set_time (gint64 seconds_since_epoch) {
|
||||
struct timespec ts;
|
||||
/* gint exit_status;
|
||||
gboolean spawn_status;
|
||||
gchar *cmd;*/
|
||||
|
||||
// Set system clock
|
||||
ts.tv_sec = (time_t) (seconds_since_epoch / USEC_PER_SEC);
|
||||
ts.tv_nsec = 0;
|
||||
if (clock_settime (CLOCK_REALTIME, &ts)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Don't save the system time to the hardware clock. The saving takes much
|
||||
// time. Anyway it is saved automatically on shutdown.
|
||||
/* Set hardware clock
|
||||
if (!g_file_test ("/sbin/hwclock", G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_EXECUTABLE)) {
|
||||
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);
|
||||
g_free (cmd);
|
||||
if ((WEXITSTATUS (exit_status) != 0) || !spawn_status) {
|
||||
return FALSE;
|
||||
}*/
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean slack_get_is_localtime () {
|
||||
FILE *fh;
|
||||
char time_str[10]; // "localtime" is longer than "UTC" and has 9 letters + \0
|
||||
gboolean is_localtime;
|
||||
|
||||
is_localtime = FALSE;
|
||||
|
||||
fh = fopen ("/etc/hardwareclock", "r");
|
||||
if (fh == NULL) return FALSE;
|
||||
while (fgets (time_str, 10, fh)) {
|
||||
if (!g_strcmp0 (time_str, "localtime")) {
|
||||
is_localtime = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose (fh);
|
||||
return is_localtime;
|
||||
}
|
||||
|
||||
gboolean slack_get_ntp () {
|
||||
if (g_file_test ("/etc/rc.d/rc.ntpd", G_FILE_TEST_IS_EXECUTABLE))
|
||||
return TRUE;
|
||||
else return FALSE;
|
||||
}
|
||||
|
||||
gboolean slack_set_ntp (gboolean xntp) {
|
||||
mode_t rc_mode;
|
||||
|
||||
rc_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
||||
|
||||
if (xntp) {
|
||||
rc_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
|
||||
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 (g_file_test ("/etc/rc.d/rc.ntpd", G_FILE_TEST_IS_REGULAR)) {
|
||||
if (chmod ("/etc/rc.d/rc.ntpd", rc_mode)) return FALSE;
|
||||
else return TRUE;
|
||||
} else {
|
||||
g_error ("The NTP daemon isn't installed");
|
||||
return FALSE;
|
||||
}
|
||||
}
|
75
slack-timedate.h
Normal file
75
slack-timedate.h
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Eugene Wissner <belka.ew@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
|
||||
#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 \
|
||||
"<node>\n" \
|
||||
" <interface name=\"org.freedesktop.timedate1\">\n" \
|
||||
" <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
|
||||
" <property name=\"LocalRTC\" type=\"b\" access=\"read\"/>\n" \
|
||||
" <property name=\"NTP\" type=\"b\" access=\"read\"/>\n" \
|
||||
" <method name=\"SetTime\">\n" \
|
||||
" <arg name=\"usec_utc\" type=\"x\" direction=\"in\"/>\n" \
|
||||
" <arg name=\"relative\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" </method>\n" \
|
||||
" <method name=\"SetTimezone\">\n" \
|
||||
" <arg name=\"timezone\" type=\"s\" direction=\"in\"/>\n" \
|
||||
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" </method>\n" \
|
||||
" <method name=\"SetLocalRTC\">\n" \
|
||||
" <arg name=\"local_rtc\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" <arg name=\"fix_system\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" </method>\n" \
|
||||
" <method name=\"SetNTP\">\n" \
|
||||
" <arg name=\"use_ntp\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
|
||||
" </method>\n" \
|
||||
" </interface>\n" \
|
||||
"</node>\n"
|
||||
|
||||
#define DEFAULT_EXIT_SEC 300
|
||||
#define USEC_PER_SEC 1000000ULL
|
||||
|
||||
// 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);
|
||||
//gboolean slack_set_time (gint64, gboolean);
|
||||
|
||||
// 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);
|
Loading…
Reference in New Issue
Block a user