Line data Source code
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 0 : int clock_get_hwclock(struct tm *tm) {
34 0 : _cleanup_close_ int fd = -1;
35 :
36 0 : assert(tm);
37 :
38 0 : fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
39 0 : if (fd < 0)
40 0 : return -errno;
41 :
42 : /* This leaves the timezone fields of struct tm
43 : * uninitialized! */
44 0 : if (ioctl(fd, RTC_RD_TIME, tm) < 0)
45 0 : return -errno;
46 :
47 : /* We don't know daylight saving, so we reset this in order not
48 : * to confuse mktime(). */
49 0 : tm->tm_isdst = -1;
50 :
51 0 : return 0;
52 : }
53 :
54 0 : int clock_set_hwclock(const struct tm *tm) {
55 0 : _cleanup_close_ int fd = -1;
56 :
57 0 : assert(tm);
58 :
59 0 : fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
60 0 : if (fd < 0)
61 0 : return -errno;
62 :
63 0 : if (ioctl(fd, RTC_SET_TIME, tm) < 0)
64 0 : return -errno;
65 :
66 0 : return 0;
67 : }
68 :
69 0 : int clock_is_localtime(void) {
70 0 : _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 0 : f = fopen("/etc/adjtime", "re");
80 0 : if (f) {
81 : char line[LINE_MAX];
82 : bool b;
83 :
84 0 : b = fgets(line, sizeof(line), f) &&
85 0 : fgets(line, sizeof(line), f) &&
86 0 : fgets(line, sizeof(line), f);
87 0 : if (!b)
88 0 : return -EIO;
89 :
90 0 : truncate_nl(line);
91 0 : return streq(line, "LOCAL");
92 :
93 0 : } else if (errno != ENOENT)
94 0 : return -errno;
95 :
96 0 : return 0;
97 : }
98 :
99 0 : int clock_set_timezone(int *min) {
100 0 : const struct timeval *tv_null = NULL;
101 : struct timespec ts;
102 : struct tm *tm;
103 : int minutesdelta;
104 : struct timezone tz;
105 :
106 0 : assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
107 0 : assert_se(tm = localtime(&ts.tv_sec));
108 0 : minutesdelta = tm->tm_gmtoff / 60;
109 :
110 0 : tz.tz_minuteswest = -minutesdelta;
111 0 : 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 0 : if (settimeofday(tv_null, &tz) < 0)
120 0 : return -errno;
121 0 : if (min)
122 0 : *min = minutesdelta;
123 0 : return 0;
124 : }
125 :
126 0 : int clock_reset_timewarp(void) {
127 0 : const struct timeval *tv_null = NULL;
128 : struct timezone tz;
129 :
130 0 : tz.tz_minuteswest = 0;
131 0 : 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 0 : if (settimeofday(tv_null, &tz) < 0)
139 0 : return -errno;
140 :
141 0 : return 0;
142 : }
|