]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/clock-util.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / basic / 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 <fcntl.h>
24 #include <linux/rtc.h>
25 #include <stdio.h>
26 #include <sys/ioctl.h>
27 #include <sys/time.h>
28
29 #include "macro.h"
30 #include "string-util.h"
31 #include "util.h"
32 #include "clock-util.h"
33
34 int clock_get_hwclock(struct tm *tm) {
35 _cleanup_close_ int fd = -1;
36
37 assert(tm);
38
39 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
40 if (fd < 0)
41 return -errno;
42
43 /* This leaves the timezone fields of struct tm
44 * uninitialized! */
45 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
46 return -errno;
47
48 /* We don't know daylight saving, so we reset this in order not
49 * to confuse mktime(). */
50 tm->tm_isdst = -1;
51
52 return 0;
53 }
54
55 int clock_set_hwclock(const struct tm *tm) {
56 _cleanup_close_ int fd = -1;
57
58 assert(tm);
59
60 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
61 if (fd < 0)
62 return -errno;
63
64 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
65 return -errno;
66
67 return 0;
68 }
69
70 int clock_is_localtime(void) {
71 _cleanup_fclose_ FILE *f;
72
73 /*
74 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
75 * # /etc/adjtime
76 * 0.0 0 0
77 * 0
78 * UTC
79 */
80 f = fopen("/etc/adjtime", "re");
81 if (f) {
82 char line[LINE_MAX];
83 bool b;
84
85 b = fgets(line, sizeof(line), f) &&
86 fgets(line, sizeof(line), f) &&
87 fgets(line, sizeof(line), f);
88 if (!b)
89 return -EIO;
90
91 truncate_nl(line);
92 return streq(line, "LOCAL");
93
94 } else if (errno != ENOENT)
95 return -errno;
96
97 return 0;
98 }
99
100 int clock_set_timezone(int *min) {
101 const struct timeval *tv_null = NULL;
102 struct timespec ts;
103 struct tm *tm;
104 int minutesdelta;
105 struct timezone tz;
106
107 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
108 assert_se(tm = localtime(&ts.tv_sec));
109 minutesdelta = tm->tm_gmtoff / 60;
110
111 tz.tz_minuteswest = -minutesdelta;
112 tz.tz_dsttime = 0; /* DST_NONE */
113
114 /*
115 * If the RTC does not run in UTC but in local time, the very first
116 * call to settimeofday() will set the kernel's timezone and will warp the
117 * system clock, so that it runs in UTC instead of the local time we
118 * have read from the RTC.
119 */
120 if (settimeofday(tv_null, &tz) < 0)
121 return -errno;
122 if (min)
123 *min = minutesdelta;
124 return 0;
125 }
126
127 int clock_reset_timewarp(void) {
128 const struct timeval *tv_null = NULL;
129 struct timezone tz;
130
131 tz.tz_minuteswest = 0;
132 tz.tz_dsttime = 0; /* DST_NONE */
133
134 /*
135 * The very first call to settimeofday() does time warp magic. Do a
136 * dummy call here, so the time warping is sealed and all later calls
137 * behave as expected.
138 */
139 if (settimeofday(tv_null, &tz) < 0)
140 return -errno;
141
142 return 0;
143 }