Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 3 | * Mini hwclock implementation for busybox |
| 4 | * |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 5 | * Copyright (C) 2002 Robert Griebl <griebl@gmx.de> |
| 6 | * |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 8 | */ |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 9 | |
| 10 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 11 | #include <sys/ioctl.h> |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 12 | #include <sys/utsname.h> |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 13 | #include <getopt.h> |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 14 | #include "busybox.h" |
| 15 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 16 | /* Copied from linux/rtc.h to eliminate the kernel dependency */ |
Robert Griebl | 7ce75f4 | 2003-01-02 07:16:53 +0000 | [diff] [blame] | 17 | struct linux_rtc_time { |
| 18 | int tm_sec; |
| 19 | int tm_min; |
| 20 | int tm_hour; |
| 21 | int tm_mday; |
| 22 | int tm_mon; |
| 23 | int tm_year; |
| 24 | int tm_wday; |
| 25 | int tm_yday; |
| 26 | int tm_isdst; |
| 27 | }; |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 28 | |
Robert Griebl | 7ce75f4 | 2003-01-02 07:16:53 +0000 | [diff] [blame] | 29 | #define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time */ |
| 30 | #define RTC_RD_TIME _IOR('p', 0x09, struct linux_rtc_time) /* Read RTC time */ |
Eric Andersen | 8882ea5 | 2002-12-11 03:41:28 +0000 | [diff] [blame] | 31 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 32 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 33 | # ifndef _GNU_SOURCE |
| 34 | # define _GNU_SOURCE |
| 35 | # endif |
| 36 | #endif |
| 37 | |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 38 | static const char *rtcname; |
| 39 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 40 | static int xopen_rtc(int flags) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 41 | { |
| 42 | int rtc; |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 43 | |
| 44 | if (!rtcname) { |
| 45 | rtc = open("/dev/rtc", flags); |
| 46 | if (rtc >= 0) |
| 47 | return rtc; |
| 48 | rtc = open("/dev/rtc0", flags); |
| 49 | if (rtc >= 0) |
| 50 | return rtc; |
| 51 | rtcname = "/dev/misc/rtc"; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 52 | } |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 53 | return xopen(rtcname, flags); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static time_t read_rtc(int utc) |
| 57 | { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 58 | struct tm tm; |
| 59 | char *oldtz = 0; |
| 60 | time_t t = 0; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 61 | int rtc = xopen_rtc(O_RDONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 63 | memset(&tm, 0, sizeof(struct tm)); |
Denis Vlasenko | 219d14d | 2007-03-24 15:40:16 +0000 | [diff] [blame] | 64 | if (ioctl(rtc, RTC_RD_TIME, &tm) < 0) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 65 | bb_perror_msg_and_die("cannot read time from RTC"); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 66 | tm.tm_isdst = -1; /* not known */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 68 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 70 | if (utc) { |
| 71 | oldtz = getenv("TZ"); |
| 72 | setenv("TZ", "UTC 0", 1); |
| 73 | tzset(); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 74 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 76 | t = mktime(&tm); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 78 | if (utc) { |
| 79 | if (oldtz) |
| 80 | setenv("TZ", oldtz, 1); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 81 | else |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 82 | unsetenv("TZ"); |
| 83 | tzset(); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 84 | } |
| 85 | return t; |
| 86 | } |
| 87 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 88 | static void write_rtc(time_t t, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 89 | { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 90 | struct tm tm; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 91 | int rtc = xopen_rtc(O_WRONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 93 | tm = *(utc ? gmtime(&t) : localtime(&t)); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 94 | tm.tm_isdst = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 95 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 96 | if (ioctl(rtc, RTC_SET_TIME, &tm) < 0) |
| 97 | bb_perror_msg_and_die("cannot set the RTC time"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 99 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 102 | static int show_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 103 | { |
| 104 | struct tm *ptm; |
| 105 | time_t t; |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 106 | RESERVE_CONFIG_BUFFER(buffer, 64); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 108 | t = read_rtc(utc); |
| 109 | ptm = localtime(&t); /* Sets 'tzname[]' */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 111 | safe_strncpy(buffer, ctime(&t), 64); |
| 112 | if (buffer[0]) |
| 113 | buffer[strlen(buffer) - 1] = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 114 | |
Denis Vlasenko | dd539f7 | 2006-11-01 20:20:37 +0000 | [diff] [blame] | 115 | //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0])); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 116 | printf( "%s %.6f seconds\n", buffer, 0.0); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 117 | RELEASE_CONFIG_BUFFER(buffer); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 118 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 122 | static int to_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 123 | { |
| 124 | struct timeval tv = { 0, 0 }; |
| 125 | const struct timezone tz = { timezone/60 - 60*daylight, 0 }; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 127 | tv.tv_sec = read_rtc(utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 129 | if (settimeofday(&tv, &tz)) |
| 130 | bb_perror_msg_and_die("settimeofday() failed"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 135 | static int from_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 136 | { |
| 137 | struct timeval tv = { 0, 0 }; |
| 138 | struct timezone tz = { 0, 0 }; |
| 139 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 140 | if (gettimeofday(&tv, &tz)) |
| 141 | bb_perror_msg_and_die("gettimeofday() failed"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 143 | write_rtc(tv.tv_sec, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Mike Frysinger | 747fc5d | 2005-09-28 03:21:21 +0000 | [diff] [blame] | 147 | #ifdef CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS |
| 148 | # define ADJTIME_PATH "/var/lib/hwclock/adjtime" |
| 149 | #else |
| 150 | # define ADJTIME_PATH "/etc/adjtime" |
| 151 | #endif |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 152 | static int check_utc(void) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 153 | { |
| 154 | int utc = 0; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 155 | FILE *f = fopen(ADJTIME_PATH, "r"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 156 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 157 | if (f) { |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 158 | RESERVE_CONFIG_BUFFER(buffer, 128); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 159 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 160 | while (fgets(buffer, sizeof(buffer), f)) { |
| 161 | int len = strlen(buffer); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 163 | while (len && isspace(buffer[len - 1])) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 164 | len--; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 166 | buffer[len] = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | 219d14d | 2007-03-24 15:40:16 +0000 | [diff] [blame] | 168 | if (strncmp(buffer, "UTC", 3) == 0) { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 169 | utc = 1; |
| 170 | break; |
| 171 | } |
| 172 | } |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 173 | fclose(f); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 174 | RELEASE_CONFIG_BUFFER(buffer); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 175 | } |
| 176 | return utc; |
| 177 | } |
| 178 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 179 | #define HWCLOCK_OPT_LOCALTIME 0x01 |
| 180 | #define HWCLOCK_OPT_UTC 0x02 |
| 181 | #define HWCLOCK_OPT_SHOW 0x04 |
| 182 | #define HWCLOCK_OPT_HCTOSYS 0x08 |
| 183 | #define HWCLOCK_OPT_SYSTOHC 0x10 |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 184 | #define HWCLOCK_OPT_RTCFILE 0x20 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 186 | int hwclock_main(int argc, char **argv ); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 187 | int hwclock_main(int argc, char **argv ) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 188 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 189 | unsigned opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 190 | int utc; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 191 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 192 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 193 | static const struct option hwclock_long_options[] = { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 194 | { "localtime", 0, 0, 'l' }, |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 195 | { "utc", 0, 0, 'u' }, |
| 196 | { "show", 0, 0, 'r' }, |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 197 | { "hctosys", 0, 0, 's' }, |
| 198 | { "systohc", 0, 0, 'w' }, |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 199 | { "file", 1, 0, 'f' }, |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 200 | { 0, 0, 0, 0 } |
| 201 | }; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 202 | applet_long_options = hwclock_long_options; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 203 | #endif |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 204 | opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l"; |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 205 | opt = getopt32(argc, argv, "lurswf:", &rtcname); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 206 | |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 207 | /* If -u or -l wasn't given check if we are using utc */ |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 208 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 209 | utc = opt & HWCLOCK_OPT_UTC; |
| 210 | else |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 211 | utc = check_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 212 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 213 | if (opt & HWCLOCK_OPT_HCTOSYS) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 214 | return to_sys_clock(utc); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 215 | } |
| 216 | else if (opt & HWCLOCK_OPT_SYSTOHC) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 217 | return from_sys_clock(utc); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 218 | } else { |
| 219 | /* default HWCLOCK_OPT_SHOW */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 220 | return show_clock(utc); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 221 | } |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 222 | } |