1*7f2fe78bSCy Schubert /* @(#)rpc_dtablesize.c 2.1 88/07/29 4.0 RPCSRC */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert * Copyright (c) 2010, Oracle America, Inc.
4*7f2fe78bSCy Schubert *
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert *
10*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert *
13*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert * distribution.
17*7f2fe78bSCy Schubert *
18*7f2fe78bSCy Schubert * * Neither the name of the "Oracle America, Inc." nor the names of
19*7f2fe78bSCy Schubert * its contributors may be used to endorse or promote products
20*7f2fe78bSCy Schubert * derived from this software without specific prior written permission.
21*7f2fe78bSCy Schubert *
22*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23*7f2fe78bSCy Schubert * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*7f2fe78bSCy Schubert * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*7f2fe78bSCy Schubert * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*7f2fe78bSCy Schubert * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*7f2fe78bSCy Schubert * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*7f2fe78bSCy Schubert * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*7f2fe78bSCy Schubert * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*7f2fe78bSCy Schubert * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*7f2fe78bSCy Schubert * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*7f2fe78bSCy Schubert * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*7f2fe78bSCy Schubert */
34*7f2fe78bSCy Schubert #if !defined(lint) && defined(SCCSIDS)
35*7f2fe78bSCy Schubert static char sccsid[] = "@(#)rpc_dtablesize.c 1.2 87/08/11 Copyr 1987 Sun Micro";
36*7f2fe78bSCy Schubert #endif
37*7f2fe78bSCy Schubert
38*7f2fe78bSCy Schubert #include <unistd.h>
39*7f2fe78bSCy Schubert #include <gssrpc/rpc.h>
40*7f2fe78bSCy Schubert
41*7f2fe78bSCy Schubert /*
42*7f2fe78bSCy Schubert * Cache the result of getdtablesize(), so we don't have to do an
43*7f2fe78bSCy Schubert * expensive system call every time.
44*7f2fe78bSCy Schubert */
45*7f2fe78bSCy Schubert int
gssrpc__rpc_dtablesize(void)46*7f2fe78bSCy Schubert gssrpc__rpc_dtablesize(void)
47*7f2fe78bSCy Schubert {
48*7f2fe78bSCy Schubert static int size;
49*7f2fe78bSCy Schubert
50*7f2fe78bSCy Schubert if (size == 0) {
51*7f2fe78bSCy Schubert #ifdef _SC_OPEN_MAX
52*7f2fe78bSCy Schubert size = (int) sysconf(_SC_OPEN_MAX);
53*7f2fe78bSCy Schubert #else
54*7f2fe78bSCy Schubert size = getdtablesize();
55*7f2fe78bSCy Schubert #endif
56*7f2fe78bSCy Schubert
57*7f2fe78bSCy Schubert /* sysconf() can return a number larger than what will fit in an
58*7f2fe78bSCy Schubert fd_set. we can't use fd's larger than this, anyway. */
59*7f2fe78bSCy Schubert
60*7f2fe78bSCy Schubert #ifdef FD_SETSIZE
61*7f2fe78bSCy Schubert if (size >= FD_SETSIZE)
62*7f2fe78bSCy Schubert size = FD_SETSIZE-1;
63*7f2fe78bSCy Schubert #endif
64*7f2fe78bSCy Schubert }
65*7f2fe78bSCy Schubert return (size);
66*7f2fe78bSCy Schubert }
67