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