clock-util.c revision 021dd87bc055a5bfb2dcef83fc868fe24648b959
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering/***
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering This file is part of systemd.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Copyright 2010-2012 Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is free software; you can redistribute it and/or modify it
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering under the terms of the GNU Lesser General Public License as published by
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (at your option) any later version.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is distributed in the hope that it will be useful, but
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Lesser General Public License for more details.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering You should have received a copy of the GNU Lesser General Public License
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering***/
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <errno.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <fcntl.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <limits.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <stdbool.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <time.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <linux/rtc.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <stdio.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <sys/ioctl.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <sys/time.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "clock-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "fd-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "macro.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "string-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint clock_get_hwclock(struct tm *tm) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_close_ int fd = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(tm);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (fd < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* This leaves the timezone fields of struct tm
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * uninitialized! */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (ioctl(fd, RTC_RD_TIME, tm) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* We don't know daylight saving, so we reset this in order not
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * to confuse mktime(). */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering tm->tm_isdst = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint clock_set_hwclock(const struct tm *tm) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_close_ int fd = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(tm);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (fd < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (ioctl(fd, RTC_SET_TIME, tm) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint clock_is_localtime(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_fclose_ FILE *f;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /*
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * The third line of adjtime is "UTC" or "LOCAL" or nothing.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * # /etc/adjtime
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * 0.0 0 0
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * 0
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * UTC
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering f = fopen("/etc/adjtime", "re");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (f) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char line[LINE_MAX];
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering bool b;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering b = fgets(line, sizeof(line), f) &&
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fgets(line, sizeof(line), f) &&
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fgets(line, sizeof(line), f);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!b)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -EIO;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering truncate_nl(line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return streq(line, "LOCAL");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering } else if (errno != ENOENT)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint clock_set_timezone(int *min) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const struct timeval *tv_null = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct timespec ts;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct tm *tm;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int minutesdelta;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct timezone tz;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(tm = localtime(&ts.tv_sec));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering minutesdelta = tm->tm_gmtoff / 60;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering tz.tz_minuteswest = -minutesdelta;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering tz.tz_dsttime = 0; /* DST_NONE */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /*
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * If the RTC does not run in UTC but in local time, the very first
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * call to settimeofday() will set the kernel's timezone and will warp the
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * system clock, so that it runs in UTC instead of the local time we
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * have read from the RTC.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (settimeofday(tv_null, &tz) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return negative_errno();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (min)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *min = minutesdelta;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint clock_reset_timewarp(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const struct timeval *tv_null = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct timezone tz;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering tz.tz_minuteswest = 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering tz.tz_dsttime = 0; /* DST_NONE */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /*
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * The very first call to settimeofday() does time warp magic. Do a
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * dummy call here, so the time warping is sealed and all later calls
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * behave as expected.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (settimeofday(tv_null, &tz) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint clock_apply_epoch(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct timespec ts;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering