chiark / gitweb /
remove unused includes
[elogind.git] / src / shared / clock-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010-2012 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #include <sys/time.h>
27 #include <linux/rtc.h>
28
29 #include "macro.h"
30 #include "util.h"
31 #include "clock-util.h"
32
33 int clock_get_hwclock(struct tm *tm) {
34         _cleanup_close_ int fd = -1;
35
36         assert(tm);
37
38         fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
39         if (fd < 0)
40                 return -errno;
41
42         /* This leaves the timezone fields of struct tm
43          * uninitialized! */
44         if (ioctl(fd, RTC_RD_TIME, tm) < 0)
45                 return -errno;
46
47         /* We don't know daylight saving, so we reset this in order not
48          * to confuse mktime(). */
49         tm->tm_isdst = -1;
50
51         return 0;
52 }
53
54 int clock_set_hwclock(const struct tm *tm) {
55         _cleanup_close_ int fd = -1;
56
57         assert(tm);
58
59         fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
60         if (fd < 0)
61                 return -errno;
62
63         if (ioctl(fd, RTC_SET_TIME, tm) < 0)
64                 return -errno;
65
66         return 0;
67 }
68
69 int clock_is_localtime(void) {
70         _cleanup_fclose_ FILE *f;
71
72         /*
73          * The third line of adjtime is "UTC" or "LOCAL" or nothing.
74          *   # /etc/adjtime
75          *   0.0 0 0
76          *   0
77          *   UTC
78          */
79         f = fopen("/etc/adjtime", "re");
80         if (f) {
81                 char line[LINE_MAX];
82                 bool b;
83
84                 b = fgets(line, sizeof(line), f) &&
85                         fgets(line, sizeof(line), f) &&
86                         fgets(line, sizeof(line), f);
87                 if (!b)
88                         return -EIO;
89
90                 truncate_nl(line);
91                 return streq(line, "LOCAL");
92
93         } else if (errno != ENOENT)
94                 return -errno;
95
96         return 0;
97 }
98
99 int clock_set_timezone(int *min) {
100         const struct timeval *tv_null = NULL;
101         struct timespec ts;
102         struct tm *tm;
103         int minutesdelta;
104         struct timezone tz;
105
106         assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
107         assert_se(tm = localtime(&ts.tv_sec));
108         minutesdelta = tm->tm_gmtoff / 60;
109
110         tz.tz_minuteswest = -minutesdelta;
111         tz.tz_dsttime = 0; /* DST_NONE */
112
113         /*
114          * If the RTC does not run in UTC but in local time, the very first
115          * call to settimeofday() will set the kernel's timezone and will warp the
116          * system clock, so that it runs in UTC instead of the local time we
117          * have read from the RTC.
118          */
119         if (settimeofday(tv_null, &tz) < 0)
120                 return -errno;
121         if (min)
122                 *min = minutesdelta;
123         return 0;
124 }
125
126 int clock_reset_timewarp(void) {
127         const struct timeval *tv_null = NULL;
128         struct timezone tz;
129
130         tz.tz_minuteswest = 0;
131         tz.tz_dsttime = 0; /* DST_NONE */
132
133         /*
134          * The very first call to settimeofday() does time warp magic. Do a
135          * dummy call here, so the time warping is sealed and all later calls
136          * behave as expected.
137          */
138         if (settimeofday(tv_null, &tz) < 0)
139                 return -errno;
140
141         return 0;
142 }