clnt_dg.c revision 1.12 1 1.12 itojun /* $NetBSD: clnt_dg.c,v 1.12 2003/09/09 03:56:39 itojun Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.1 fvdl * unrestricted use provided that this legend is included on all tape
6 1.1 fvdl * media and as a part of the software program in whole or part. Users
7 1.1 fvdl * may copy or modify Sun RPC without charge, but are not authorized
8 1.1 fvdl * to license or distribute it to anyone else except as part of a product or
9 1.1 fvdl * program developed by the user.
10 1.1 fvdl *
11 1.1 fvdl * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 fvdl * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 fvdl * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.1 fvdl *
15 1.1 fvdl * Sun RPC is provided with no support and without any obligation on the
16 1.1 fvdl * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 fvdl * modification or enhancement.
18 1.1 fvdl *
19 1.1 fvdl * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 fvdl * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 fvdl * OR ANY PART THEREOF.
22 1.1 fvdl *
23 1.1 fvdl * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 fvdl * or profits or other special, indirect and consequential damages, even if
25 1.1 fvdl * Sun has been advised of the possibility of such damages.
26 1.1 fvdl *
27 1.1 fvdl * Sun Microsystems, Inc.
28 1.1 fvdl * 2550 Garcia Avenue
29 1.1 fvdl * Mountain View, California 94043
30 1.1 fvdl */
31 1.1 fvdl /*
32 1.1 fvdl * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 1.1 fvdl */
34 1.1 fvdl
35 1.1 fvdl /* #ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" */
36 1.1 fvdl
37 1.12 itojun #include <sys/cdefs.h>
38 1.12 itojun #if defined(LIBC_SCCS) && !defined(lint)
39 1.1 fvdl #if 0
40 1.1 fvdl static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
41 1.12 itojun #else
42 1.12 itojun __RCSID("$NetBSD: clnt_dg.c,v 1.12 2003/09/09 03:56:39 itojun Exp $");
43 1.1 fvdl #endif
44 1.1 fvdl #endif
45 1.1 fvdl
46 1.1 fvdl /*
47 1.1 fvdl * Implements a connectionless client side RPC.
48 1.1 fvdl */
49 1.1 fvdl
50 1.1 fvdl #include "namespace.h"
51 1.1 fvdl #include "reentrant.h"
52 1.1 fvdl #include <sys/poll.h>
53 1.1 fvdl #include <sys/types.h>
54 1.1 fvdl #include <sys/time.h>
55 1.1 fvdl #include <sys/socket.h>
56 1.1 fvdl #include <sys/ioctl.h>
57 1.1 fvdl #include <rpc/rpc.h>
58 1.7 lukem #include <assert.h>
59 1.1 fvdl #include <errno.h>
60 1.1 fvdl #include <stdlib.h>
61 1.2 thorpej #include <string.h>
62 1.1 fvdl #include <signal.h>
63 1.1 fvdl #include <unistd.h>
64 1.1 fvdl #include <err.h>
65 1.8 fvdl #include "rpc_internal.h"
66 1.1 fvdl
67 1.1 fvdl #ifdef __weak_alias
68 1.1 fvdl __weak_alias(clnt_dg_create,_clnt_dg_create)
69 1.1 fvdl #endif
70 1.1 fvdl
71 1.1 fvdl #define RPC_MAX_BACKOFF 30 /* seconds */
72 1.1 fvdl
73 1.1 fvdl
74 1.1 fvdl static struct clnt_ops *clnt_dg_ops __P((void));
75 1.1 fvdl static bool_t time_not_ok __P((struct timeval *));
76 1.1 fvdl static enum clnt_stat clnt_dg_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t,
77 1.1 fvdl xdrproc_t, caddr_t, struct timeval));
78 1.1 fvdl static void clnt_dg_geterr __P((CLIENT *, struct rpc_err *));
79 1.1 fvdl static bool_t clnt_dg_freeres __P((CLIENT *, xdrproc_t, caddr_t));
80 1.1 fvdl static void clnt_dg_abort __P((CLIENT *));
81 1.1 fvdl static bool_t clnt_dg_control __P((CLIENT *, u_int, char *));
82 1.1 fvdl static void clnt_dg_destroy __P((CLIENT *));
83 1.1 fvdl static int __rpc_timeval_to_msec __P((struct timeval *));
84 1.1 fvdl
85 1.1 fvdl
86 1.1 fvdl
87 1.1 fvdl
88 1.1 fvdl /*
89 1.1 fvdl * This machinery implements per-fd locks for MT-safety. It is not
90 1.1 fvdl * sufficient to do per-CLIENT handle locks for MT-safety because a
91 1.1 fvdl * user may create more than one CLIENT handle with the same fd behind
92 1.1 fvdl * it. Therfore, we allocate an array of flags (dg_fd_locks), protected
93 1.1 fvdl * by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
94 1.1 fvdl * similarly protected. Dg_fd_lock[fd] == 1 => a call is activte on some
95 1.1 fvdl * CLIENT handle created for that fd.
96 1.1 fvdl * The current implementation holds locks across the entire RPC and reply,
97 1.1 fvdl * including retransmissions. Yes, this is silly, and as soon as this
98 1.1 fvdl * code is proven to work, this should be the first thing fixed. One step
99 1.1 fvdl * at a time.
100 1.1 fvdl */
101 1.1 fvdl static int *dg_fd_locks;
102 1.9 thorpej #ifdef _REENTRANT
103 1.9 thorpej extern int __isthreaded;
104 1.9 thorpej #define __rpc_lock_value __isthreaded;
105 1.1 fvdl extern mutex_t clnt_fd_lock;
106 1.1 fvdl static cond_t *dg_cv;
107 1.1 fvdl #define release_fd_lock(fd, mask) { \
108 1.1 fvdl mutex_lock(&clnt_fd_lock); \
109 1.1 fvdl dg_fd_locks[fd] = 0; \
110 1.1 fvdl mutex_unlock(&clnt_fd_lock); \
111 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL); \
112 1.1 fvdl cond_signal(&dg_cv[fd]); \
113 1.1 fvdl }
114 1.1 fvdl #else
115 1.1 fvdl #define release_fd_lock(fd,mask)
116 1.1 fvdl #define __rpc_lock_value 0
117 1.1 fvdl #endif
118 1.1 fvdl
119 1.1 fvdl static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
120 1.1 fvdl
121 1.1 fvdl /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
122 1.1 fvdl
123 1.1 fvdl /*
124 1.1 fvdl * Private data kept per client handle
125 1.1 fvdl */
126 1.1 fvdl struct cu_data {
127 1.1 fvdl int cu_fd; /* connections fd */
128 1.1 fvdl bool_t cu_closeit; /* opened by library */
129 1.1 fvdl struct sockaddr_storage cu_raddr; /* remote address */
130 1.1 fvdl int cu_rlen;
131 1.1 fvdl struct timeval cu_wait; /* retransmit interval */
132 1.1 fvdl struct timeval cu_total; /* total time for the call */
133 1.1 fvdl struct rpc_err cu_error;
134 1.1 fvdl XDR cu_outxdrs;
135 1.1 fvdl u_int cu_xdrpos;
136 1.1 fvdl u_int cu_sendsz; /* send size */
137 1.1 fvdl char *cu_outbuf;
138 1.1 fvdl u_int cu_recvsz; /* recv size */
139 1.1 fvdl struct pollfd pfdp;
140 1.1 fvdl char cu_inbuf[1];
141 1.1 fvdl };
142 1.1 fvdl
143 1.1 fvdl /*
144 1.1 fvdl * Connection less client creation returns with client handle parameters.
145 1.1 fvdl * Default options are set, which the user can change using clnt_control().
146 1.1 fvdl * fd should be open and bound.
147 1.1 fvdl * NB: The rpch->cl_auth is initialized to null authentication.
148 1.1 fvdl * Caller may wish to set this something more useful.
149 1.1 fvdl *
150 1.1 fvdl * sendsz and recvsz are the maximum allowable packet sizes that can be
151 1.1 fvdl * sent and received. Normally they are the same, but they can be
152 1.1 fvdl * changed to improve the program efficiency and buffer allocation.
153 1.1 fvdl * If they are 0, use the transport default.
154 1.1 fvdl *
155 1.1 fvdl * If svcaddr is NULL, returns NULL.
156 1.1 fvdl */
157 1.1 fvdl CLIENT *
158 1.1 fvdl clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
159 1.1 fvdl int fd; /* open file descriptor */
160 1.3 christos const struct netbuf *svcaddr; /* servers address */
161 1.3 christos rpcprog_t program; /* program number */
162 1.3 christos rpcvers_t version; /* version number */
163 1.1 fvdl u_int sendsz; /* buffer recv size */
164 1.1 fvdl u_int recvsz; /* buffer send size */
165 1.1 fvdl {
166 1.3 christos CLIENT *cl = NULL; /* client handle */
167 1.3 christos struct cu_data *cu = NULL; /* private data */
168 1.1 fvdl struct rpc_msg call_msg;
169 1.9 thorpej #ifdef _REENTRANT
170 1.1 fvdl sigset_t mask;
171 1.1 fvdl #endif
172 1.1 fvdl sigset_t newmask;
173 1.1 fvdl struct __rpc_sockinfo si;
174 1.1 fvdl int one = 1;
175 1.1 fvdl
176 1.1 fvdl sigfillset(&newmask);
177 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
178 1.1 fvdl mutex_lock(&clnt_fd_lock);
179 1.1 fvdl if (dg_fd_locks == (int *) NULL) {
180 1.9 thorpej #ifdef _REENTRANT
181 1.9 thorpej size_t cv_allocsz;
182 1.1 fvdl #endif
183 1.3 christos size_t fd_allocsz;
184 1.1 fvdl int dtbsize = __rpc_dtbsize();
185 1.1 fvdl
186 1.1 fvdl fd_allocsz = dtbsize * sizeof (int);
187 1.1 fvdl dg_fd_locks = (int *) mem_alloc(fd_allocsz);
188 1.1 fvdl if (dg_fd_locks == (int *) NULL) {
189 1.1 fvdl mutex_unlock(&clnt_fd_lock);
190 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
191 1.1 fvdl goto err1;
192 1.1 fvdl } else
193 1.1 fvdl memset(dg_fd_locks, '\0', fd_allocsz);
194 1.1 fvdl
195 1.9 thorpej #ifdef _REENTRANT
196 1.1 fvdl cv_allocsz = dtbsize * sizeof (cond_t);
197 1.1 fvdl dg_cv = (cond_t *) mem_alloc(cv_allocsz);
198 1.1 fvdl if (dg_cv == (cond_t *) NULL) {
199 1.1 fvdl mem_free(dg_fd_locks, fd_allocsz);
200 1.1 fvdl dg_fd_locks = (int *) NULL;
201 1.1 fvdl mutex_unlock(&clnt_fd_lock);
202 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
203 1.1 fvdl goto err1;
204 1.1 fvdl } else {
205 1.1 fvdl int i;
206 1.1 fvdl
207 1.1 fvdl for (i = 0; i < dtbsize; i++)
208 1.1 fvdl cond_init(&dg_cv[i], 0, (void *) 0);
209 1.1 fvdl }
210 1.1 fvdl #endif
211 1.1 fvdl }
212 1.1 fvdl
213 1.1 fvdl mutex_unlock(&clnt_fd_lock);
214 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
215 1.1 fvdl
216 1.3 christos if (svcaddr == NULL) {
217 1.1 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
218 1.3 christos return (NULL);
219 1.1 fvdl }
220 1.1 fvdl
221 1.1 fvdl if (!__rpc_fd2sockinfo(fd, &si)) {
222 1.1 fvdl rpc_createerr.cf_stat = RPC_TLIERROR;
223 1.1 fvdl rpc_createerr.cf_error.re_errno = 0;
224 1.3 christos return (NULL);
225 1.1 fvdl }
226 1.1 fvdl /*
227 1.1 fvdl * Find the receive and the send size
228 1.1 fvdl */
229 1.1 fvdl sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
230 1.1 fvdl recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
231 1.1 fvdl if ((sendsz == 0) || (recvsz == 0)) {
232 1.1 fvdl rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
233 1.1 fvdl rpc_createerr.cf_error.re_errno = 0;
234 1.3 christos return (NULL);
235 1.1 fvdl }
236 1.1 fvdl
237 1.3 christos if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
238 1.1 fvdl goto err1;
239 1.1 fvdl /*
240 1.1 fvdl * Should be multiple of 4 for XDR.
241 1.1 fvdl */
242 1.1 fvdl sendsz = ((sendsz + 3) / 4) * 4;
243 1.1 fvdl recvsz = ((recvsz + 3) / 4) * 4;
244 1.3 christos cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
245 1.3 christos if (cu == NULL)
246 1.1 fvdl goto err1;
247 1.3 christos (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
248 1.1 fvdl cu->cu_rlen = svcaddr->len;
249 1.1 fvdl cu->cu_outbuf = &cu->cu_inbuf[recvsz];
250 1.1 fvdl /* Other values can also be set through clnt_control() */
251 1.1 fvdl cu->cu_wait.tv_sec = 15; /* heuristically chosen */
252 1.1 fvdl cu->cu_wait.tv_usec = 0;
253 1.1 fvdl cu->cu_total.tv_sec = -1;
254 1.1 fvdl cu->cu_total.tv_usec = -1;
255 1.1 fvdl cu->cu_sendsz = sendsz;
256 1.1 fvdl cu->cu_recvsz = recvsz;
257 1.11 itojun call_msg.rm_xid = __RPC_GETXID();
258 1.1 fvdl call_msg.rm_call.cb_prog = program;
259 1.1 fvdl call_msg.rm_call.cb_vers = version;
260 1.1 fvdl xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
261 1.1 fvdl if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
262 1.1 fvdl rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */
263 1.1 fvdl rpc_createerr.cf_error.re_errno = 0;
264 1.1 fvdl goto err2;
265 1.1 fvdl }
266 1.1 fvdl cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
267 1.1 fvdl
268 1.1 fvdl /* XXX fvdl - do we still want this? */
269 1.1 fvdl #if 0
270 1.1 fvdl (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
271 1.1 fvdl #endif
272 1.1 fvdl ioctl(fd, FIONBIO, (char *)(void *)&one);
273 1.1 fvdl
274 1.1 fvdl /*
275 1.1 fvdl * By default, closeit is always FALSE. It is users responsibility
276 1.1 fvdl * to do a close on it, else the user may use clnt_control
277 1.1 fvdl * to let clnt_destroy do it for him/her.
278 1.1 fvdl */
279 1.1 fvdl cu->cu_closeit = FALSE;
280 1.1 fvdl cu->cu_fd = fd;
281 1.1 fvdl cl->cl_ops = clnt_dg_ops();
282 1.3 christos cl->cl_private = (caddr_t)(void *)cu;
283 1.1 fvdl cl->cl_auth = authnone_create();
284 1.3 christos cl->cl_tp = NULL;
285 1.3 christos cl->cl_netid = NULL;
286 1.1 fvdl cu->pfdp.fd = cu->cu_fd;
287 1.1 fvdl cu->pfdp.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
288 1.1 fvdl return (cl);
289 1.1 fvdl err1:
290 1.1 fvdl warnx(mem_err_clnt_dg);
291 1.1 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
292 1.1 fvdl rpc_createerr.cf_error.re_errno = errno;
293 1.1 fvdl err2:
294 1.1 fvdl if (cl) {
295 1.3 christos mem_free(cl, sizeof (CLIENT));
296 1.1 fvdl if (cu)
297 1.3 christos mem_free(cu, sizeof (*cu) + sendsz + recvsz);
298 1.1 fvdl }
299 1.3 christos return (NULL);
300 1.1 fvdl }
301 1.1 fvdl
302 1.1 fvdl static enum clnt_stat
303 1.1 fvdl clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
304 1.3 christos CLIENT *cl; /* client handle */
305 1.1 fvdl rpcproc_t proc; /* procedure number */
306 1.1 fvdl xdrproc_t xargs; /* xdr routine for args */
307 1.1 fvdl caddr_t argsp; /* pointer to args */
308 1.1 fvdl xdrproc_t xresults; /* xdr routine for results */
309 1.1 fvdl caddr_t resultsp; /* pointer to results */
310 1.1 fvdl struct timeval utimeout; /* seconds to wait before giving up */
311 1.1 fvdl {
312 1.7 lukem struct cu_data *cu;
313 1.3 christos XDR *xdrs;
314 1.3 christos size_t outlen;
315 1.1 fvdl struct rpc_msg reply_msg;
316 1.1 fvdl XDR reply_xdrs;
317 1.1 fvdl struct timeval time_waited;
318 1.1 fvdl bool_t ok;
319 1.1 fvdl int nrefreshes = 2; /* number of times to refresh cred */
320 1.1 fvdl struct timeval timeout;
321 1.1 fvdl struct timeval retransmit_time;
322 1.1 fvdl struct timeval startime, curtime;
323 1.1 fvdl int firsttimeout = 1;
324 1.9 thorpej #ifdef _REENTRANT
325 1.1 fvdl sigset_t mask;
326 1.1 fvdl #endif
327 1.1 fvdl sigset_t newmask;
328 1.3 christos socklen_t fromlen, inlen;
329 1.3 christos ssize_t recvlen = 0;
330 1.1 fvdl
331 1.7 lukem _DIAGASSERT(cl != NULL);
332 1.7 lukem
333 1.7 lukem cu = (struct cu_data *)cl->cl_private;
334 1.7 lukem
335 1.1 fvdl sigfillset(&newmask);
336 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
337 1.1 fvdl mutex_lock(&clnt_fd_lock);
338 1.1 fvdl while (dg_fd_locks[cu->cu_fd])
339 1.1 fvdl cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
340 1.1 fvdl dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
341 1.1 fvdl mutex_unlock(&clnt_fd_lock);
342 1.1 fvdl if (cu->cu_total.tv_usec == -1) {
343 1.1 fvdl timeout = utimeout; /* use supplied timeout */
344 1.1 fvdl } else {
345 1.1 fvdl timeout = cu->cu_total; /* use default timeout */
346 1.1 fvdl }
347 1.1 fvdl
348 1.1 fvdl time_waited.tv_sec = 0;
349 1.1 fvdl time_waited.tv_usec = 0;
350 1.1 fvdl retransmit_time = cu->cu_wait;
351 1.1 fvdl
352 1.1 fvdl call_again:
353 1.1 fvdl xdrs = &(cu->cu_outxdrs);
354 1.1 fvdl xdrs->x_op = XDR_ENCODE;
355 1.1 fvdl XDR_SETPOS(xdrs, cu->cu_xdrpos);
356 1.1 fvdl /*
357 1.1 fvdl * the transaction is the first thing in the out buffer
358 1.1 fvdl */
359 1.3 christos (*(u_int32_t *)(void *)(cu->cu_outbuf))++;
360 1.6 christos if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
361 1.1 fvdl (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
362 1.1 fvdl (! (*xargs)(xdrs, argsp))) {
363 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
364 1.1 fvdl return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
365 1.1 fvdl }
366 1.3 christos outlen = (size_t)XDR_GETPOS(xdrs);
367 1.1 fvdl
368 1.1 fvdl send_again:
369 1.1 fvdl if (sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0,
370 1.3 christos (struct sockaddr *)(void *)&cu->cu_raddr, (socklen_t)cu->cu_rlen)
371 1.1 fvdl != outlen) {
372 1.1 fvdl cu->cu_error.re_errno = errno;
373 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
374 1.1 fvdl return (cu->cu_error.re_status = RPC_CANTSEND);
375 1.1 fvdl }
376 1.1 fvdl
377 1.1 fvdl /*
378 1.1 fvdl * Hack to provide rpc-based message passing
379 1.1 fvdl */
380 1.1 fvdl if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
381 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
382 1.1 fvdl return (cu->cu_error.re_status = RPC_TIMEDOUT);
383 1.1 fvdl }
384 1.1 fvdl /*
385 1.1 fvdl * sub-optimal code appears here because we have
386 1.1 fvdl * some clock time to spare while the packets are in flight.
387 1.1 fvdl * (We assume that this is actually only executed once.)
388 1.1 fvdl */
389 1.1 fvdl reply_msg.acpted_rply.ar_verf = _null_auth;
390 1.1 fvdl reply_msg.acpted_rply.ar_results.where = resultsp;
391 1.1 fvdl reply_msg.acpted_rply.ar_results.proc = xresults;
392 1.1 fvdl
393 1.1 fvdl
394 1.1 fvdl for (;;) {
395 1.3 christos switch (poll(&cu->pfdp, 1,
396 1.3 christos __rpc_timeval_to_msec(&retransmit_time))) {
397 1.1 fvdl case 0:
398 1.1 fvdl time_waited.tv_sec += retransmit_time.tv_sec;
399 1.1 fvdl time_waited.tv_usec += retransmit_time.tv_usec;
400 1.1 fvdl while (time_waited.tv_usec >= 1000000) {
401 1.1 fvdl time_waited.tv_sec++;
402 1.1 fvdl time_waited.tv_usec -= 1000000;
403 1.1 fvdl }
404 1.1 fvdl /* update retransmit_time */
405 1.1 fvdl if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) {
406 1.1 fvdl retransmit_time.tv_usec *= 2;
407 1.1 fvdl retransmit_time.tv_sec *= 2;
408 1.1 fvdl while (retransmit_time.tv_usec >= 1000000) {
409 1.1 fvdl retransmit_time.tv_sec++;
410 1.1 fvdl retransmit_time.tv_usec -= 1000000;
411 1.1 fvdl }
412 1.1 fvdl }
413 1.1 fvdl
414 1.1 fvdl if ((time_waited.tv_sec < timeout.tv_sec) ||
415 1.1 fvdl ((time_waited.tv_sec == timeout.tv_sec) &&
416 1.1 fvdl (time_waited.tv_usec < timeout.tv_usec)))
417 1.1 fvdl goto send_again;
418 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
419 1.1 fvdl return (cu->cu_error.re_status = RPC_TIMEDOUT);
420 1.1 fvdl
421 1.1 fvdl case -1:
422 1.1 fvdl if (errno == EBADF) {
423 1.1 fvdl cu->cu_error.re_errno = errno;
424 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
425 1.1 fvdl return (cu->cu_error.re_status = RPC_CANTRECV);
426 1.1 fvdl }
427 1.1 fvdl if (errno != EINTR) {
428 1.1 fvdl errno = 0; /* reset it */
429 1.1 fvdl continue;
430 1.1 fvdl }
431 1.1 fvdl /* interrupted by another signal, update time_waited */
432 1.1 fvdl if (firsttimeout) {
433 1.1 fvdl /*
434 1.1 fvdl * Could have done gettimeofday before clnt_call
435 1.1 fvdl * but that means 1 more system call per each
436 1.1 fvdl * clnt_call, so do it after first time out
437 1.1 fvdl */
438 1.1 fvdl if (gettimeofday(&startime,
439 1.1 fvdl (struct timezone *) NULL) == -1) {
440 1.1 fvdl errno = 0;
441 1.1 fvdl continue;
442 1.1 fvdl }
443 1.1 fvdl firsttimeout = 0;
444 1.1 fvdl errno = 0;
445 1.1 fvdl continue;
446 1.1 fvdl };
447 1.1 fvdl if (gettimeofday(&curtime,
448 1.1 fvdl (struct timezone *) NULL) == -1) {
449 1.1 fvdl errno = 0;
450 1.1 fvdl continue;
451 1.1 fvdl };
452 1.1 fvdl time_waited.tv_sec += curtime.tv_sec - startime.tv_sec;
453 1.1 fvdl time_waited.tv_usec += curtime.tv_usec -
454 1.1 fvdl startime.tv_usec;
455 1.1 fvdl while (time_waited.tv_usec < 0) {
456 1.1 fvdl time_waited.tv_sec--;
457 1.1 fvdl time_waited.tv_usec += 1000000;
458 1.1 fvdl };
459 1.1 fvdl while (time_waited.tv_usec >= 1000000) {
460 1.1 fvdl time_waited.tv_sec++;
461 1.1 fvdl time_waited.tv_usec -= 1000000;
462 1.1 fvdl }
463 1.1 fvdl startime.tv_sec = curtime.tv_sec;
464 1.1 fvdl startime.tv_usec = curtime.tv_usec;
465 1.1 fvdl if ((time_waited.tv_sec > timeout.tv_sec) ||
466 1.1 fvdl ((time_waited.tv_sec == timeout.tv_sec) &&
467 1.1 fvdl (time_waited.tv_usec > timeout.tv_usec))) {
468 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
469 1.1 fvdl return (cu->cu_error.re_status = RPC_TIMEDOUT);
470 1.1 fvdl }
471 1.1 fvdl errno = 0; /* reset it */
472 1.1 fvdl continue;
473 1.1 fvdl };
474 1.1 fvdl
475 1.1 fvdl if (cu->pfdp.revents & POLLNVAL || (cu->pfdp.revents == 0)) {
476 1.1 fvdl cu->cu_error.re_status = RPC_CANTRECV;
477 1.1 fvdl /*
478 1.1 fvdl * Note: we're faking errno here because we
479 1.1 fvdl * previously would have expected poll() to
480 1.1 fvdl * return -1 with errno EBADF. Poll(BA_OS)
481 1.1 fvdl * returns 0 and sets the POLLNVAL revents flag
482 1.1 fvdl * instead.
483 1.1 fvdl */
484 1.1 fvdl cu->cu_error.re_errno = errno = EBADF;
485 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
486 1.1 fvdl return (-1);
487 1.1 fvdl }
488 1.1 fvdl
489 1.1 fvdl /* We have some data now */
490 1.1 fvdl do {
491 1.1 fvdl if (errno == EINTR) {
492 1.1 fvdl /*
493 1.1 fvdl * Must make sure errno was not already
494 1.1 fvdl * EINTR in case recvfrom() returns -1.
495 1.1 fvdl */
496 1.1 fvdl errno = 0;
497 1.1 fvdl }
498 1.1 fvdl fromlen = sizeof (struct sockaddr_storage);
499 1.3 christos recvlen = recvfrom(cu->cu_fd, cu->cu_inbuf,
500 1.3 christos cu->cu_recvsz, 0, (struct sockaddr *)(void *)&cu->cu_raddr,
501 1.1 fvdl &fromlen);
502 1.3 christos } while (recvlen < 0 && errno == EINTR);
503 1.3 christos if (recvlen < 0) {
504 1.1 fvdl if (errno == EWOULDBLOCK)
505 1.1 fvdl continue;
506 1.1 fvdl cu->cu_error.re_errno = errno;
507 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
508 1.1 fvdl return (cu->cu_error.re_status = RPC_CANTRECV);
509 1.1 fvdl }
510 1.3 christos if (recvlen < sizeof (u_int32_t))
511 1.1 fvdl continue;
512 1.1 fvdl /* see if reply transaction id matches sent id */
513 1.3 christos if (*((u_int32_t *)(void *)(cu->cu_inbuf)) !=
514 1.3 christos *((u_int32_t *)(void *)(cu->cu_outbuf)))
515 1.1 fvdl continue;
516 1.1 fvdl /* we now assume we have the proper reply */
517 1.1 fvdl break;
518 1.1 fvdl }
519 1.3 christos inlen = (socklen_t)recvlen;
520 1.1 fvdl
521 1.1 fvdl /*
522 1.1 fvdl * now decode and validate the response
523 1.1 fvdl */
524 1.1 fvdl
525 1.1 fvdl xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
526 1.1 fvdl ok = xdr_replymsg(&reply_xdrs, &reply_msg);
527 1.1 fvdl /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
528 1.1 fvdl if (ok) {
529 1.1 fvdl if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
530 1.1 fvdl (reply_msg.acpted_rply.ar_stat == SUCCESS))
531 1.1 fvdl cu->cu_error.re_status = RPC_SUCCESS;
532 1.1 fvdl else
533 1.1 fvdl _seterr_reply(&reply_msg, &(cu->cu_error));
534 1.1 fvdl
535 1.1 fvdl if (cu->cu_error.re_status == RPC_SUCCESS) {
536 1.1 fvdl if (! AUTH_VALIDATE(cl->cl_auth,
537 1.1 fvdl &reply_msg.acpted_rply.ar_verf)) {
538 1.1 fvdl cu->cu_error.re_status = RPC_AUTHERROR;
539 1.1 fvdl cu->cu_error.re_why = AUTH_INVALIDRESP;
540 1.1 fvdl }
541 1.1 fvdl if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
542 1.1 fvdl xdrs->x_op = XDR_FREE;
543 1.1 fvdl (void) xdr_opaque_auth(xdrs,
544 1.1 fvdl &(reply_msg.acpted_rply.ar_verf));
545 1.1 fvdl }
546 1.1 fvdl } /* end successful completion */
547 1.1 fvdl /*
548 1.1 fvdl * If unsuccesful AND error is an authentication error
549 1.1 fvdl * then refresh credentials and try again, else break
550 1.1 fvdl */
551 1.1 fvdl else if (cu->cu_error.re_status == RPC_AUTHERROR)
552 1.1 fvdl /* maybe our credentials need to be refreshed ... */
553 1.1 fvdl if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
554 1.1 fvdl nrefreshes--;
555 1.1 fvdl goto call_again;
556 1.1 fvdl }
557 1.1 fvdl /* end of unsuccessful completion */
558 1.1 fvdl } /* end of valid reply message */
559 1.1 fvdl else {
560 1.1 fvdl cu->cu_error.re_status = RPC_CANTDECODERES;
561 1.1 fvdl
562 1.1 fvdl }
563 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
564 1.1 fvdl return (cu->cu_error.re_status);
565 1.1 fvdl }
566 1.1 fvdl
567 1.1 fvdl static void
568 1.1 fvdl clnt_dg_geterr(cl, errp)
569 1.1 fvdl CLIENT *cl;
570 1.1 fvdl struct rpc_err *errp;
571 1.1 fvdl {
572 1.7 lukem struct cu_data *cu;
573 1.1 fvdl
574 1.7 lukem _DIAGASSERT(cl != NULL);
575 1.7 lukem _DIAGASSERT(errp != NULL);
576 1.7 lukem
577 1.7 lukem cu = (struct cu_data *)cl->cl_private;
578 1.1 fvdl *errp = cu->cu_error;
579 1.1 fvdl }
580 1.1 fvdl
581 1.1 fvdl static bool_t
582 1.1 fvdl clnt_dg_freeres(cl, xdr_res, res_ptr)
583 1.1 fvdl CLIENT *cl;
584 1.1 fvdl xdrproc_t xdr_res;
585 1.1 fvdl caddr_t res_ptr;
586 1.1 fvdl {
587 1.7 lukem struct cu_data *cu;
588 1.7 lukem XDR *xdrs;
589 1.1 fvdl bool_t dummy;
590 1.9 thorpej #ifdef _REENTRANT
591 1.1 fvdl sigset_t mask;
592 1.1 fvdl #endif
593 1.1 fvdl sigset_t newmask;
594 1.1 fvdl
595 1.7 lukem _DIAGASSERT(cl != NULL);
596 1.7 lukem cu = (struct cu_data *)cl->cl_private;
597 1.7 lukem xdrs = &(cu->cu_outxdrs);
598 1.7 lukem
599 1.1 fvdl sigfillset(&newmask);
600 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
601 1.1 fvdl mutex_lock(&clnt_fd_lock);
602 1.1 fvdl while (dg_fd_locks[cu->cu_fd])
603 1.1 fvdl cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
604 1.1 fvdl xdrs->x_op = XDR_FREE;
605 1.1 fvdl dummy = (*xdr_res)(xdrs, res_ptr);
606 1.1 fvdl mutex_unlock(&clnt_fd_lock);
607 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &mask, NULL);
608 1.1 fvdl cond_signal(&dg_cv[cu->cu_fd]);
609 1.1 fvdl return (dummy);
610 1.1 fvdl }
611 1.1 fvdl
612 1.1 fvdl /*ARGSUSED*/
613 1.1 fvdl static void
614 1.1 fvdl clnt_dg_abort(h)
615 1.1 fvdl CLIENT *h;
616 1.1 fvdl {
617 1.1 fvdl }
618 1.1 fvdl
619 1.1 fvdl static bool_t
620 1.1 fvdl clnt_dg_control(cl, request, info)
621 1.1 fvdl CLIENT *cl;
622 1.1 fvdl u_int request;
623 1.1 fvdl char *info;
624 1.1 fvdl {
625 1.7 lukem struct cu_data *cu;
626 1.1 fvdl struct netbuf *addr;
627 1.9 thorpej #ifdef _REENTRANT
628 1.1 fvdl sigset_t mask;
629 1.1 fvdl #endif
630 1.1 fvdl sigset_t newmask;
631 1.1 fvdl
632 1.7 lukem _DIAGASSERT(cl != NULL);
633 1.7 lukem /* info is handled below */
634 1.7 lukem
635 1.7 lukem cu = (struct cu_data *)cl->cl_private;
636 1.7 lukem
637 1.1 fvdl sigfillset(&newmask);
638 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
639 1.1 fvdl mutex_lock(&clnt_fd_lock);
640 1.1 fvdl while (dg_fd_locks[cu->cu_fd])
641 1.1 fvdl cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
642 1.1 fvdl dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
643 1.1 fvdl mutex_unlock(&clnt_fd_lock);
644 1.1 fvdl switch (request) {
645 1.1 fvdl case CLSET_FD_CLOSE:
646 1.1 fvdl cu->cu_closeit = TRUE;
647 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
648 1.1 fvdl return (TRUE);
649 1.1 fvdl case CLSET_FD_NCLOSE:
650 1.1 fvdl cu->cu_closeit = FALSE;
651 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
652 1.1 fvdl return (TRUE);
653 1.1 fvdl }
654 1.1 fvdl
655 1.1 fvdl /* for other requests which use info */
656 1.1 fvdl if (info == NULL) {
657 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
658 1.1 fvdl return (FALSE);
659 1.1 fvdl }
660 1.1 fvdl switch (request) {
661 1.1 fvdl case CLSET_TIMEOUT:
662 1.3 christos if (time_not_ok((struct timeval *)(void *)info)) {
663 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
664 1.1 fvdl return (FALSE);
665 1.1 fvdl }
666 1.3 christos cu->cu_total = *(struct timeval *)(void *)info;
667 1.1 fvdl break;
668 1.1 fvdl case CLGET_TIMEOUT:
669 1.3 christos *(struct timeval *)(void *)info = cu->cu_total;
670 1.1 fvdl break;
671 1.1 fvdl case CLGET_SERVER_ADDR: /* Give him the fd address */
672 1.1 fvdl /* Now obsolete. Only for backward compatibility */
673 1.3 christos (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
674 1.1 fvdl break;
675 1.1 fvdl case CLSET_RETRY_TIMEOUT:
676 1.3 christos if (time_not_ok((struct timeval *)(void *)info)) {
677 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
678 1.1 fvdl return (FALSE);
679 1.1 fvdl }
680 1.3 christos cu->cu_wait = *(struct timeval *)(void *)info;
681 1.1 fvdl break;
682 1.1 fvdl case CLGET_RETRY_TIMEOUT:
683 1.3 christos *(struct timeval *)(void *)info = cu->cu_wait;
684 1.1 fvdl break;
685 1.1 fvdl case CLGET_FD:
686 1.3 christos *(int *)(void *)info = cu->cu_fd;
687 1.1 fvdl break;
688 1.1 fvdl case CLGET_SVC_ADDR:
689 1.3 christos addr = (struct netbuf *)(void *)info;
690 1.1 fvdl addr->buf = &cu->cu_raddr;
691 1.1 fvdl addr->len = cu->cu_rlen;
692 1.1 fvdl addr->maxlen = sizeof cu->cu_raddr;
693 1.1 fvdl break;
694 1.1 fvdl case CLSET_SVC_ADDR: /* set to new address */
695 1.3 christos addr = (struct netbuf *)(void *)info;
696 1.10 yamt if (addr->len < sizeof cu->cu_raddr) {
697 1.10 yamt release_fd_lock(cu->cu_fd, mask);
698 1.1 fvdl return (FALSE);
699 1.10 yamt }
700 1.1 fvdl (void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
701 1.1 fvdl cu->cu_rlen = addr->len;
702 1.1 fvdl break;
703 1.1 fvdl case CLGET_XID:
704 1.1 fvdl /*
705 1.1 fvdl * use the knowledge that xid is the
706 1.1 fvdl * first element in the call structure *.
707 1.1 fvdl * This will get the xid of the PREVIOUS call
708 1.1 fvdl */
709 1.3 christos *(u_int32_t *)(void *)info =
710 1.3 christos ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
711 1.1 fvdl break;
712 1.1 fvdl
713 1.1 fvdl case CLSET_XID:
714 1.1 fvdl /* This will set the xid of the NEXT call */
715 1.3 christos *(u_int32_t *)(void *)cu->cu_outbuf =
716 1.3 christos htonl(*(u_int32_t *)(void *)info - 1);
717 1.1 fvdl /* decrement by 1 as clnt_dg_call() increments once */
718 1.1 fvdl break;
719 1.1 fvdl
720 1.1 fvdl case CLGET_VERS:
721 1.1 fvdl /*
722 1.1 fvdl * This RELIES on the information that, in the call body,
723 1.1 fvdl * the version number field is the fifth field from the
724 1.1 fvdl * begining of the RPC header. MUST be changed if the
725 1.1 fvdl * call_struct is changed
726 1.1 fvdl */
727 1.3 christos *(u_int32_t *)(void *)info =
728 1.3 christos ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
729 1.3 christos 4 * BYTES_PER_XDR_UNIT));
730 1.1 fvdl break;
731 1.1 fvdl
732 1.1 fvdl case CLSET_VERS:
733 1.3 christos *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
734 1.3 christos = htonl(*(u_int32_t *)(void *)info);
735 1.1 fvdl break;
736 1.1 fvdl
737 1.1 fvdl case CLGET_PROG:
738 1.1 fvdl /*
739 1.1 fvdl * This RELIES on the information that, in the call body,
740 1.1 fvdl * the program number field is the fourth field from the
741 1.1 fvdl * begining of the RPC header. MUST be changed if the
742 1.1 fvdl * call_struct is changed
743 1.1 fvdl */
744 1.3 christos *(u_int32_t *)(void *)info =
745 1.3 christos ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
746 1.3 christos 3 * BYTES_PER_XDR_UNIT));
747 1.1 fvdl break;
748 1.1 fvdl
749 1.1 fvdl case CLSET_PROG:
750 1.3 christos *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
751 1.3 christos = htonl(*(u_int32_t *)(void *)info);
752 1.1 fvdl break;
753 1.1 fvdl
754 1.1 fvdl default:
755 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
756 1.1 fvdl return (FALSE);
757 1.1 fvdl }
758 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
759 1.1 fvdl return (TRUE);
760 1.1 fvdl }
761 1.1 fvdl
762 1.1 fvdl static void
763 1.1 fvdl clnt_dg_destroy(cl)
764 1.1 fvdl CLIENT *cl;
765 1.1 fvdl {
766 1.7 lukem struct cu_data *cu;
767 1.7 lukem int cu_fd;
768 1.9 thorpej #ifdef _REENTRANT
769 1.1 fvdl sigset_t mask;
770 1.1 fvdl #endif
771 1.1 fvdl sigset_t newmask;
772 1.1 fvdl
773 1.7 lukem _DIAGASSERT(cl != NULL);
774 1.7 lukem
775 1.7 lukem cu = (struct cu_data *)cl->cl_private;
776 1.7 lukem cu_fd = cu->cu_fd;
777 1.7 lukem
778 1.1 fvdl sigfillset(&newmask);
779 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
780 1.1 fvdl mutex_lock(&clnt_fd_lock);
781 1.1 fvdl while (dg_fd_locks[cu_fd])
782 1.1 fvdl cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
783 1.1 fvdl if (cu->cu_closeit)
784 1.1 fvdl (void) close(cu_fd);
785 1.1 fvdl XDR_DESTROY(&(cu->cu_outxdrs));
786 1.3 christos mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
787 1.1 fvdl if (cl->cl_netid && cl->cl_netid[0])
788 1.1 fvdl mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
789 1.1 fvdl if (cl->cl_tp && cl->cl_tp[0])
790 1.1 fvdl mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
791 1.3 christos mem_free(cl, sizeof (CLIENT));
792 1.1 fvdl mutex_unlock(&clnt_fd_lock);
793 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &mask, NULL);
794 1.1 fvdl cond_signal(&dg_cv[cu_fd]);
795 1.1 fvdl }
796 1.1 fvdl
797 1.1 fvdl static struct clnt_ops *
798 1.1 fvdl clnt_dg_ops()
799 1.1 fvdl {
800 1.1 fvdl static struct clnt_ops ops;
801 1.9 thorpej #ifdef _REENTRANT
802 1.1 fvdl extern mutex_t ops_lock;
803 1.1 fvdl sigset_t mask;
804 1.1 fvdl #endif
805 1.1 fvdl sigset_t newmask;
806 1.1 fvdl
807 1.1 fvdl /* VARIABLES PROTECTED BY ops_lock: ops */
808 1.1 fvdl
809 1.1 fvdl sigfillset(&newmask);
810 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
811 1.1 fvdl mutex_lock(&ops_lock);
812 1.1 fvdl if (ops.cl_call == NULL) {
813 1.1 fvdl ops.cl_call = clnt_dg_call;
814 1.1 fvdl ops.cl_abort = clnt_dg_abort;
815 1.1 fvdl ops.cl_geterr = clnt_dg_geterr;
816 1.1 fvdl ops.cl_freeres = clnt_dg_freeres;
817 1.1 fvdl ops.cl_destroy = clnt_dg_destroy;
818 1.1 fvdl ops.cl_control = clnt_dg_control;
819 1.1 fvdl }
820 1.1 fvdl mutex_unlock(&ops_lock);
821 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &mask, NULL);
822 1.1 fvdl return (&ops);
823 1.1 fvdl }
824 1.1 fvdl
825 1.1 fvdl /*
826 1.1 fvdl * Make sure that the time is not garbage. -1 value is allowed.
827 1.1 fvdl */
828 1.1 fvdl static bool_t
829 1.1 fvdl time_not_ok(t)
830 1.1 fvdl struct timeval *t;
831 1.1 fvdl {
832 1.7 lukem
833 1.7 lukem _DIAGASSERT(t != NULL);
834 1.7 lukem
835 1.1 fvdl return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
836 1.1 fvdl t->tv_usec < -1 || t->tv_usec > 1000000);
837 1.1 fvdl }
838 1.1 fvdl
839 1.1 fvdl
840 1.1 fvdl /*
841 1.1 fvdl * Convert from timevals (used by select) to milliseconds (used by poll).
842 1.1 fvdl */
843 1.1 fvdl static int
844 1.1 fvdl __rpc_timeval_to_msec(t)
845 1.1 fvdl struct timeval *t;
846 1.1 fvdl {
847 1.1 fvdl int t1, tmp;
848 1.7 lukem
849 1.7 lukem _DIAGASSERT(t != NULL);
850 1.1 fvdl
851 1.1 fvdl /*
852 1.1 fvdl * We're really returning t->tv_sec * 1000 + (t->tv_usec / 1000)
853 1.1 fvdl * but try to do so efficiently. Note: 1000 = 1024 - 16 - 8.
854 1.1 fvdl */
855 1.3 christos tmp = (int)t->tv_sec << 3;
856 1.1 fvdl t1 = -tmp;
857 1.1 fvdl t1 += t1 << 1;
858 1.1 fvdl t1 += tmp << 7;
859 1.1 fvdl if (t->tv_usec)
860 1.3 christos t1 += (int)(t->tv_usec / 1000);
861 1.1 fvdl
862 1.1 fvdl return (t1);
863 1.1 fvdl }
864