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