hwclock.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini hwclock implementation for busybox
  4. *
  5. * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* After libbb.h, since it needs sys/types.h on some systems */
  11. #include <sys/utsname.h>
  12. #include "rtc_.h"
  13. /* diff code is disabled: it's not sys/hw clock diff, it's some useless
  14. * "time between hwclock was started and we saw CMOS tick" quantity.
  15. * It's useless since hwclock is started at a random moment,
  16. * thus the quantity is also random, useless. Showing 0.000000 does not
  17. * deprive us from any useful info.
  18. *
  19. * SHOW_HWCLOCK_DIFF code in this file shows the difference between system
  20. * and hw clock. It is useful, but not compatible with standard hwclock.
  21. * Thus disabled.
  22. */
  23. #define SHOW_HWCLOCK_DIFF 0
  24. #if !SHOW_HWCLOCK_DIFF
  25. # define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
  26. #endif
  27. static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
  28. {
  29. struct tm tm_time;
  30. int fd;
  31. fd = rtc_xopen(pp_rtcname, O_RDONLY);
  32. rtc_read_tm(&tm_time, fd);
  33. #if SHOW_HWCLOCK_DIFF
  34. {
  35. int before = tm_time.tm_sec;
  36. while (1) {
  37. rtc_read_tm(&tm_time, fd);
  38. gettimeofday(sys_tv, NULL);
  39. if (before != (int)tm_time.tm_sec)
  40. break;
  41. }
  42. }
  43. #endif
  44. if (ENABLE_FEATURE_CLEAN_UP)
  45. close(fd);
  46. return rtc_tm2time(&tm_time, utc);
  47. }
  48. static void show_clock(const char **pp_rtcname, int utc)
  49. {
  50. #if SHOW_HWCLOCK_DIFF
  51. struct timeval sys_tv;
  52. #endif
  53. time_t t = read_rtc(pp_rtcname, &sys_tv, utc);
  54. #if ENABLE_LOCALE_SUPPORT
  55. /* Standard hwclock uses locale-specific output format */
  56. char cp[64];
  57. struct tm *ptm = localtime(&t);
  58. strftime(cp, sizeof(cp), "%c", ptm);
  59. #else
  60. char *cp = ctime(&t);
  61. strchrnul(cp, '\n')[0] = '\0';
  62. #endif
  63. #if !SHOW_HWCLOCK_DIFF
  64. printf("%s 0.000000 seconds\n", cp);
  65. #else
  66. {
  67. long diff = sys_tv.tv_sec - t;
  68. if (diff < 0 /*&& tv.tv_usec != 0*/) {
  69. /* Why we need diff++? */
  70. /* diff >= 0 is ok: | diff < 0, can't just use tv.tv_usec: */
  71. /* 45.520820 | 43.520820 */
  72. /* - 44.000000 | - 45.000000 */
  73. /* = 1.520820 | = -1.479180, not -2.520820! */
  74. diff++;
  75. /* Should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
  76. sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
  77. }
  78. printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
  79. }
  80. #endif
  81. }
  82. static void to_sys_clock(const char **pp_rtcname, int utc)
  83. {
  84. struct timeval tv;
  85. struct timezone tz;
  86. tz.tz_minuteswest = timezone/60 - 60*daylight;
  87. tz.tz_dsttime = 0;
  88. tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
  89. tv.tv_usec = 0;
  90. if (settimeofday(&tv, &tz))
  91. bb_perror_msg_and_die("settimeofday");
  92. }
  93. static void from_sys_clock(const char **pp_rtcname, int utc)
  94. {
  95. #if 1
  96. struct timeval tv;
  97. struct tm tm_time;
  98. int rtc;
  99. rtc = rtc_xopen(pp_rtcname, O_WRONLY);
  100. gettimeofday(&tv, NULL);
  101. /* Prepare tm_time */
  102. if (sizeof(time_t) == sizeof(tv.tv_sec)) {
  103. if (utc)
  104. gmtime_r((time_t*)&tv.tv_sec, &tm_time);
  105. else
  106. localtime_r((time_t*)&tv.tv_sec, &tm_time);
  107. } else {
  108. time_t t = tv.tv_sec;
  109. if (utc)
  110. gmtime_r(&t, &tm_time);
  111. else
  112. localtime_r(&t, &tm_time);
  113. }
  114. #else
  115. /* Bloated code which tries to set hw clock with better precision.
  116. * On x86, even though code does set hw clock within <1ms of exact
  117. * whole seconds, apparently hw clock (at least on some machines)
  118. * doesn't reset internal fractional seconds to 0,
  119. * making all this a pointless excercise.
  120. */
  121. /* If we see that we are N usec away from whole second,
  122. * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
  123. * that CPU is not infinitely fast.
  124. * On infinitely fast CPU, next wakeup would be
  125. * on (exactly_next_whole_second - ADJ). On real CPUs,
  126. * this difference between current time and whole second
  127. * is less than ADJ (assuming system isn't heavily loaded).
  128. */
  129. /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
  130. * Slower CPUs will fail to sync and will go to bigger
  131. * ADJ values. qemu-emulated armv4tl with ~100 MHz
  132. * performance ends up using ADJ ~= 4*1024 and it takes
  133. * 2+ secs (2 tries with successively larger ADJ)
  134. * to sync. Even straced one on the same qemu (very slow)
  135. * takes only 4 tries.
  136. */
  137. #define TWEAK_USEC 256
  138. unsigned adj = TWEAK_USEC;
  139. struct tm tm_time;
  140. struct timeval tv;
  141. int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
  142. /* Try to catch the moment when whole second is close */
  143. while (1) {
  144. unsigned rem_usec;
  145. time_t t;
  146. gettimeofday(&tv, NULL);
  147. t = tv.tv_sec;
  148. rem_usec = 1000000 - tv.tv_usec;
  149. if (rem_usec < adj) {
  150. /* Close enough */
  151. small_rem:
  152. t++;
  153. }
  154. /* Prepare tm_time from t */
  155. if (utc)
  156. gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
  157. else
  158. localtime_r(&t, &tm_time); /* same */
  159. if (adj >= 32*1024) {
  160. break; /* 32 ms diff and still no luck?? give up trying to sync */
  161. }
  162. /* gmtime/localtime took some time, re-get cur time */
  163. gettimeofday(&tv, NULL);
  164. if (tv.tv_sec < t /* we are still in old second */
  165. || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
  166. ) {
  167. break; /* good, we are in sync! */
  168. }
  169. rem_usec = 1000000 - tv.tv_usec;
  170. if (rem_usec < adj) {
  171. t = tv.tv_sec;
  172. goto small_rem; /* already close to next sec, don't sleep */
  173. }
  174. /* Try to sync up by sleeping */
  175. usleep(rem_usec - adj);
  176. /* Jump to 1ms diff, then increase fast (x2): EVERY loop
  177. * takes ~1 sec, people won't like slowly converging code here!
  178. */
  179. //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
  180. if (adj < 512)
  181. adj = 512;
  182. /* ... and if last "overshoot" does not look insanely big,
  183. * just use it as adj increment. This makes convergence faster.
  184. */
  185. if (tv.tv_usec < adj * 8) {
  186. adj += tv.tv_usec;
  187. continue;
  188. }
  189. adj *= 2;
  190. }
  191. /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
  192. * Look for a value which makes tv_usec close to 999999 or 0.
  193. * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
  194. */
  195. //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
  196. #endif
  197. tm_time.tm_isdst = 0;
  198. xioctl(rtc, RTC_SET_TIME, &tm_time);
  199. if (ENABLE_FEATURE_CLEAN_UP)
  200. close(rtc);
  201. }
  202. /*
  203. * At system boot, kernel may set system time from RTC,
  204. * but it knows nothing about timezones. If RTC is in local time,
  205. * then system time is wrong - it is offset by timezone.
  206. * This option corrects system time if RTC is in local time,
  207. * and (always) sets in-kernel timezone.
  208. *
  209. * This is an alternate option to --hctosys that does not read the
  210. * hardware clock.
  211. */
  212. static void set_system_clock_timezone(int utc)
  213. {
  214. struct timeval tv;
  215. struct tm *broken;
  216. struct timezone tz;
  217. gettimeofday(&tv, NULL);
  218. broken = localtime(&tv.tv_sec);
  219. tz.tz_minuteswest = timezone / 60;
  220. if (broken->tm_isdst)
  221. tz.tz_minuteswest -= 60;
  222. tz.tz_dsttime = 0;
  223. gettimeofday(&tv, NULL);
  224. if (!utc)
  225. tv.tv_sec += tz.tz_minuteswest * 60;
  226. if (settimeofday(&tv, &tz))
  227. bb_perror_msg_and_die("settimeofday");
  228. }
  229. //usage:#define hwclock_trivial_usage
  230. //usage: IF_FEATURE_HWCLOCK_LONG_OPTIONS(
  231. //usage: "[-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz]"
  232. //usage: " [-l|--localtime] [-u|--utc]"
  233. //usage: " [-f|--rtc FILE]"
  234. //usage: )
  235. //usage: IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS(
  236. //usage: "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]"
  237. //usage: )
  238. //usage:#define hwclock_full_usage "\n\n"
  239. //usage: "Query and set hardware clock (RTC)\n"
  240. //usage: "\n -r Show hardware clock time"
  241. //usage: "\n -s Set system time from hardware clock"
  242. //usage: "\n -w Set hardware clock from system time"
  243. //usage: "\n -t Set in-kernel timezone, correct system time"
  244. //usage: "\n if hardware clock is in local time"
  245. //usage: "\n -u Assume hardware clock is kept in UTC"
  246. //usage: "\n -l Assume hardware clock is kept in local time"
  247. //usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)"
  248. #define HWCLOCK_OPT_LOCALTIME 0x01
  249. #define HWCLOCK_OPT_UTC 0x02
  250. #define HWCLOCK_OPT_SHOW 0x04
  251. #define HWCLOCK_OPT_HCTOSYS 0x08
  252. #define HWCLOCK_OPT_SYSTOHC 0x10
  253. #define HWCLOCK_OPT_SYSTZ 0x20
  254. #define HWCLOCK_OPT_RTCFILE 0x40
  255. int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  256. int hwclock_main(int argc UNUSED_PARAM, char **argv)
  257. {
  258. const char *rtcname = NULL;
  259. unsigned opt;
  260. int utc;
  261. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  262. static const char hwclock_longopts[] ALIGN1 =
  263. "localtime\0" No_argument "l" /* short opt is non-standard */
  264. "utc\0" No_argument "u"
  265. "show\0" No_argument "r"
  266. "hctosys\0" No_argument "s"
  267. "systohc\0" No_argument "w"
  268. "systz\0" No_argument "t" /* short opt is non-standard */
  269. "rtc\0" Required_argument "f"
  270. ;
  271. applet_long_options = hwclock_longopts;
  272. #endif
  273. opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l";
  274. opt = getopt32(argv, "lurswtf:", &rtcname);
  275. /* If -u or -l wasn't given check if we are using utc */
  276. if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
  277. utc = (opt & HWCLOCK_OPT_UTC);
  278. else
  279. utc = rtc_adjtime_is_utc();
  280. if (opt & HWCLOCK_OPT_HCTOSYS)
  281. to_sys_clock(&rtcname, utc);
  282. else if (opt & HWCLOCK_OPT_SYSTOHC)
  283. from_sys_clock(&rtcname, utc);
  284. else if (opt & HWCLOCK_OPT_SYSTZ)
  285. set_system_clock_timezone(utc);
  286. else
  287. /* default HWCLOCK_OPT_SHOW */
  288. show_clock(&rtcname, utc);
  289. return 0;
  290. }