hwclock.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 tarball for details.
  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 != 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;
  54. char *cp;
  55. t = read_rtc(pp_rtcname, &sys_tv, utc);
  56. cp = ctime(&t);
  57. strchrnul(cp, '\n')[0] = '\0';
  58. #if !SHOW_HWCLOCK_DIFF
  59. printf("%s 0.000000 seconds\n", cp);
  60. #else
  61. {
  62. long diff = sys_tv.tv_sec - t;
  63. if (diff < 0 /*&& tv.tv_usec != 0*/) {
  64. /* Why? */
  65. /* diff >= 0 is ok: diff < 0, can't just use tv.tv_usec: */
  66. /* 45.520820 43.520820 */
  67. /* - 44.000000 - 45.000000 */
  68. /* = 1.520820 = -1.479180, not -2.520820! */
  69. diff++;
  70. /* should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
  71. sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
  72. }
  73. printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
  74. }
  75. #endif
  76. }
  77. static void to_sys_clock(const char **pp_rtcname, int utc)
  78. {
  79. struct timeval tv;
  80. struct timezone tz;
  81. tz.tz_minuteswest = timezone/60 - 60*daylight;
  82. tz.tz_dsttime = 0;
  83. tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
  84. tv.tv_usec = 0;
  85. if (settimeofday(&tv, &tz))
  86. bb_perror_msg_and_die("settimeofday");
  87. }
  88. static void from_sys_clock(const char **pp_rtcname, int utc)
  89. {
  90. #if 1
  91. struct timeval tv;
  92. struct tm tm_time;
  93. int rtc;
  94. rtc = rtc_xopen(pp_rtcname, O_WRONLY);
  95. gettimeofday(&tv, NULL);
  96. /* Prepare tm_time */
  97. if (sizeof(time_t) == sizeof(tv.tv_sec)) {
  98. if (utc)
  99. gmtime_r((time_t*)&tv.tv_sec, &tm_time);
  100. else
  101. localtime_r((time_t*)&tv.tv_sec, &tm_time);
  102. } else {
  103. time_t t = tv.tv_sec;
  104. if (utc)
  105. gmtime_r(&t, &tm_time);
  106. else
  107. localtime_r(&t, &tm_time);
  108. }
  109. #else
  110. /* Bloated code which tries to set hw clock with better precision.
  111. * On x86, even though code does set hw clock within <1ms of exact
  112. * whole seconds, apparently hw clock (at least on some machines)
  113. * doesn't reset internal fractional seconds to 0,
  114. * making all this a pointless excercise.
  115. */
  116. /* If we see that we are N usec away from whole second,
  117. * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
  118. * that CPU is not infinitely fast.
  119. * On infinitely fast CPU, next wakeup would be
  120. * on (exactly_next_whole_second - ADJ). On real CPUs,
  121. * this difference between current time and whole second
  122. * is less than ADJ (assuming system isn't heavily loaded).
  123. */
  124. /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
  125. * Slower CPUs will fail to sync and will go to bigger
  126. * ADJ values. qemu-emulated armv4tl with ~100 MHz
  127. * performance ends up using ADJ ~= 4*1024 and it takes
  128. * 2+ secs (2 tries with successively larger ADJ)
  129. * to sync. Even straced one on the same qemu (very slow)
  130. * takes only 4 tries.
  131. */
  132. #define TWEAK_USEC 256
  133. unsigned adj = TWEAK_USEC;
  134. struct tm tm_time;
  135. struct timeval tv;
  136. int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
  137. /* Try to catch the moment when whole second is close */
  138. while (1) {
  139. unsigned rem_usec;
  140. time_t t;
  141. gettimeofday(&tv, NULL);
  142. t = tv.tv_sec;
  143. rem_usec = 1000000 - tv.tv_usec;
  144. if (rem_usec < adj) {
  145. /* Close enough */
  146. small_rem:
  147. t++;
  148. }
  149. /* Prepare tm_time from t */
  150. if (utc)
  151. gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
  152. else
  153. localtime_r(&t, &tm_time); /* same */
  154. if (adj >= 32*1024) {
  155. break; /* 32 ms diff and still no luck?? give up trying to sync */
  156. }
  157. /* gmtime/localtime took some time, re-get cur time */
  158. gettimeofday(&tv, NULL);
  159. if (tv.tv_sec < t /* we are still in old second */
  160. || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
  161. ) {
  162. break; /* good, we are in sync! */
  163. }
  164. rem_usec = 1000000 - tv.tv_usec;
  165. if (rem_usec < adj) {
  166. t = tv.tv_sec;
  167. goto small_rem; /* already close to next sec, don't sleep */
  168. }
  169. /* Try to sync up by sleeping */
  170. usleep(rem_usec - adj);
  171. /* Jump to 1ms diff, then increase fast (x2): EVERY loop
  172. * takes ~1 sec, people won't like slowly converging code here!
  173. */
  174. //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
  175. if (adj < 512)
  176. adj = 512;
  177. /* ... and if last "overshoot" does not look insanely big,
  178. * just use it as adj increment. This makes convergence faster.
  179. */
  180. if (tv.tv_usec < adj * 8) {
  181. adj += tv.tv_usec;
  182. continue;
  183. }
  184. adj *= 2;
  185. }
  186. /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
  187. * Look for a value which makes tv_usec close to 999999 or 0.
  188. * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
  189. */
  190. //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
  191. #endif
  192. tm_time.tm_isdst = 0;
  193. xioctl(rtc, RTC_SET_TIME, &tm_time);
  194. if (ENABLE_FEATURE_CLEAN_UP)
  195. close(rtc);
  196. }
  197. #define HWCLOCK_OPT_LOCALTIME 0x01
  198. #define HWCLOCK_OPT_UTC 0x02
  199. #define HWCLOCK_OPT_SHOW 0x04
  200. #define HWCLOCK_OPT_HCTOSYS 0x08
  201. #define HWCLOCK_OPT_SYSTOHC 0x10
  202. #define HWCLOCK_OPT_RTCFILE 0x20
  203. int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  204. int hwclock_main(int argc UNUSED_PARAM, char **argv)
  205. {
  206. const char *rtcname = NULL;
  207. unsigned opt;
  208. int utc;
  209. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  210. static const char hwclock_longopts[] ALIGN1 =
  211. "localtime\0" No_argument "l"
  212. "utc\0" No_argument "u"
  213. "show\0" No_argument "r"
  214. "hctosys\0" No_argument "s"
  215. "systohc\0" No_argument "w"
  216. "file\0" Required_argument "f"
  217. ;
  218. applet_long_options = hwclock_longopts;
  219. #endif
  220. opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
  221. opt = getopt32(argv, "lurswf:", &rtcname);
  222. /* If -u or -l wasn't given check if we are using utc */
  223. if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
  224. utc = (opt & HWCLOCK_OPT_UTC);
  225. else
  226. utc = rtc_adjtime_is_utc();
  227. if (opt & HWCLOCK_OPT_HCTOSYS)
  228. to_sys_clock(&rtcname, utc);
  229. else if (opt & HWCLOCK_OPT_SYSTOHC)
  230. from_sys_clock(&rtcname, utc);
  231. else
  232. /* default HWCLOCK_OPT_SHOW */
  233. show_clock(&rtcname, utc);
  234. return 0;
  235. }