70dcb24e94921b93c0954d9ee3b5030523977bd0
[oweals/busybox.git] / util-linux / rdate.c
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
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <netdb.h>
29 #include <stdio.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <time.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include "busybox.h"
37
38
39 static const int RFC_868_BIAS = 2208988800UL;
40
41 static void socket_timeout(void)
42 {
43         bb_error_msg_and_die("timeout connecting to time server");
44 }
45
46 static time_t askremotedate(const char *host)
47 {
48         unsigned long int nett, localt;
49         const char *port="37";
50         int fd;
51
52         if (getservbyname("time", "tcp") != NULL)
53                 port="time";
54
55         /* Add a timeout for dead or non accessable servers */
56         alarm(10);
57         signal(SIGALRM, socket_timeout);
58
59         fd = xconnect(host, port);
60
61         if (safe_read(fd, (void *)&nett, 4) != 4)    /* read time from server */
62                 bb_error_msg_and_die("%s did not send the complete time", host);
63
64         close(fd);
65
66         /* convert from network byte order to local byte order.
67          * RFC 868 time is the number of seconds
68          *  since 00:00 (midnight) 1 January 1900 GMT
69          *  the RFC 868 time 2,208,988,800 corresponds to 00:00  1 Jan 1970 GMT
70          * Subtract the RFC 868 time  to get Linux epoch
71          */
72         localt= ntohl(nett) - RFC_868_BIAS;
73
74         return(localt);
75 }
76
77 int rdate_main(int argc, char **argv)
78 {
79         time_t remote_time;
80         int opt;
81         int setdate = 1;
82         int printdate = 1;
83
84         /* Interpret command line args */
85         while ((opt = getopt(argc, argv, "sp")) > 0) {
86                 switch (opt) {
87                         case 's':
88                                 printdate = 0;
89                                 setdate = 1;
90                                 break;
91                         case 'p':
92                                 printdate = 1;
93                                 setdate = 0;
94                                 break;
95                         default:
96                                 bb_show_usage();
97                 }
98         }
99
100         if (optind == argc)
101                 bb_show_usage();
102
103         remote_time = askremotedate(argv[optind]);
104
105         if (setdate) {
106                 if (stime(&remote_time) < 0)
107                         bb_perror_msg_and_die("Could not set time of day");
108         }
109
110         if (printdate)
111                 printf("%s", ctime(&remote_time));
112
113         return EXIT_SUCCESS;
114 }