1 /* vi: set sw=4 ts=4: */
3 * The Rdate command will ask a time server for the RFC 868 time
4 * and optionally set the system time.
6 * by Sterling Huxley <sterling@europa.com>
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.
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.
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
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
40 static const int RFC_868_BIAS = 2208988800UL;
42 static void socket_timeout(int sig)
44 bb_error_msg_and_die("timeout connecting to time server");
47 static time_t askremotedate(const char *host)
49 unsigned long int nett, localt;
50 const char *port="37";
53 if (getservbyname("time", "tcp") != NULL)
56 /* Add a timeout for dead or non accessable servers */
58 signal(SIGALRM, socket_timeout);
60 fd = xconnect(host, port);
62 if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */
63 bb_error_msg_and_die("%s did not send the complete time", host);
67 /* convert from network byte order to local byte order.
68 * RFC 868 time is the number of seconds
69 * since 00:00 (midnight) 1 January 1900 GMT
70 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
71 * Subtract the RFC 868 time to get Linux epoch
73 localt= ntohl(nett) - RFC_868_BIAS;
78 int rdate_main(int argc, char **argv)
85 /* Interpret command line args */
86 while ((opt = getopt(argc, argv, "sp")) > 0) {
104 remote_time = askremotedate(argv[optind]);
107 if (stime(&remote_time) < 0)
108 bb_perror_msg_and_die("Could not set time of day");
112 printf("%s", ctime(&remote_time));