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 | * |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 22 | |
| 23 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/time.h> |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 26 | #include <sys/utsname.h> |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | #include <fcntl.h> |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <syslog.h> |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 33 | #include <time.h> |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
| 36 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 37 | /* Copied from linux/rtc.h to eliminate the kernel dependency */ |
Robert Griebl | 7ce75f4 | 2003-01-02 07:16:53 +0000 | [diff] [blame] | 38 | struct linux_rtc_time { |
| 39 | int tm_sec; |
| 40 | int tm_min; |
| 41 | int tm_hour; |
| 42 | int tm_mday; |
| 43 | int tm_mon; |
| 44 | int tm_year; |
| 45 | int tm_wday; |
| 46 | int tm_yday; |
| 47 | int tm_isdst; |
| 48 | }; |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 49 | |
Robert Griebl | 7ce75f4 | 2003-01-02 07:16:53 +0000 | [diff] [blame] | 50 | #define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time */ |
| 51 | #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] | 52 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 53 | #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS |
| 54 | # ifndef _GNU_SOURCE |
| 55 | # define _GNU_SOURCE |
| 56 | # endif |
| 57 | #endif |
| 58 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 59 | static time_t read_rtc(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 60 | { |
| 61 | int rtc; |
| 62 | struct tm tm; |
| 63 | char *oldtz = 0; |
| 64 | time_t t = 0; |
| 65 | |
| 66 | if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) { |
| 67 | if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | bb_perror_msg_and_die ( "Could not access RTC" ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 69 | } |
| 70 | memset ( &tm, 0, sizeof( struct tm )); |
| 71 | if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | bb_perror_msg_and_die ( "Could not read time from RTC" ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 73 | tm. tm_isdst = -1; // not known |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 74 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 75 | close ( rtc ); |
| 76 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | if ( utc ) { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 78 | oldtz = getenv ( "TZ" ); |
| 79 | setenv ( "TZ", "UTC 0", 1 ); |
| 80 | tzset ( ); |
| 81 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 82 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 83 | t = mktime ( &tm ); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 84 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 85 | if ( utc ) { |
| 86 | if ( oldtz ) |
| 87 | setenv ( "TZ", oldtz, 1 ); |
| 88 | else |
| 89 | unsetenv ( "TZ" ); |
| 90 | tzset ( ); |
| 91 | } |
| 92 | return t; |
| 93 | } |
| 94 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 95 | static void write_rtc(time_t t, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 96 | { |
| 97 | int rtc; |
| 98 | struct tm tm; |
| 99 | |
| 100 | if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) { |
| 101 | if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | bb_perror_msg_and_die ( "Could not access RTC" ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 104 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 105 | tm = *( utc ? gmtime ( &t ) : localtime ( &t )); |
| 106 | tm. tm_isdst = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 107 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 108 | if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | bb_perror_msg_and_die ( "Could not set the RTC time" ); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 110 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 111 | close ( rtc ); |
| 112 | } |
| 113 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 114 | static int show_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 115 | { |
| 116 | struct tm *ptm; |
| 117 | time_t t; |
| 118 | char buffer [64]; |
| 119 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 120 | t = read_rtc ( utc ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 121 | ptm = localtime ( &t ); /* Sets 'tzname[]' */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 122 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 123 | safe_strncpy ( buffer, ctime ( &t ), sizeof( buffer )); |
| 124 | if ( buffer [0] ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | buffer [bb_strlen ( buffer ) - 1] = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 126 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 127 | //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] )); |
| 128 | printf ( "%s %.6f seconds\n", buffer, 0.0 ); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 129 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 133 | static int to_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 134 | { |
| 135 | struct timeval tv = { 0, 0 }; |
| 136 | const struct timezone tz = { timezone/60 - 60*daylight, 0 }; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 137 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 138 | tv. tv_sec = read_rtc ( utc ); |
| 139 | |
| 140 | if ( settimeofday ( &tv, &tz )) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 141 | bb_perror_msg_and_die ( "settimeofday() failed" ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 146 | static int from_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 147 | { |
| 148 | struct timeval tv = { 0, 0 }; |
| 149 | struct timezone tz = { 0, 0 }; |
| 150 | |
| 151 | if ( gettimeofday ( &tv, &tz )) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 152 | bb_perror_msg_and_die ( "gettimeofday() failed" ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 153 | |
| 154 | write_rtc ( tv. tv_sec, utc ); |
| 155 | return 0; |
| 156 | } |
| 157 | |
Mike Frysinger | 747fc5d | 2005-09-28 03:21:21 +0000 | [diff] [blame] | 158 | #ifdef CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS |
| 159 | # define ADJTIME_PATH "/var/lib/hwclock/adjtime" |
| 160 | #else |
| 161 | # define ADJTIME_PATH "/etc/adjtime" |
| 162 | #endif |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 163 | static int check_utc(void) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 164 | { |
| 165 | int utc = 0; |
Mike Frysinger | 747fc5d | 2005-09-28 03:21:21 +0000 | [diff] [blame] | 166 | FILE *f = fopen ( ADJTIME_PATH, "r" ); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 167 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 168 | if ( f ) { |
| 169 | char buffer [128]; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 170 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 171 | while ( fgets ( buffer, sizeof( buffer ), f )) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | int len = bb_strlen ( buffer ); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 173 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 174 | while ( len && isspace ( buffer [len - 1] )) |
| 175 | len--; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 176 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 177 | buffer [len] = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 178 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 179 | if ( strncmp ( buffer, "UTC", 3 ) == 0 ) { |
| 180 | utc = 1; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | fclose ( f ); |
| 185 | } |
| 186 | return utc; |
| 187 | } |
| 188 | |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 189 | #define HWCLOCK_OPT_LOCALTIME 0x01 |
| 190 | #define HWCLOCK_OPT_UTC 0x02 |
| 191 | #define HWCLOCK_OPT_SHOW 0x04 |
| 192 | #define HWCLOCK_OPT_HCTOSYS 0x08 |
| 193 | #define HWCLOCK_OPT_SYSTOHC 0x10 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 194 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 195 | extern int hwclock_main ( int argc, char **argv ) |
| 196 | { |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 197 | unsigned long opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 198 | int utc; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 199 | |
| 200 | #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 201 | static const struct option hwclock_long_options[] = { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 202 | { "localtime", 0, 0, 'l' }, |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 203 | { "utc", 0, 0, 'u' }, |
| 204 | { "show", 0, 0, 'r' }, |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 205 | { "hctosys", 0, 0, 's' }, |
| 206 | { "systohc", 0, 0, 'w' }, |
| 207 | { 0, 0, 0, 0 } |
| 208 | }; |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 209 | bb_applet_long_options = hwclock_long_options; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 210 | #endif |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 211 | |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 212 | bb_opt_complementally = "?r~ws:w~rs:s~wr:l~u:u~l"; |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 213 | opt = bb_getopt_ulflags(argc, argv, "lursw"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 214 | |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 215 | /* 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] | 216 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 217 | utc = opt & HWCLOCK_OPT_UTC; |
| 218 | else |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 219 | utc = check_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 220 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 221 | if (opt & HWCLOCK_OPT_HCTOSYS) { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 222 | return to_sys_clock ( utc ); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 223 | } |
| 224 | else if (opt & HWCLOCK_OPT_SYSTOHC) { |
| 225 | return from_sys_clock ( utc ); |
| 226 | } else { |
| 227 | /* default HWCLOCK_OPT_SHOW */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 228 | return show_clock ( utc ); |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 229 | } |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 230 | } |