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
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]
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
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
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.
50 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/tcp.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
75 #define WRITTEN 440199955 /* 22:45:55 13/Dec/1983 */
76 #define SECONDS_TO_MS 1000
78 static void timeout(int);
81 main(int argc
, char **argv
)
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
;
94 (void) fputs("usage: rdate host\n", stderr
);
98 (void) memset(&hints
, 0, sizeof (hints
));
99 hints
.ai_protocol
= IPPROTO_TCP
;
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
);
120 (void) fprintf(stderr
, "Host name %s not found: %s\n", argv
[1],
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
,
130 perror("rdate: socket");
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)
142 if (res
->ai_next
== NULL
) {
143 perror("rdate: connect");
151 (void) alarm(rdate_timeout
);
152 if (read(s
, (char *)&time
, sizeof (time
)) != sizeof (time
)) {
153 perror("rdate: read");
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",
165 timestruct
.tv_usec
= 0;
166 timestruct
.tv_sec
= time
;
167 i
= settimeofday(×truct
, 0);
169 perror("couldn't set time of day");
172 (void) printf("%s", ctime(×truct
.tv_sec
));
174 (void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
177 return (EXIT_SUCCESS
);
184 (void) fputs("couldn't contact time server\n", stderr
);