rdate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * The Rdate command will ask a time server for the RFC 868 time
  4. * and optionally set the system time.
  5. *
  6. * by Sterling Huxley <sterling@europa.com>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //config:config RDATE
  11. //config: bool "rdate (6 kb)"
  12. //config: default y
  13. //config: help
  14. //config: The rdate utility allows you to synchronize the date and time of your
  15. //config: system clock with the date and time of a remote networked system using
  16. //config: the RFC868 protocol, which is built into the inetd daemon on most
  17. //config: systems.
  18. //applet:IF_RDATE(APPLET(rdate, BB_DIR_USR_SBIN, BB_SUID_DROP))
  19. //kbuild:lib-$(CONFIG_RDATE) += rdate.o
  20. //usage:#define rdate_trivial_usage
  21. //usage: "[-s/-p] HOST"
  22. //usage:#define rdate_full_usage "\n\n"
  23. //usage: "Set and print time from HOST using RFC 868\n"
  24. //usage: "\n -s Only set system time"
  25. //usage: "\n -p Only print time"
  26. #include "libbb.h"
  27. enum { RFC_868_BIAS = 2208988800UL };
  28. static void socket_timeout(int sig UNUSED_PARAM)
  29. {
  30. bb_error_msg_and_die("timeout connecting to time server");
  31. }
  32. static time_t askremotedate(const char *host)
  33. {
  34. uint32_t nett;
  35. int fd;
  36. /* Timeout for dead or inaccessible servers */
  37. alarm(10);
  38. signal(SIGALRM, socket_timeout);
  39. fd = create_and_connect_stream_or_die(host, bb_lookup_std_port("time", "tcp", 37));
  40. if (safe_read(fd, &nett, 4) != 4) /* read time from server */
  41. bb_error_msg_and_die("%s: %s", host, "short read");
  42. if (ENABLE_FEATURE_CLEAN_UP)
  43. close(fd);
  44. /* Convert from network byte order to local byte order.
  45. * RFC 868 time is seconds since 1900-01-01 00:00 GMT.
  46. * RFC 868 time 2,208,988,800 corresponds to 1970-01-01 00:00 GMT.
  47. * Subtract the RFC 868 time to get Linux epoch.
  48. */
  49. nett = ntohl(nett) - RFC_868_BIAS;
  50. if (sizeof(time_t) > 4) {
  51. /* Now we have 32-bit lsb of a wider time_t
  52. * Imagine that nett = 0x00000001,
  53. * current time cur = 0x123ffffffff.
  54. * Assuming our time is not some 40 years off,
  55. * remote time must be 0x12400000001.
  56. * Need to adjust our time by (int32_t)(nett - cur).
  57. */
  58. time_t cur = time(NULL);
  59. int32_t adjust = (int32_t)(nett - (uint32_t)cur);
  60. return cur + adjust;
  61. }
  62. /* This is not going to work, but what can we do */
  63. return (time_t)nett;
  64. }
  65. int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  66. int rdate_main(int argc UNUSED_PARAM, char **argv)
  67. {
  68. time_t remote_time;
  69. unsigned flags;
  70. flags = getopt32(argv, "^" "sp" "\0" "-1");
  71. remote_time = askremotedate(argv[optind]);
  72. /* Manpages of various Unixes are confusing. What happens is:
  73. * (no opts) set and print time
  74. * -s: set time ("do not print the time")
  75. * -p: print time ("do not set, just print the remote time")
  76. * -sp: print time (that's what we do, not sure this is right)
  77. */
  78. if (!(flags & 2)) { /* no -p (-s may be present) */
  79. if (time(NULL) == remote_time)
  80. bb_error_msg("current time matches remote time");
  81. else
  82. if (stime(&remote_time) < 0)
  83. bb_perror_msg_and_die("can't set time of day");
  84. }
  85. if (flags != 1) /* not lone -s */
  86. printf("%s", ctime(&remote_time));
  87. return EXIT_SUCCESS;
  88. }