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