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 | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 38 | static int xopen_rtc(int flags) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 39 | { |
| 40 | int rtc; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 41 | rtc = open("/dev/rtc", flags); |
| 42 | if (rtc < 0) { |
| 43 | rtc = open("/dev/misc/rtc", flags); |
| 44 | if (rtc < 0) |
| 45 | bb_perror_msg_and_die("cannot access RTC"); |
| 46 | } |
| 47 | return rtc; |
| 48 | } |
| 49 | |
| 50 | static time_t read_rtc(int utc) |
| 51 | { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 52 | struct tm tm; |
| 53 | char *oldtz = 0; |
| 54 | time_t t = 0; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 55 | int rtc = xopen_rtc(O_RDONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 57 | memset(&tm, 0, sizeof(struct tm)); |
| 58 | if (ioctl(rtc, RTC_RD_TIME, &tm) < 0 ) |
| 59 | bb_perror_msg_and_die("cannot read time from RTC"); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 60 | tm.tm_isdst = -1; /* not known */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 62 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 64 | if (utc) { |
| 65 | oldtz = getenv("TZ"); |
| 66 | setenv("TZ", "UTC 0", 1); |
| 67 | tzset(); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 68 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 70 | t = mktime(&tm); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 72 | if (utc) { |
| 73 | if (oldtz) |
| 74 | setenv("TZ", oldtz, 1); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 75 | else |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 76 | unsetenv("TZ"); |
| 77 | tzset(); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 78 | } |
| 79 | return t; |
| 80 | } |
| 81 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 82 | static void write_rtc(time_t t, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 83 | { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 84 | struct tm tm; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 85 | int rtc = xopen_rtc(O_WRONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 87 | tm = *(utc ? gmtime(&t) : localtime(&t)); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 88 | tm.tm_isdst = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 90 | if (ioctl(rtc, RTC_SET_TIME, &tm) < 0) |
| 91 | bb_perror_msg_and_die("cannot set the RTC time"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 93 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 96 | static int show_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 97 | { |
| 98 | struct tm *ptm; |
| 99 | time_t t; |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 100 | RESERVE_CONFIG_BUFFER(buffer, 64); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 102 | t = read_rtc(utc); |
| 103 | ptm = localtime(&t); /* Sets 'tzname[]' */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 105 | safe_strncpy(buffer, ctime(&t), 64); |
| 106 | if (buffer[0]) |
| 107 | buffer[strlen(buffer) - 1] = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | dd539f7 | 2006-11-01 20:20:37 +0000 | [diff] [blame] | 109 | //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] | 110 | printf( "%s %.6f seconds\n", buffer, 0.0); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 111 | RELEASE_CONFIG_BUFFER(buffer); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 112 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 116 | static int to_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 117 | { |
| 118 | struct timeval tv = { 0, 0 }; |
| 119 | const struct timezone tz = { timezone/60 - 60*daylight, 0 }; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 121 | tv.tv_sec = read_rtc(utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 123 | if (settimeofday(&tv, &tz)) |
| 124 | bb_perror_msg_and_die("settimeofday() failed"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 129 | static int from_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 130 | { |
| 131 | struct timeval tv = { 0, 0 }; |
| 132 | struct timezone tz = { 0, 0 }; |
| 133 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 134 | if (gettimeofday(&tv, &tz)) |
| 135 | bb_perror_msg_and_die("gettimeofday() failed"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 137 | write_rtc(tv.tv_sec, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Mike Frysinger | 747fc5d | 2005-09-28 03:21:21 +0000 | [diff] [blame] | 141 | #ifdef CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS |
| 142 | # define ADJTIME_PATH "/var/lib/hwclock/adjtime" |
| 143 | #else |
| 144 | # define ADJTIME_PATH "/etc/adjtime" |
| 145 | #endif |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 146 | static int check_utc(void) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 147 | { |
| 148 | int utc = 0; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 149 | FILE *f = fopen(ADJTIME_PATH, "r"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 151 | if (f) { |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 152 | RESERVE_CONFIG_BUFFER(buffer, 128); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 154 | while (fgets(buffer, sizeof(buffer), f)) { |
| 155 | int len = strlen(buffer); |
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 | while (len && isspace(buffer[len - 1])) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 158 | len--; |
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 | buffer[len] = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 162 | if (strncmp(buffer, "UTC", 3) == 0 ) { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 163 | utc = 1; |
| 164 | break; |
| 165 | } |
| 166 | } |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 167 | fclose(f); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 168 | RELEASE_CONFIG_BUFFER(buffer); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 169 | } |
| 170 | return utc; |
| 171 | } |
| 172 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 173 | #define HWCLOCK_OPT_LOCALTIME 0x01 |
| 174 | #define HWCLOCK_OPT_UTC 0x02 |
| 175 | #define HWCLOCK_OPT_SHOW 0x04 |
| 176 | #define HWCLOCK_OPT_HCTOSYS 0x08 |
| 177 | #define HWCLOCK_OPT_SYSTOHC 0x10 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 178 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 179 | int hwclock_main(int argc, char **argv ) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 180 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 181 | unsigned opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 182 | int utc; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 183 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 184 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 185 | static const struct option hwclock_long_options[] = { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 186 | { "localtime", 0, 0, 'l' }, |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 187 | { "utc", 0, 0, 'u' }, |
| 188 | { "show", 0, 0, 'r' }, |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 189 | { "hctosys", 0, 0, 's' }, |
| 190 | { "systohc", 0, 0, 'w' }, |
| 191 | { 0, 0, 0, 0 } |
| 192 | }; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 193 | applet_long_options = hwclock_long_options; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 194 | #endif |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 195 | opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l"; |
| 196 | opt = getopt32(argc, argv, "lursw"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 197 | |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 198 | /* 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] | 199 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 200 | utc = opt & HWCLOCK_OPT_UTC; |
| 201 | else |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 202 | utc = check_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 203 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 204 | if (opt & HWCLOCK_OPT_HCTOSYS) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 205 | return to_sys_clock(utc); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 206 | } |
| 207 | else if (opt & HWCLOCK_OPT_SYSTOHC) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 208 | return from_sys_clock(utc); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 209 | } else { |
| 210 | /* default HWCLOCK_OPT_SHOW */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 211 | return show_clock(utc); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 212 | } |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 213 | } |