xref: /illumos-gate/usr/src/lib/libnsl/rpc/rpc_sel2poll.c (revision d5ace9454616652a717c9831d949dffa319381f9)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /* Copyright (c) 1988 AT&T */
29 /* All Rights Reserved */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "mt.h"
34 #include <sys/select.h>
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/poll.h>
38 #include "rpc_mt.h"
39 
40 
41 /*
42  *	Given an fd_set pointer and the number of bits to check in it,
43  *	initialize the supplied pollfd array for RPC's use (RPC only
44  *	polls for input events).  We return the number of pollfd slots
45  *	we initialized.
46  */
47 int
48 __rpc_select_to_poll(
49 	int	fdmax,		/* number of bits we must test */
50 	fd_set	*fdset,		/* source fd_set array */
51 	struct pollfd	*p0)	/* target pollfd array */
52 {
53 	int j;		/* loop counter */
54 	int n;
55 	struct pollfd	*p = p0;
56 
57 	/*
58 	 * For each fd, if the appropriate bit is set convert it into
59 	 * the appropriate pollfd struct.
60 	 */
61 	j = ((fdmax >= FD_SETSIZE) ? FD_SETSIZE : fdmax);
62 	for (n = 0; n < j; n++) {
63 		if (FD_ISSET(n, fdset)) {
64 			p->fd = n;
65 			p->events = MASKVAL;
66 			p->revents = 0;
67 			p++;
68 		}
69 	}
70 	return (p - p0);
71 }
72 
73 /*
74  *	Arguments are similar to rpc_select_to_poll() except that
75  *	the second argument is pointer to an array of pollfd_t
76  *	which is the source array which will be compressed and
77  *	copied to the target array in p0.  The size of the
78  *	source argument is given by pollfdmax. The array can be
79  *	sparse. The space for the target is allocated before
80  *	calling this function. It should have atleast pollfdmax
81  *	elements.  This function scans the source pollfd array
82  *	and copies only the valid ones to the target p0.
83  */
84 int
85 __rpc_compress_pollfd(int pollfdmax, pollfd_t *srcp, pollfd_t *p0)
86 {
87 	int n;
88 	pollfd_t *p = p0;
89 
90 	for (n = 0; n < pollfdmax; n++) {
91 		if (POLLFD_ISSET(n, srcp)) {
92 			p->fd = srcp[n].fd;
93 			p->events = srcp[n].events;
94 			p->revents = 0;
95 			p++;
96 		}
97 	}
98 	return (p - p0);
99 }
100 
101 /*
102  *	Convert from timevals (used by select) to milliseconds (used by poll).
103  */
104 int
105 __rpc_timeval_to_msec(struct timeval *t)
106 {
107 	int	t1, tmp;
108 
109 	/*
110 	 * We're really returning t->tv_sec * 1000 + (t->tv_usec / 1000)
111 	 * but try to do so efficiently.  Note:  1000 = 1024 - 16 - 8.
112 	 */
113 	tmp = (int)t->tv_sec << 3;
114 	t1 = -tmp;
115 	t1 += t1 << 1;
116 	t1 += tmp << 7;
117 	if (t->tv_usec)
118 		t1 += t->tv_usec / 1000;
119 
120 	return (t1);
121 }
122