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