timedated.c revision f401e48c2db22ff9d1a05885b5599bebf19c2707
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
#include "strv.h"
#include "dbus-common.h"
#include "polkit.h"
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
#define INTROSPECTION \
"<node>\n" \
" <interface name=\"org.freedesktop.timedate1\">\n" \
" <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"LocalRTC\" 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=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" </interface>\n" \
"</node>\n"
#define INTERFACES_LIST \
"org.freedesktop.locale1\0"
static bool local_rtc = false;
static void free_data(void) {
local_rtc = false;
}
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 void verify_timezone(void) {
size_t l, q;
int j, k;
if (!zone)
return;
if (!p) {
log_error("Out of memory");
return;
}
j = read_full_file("/etc/localtime", &a, &l);
k = read_full_file(p, &b, &q);
free(p);
if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
log_warning("/etc/localtime and /etc/timezone out of sync.");
}
free(a);
free(b);
}
static int read_data(void) {
int r;
FILE *f;
free_data();
if (r < 0 && r != -ENOENT)
return r;
if (f) {
bool b;
fclose(f);
if (!b)
return -EIO;
return -errno;
return 0;
}
static int write_data_timezone(void) {
int r = 0;
char *p;
if (!zone) {
r = -errno;
r = -errno;
return r;
}
if (!p) {
log_error("Out of memory");
return -ENOMEM;
}
r = symlink_or_copy_atomic(p, "/etc/localtime");
free(p);
if (r < 0)
return r;
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;
if (!local_rtc)
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 (!p) {
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;
}
}
r = write_one_line_file_atomic("/etc/adjtime", w);
free(w);
return r;
}
void *userdata) {
const BusProperty properties[] = {
};
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, &error);
if (r < 0)
t = strdup(z);
if (!t)
goto oom;
zone = t;
r = write_data_timezone();
if (r < 0) {
}
"/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, &error);
if (r < 0)
r = write_data_local_rtc();
if (r < 0) {
}
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
"LocalRTC\0");
if (!changed)
goto oom;
}
if (!dbus_message_get_args(
&error,
if (r < 0)
if (relative)
else
log_error("Failed to set local time: %m");
}
}
} else
return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
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 fail;
}
if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL)) {
log_error("Not enough memory");
r = -ENOMEM;
goto fail;
}
if (dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
r = -EEXIST;
goto fail;
}
if (_bus)
return 0;
fail:
return r;
}
int r;
log_open();
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
umask(0022);
r = read_data();
if (r < 0) {
goto finish;
}
r = connect_bus(&bus);
if (r < 0)
goto finish;
;
r = 0;
free_data();
if (bus) {
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}