3
0

hwclock.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. chomp(cp);
  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;
  87. /* ^^^ used to also subtract 60*daylight, but it's wrong:
  88. * daylight!=0 means "this timezone has some DST
  89. * during the year", not "DST is in effect now".
  90. */
  91. tz.tz_dsttime = 0;
  92. tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
  93. tv.tv_usec = 0;
  94. if (settimeofday(&tv, &tz))
  95. bb_perror_msg_and_die("settimeofday");
  96. }
  97. static void from_sys_clock(const char **pp_rtcname, int utc)
  98. {
  99. #if 1
  100. struct timeval tv;
  101. struct tm tm_time;
  102. int rtc;
  103. rtc = rtc_xopen(pp_rtcname, O_WRONLY);
  104. gettimeofday(&tv, NULL);
  105. /* Prepare tm_time */
  106. if (sizeof(time_t) == sizeof(tv.tv_sec)) {
  107. if (utc)
  108. gmtime_r((time_t*)&tv.tv_sec, &tm_time);
  109. else
  110. localtime_r((time_t*)&tv.tv_sec, &tm_time);
  111. } else {
  112. time_t t = tv.tv_sec;
  113. if (utc)
  114. gmtime_r(&t, &tm_time);
  115. else
  116. localtime_r(&t, &tm_time);
  117. }
  118. #else
  119. /* Bloated code which tries to set hw clock with better precision.
  120. * On x86, even though code does set hw clock within <1ms of exact
  121. * whole seconds, apparently hw clock (at least on some machines)
  122. * doesn't reset internal fractional seconds to 0,
  123. * making all this a pointless excercise.
  124. */
  125. /* If we see that we are N usec away from whole second,
  126. * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
  127. * that CPU is not infinitely fast.
  128. * On infinitely fast CPU, next wakeup would be
  129. * on (exactly_next_whole_second - ADJ). On real CPUs,
  130. * this difference between current time and whole second
  131. * is less than ADJ (assuming system isn't heavily loaded).
  132. */
  133. /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
  134. * Slower CPUs will fail to sync and will go to bigger
  135. * ADJ values. qemu-emulated armv4tl with ~100 MHz
  136. * performance ends up using ADJ ~= 4*1024 and it takes
  137. * 2+ secs (2 tries with successively larger ADJ)
  138. * to sync. Even straced one on the same qemu (very slow)
  139. * takes only 4 tries.
  140. */
  141. #define TWEAK_USEC 256
  142. unsigned adj = TWEAK_USEC;
  143. struct tm tm_time;
  144. struct timeval tv;
  145. int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
  146. /* Try to catch the moment when whole second is close */
  147. while (1) {
  148. unsigned rem_usec;
  149. time_t t;
  150. gettimeofday(&tv, NULL);
  151. t = tv.tv_sec;
  152. rem_usec = 1000000 - tv.tv_usec;
  153. if (rem_usec < adj) {
  154. /* Close enough */
  155. small_rem:
  156. t++;
  157. }
  158. /* Prepare tm_time from t */
  159. if (utc)
  160. gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
  161. else
  162. localtime_r(&t, &tm_time); /* same */
  163. if (adj >= 32*1024) {
  164. break; /* 32 ms diff and still no luck?? give up trying to sync */
  165. }
  166. /* gmtime/localtime took some time, re-get cur time */
  167. gettimeofday(&tv, NULL);
  168. if (tv.tv_sec < t /* we are still in old second */
  169. || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
  170. ) {
  171. break; /* good, we are in sync! */
  172. }
  173. rem_usec = 1000000 - tv.tv_usec;
  174. if (rem_usec < adj) {
  175. t = tv.tv_sec;
  176. goto small_rem; /* already close to next sec, don't sleep */
  177. }
  178. /* Try to sync up by sleeping */
  179. usleep(rem_usec - adj);
  180. /* Jump to 1ms diff, then increase fast (x2): EVERY loop
  181. * takes ~1 sec, people won't like slowly converging code here!
  182. */
  183. //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
  184. if (adj < 512)
  185. adj = 512;
  186. /* ... and if last "overshoot" does not look insanely big,
  187. * just use it as adj increment. This makes convergence faster.
  188. */
  189. if (tv.tv_usec < adj * 8) {
  190. adj += tv.tv_usec;
  191. continue;
  192. }
  193. adj *= 2;
  194. }
  195. /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
  196. * Look for a value which makes tv_usec close to 999999 or 0.
  197. * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
  198. */
  199. //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
  200. #endif
  201. tm_time.tm_isdst = 0;
  202. xioctl(rtc, RTC_SET_TIME, &tm_time);
  203. if (ENABLE_FEATURE_CLEAN_UP)
  204. close(rtc);
  205. }
  206. /*
  207. * At system boot, kernel may set system time from RTC,
  208. * but it knows nothing about timezones. If RTC is in local time,
  209. * then system time is wrong - it is offset by timezone.
  210. * This option corrects system time if RTC is in local time,
  211. * and (always) sets in-kernel timezone.
  212. *
  213. * This is an alternate option to --hctosys that does not read the
  214. * hardware clock.
  215. */
  216. static void set_system_clock_timezone(int utc)
  217. {
  218. struct timeval tv;
  219. struct tm *broken;
  220. struct timezone tz;
  221. gettimeofday(&tv, NULL);
  222. broken = localtime(&tv.tv_sec);
  223. tz.tz_minuteswest = timezone / 60;
  224. if (broken->tm_isdst > 0)
  225. tz.tz_minuteswest -= 60;
  226. tz.tz_dsttime = 0;
  227. gettimeofday(&tv, NULL);
  228. if (!utc)
  229. tv.tv_sec += tz.tz_minuteswest * 60;
  230. if (settimeofday(&tv, &tz))
  231. bb_perror_msg_and_die("settimeofday");
  232. }
  233. //usage:#define hwclock_trivial_usage
  234. //usage: IF_FEATURE_HWCLOCK_LONG_OPTIONS(
  235. //usage: "[-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz]"
  236. //usage: " [-l|--localtime] [-u|--utc]"
  237. //usage: " [-f|--rtc FILE]"
  238. //usage: )
  239. //usage: IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS(
  240. //usage: "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]"
  241. //usage: )
  242. //usage:#define hwclock_full_usage "\n\n"
  243. //usage: "Query and set hardware clock (RTC)\n"
  244. //usage: "\n -r Show hardware clock time"
  245. //usage: "\n -s Set system time from hardware clock"
  246. //usage: "\n -w Set hardware clock from system time"
  247. //usage: "\n -t Set in-kernel timezone, correct system time"
  248. //usage: "\n if hardware clock is in local time"
  249. //usage: "\n -u Assume hardware clock is kept in UTC"
  250. //usage: "\n -l Assume hardware clock is kept in local time"
  251. //usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)"
  252. #define HWCLOCK_OPT_LOCALTIME 0x01
  253. #define HWCLOCK_OPT_UTC 0x02
  254. #define HWCLOCK_OPT_SHOW 0x04
  255. #define HWCLOCK_OPT_HCTOSYS 0x08
  256. #define HWCLOCK_OPT_SYSTOHC 0x10
  257. #define HWCLOCK_OPT_SYSTZ 0x20
  258. #define HWCLOCK_OPT_RTCFILE 0x40
  259. int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  260. int hwclock_main(int argc UNUSED_PARAM, char **argv)
  261. {
  262. const char *rtcname = NULL;
  263. unsigned opt;
  264. int utc;
  265. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  266. static const char hwclock_longopts[] ALIGN1 =
  267. "localtime\0" No_argument "l" /* short opt is non-standard */
  268. "utc\0" No_argument "u"
  269. "show\0" No_argument "r"
  270. "hctosys\0" No_argument "s"
  271. "systohc\0" No_argument "w"
  272. "systz\0" No_argument "t" /* short opt is non-standard */
  273. "rtc\0" Required_argument "f"
  274. ;
  275. applet_long_options = hwclock_longopts;
  276. #endif
  277. /* Initialize "timezone" (libc global variable) */
  278. tzset();
  279. opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l";
  280. opt = getopt32(argv, "lurswtf:", &rtcname);
  281. /* If -u or -l wasn't given check if we are using utc */
  282. if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
  283. utc = (opt & HWCLOCK_OPT_UTC);
  284. else
  285. utc = rtc_adjtime_is_utc();
  286. if (opt & HWCLOCK_OPT_HCTOSYS)
  287. to_sys_clock(&rtcname, utc);
  288. else if (opt & HWCLOCK_OPT_SYSTOHC)
  289. from_sys_clock(&rtcname, utc);
  290. else if (opt & HWCLOCK_OPT_SYSTZ)
  291. set_system_clock_timezone(utc);
  292. else
  293. /* default HWCLOCK_OPT_SHOW */
  294. show_clock(&rtcname, utc);
  295. return 0;
  296. }