hwclock.c 4.1 KB

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