timedated.c revision a5c32cff1f56afe6f0c6c70d91a88a7a8238b2d7
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
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.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "systemd/sd-id128.h"
#include "systemd/sd-messages.h"
#include "util.h"
#include "strv.h"
#include "dbus-common.h"
#include "polkit.h"
#include "def.h"
#include "hwclock.h"
#include "conf-files.h"
#include "path-util.h"
#include "fileio-label.h"
#include "label.h"
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
#define INTERFACE \
" <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"
#define INTROSPECTION \
"<node>\n" \
"</node>\n"
#define INTERFACES_LIST \
"org.freedesktop.timedate1\0"
typedef struct TZ {
char *zone;
bool local_rtc;
int use_ntp;
} TZ;
.use_ntp = -1,
};
static usec_t remain_until;
static void free_data(void) {
}
static bool valid_timezone(const char *name) {
const char *p;
char *t;
bool slash = false;
int r;
return false;
for (p = name; *p; p++) {
if (!(*p >= '0' && *p <= '9') &&
!(*p >= 'a' && *p <= 'z') &&
!(*p >= 'A' && *p <= 'Z') &&
!(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
return false;
if (*p == '/') {
if (slash)
return false;
slash = true;
} else
slash = false;
}
if (slash)
return false;
if (!t)
return false;
free(t);
if (r < 0)
return false;
return false;
return true;
}
static int read_data(void) {
int r;
_cleanup_free_ char *t = NULL;
free_data();
r = readlink_malloc("/etc/localtime", &t);
if (r < 0) {
if (r == -EINVAL)
log_warning("/etc/localtime should be a symbolic link to a timezone data file in /usr/share/zoneinfo/.");
else
} else {
const char *e;
e = path_startswith(t, "/usr/share/zoneinfo/");
if (!e)
e = path_startswith(t, "../usr/share/zoneinfo/");
if (!e)
log_warning("/etc/localtime should be a symbolic link to a timezone data file in /usr/share/zoneinfo/.");
else {
return log_oom();
goto have_timezone;
}
}
}
return 0;
}
static int write_data_timezone(void) {
int r = 0;
_cleanup_free_ char *p = NULL;
r = -errno;
return r;
}
if (!p)
return log_oom();
r = symlink_atomic(p, "/etc/localtime");
if (r < 0)
return r;
return 0;
}
static int write_data_local_rtc(void) {
int r;
char *s, *w;
if (r < 0) {
if (r != -ENOENT)
return r;
return 0;
w = strdup(NULL_ADJTIME_LOCAL);
if (!w)
return -ENOMEM;
} else {
char *p, *e;
size_t a, b;
p = strchr(s, '\n');
if (!p) {
free(s);
return -EIO;
}
if (!p) {
free(s);
return -EIO;
}
p++;
e = strchr(p, '\n');
if (!e) {
free(s);
return -EIO;
}
a = p - s;
b = strlen(e);
if (!w) {
free(s);
return -ENOMEM;
}
if (streq(w, NULL_ADJTIME_UTC)) {
free(w);
return -errno;
}
return 0;
}
}
label_init("/etc");
r = write_one_line_file_atomic_label("/etc/adjtime", w);
free(w);
return r;
}
static char** get_ntp_services(void) {
int k;
"/etc/systemd/ntp-units.d",
"/run/systemd/ntp-units.d",
"/usr/local/lib/systemd/ntp-units.d",
"/usr/lib/systemd/ntp-units.d",
NULL);
if (k < 0)
return NULL;
STRV_FOREACH(i, files) {
FILE *f;
f = fopen(*i, "re");
if (!f)
continue;
for (;;) {
if (ferror(f))
log_error("Failed to read NTP units file: %m");
break;
}
if (l[0] == 0 || l[0] == '#')
continue;
q = strv_append(r, l);
if (!q) {
log_oom();
break;
}
strv_free(r);
r = q;
}
fclose(f);
}
return strv_uniq(r);
}
int r;
char **i, **l;
l = get_ntp_services();
STRV_FOREACH(i, l) {
const char *s;
if (m)
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitFileState");
if (!m) {
r = log_oom();
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, i,
r = log_oom();
goto finish;
}
if (reply)
if (!reply) {
/* This implementation does not exist, try next one */
continue;
}
r = -EIO;
goto finish;
}
DBUS_TYPE_STRING, &s,
r = -EIO;
goto finish;
}
streq(s, "enabled") ||
streq(s, "enabled-runtime");
r = 0;
goto finish;
}
/* NTP is not installed. */
r = 0;
if (m)
if (reply)
strv_free(l);
return r;
}
const char *mode = "replace";
char **i, **l;
int r;
l = get_ntp_services();
STRV_FOREACH(i, l) {
if (m)
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
if (!m) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, i,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
if (reply)
if (!reply) {
/* This implementation does not exist, try next one */
continue;
}
r = -EIO;
goto finish;
}
r = 0;
goto finish;
}
/* No implementaiton available... */
r = -ENOENT;
if (m)
if (reply)
strv_free(l);
return r;
}
int r;
char **i, **l;
l = get_ntp_services();
STRV_FOREACH(i, l) {
char* k[2];
if (m)
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
if (!m) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
k[0] = *i;
k[1] = NULL;
r = bus_append_strv_iter(&iter, k);
if (r < 0) {
log_error("Failed to append unit files.");
goto finish;
}
/* send runtime bool */
log_error("Failed to append runtime boolean.");
r = -ENOMEM;
goto finish;
}
/* send force bool */
log_error("Failed to append force boolean.");
r = -ENOMEM;
goto finish;
}
}
if (reply)
if (!reply) {
/* This implementation does not exist, try next one */
continue;
}
r = -EIO;
goto finish;
}
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Reload");
if (!m) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!reply) {
r = -EIO;
goto finish;
}
r = 0;
goto finish;
}
r = -ENOENT;
if (m)
if (reply)
strv_free(l);
return r;
}
assert(i);
return -ENOMEM;
return 0;
}
static const BusProperty bus_timedate_properties[] = {
{ NULL, }
};
static const BusBoundProperties bps[] = {
{ NULL, }
};
void *userdata) {
int r;
const char *z;
if (!dbus_message_get_args(
&error,
DBUS_TYPE_STRING, &z,
if (!valid_timezone(z))
char *t;
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, NULL, &error);
if (r < 0)
t = strdup(z);
if (!t)
goto oom;
/* 1. Write new configuration file */
r = write_data_timezone();
if (r < 0) {
}
/* 2. Tell the kernel our time zone */
/* 3. Sync RTC from system clock, with the new delta */
}
NULL);
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
"Timezone\0");
if (!changed)
goto oom;
}
if (!dbus_message_get_args(
&error,
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, NULL, &error);
if (r < 0)
/* 1. Write new configuration file */
r = write_data_local_rtc();
if (r < 0) {
}
/* 2. Tell the kernel our time zone */
/* 3. Synchronize clocks */
if (fix_system) {
/* Sync system clock from RTC; first,
* initialize the timezone fields of
* struct tm. */
else
/* Override the main fields of
* struct tm, but not the timezone
* fields */
if (hwclock_get_time(&tm) >= 0) {
/* And set the system clock
* with this */
else
}
} else {
/* Sync RTC from system clock */
else
}
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
"LocalRTC\0");
if (!changed)
goto oom;
}
if (!dbus_message_get_args(
&error,
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, NULL, &error);
if (r < 0)
if (relative)
else
/* Set system clock */
log_error("Failed to set local time: %m");
}
/* Sync down to RTC */
else
NULL);
}
if (!dbus_message_get_args(
&error,
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-ntp", interactive, NULL, &error);
if (r < 0)
if (r < 0)
if (r < 0)
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
"NTP\0");
if (!changed)
goto oom;
}
} else
goto oom;
goto oom;
if (changed) {
goto oom;
}
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
if (changed)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static const DBusObjectPathVTable timedate_vtable = {
};
int r;
if (!bus) {
r = -ECONNREFUSED;
goto fail2;
}
if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL) ||
r = log_oom();
goto fail;
}
if (dbus_error_is_set(&error)) {
r = -EEXIST;
goto fail;
}
if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
r = -EEXIST;
goto fail;
}
if (_bus)
return 0;
fail:
return r;
}
int r;
bool exiting = false;
log_open();
umask(0022);
"<node>\n", stdout);
return 0;
}
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
r = read_data();
if (r < 0) {
goto finish;
}
r = connect_bus(&bus);
if (r < 0)
goto finish;
if (r < 0) {
goto finish;
}
for (;;) {
if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
break;
exiting = true;
}
}
r = 0;
free_data();
if (bus) {
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}