annotate lib/getdtablesize.c @ 17907:0a1c2535cad9

euidaccess: Fix Android build * modules/euidaccess (Depends-on): Add fcntl-h to ensure that AT_EACCESS gets declared.
author Kevin Cernekee <cernekee@google.com>
date Wed, 11 Feb 2015 15:22:54 -0800
parents ab58d4870664
children c513a8c0f555
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* getdtablesize() function for platforms that don't have it.
17848
ab58d4870664 version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 17740
diff changeset
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <unistd.h>
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
17508
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
25 # include <stdio.h>
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
17508
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
27 # include "msvc-inval.h"
15759
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
28
17508
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
29 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
30 static int
15759
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
31 _setmaxstdio_nothrow (int newmax)
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
32 {
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
33 int result;
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
34
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
35 TRY_MSVC_INVAL
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
36 {
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
37 result = _setmaxstdio (newmax);
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
38 }
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
39 CATCH_MSVC_INVAL
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
40 {
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
41 result = -1;
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
42 }
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
43 DONE_MSVC_INVAL;
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
44
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
45 return result;
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
46 }
17508
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
47 # define _setmaxstdio _setmaxstdio_nothrow
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
48 # endif
15759
4583838a5587 getdtablesize: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
49
17508
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
50 /* Cache for the previous getdtablesize () result. Safe to cache because
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
51 Windows also lacks setrlimit. */
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
52 static int dtablesize;
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
53
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 int
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 getdtablesize (void)
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 {
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
57 if (dtablesize == 0)
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
58 {
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
59 /* We are looking for the number N such that the valid file descriptors
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
60 are 0..N-1. It can be obtained through a loop as follows:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
61 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
62 int fd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
63 for (fd = 3; fd < 65536; fd++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
64 if (dup2 (0, fd) == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
65 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
66 return fd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
67 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
68 On Windows XP, the result is 2048.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
69 The drawback of this loop is that it allocates memory for a libc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
70 internal array that is never freed.
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
71
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
72 The number N can also be obtained as the upper bound for
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
73 _getmaxstdio (). _getmaxstdio () returns the maximum number of open
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
74 FILE objects. The sanity check in _setmaxstdio reveals the maximum
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
75 number of file descriptors. This too allocates memory, but it is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
76 freed when we call _setmaxstdio with the original value. */
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
77 int orig_max_stdio = _getmaxstdio ();
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
78 unsigned int bound;
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
79 for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
80 ;
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
81 _setmaxstdio (orig_max_stdio);
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
82 dtablesize = bound;
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
83 }
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
84 return dtablesize;
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 }
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
17508
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
87 #elif HAVE_GETDTABLESIZE
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
88
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
89 # include <sys/resource.h>
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
90 # undef getdtablesize
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
91
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
92 int
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
93 rpl_getdtablesize(void)
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
94 {
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
95 /* To date, this replacement is only compiled for Cygwin 1.7.25,
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
96 which auto-increased the RLIMIT_NOFILE soft limit until it
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
97 hits the compile-time constant hard limit of 3200. Although
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
98 that version of cygwin supported a child process inheriting
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
99 a smaller soft limit, the smaller limit is not enforced, so
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
100 we might as well just report the hard limit. */
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
101 struct rlimit lim;
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
102 if (!getrlimit (RLIMIT_NOFILE, &lim) && lim.rlim_max != RLIM_INFINITY)
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
103 return lim.rlim_max;
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
104 return getdtablesize ();
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
105 }
52cc408447aa getdtablesize: work around cygwin issue
Eric Blake <eblake@redhat.com>
parents: 17249
diff changeset
106
17740
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
107 #elif defined _SC_OPEN_MAX
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
108
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
109 int
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
110 getdtablesize (void)
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
111 {
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
112 return sysconf (_SC_OPEN_MAX);
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
113 }
be8b85935367 getdtablesize: fall back on sysconf (_SC_OPEN_MAX)
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
114
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 #endif