]>
Commit | Line | Data |
---|---|---|
bbc98d32 KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010-2012 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
bbc98d32 | 20 | #include <errno.h> |
bbc98d32 | 21 | #include <fcntl.h> |
11c3a366 TA |
22 | #include <limits.h> |
23 | #include <stdbool.h> | |
24 | #include <time.h> | |
07630cea LP |
25 | #include <linux/rtc.h> |
26 | #include <stdio.h> | |
bbc98d32 | 27 | #include <sys/ioctl.h> |
bbc98d32 | 28 | #include <sys/time.h> |
bbc98d32 | 29 | |
3ffd4af2 LP |
30 | #include "clock-util.h" |
31 | #include "fd-util.h" | |
bbc98d32 | 32 | #include "macro.h" |
07630cea | 33 | #include "string-util.h" |
9c4615fb | 34 | #include "util.h" |
bbc98d32 | 35 | |
60989612 | 36 | int clock_get_hwclock(struct tm *tm) { |
8e64fd11 | 37 | _cleanup_close_ int fd = -1; |
bbc98d32 KS |
38 | |
39 | assert(tm); | |
40 | ||
67fb4482 | 41 | fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC); |
bbc98d32 KS |
42 | if (fd < 0) |
43 | return -errno; | |
44 | ||
45 | /* This leaves the timezone fields of struct tm | |
46 | * uninitialized! */ | |
47 | if (ioctl(fd, RTC_RD_TIME, tm) < 0) | |
8e64fd11 | 48 | return -errno; |
bbc98d32 KS |
49 | |
50 | /* We don't know daylight saving, so we reset this in order not | |
2f6a5907 | 51 | * to confuse mktime(). */ |
bbc98d32 KS |
52 | tm->tm_isdst = -1; |
53 | ||
8e64fd11 | 54 | return 0; |
bbc98d32 KS |
55 | } |
56 | ||
60989612 | 57 | int clock_set_hwclock(const struct tm *tm) { |
8e64fd11 | 58 | _cleanup_close_ int fd = -1; |
bbc98d32 KS |
59 | |
60 | assert(tm); | |
61 | ||
67fb4482 | 62 | fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC); |
bbc98d32 KS |
63 | if (fd < 0) |
64 | return -errno; | |
65 | ||
66 | if (ioctl(fd, RTC_SET_TIME, tm) < 0) | |
8e64fd11 | 67 | return -errno; |
bbc98d32 | 68 | |
8e64fd11 | 69 | return 0; |
bbc98d32 | 70 | } |
a866073d | 71 | |
6369641d | 72 | int clock_is_localtime(const char* adjtime_path) { |
7fd1b19b | 73 | _cleanup_fclose_ FILE *f; |
bbc98d32 | 74 | |
6369641d MP |
75 | if (adjtime_path == NULL) |
76 | adjtime_path = "/etc/adjtime"; | |
77 | ||
bbc98d32 KS |
78 | /* |
79 | * The third line of adjtime is "UTC" or "LOCAL" or nothing. | |
80 | * # /etc/adjtime | |
81 | * 0.0 0 0 | |
82 | * 0 | |
83 | * UTC | |
84 | */ | |
6369641d | 85 | f = fopen(adjtime_path, "re"); |
bbc98d32 KS |
86 | if (f) { |
87 | char line[LINE_MAX]; | |
88 | bool b; | |
89 | ||
90 | b = fgets(line, sizeof(line), f) && | |
91 | fgets(line, sizeof(line), f) && | |
92 | fgets(line, sizeof(line), f); | |
bbc98d32 | 93 | if (!b) |
35f7216f MP |
94 | /* less than three lines -> default to UTC */ |
95 | return 0; | |
bbc98d32 KS |
96 | |
97 | truncate_nl(line); | |
8e2f9ebf | 98 | return streq(line, "LOCAL"); |
bbc98d32 | 99 | |
bcb161b0 | 100 | } else if (errno != ENOENT) |
bbc98d32 KS |
101 | return -errno; |
102 | ||
35f7216f | 103 | /* adjtime not present -> default to UTC */ |
8e2f9ebf | 104 | return 0; |
bbc98d32 KS |
105 | } |
106 | ||
24efb112 | 107 | int clock_set_timezone(int *min) { |
bbc98d32 KS |
108 | const struct timeval *tv_null = NULL; |
109 | struct timespec ts; | |
110 | struct tm *tm; | |
19e65613 | 111 | int minutesdelta; |
bbc98d32 KS |
112 | struct timezone tz; |
113 | ||
114 | assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); | |
115 | assert_se(tm = localtime(&ts.tv_sec)); | |
19e65613 | 116 | minutesdelta = tm->tm_gmtoff / 60; |
bbc98d32 | 117 | |
19e65613 | 118 | tz.tz_minuteswest = -minutesdelta; |
cc13b327 | 119 | tz.tz_dsttime = 0; /* DST_NONE */ |
bbc98d32 KS |
120 | |
121 | /* | |
c264aeab KS |
122 | * If the RTC does not run in UTC but in local time, the very first |
123 | * call to settimeofday() will set the kernel's timezone and will warp the | |
124 | * system clock, so that it runs in UTC instead of the local time we | |
125 | * have read from the RTC. | |
bbc98d32 KS |
126 | */ |
127 | if (settimeofday(tv_null, &tz) < 0) | |
9c4615fb ZJS |
128 | return negative_errno(); |
129 | ||
bbc98d32 | 130 | if (min) |
19e65613 | 131 | *min = minutesdelta; |
bbc98d32 KS |
132 | return 0; |
133 | } | |
134 | ||
c264aeab | 135 | int clock_reset_timewarp(void) { |
bbc98d32 KS |
136 | const struct timeval *tv_null = NULL; |
137 | struct timezone tz; | |
138 | ||
139 | tz.tz_minuteswest = 0; | |
cc13b327 | 140 | tz.tz_dsttime = 0; /* DST_NONE */ |
bbc98d32 | 141 | |
72edcff5 | 142 | /* |
c264aeab KS |
143 | * The very first call to settimeofday() does time warp magic. Do a |
144 | * dummy call here, so the time warping is sealed and all later calls | |
145 | * behave as expected. | |
72edcff5 | 146 | */ |
bbc98d32 KS |
147 | if (settimeofday(tv_null, &tz) < 0) |
148 | return -errno; | |
149 | ||
150 | return 0; | |
151 | } | |
021dd87b LP |
152 | |
153 | #define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC) | |
154 | ||
155 | int clock_apply_epoch(void) { | |
156 | struct timespec ts; | |
157 | ||
158 | if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC) | |
159 | return 0; | |
160 | ||
161 | if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0) | |
162 | return -errno; | |
163 | ||
164 | return 1; | |
165 | } |