hwclock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <sys/ioctl.h>
  10. #include <sys/utsname.h>
  11. #include <getopt.h>
  12. #include "busybox.h"
  13. /* Copied from linux/rtc.h to eliminate the kernel dependency */
  14. struct linux_rtc_time {
  15. int tm_sec;
  16. int tm_min;
  17. int tm_hour;
  18. int tm_mday;
  19. int tm_mon;
  20. int tm_year;
  21. int tm_wday;
  22. int tm_yday;
  23. int tm_isdst;
  24. };
  25. #define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time */
  26. #define RTC_RD_TIME _IOR('p', 0x09, struct linux_rtc_time) /* Read RTC time */
  27. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  28. # ifndef _GNU_SOURCE
  29. # define _GNU_SOURCE
  30. # endif
  31. #endif
  32. static int xopen_rtc(int flags)
  33. {
  34. int rtc;
  35. rtc = open("/dev/rtc", flags);
  36. if (rtc < 0) {
  37. rtc = open("/dev/misc/rtc", flags);
  38. if (rtc < 0)
  39. bb_perror_msg_and_die("cannot access RTC");
  40. }
  41. return rtc;
  42. }
  43. static time_t read_rtc(int utc)
  44. {
  45. struct tm tm;
  46. char *oldtz = 0;
  47. time_t t = 0;
  48. int rtc = xopen_rtc(O_RDONLY);
  49. memset(&tm, 0, sizeof(struct tm));
  50. if (ioctl(rtc, RTC_RD_TIME, &tm) < 0 )
  51. bb_perror_msg_and_die("cannot read time from RTC");
  52. tm.tm_isdst = -1; /* not known */
  53. close(rtc);
  54. if (utc) {
  55. oldtz = getenv("TZ");
  56. setenv("TZ", "UTC 0", 1);
  57. tzset();
  58. }
  59. t = mktime(&tm);
  60. if (utc) {
  61. if (oldtz)
  62. setenv("TZ", oldtz, 1);
  63. else
  64. unsetenv("TZ");
  65. tzset();
  66. }
  67. return t;
  68. }
  69. static void write_rtc(time_t t, int utc)
  70. {
  71. struct tm tm;
  72. int rtc = xopen_rtc(O_WRONLY);
  73. tm = *(utc ? gmtime(&t) : localtime(&t));
  74. tm.tm_isdst = 0;
  75. if (ioctl(rtc, RTC_SET_TIME, &tm) < 0)
  76. bb_perror_msg_and_die("cannot set the RTC time");
  77. close(rtc);
  78. }
  79. static int show_clock(int utc)
  80. {
  81. struct tm *ptm;
  82. time_t t;
  83. RESERVE_CONFIG_BUFFER(buffer, 64);
  84. t = read_rtc(utc);
  85. ptm = localtime(&t); /* Sets 'tzname[]' */
  86. safe_strncpy(buffer, ctime(&t), 64);
  87. if (buffer[0])
  88. buffer[strlen(buffer) - 1] = 0;
  89. //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
  90. printf( "%s %.6f seconds\n", buffer, 0.0);
  91. RELEASE_CONFIG_BUFFER(buffer);
  92. return 0;
  93. }
  94. static int to_sys_clock(int utc)
  95. {
  96. struct timeval tv = { 0, 0 };
  97. const struct timezone tz = { timezone/60 - 60*daylight, 0 };
  98. tv.tv_sec = read_rtc(utc);
  99. if (settimeofday(&tv, &tz))
  100. bb_perror_msg_and_die("settimeofday() failed");
  101. return 0;
  102. }
  103. static int from_sys_clock(int utc)
  104. {
  105. struct timeval tv = { 0, 0 };
  106. struct timezone tz = { 0, 0 };
  107. if (gettimeofday(&tv, &tz))
  108. bb_perror_msg_and_die("gettimeofday() failed");
  109. write_rtc(tv.tv_sec, utc);
  110. return 0;
  111. }
  112. #ifdef CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS
  113. # define ADJTIME_PATH "/var/lib/hwclock/adjtime"
  114. #else
  115. # define ADJTIME_PATH "/etc/adjtime"
  116. #endif
  117. static int check_utc(void)
  118. {
  119. int utc = 0;
  120. FILE *f = fopen(ADJTIME_PATH, "r");
  121. if (f) {
  122. RESERVE_CONFIG_BUFFER(buffer, 128);
  123. while (fgets(buffer, sizeof(buffer), f)) {
  124. int len = strlen(buffer);
  125. while (len && isspace(buffer[len - 1]))
  126. len--;
  127. buffer[len] = 0;
  128. if (strncmp(buffer, "UTC", 3) == 0 ) {
  129. utc = 1;
  130. break;
  131. }
  132. }
  133. fclose(f);
  134. RELEASE_CONFIG_BUFFER(buffer);
  135. }
  136. return utc;
  137. }
  138. #define HWCLOCK_OPT_LOCALTIME 0x01
  139. #define HWCLOCK_OPT_UTC 0x02
  140. #define HWCLOCK_OPT_SHOW 0x04
  141. #define HWCLOCK_OPT_HCTOSYS 0x08
  142. #define HWCLOCK_OPT_SYSTOHC 0x10
  143. int hwclock_main(int argc, char **argv )
  144. {
  145. unsigned opt;
  146. int utc;
  147. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  148. static const struct option hwclock_long_options[] = {
  149. { "localtime", 0, 0, 'l' },
  150. { "utc", 0, 0, 'u' },
  151. { "show", 0, 0, 'r' },
  152. { "hctosys", 0, 0, 's' },
  153. { "systohc", 0, 0, 'w' },
  154. { 0, 0, 0, 0 }
  155. };
  156. applet_long_options = hwclock_long_options;
  157. #endif
  158. opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l";
  159. opt = getopt32(argc, argv, "lursw");
  160. /* If -u or -l wasn't given check if we are using utc */
  161. if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
  162. utc = opt & HWCLOCK_OPT_UTC;
  163. else
  164. utc = check_utc();
  165. if (opt & HWCLOCK_OPT_HCTOSYS) {
  166. return to_sys_clock(utc);
  167. }
  168. else if (opt & HWCLOCK_OPT_SYSTOHC) {
  169. return from_sys_clock(utc);
  170. } else {
  171. /* default HWCLOCK_OPT_SHOW */
  172. return show_clock(utc);
  173. }
  174. }