hwclock.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  14. # ifndef _GNU_SOURCE
  15. # define _GNU_SOURCE
  16. # endif
  17. #endif
  18. /* diff code is disabled: it's not sys/hw clock diff, it's some useless
  19. * "time between hwclock was started and we saw CMOS tick" quantity.
  20. * It's useless since hwclock is started at a random moment,
  21. * thus the quantity is also random, useless. Showing 0.000000 does not
  22. * deprive us from any useful info.
  23. *
  24. * SHOW_HWCLOCK_DIFF code in this file shows the difference between system
  25. * and hw clock. It is useful, but not compatible with standard hwclock.
  26. * Thus disabled.
  27. */
  28. #define SHOW_HWCLOCK_DIFF 0
  29. #if !SHOW_HWCLOCK_DIFF
  30. # define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
  31. #endif
  32. static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
  33. {
  34. struct tm tm_time;
  35. int fd;
  36. fd = rtc_xopen(pp_rtcname, O_RDONLY);
  37. rtc_read_tm(&tm_time, fd);
  38. #if SHOW_HWCLOCK_DIFF
  39. {
  40. int before = tm_time.tm_sec;
  41. while (1) {
  42. rtc_read_tm(&tm_time, fd);
  43. gettimeofday(sys_tv, NULL);
  44. if (before != tm_time.tm_sec)
  45. break;
  46. }
  47. }
  48. #endif
  49. if (ENABLE_FEATURE_CLEAN_UP)
  50. close(fd);
  51. return rtc_tm2time(&tm_time, utc);
  52. }
  53. static void show_clock(const char **pp_rtcname, int utc)
  54. {
  55. #if SHOW_HWCLOCK_DIFF
  56. struct timeval sys_tv;
  57. #endif
  58. time_t t;
  59. char *cp;
  60. t = read_rtc(pp_rtcname, &sys_tv, utc);
  61. cp = ctime(&t);
  62. strchrnul(cp, '\n')[0] = '\0';
  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? */
  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. #define HWCLOCK_OPT_LOCALTIME 0x01
  203. #define HWCLOCK_OPT_UTC 0x02
  204. #define HWCLOCK_OPT_SHOW 0x04
  205. #define HWCLOCK_OPT_HCTOSYS 0x08
  206. #define HWCLOCK_OPT_SYSTOHC 0x10
  207. #define HWCLOCK_OPT_RTCFILE 0x20
  208. int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  209. int hwclock_main(int argc UNUSED_PARAM, char **argv)
  210. {
  211. const char *rtcname = NULL;
  212. unsigned opt;
  213. int utc;
  214. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  215. static const char hwclock_longopts[] ALIGN1 =
  216. "localtime\0" No_argument "l"
  217. "utc\0" No_argument "u"
  218. "show\0" No_argument "r"
  219. "hctosys\0" No_argument "s"
  220. "systohc\0" No_argument "w"
  221. "file\0" Required_argument "f"
  222. ;
  223. applet_long_options = hwclock_longopts;
  224. #endif
  225. opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
  226. opt = getopt32(argv, "lurswf:", &rtcname);
  227. /* If -u or -l wasn't given check if we are using utc */
  228. if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
  229. utc = (opt & HWCLOCK_OPT_UTC);
  230. else
  231. utc = rtc_adjtime_is_utc();
  232. if (opt & HWCLOCK_OPT_HCTOSYS)
  233. to_sys_clock(&rtcname, utc);
  234. else if (opt & HWCLOCK_OPT_SYSTOHC)
  235. from_sys_clock(&rtcname, utc);
  236. else
  237. /* default HWCLOCK_OPT_SHOW */
  238. show_clock(&rtcname, utc);
  239. return 0;
  240. }