Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / cmd-inet / usr.bin / rdate.c
blobf5e7139e2720866ba073510383ffa06f61ececf3
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * rdate - get date from remote machine
45 * sets time, obtaining value from host
46 * on the tcp/time socket.
49 #include <signal.h>
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <sys/socket.h>
53 #include <netinet/tcp.h>
54 #include <strings.h>
55 #include <unistd.h>
56 #include <stdlib.h>
57 #include <netdb.h>
58 #include <stdio.h>
61 * The timeserver returns with time of day in seconds since
62 * Jan 1, 1900. We must subtract 86400(365*70 + 17) to get time
63 * since Jan 1, 1970, which is what get/settimeofday uses.
66 #define TOFFSET ((unsigned int)86400*(365*70 + 17))
69 * Before setting the system time, the value returned by the
70 * timeserver is checked for plausibility. If the returned date
71 * is before the time this program was written it cannot be
72 * correct.
75 #define WRITTEN 440199955 /* 22:45:55 13/Dec/1983 */
76 #define SECONDS_TO_MS 1000
78 static void timeout(int);
80 int
81 main(int argc, char **argv)
83 int s, i;
84 uint32_t time;
85 struct timeval timestruct;
86 unsigned int connect_timeout;
87 /* number of seconds to wait for something to happen. */
88 unsigned int rdate_timeout = 30; /* seconds */
89 struct addrinfo hints;
90 struct addrinfo *res;
91 int rc;
93 if (argc != 2) {
94 (void) fputs("usage: rdate host\n", stderr);
95 exit(EXIT_FAILURE);
98 (void) memset(&hints, 0, sizeof (hints));
99 hints.ai_protocol = IPPROTO_TCP;
100 res = NULL;
103 * getaddrinfo() may take a long time, because it can involve
104 * NIS, DNS, or LDAP lookups. Set an alarm timer. Note that this
105 * may still fail, depending on how SIGALRM is handled by the
106 * functions invoked by getaddrinfo().
109 (void) signal(SIGALRM, timeout);
110 (void) alarm(rdate_timeout);
113 * Note: memory not freed due to short lifetime of program.
116 rc = getaddrinfo(argv[1], "time", &hints, &res);
117 (void) alarm(0);
119 if (rc != 0) {
120 (void) fprintf(stderr, "Host name %s not found: %s\n", argv[1],
121 gai_strerror(rc));
122 exit(EXIT_FAILURE);
125 connect_timeout = rdate_timeout * SECONDS_TO_MS;
126 for (; res != NULL; res = res->ai_next) {
127 s = socket(res->ai_addr->sa_family, res->ai_socktype,
128 res->ai_protocol);
129 if (s < 0) {
130 perror("rdate: socket");
131 exit(EXIT_FAILURE);
134 if (setsockopt(s, IPPROTO_TCP, TCP_CONN_ABORT_THRESHOLD,
135 (char *)&connect_timeout, sizeof (connect_timeout)) == -1) {
136 perror("setsockopt TCP_CONN_ABORT_THRESHOLD");
139 if (connect(s, res->ai_addr, res->ai_addrlen) >= 0)
140 break;
142 if (res->ai_next == NULL) {
143 perror("rdate: connect");
144 (void) close(s);
145 exit(EXIT_FAILURE);
148 (void) close(s);
151 (void) alarm(rdate_timeout);
152 if (read(s, (char *)&time, sizeof (time)) != sizeof (time)) {
153 perror("rdate: read");
154 exit(EXIT_FAILURE);
156 (void) alarm(0);
158 time = ntohl(time) - TOFFSET;
159 /* date must be later than when program was written */
160 if (time < WRITTEN) {
161 (void) fprintf(stderr, "didn't get plausible time from %s\n",
162 argv[1]);
163 exit(EXIT_FAILURE);
165 timestruct.tv_usec = 0;
166 timestruct.tv_sec = time;
167 i = settimeofday(&timestruct, 0);
168 if (i == -1) {
169 perror("couldn't set time of day");
170 exit(EXIT_FAILURE);
171 } else {
172 (void) printf("%s", ctime(&timestruct.tv_sec));
173 #if defined(i386)
174 (void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
175 #endif
177 return (EXIT_SUCCESS);
180 /*ARGSUSED*/
181 static void
182 timeout(int sig)
184 (void) fputs("couldn't contact time server\n", stderr);
185 exit(EXIT_FAILURE);
186 /* NOTREACHED */