blob: 1f36d8f37bbe24a73c7574a4a5688c14ab9af0cc [file] [log] [blame]
Eric Andersen6ab22022000-08-21 23:04:00 +00001/* 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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen6ab22022000-08-21 23:04:00 +00009*/
10
Pere Orga5bc8c002011-04-11 03:29:49 +020011//usage:#define rdate_trivial_usage
12//usage: "[-sp] HOST"
13//usage:#define rdate_full_usage "\n\n"
14//usage: "Get and possibly set the system date and time from a remote HOST\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020015//usage: "\n -s Set the system date and time (default)"
16//usage: "\n -p Print the date and time"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Eric Andersen6ab22022000-08-21 23:04:00 +000019
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000020enum { RFC_868_BIAS = 2208988800UL };
Eric Andersen6ab22022000-08-21 23:04:00 +000021
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000022static void socket_timeout(int sig UNUSED_PARAM)
Eric Andersene15138a2003-09-12 05:50:51 +000023{
Eric Andersen7f2935b2003-09-12 08:32:24 +000024 bb_error_msg_and_die("timeout connecting to time server");
Eric Andersene15138a2003-09-12 05:50:51 +000025}
26
Eric Andersen40eaa9f2001-03-19 19:41:54 +000027static time_t askremotedate(const char *host)
Eric Andersen6ab22022000-08-21 23:04:00 +000028{
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000029 uint32_t nett;
Eric Andersen6ab22022000-08-21 23:04:00 +000030 int fd;
31
Rob Landley1b751c82005-10-28 09:24:33 +000032 /* Add a timeout for dead or inaccessible servers */
Eric Andersene15138a2003-09-12 05:50:51 +000033 alarm(10);
34 signal(SIGALRM, socket_timeout);
35
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000036 fd = create_and_connect_stream_or_die(host, bb_lookup_port("time", "tcp", 37));
Eric Andersen40eaa9f2001-03-19 19:41:54 +000037
Eric Andersen725db192003-07-22 08:26:05 +000038 if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_error_msg_and_die("%s did not send the complete time", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000040 close(fd);
41
42 /* convert from network byte order to local byte order.
43 * RFC 868 time is the number of seconds
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000044 * since 00:00 (midnight) 1 January 1900 GMT
45 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
46 * Subtract the RFC 868 time to get Linux epoch
Eric Andersen6ab22022000-08-21 23:04:00 +000047 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000048
Denis Vlasenko079f8af2006-11-27 16:49:31 +000049 return ntohl(nett) - RFC_868_BIAS;
Eric Andersen6ab22022000-08-21 23:04:00 +000050}
51
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000052int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000053int rdate_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen6ab22022000-08-21 23:04:00 +000054{
Eric Andersen1ca20a72001-03-21 07:34:27 +000055 time_t remote_time;
"Vladimir N. Oleynik"ea972822005-10-28 15:43:41 +000056 unsigned long flags;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
Denis Vlasenko67b23e62006-10-03 21:00:06 +000058 opt_complementary = "-1";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000059 flags = getopt32(argv, "sp");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060
Eric Andersen1ca20a72001-03-21 07:34:27 +000061 remote_time = askremotedate(argv[optind]);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000062
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +000063 if ((flags & 2) == 0) {
Eric Andersen89f10bc2003-12-19 11:29:29 +000064 time_t current_time;
65
66 time(&current_time);
67 if (current_time == remote_time)
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000068 bb_error_msg("current time matches remote time");
Eric Andersen89f10bc2003-12-19 11:29:29 +000069 else
70 if (stime(&remote_time) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +010071 bb_perror_msg_and_die("can't set time of day");
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +000072 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000073
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +000074 if ((flags & 1) == 0)
75 printf("%s", ctime(&remote_time));
Eric Andersen6ab22022000-08-21 23:04:00 +000076
Matt Kraai3e856ce2000-12-01 02:55:13 +000077 return EXIT_SUCCESS;
Eric Andersen6ab22022000-08-21 23:04:00 +000078}