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