<sys/{ipc,stat}.h>: Remove duplicate inclusion of <sys/cdefs.h>.
[dragonfly.git] / lib / libc / rpc / rtime.c
blob60d323244f8a7eb07fd58fd69ced3fcce6dc0546
1 /*-
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI
29 * $FreeBSD: src/lib/libc/rpc/rtime.c,v 1.9 2005/03/10 00:57:01 stefanf Exp $
30 * $DragonFly: src/lib/libc/rpc/rtime.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
34 * Copyright (c) 1988 by Sun Microsystems, Inc.
38 * rtime - get time from remote machine
40 * gets time, obtaining value from host
41 * on the udp/time socket. Since timeserver returns
42 * with time of day in seconds since Jan 1, 1900, must
43 * subtract seconds before Jan 1, 1970 to get
44 * what unix uses.
46 #include "namespace.h"
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <sys/time.h>
54 #include <netinet/in.h>
55 #include <stdio.h>
56 #include <netdb.h>
57 #include "un-namespace.h"
59 extern int _rpc_dtablesize(void);
61 #define NYEARS (unsigned long)(1970 - 1900)
62 #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4)))
64 static void do_close(int);
66 int
67 rtime(struct sockaddr_in *addrp, struct timeval *timep, struct timeval *timeout)
69 int s;
70 fd_set readfds;
71 int res;
72 unsigned long thetime;
73 struct sockaddr_in from;
74 socklen_t fromlen;
75 int type;
76 struct servent *serv;
78 if (timeout == NULL) {
79 type = SOCK_STREAM;
80 } else {
81 type = SOCK_DGRAM;
83 s = _socket(AF_INET, type, 0);
84 if (s < 0) {
85 return(-1);
87 addrp->sin_family = AF_INET;
89 /* TCP and UDP port are the same in this case */
90 if ((serv = getservbyname("time", "tcp")) == NULL) {
91 return(-1);
94 addrp->sin_port = serv->s_port;
96 if (type == SOCK_DGRAM) {
97 res = _sendto(s, (char *)&thetime, sizeof(thetime), 0,
98 (struct sockaddr *)addrp, sizeof(*addrp));
99 if (res < 0) {
100 do_close(s);
101 return(-1);
103 do {
104 FD_ZERO(&readfds);
105 FD_SET(s, &readfds);
106 res = _select(_rpc_dtablesize(), &readfds,
107 NULL, NULL, timeout);
108 } while (res < 0 && errno == EINTR);
109 if (res <= 0) {
110 if (res == 0) {
111 errno = ETIMEDOUT;
113 do_close(s);
114 return(-1);
116 fromlen = sizeof(from);
117 res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
118 (struct sockaddr *)&from, &fromlen);
119 do_close(s);
120 if (res < 0) {
121 return(-1);
123 } else {
124 if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) {
125 do_close(s);
126 return(-1);
128 res = _read(s, (char *)&thetime, sizeof(thetime));
129 do_close(s);
130 if (res < 0) {
131 return(-1);
134 if (res != sizeof(thetime)) {
135 errno = EIO;
136 return(-1);
138 thetime = ntohl(thetime);
139 timep->tv_sec = thetime - TOFFSET;
140 timep->tv_usec = 0;
141 return(0);
144 static void
145 do_close(int s)
147 int save;
149 save = errno;
150 _close(s);
151 errno = save;