xref: /titanic_41/usr/src/lib/libnsl/rpc/rpc_sel2poll.c (revision e8031f0a8ed0e45c6d8847c5e09424e66fd34a4b)
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 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
33*e8031f0aSraf #include "mt.h"
347c478bd9Sstevel@tonic-gate #include <sys/select.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <sys/poll.h>
387c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  *	Given an fd_set pointer and the number of bits to check in it,
437c478bd9Sstevel@tonic-gate  *	initialize the supplied pollfd array for RPC's use (RPC only
447c478bd9Sstevel@tonic-gate  *	polls for input events).  We return the number of pollfd slots
457c478bd9Sstevel@tonic-gate  *	we initialized.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate int
__rpc_select_to_poll(int fdmax,fd_set * fdset,struct pollfd * p0)487c478bd9Sstevel@tonic-gate __rpc_select_to_poll(
497c478bd9Sstevel@tonic-gate 	int	fdmax,		/* number of bits we must test */
507c478bd9Sstevel@tonic-gate 	fd_set	*fdset,		/* source fd_set array */
517c478bd9Sstevel@tonic-gate 	struct pollfd	*p0)	/* target pollfd array */
527c478bd9Sstevel@tonic-gate {
5361961e0fSrobinson 	int j;		/* loop counter */
5461961e0fSrobinson 	int n;
5561961e0fSrobinson 	struct pollfd	*p = p0;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/*
587c478bd9Sstevel@tonic-gate 	 * For each fd, if the appropriate bit is set convert it into
597c478bd9Sstevel@tonic-gate 	 * the appropriate pollfd struct.
607c478bd9Sstevel@tonic-gate 	 */
617c478bd9Sstevel@tonic-gate 	j = ((fdmax >= FD_SETSIZE) ? FD_SETSIZE : fdmax);
627c478bd9Sstevel@tonic-gate 	for (n = 0; n < j; n++) {
637c478bd9Sstevel@tonic-gate 		if (FD_ISSET(n, fdset)) {
647c478bd9Sstevel@tonic-gate 			p->fd = n;
657c478bd9Sstevel@tonic-gate 			p->events = MASKVAL;
667c478bd9Sstevel@tonic-gate 			p->revents = 0;
677c478bd9Sstevel@tonic-gate 			p++;
687c478bd9Sstevel@tonic-gate 		}
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 	return (p - p0);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  *	Arguments are similar to rpc_select_to_poll() except that
757c478bd9Sstevel@tonic-gate  *	the second argument is pointer to an array of pollfd_t
767c478bd9Sstevel@tonic-gate  *	which is the source array which will be compressed and
777c478bd9Sstevel@tonic-gate  *	copied to the target array in p0.  The size of the
787c478bd9Sstevel@tonic-gate  *	source argument is given by pollfdmax. The array can be
797c478bd9Sstevel@tonic-gate  *	sparse. The space for the target is allocated before
807c478bd9Sstevel@tonic-gate  *	calling this function. It should have atleast pollfdmax
817c478bd9Sstevel@tonic-gate  *	elements.  This function scans the source pollfd array
827c478bd9Sstevel@tonic-gate  *	and copies only the valid ones to the target p0.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate int
__rpc_compress_pollfd(int pollfdmax,pollfd_t * srcp,pollfd_t * p0)857c478bd9Sstevel@tonic-gate __rpc_compress_pollfd(int pollfdmax, pollfd_t *srcp, pollfd_t *p0)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	int n;
887c478bd9Sstevel@tonic-gate 	pollfd_t *p = p0;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	for (n = 0; n < pollfdmax; n++) {
917c478bd9Sstevel@tonic-gate 		if (POLLFD_ISSET(n, srcp)) {
927c478bd9Sstevel@tonic-gate 			p->fd = srcp[n].fd;
937c478bd9Sstevel@tonic-gate 			p->events = srcp[n].events;
947c478bd9Sstevel@tonic-gate 			p->revents = 0;
957c478bd9Sstevel@tonic-gate 			p++;
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	return (p - p0);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  *	Convert from timevals (used by select) to milliseconds (used by poll).
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate int
__rpc_timeval_to_msec(struct timeval * t)1057c478bd9Sstevel@tonic-gate __rpc_timeval_to_msec(struct timeval *t)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	int	t1, tmp;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * We're really returning t->tv_sec * 1000 + (t->tv_usec / 1000)
1117c478bd9Sstevel@tonic-gate 	 * but try to do so efficiently.  Note:  1000 = 1024 - 16 - 8.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	tmp = (int)t->tv_sec << 3;
1147c478bd9Sstevel@tonic-gate 	t1 = -tmp;
1157c478bd9Sstevel@tonic-gate 	t1 += t1 << 1;
1167c478bd9Sstevel@tonic-gate 	t1 += tmp << 7;
1177c478bd9Sstevel@tonic-gate 	if (t->tv_usec)
1187c478bd9Sstevel@tonic-gate 		t1 += t->tv_usec / 1000;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (t1);
1217c478bd9Sstevel@tonic-gate }
122