| 1 | /* getdtablesize() function for platforms that don't have it. |
| 2 | Copyright (C) 2008-2014 Free Software Foundation, Inc. |
| 3 | Written by Bruno Haible <bruno@clisp.org>, 2008. |
| 4 | |
| 5 | This program is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <config.h> |
| 19 | |
| 20 | /* Specification. */ |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
| 24 | |
| 25 | # include <stdio.h> |
| 26 | |
| 27 | # include "msvc-inval.h" |
| 28 | |
| 29 | # if HAVE_MSVC_INVALID_PARAMETER_HANDLER |
| 30 | static int |
| 31 | _setmaxstdio_nothrow (int newmax) |
| 32 | { |
| 33 | int result; |
| 34 | |
| 35 | TRY_MSVC_INVAL |
| 36 | { |
| 37 | result = _setmaxstdio (newmax); |
| 38 | } |
| 39 | CATCH_MSVC_INVAL |
| 40 | { |
| 41 | result = -1; |
| 42 | } |
| 43 | DONE_MSVC_INVAL; |
| 44 | |
| 45 | return result; |
| 46 | } |
| 47 | # define _setmaxstdio _setmaxstdio_nothrow |
| 48 | # endif |
| 49 | |
| 50 | /* Cache for the previous getdtablesize () result. Safe to cache because |
| 51 | Windows also lacks setrlimit. */ |
| 52 | static int dtablesize; |
| 53 | |
| 54 | int |
| 55 | getdtablesize (void) |
| 56 | { |
| 57 | if (dtablesize == 0) |
| 58 | { |
| 59 | /* We are looking for the number N such that the valid file descriptors |
| 60 | are 0..N-1. It can be obtained through a loop as follows: |
| 61 | { |
| 62 | int fd; |
| 63 | for (fd = 3; fd < 65536; fd++) |
| 64 | if (dup2 (0, fd) == -1) |
| 65 | break; |
| 66 | return fd; |
| 67 | } |
| 68 | On Windows XP, the result is 2048. |
| 69 | The drawback of this loop is that it allocates memory for a libc |
| 70 | internal array that is never freed. |
| 71 | |
| 72 | The number N can also be obtained as the upper bound for |
| 73 | _getmaxstdio (). _getmaxstdio () returns the maximum number of open |
| 74 | FILE objects. The sanity check in _setmaxstdio reveals the maximum |
| 75 | number of file descriptors. This too allocates memory, but it is |
| 76 | freed when we call _setmaxstdio with the original value. */ |
| 77 | int orig_max_stdio = _getmaxstdio (); |
| 78 | unsigned int bound; |
| 79 | for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2) |
| 80 | ; |
| 81 | _setmaxstdio (orig_max_stdio); |
| 82 | dtablesize = bound; |
| 83 | } |
| 84 | return dtablesize; |
| 85 | } |
| 86 | |
| 87 | #elif HAVE_GETDTABLESIZE |
| 88 | |
| 89 | # include <sys/resource.h> |
| 90 | # undef getdtablesize |
| 91 | |
| 92 | int |
| 93 | rpl_getdtablesize(void) |
| 94 | { |
| 95 | /* To date, this replacement is only compiled for Cygwin 1.7.25, |
| 96 | which auto-increased the RLIMIT_NOFILE soft limit until it |
| 97 | hits the compile-time constant hard limit of 3200. Although |
| 98 | that version of cygwin supported a child process inheriting |
| 99 | a smaller soft limit, the smaller limit is not enforced, so |
| 100 | we might as well just report the hard limit. */ |
| 101 | struct rlimit lim; |
| 102 | if (!getrlimit (RLIMIT_NOFILE, &lim) && lim.rlim_max != RLIM_INFINITY) |
| 103 | return lim.rlim_max; |
| 104 | return getdtablesize (); |
| 105 | } |
| 106 | |
| 107 | #endif |