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