rdate.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <netdb.h>
  28. #include <stdio.h>
  29. #include <getopt.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <signal.h>
  35. #include "busybox.h"
  36. static const int RFC_868_BIAS = 2208988800UL;
  37. static void socket_timeout(int sig)
  38. {
  39. bb_error_msg_and_die("timeout connecting to time server");
  40. }
  41. static time_t askremotedate(const char *host)
  42. {
  43. unsigned long int nett, localt;
  44. struct sockaddr_in s_in;
  45. int fd;
  46. bb_lookup_host(&s_in, host);
  47. s_in.sin_port = bb_lookup_port("time", "tcp", 37);
  48. /* Add a timeout for dead or non accessable servers */
  49. alarm(10);
  50. signal(SIGALRM, socket_timeout);
  51. fd = xconnect(&s_in);
  52. if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */
  53. bb_error_msg_and_die("%s did not send the complete time", host);
  54. close(fd);
  55. /* convert from network byte order to local byte order.
  56. * RFC 868 time is the number of seconds
  57. * since 00:00 (midnight) 1 January 1900 GMT
  58. * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
  59. * Subtract the RFC 868 time to get Linux epoch
  60. */
  61. localt= ntohl(nett) - RFC_868_BIAS;
  62. return(localt);
  63. }
  64. int rdate_main(int argc, char **argv)
  65. {
  66. time_t remote_time;
  67. int opt;
  68. int setdate = 1;
  69. int printdate = 1;
  70. /* Interpret command line args */
  71. while ((opt = getopt(argc, argv, "sp")) > 0) {
  72. switch (opt) {
  73. case 's':
  74. printdate = 0;
  75. setdate = 1;
  76. break;
  77. case 'p':
  78. printdate = 1;
  79. setdate = 0;
  80. break;
  81. default:
  82. bb_show_usage();
  83. }
  84. }
  85. if (optind == argc)
  86. bb_show_usage();
  87. remote_time = askremotedate(argv[optind]);
  88. if (setdate) {
  89. time_t current_time;
  90. time(&current_time);
  91. if (current_time == remote_time)
  92. bb_error_msg("Current time matches remote time.");
  93. else
  94. if (stime(&remote_time) < 0)
  95. bb_perror_msg_and_die("Could not set time of day");
  96. }
  97. if (printdate)
  98. printf("%s", ctime(&remote_time));
  99. return EXIT_SUCCESS;
  100. }