xref: /illumos-gate/usr/src/lib/libnsl/rpc/rpc_sel2poll.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
24*e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
27*e8031f0aSraf 
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate 
31*e8031f0aSraf #include "mt.h"
327c478bd9Sstevel@tonic-gate #include <sys/select.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/time.h>
357c478bd9Sstevel@tonic-gate #include <sys/poll.h>
367c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  *	Given an fd_set pointer and the number of bits to check in it,
417c478bd9Sstevel@tonic-gate  *	initialize the supplied pollfd array for RPC's use (RPC only
427c478bd9Sstevel@tonic-gate  *	polls for input events).  We return the number of pollfd slots
437c478bd9Sstevel@tonic-gate  *	we initialized.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate int
__rpc_select_to_poll(int fdmax,fd_set * fdset,struct pollfd * p0)467c478bd9Sstevel@tonic-gate __rpc_select_to_poll(
477c478bd9Sstevel@tonic-gate 	int	fdmax,		/* number of bits we must test */
487c478bd9Sstevel@tonic-gate 	fd_set	*fdset,		/* source fd_set array */
497c478bd9Sstevel@tonic-gate 	struct pollfd	*p0)	/* target pollfd array */
507c478bd9Sstevel@tonic-gate {
5161961e0fSrobinson 	int j;		/* loop counter */
5261961e0fSrobinson 	int n;
5361961e0fSrobinson 	struct pollfd	*p = p0;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	/*
567c478bd9Sstevel@tonic-gate 	 * For each fd, if the appropriate bit is set convert it into
577c478bd9Sstevel@tonic-gate 	 * the appropriate pollfd struct.
587c478bd9Sstevel@tonic-gate 	 */
597c478bd9Sstevel@tonic-gate 	j = ((fdmax >= FD_SETSIZE) ? FD_SETSIZE : fdmax);
607c478bd9Sstevel@tonic-gate 	for (n = 0; n < j; n++) {
617c478bd9Sstevel@tonic-gate 		if (FD_ISSET(n, fdset)) {
627c478bd9Sstevel@tonic-gate 			p->fd = n;
637c478bd9Sstevel@tonic-gate 			p->events = MASKVAL;
647c478bd9Sstevel@tonic-gate 			p->revents = 0;
657c478bd9Sstevel@tonic-gate 			p++;
667c478bd9Sstevel@tonic-gate 		}
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 	return (p - p0);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  *	Arguments are similar to rpc_select_to_poll() except that
737c478bd9Sstevel@tonic-gate  *	the second argument is pointer to an array of pollfd_t
747c478bd9Sstevel@tonic-gate  *	which is the source array which will be compressed and
757c478bd9Sstevel@tonic-gate  *	copied to the target array in p0.  The size of the
767c478bd9Sstevel@tonic-gate  *	source argument is given by pollfdmax. The array can be
777c478bd9Sstevel@tonic-gate  *	sparse. The space for the target is allocated before
787c478bd9Sstevel@tonic-gate  *	calling this function. It should have atleast pollfdmax
797c478bd9Sstevel@tonic-gate  *	elements.  This function scans the source pollfd array
807c478bd9Sstevel@tonic-gate  *	and copies only the valid ones to the target p0.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate int
__rpc_compress_pollfd(int pollfdmax,pollfd_t * srcp,pollfd_t * p0)837c478bd9Sstevel@tonic-gate __rpc_compress_pollfd(int pollfdmax, pollfd_t *srcp, pollfd_t *p0)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	int n;
867c478bd9Sstevel@tonic-gate 	pollfd_t *p = p0;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	for (n = 0; n < pollfdmax; n++) {
897c478bd9Sstevel@tonic-gate 		if (POLLFD_ISSET(n, srcp)) {
907c478bd9Sstevel@tonic-gate 			p->fd = srcp[n].fd;
917c478bd9Sstevel@tonic-gate 			p->events = srcp[n].events;
927c478bd9Sstevel@tonic-gate 			p->revents = 0;
937c478bd9Sstevel@tonic-gate 			p++;
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	return (p - p0);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  *	Convert from timevals (used by select) to milliseconds (used by poll).
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate int
__rpc_timeval_to_msec(struct timeval * t)1037c478bd9Sstevel@tonic-gate __rpc_timeval_to_msec(struct timeval *t)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	int	t1, tmp;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * We're really returning t->tv_sec * 1000 + (t->tv_usec / 1000)
1097c478bd9Sstevel@tonic-gate 	 * but try to do so efficiently.  Note:  1000 = 1024 - 16 - 8.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	tmp = (int)t->tv_sec << 3;
1127c478bd9Sstevel@tonic-gate 	t1 = -tmp;
1137c478bd9Sstevel@tonic-gate 	t1 += t1 << 1;
1147c478bd9Sstevel@tonic-gate 	t1 += tmp << 7;
1157c478bd9Sstevel@tonic-gate 	if (t->tv_usec)
1167c478bd9Sstevel@tonic-gate 		t1 += t->tv_usec / 1000;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	return (t1);
1197c478bd9Sstevel@tonic-gate }
120