getdtablesize.c revision 178826
195584Sanholt/*
2145132Sanholt * Copyright (c) 1995-2001 Kungliga Tekniska H�gskolan
3145132Sanholt * (Royal Institute of Technology, Stockholm, Sweden).
4139749Simp * All rights reserved.
595584Sanholt *
695584Sanholt * Redistribution and use in source and binary forms, with or without
795584Sanholt * modification, are permitted provided that the following conditions
895584Sanholt * are met:
995584Sanholt *
1095584Sanholt * 1. Redistributions of source code must retain the above copyright
1195584Sanholt *    notice, this list of conditions and the following disclaimer.
1295584Sanholt *
1395584Sanholt * 2. Redistributions in binary form must reproduce the above copyright
1495584Sanholt *    notice, this list of conditions and the following disclaimer in the
1595584Sanholt *    documentation and/or other materials provided with the distribution.
1695584Sanholt *
1795584Sanholt * 3. Neither the name of the Institute nor the names of its contributors
1895584Sanholt *    may be used to endorse or promote products derived from this software
1995584Sanholt *    without specific prior written permission.
2095584Sanholt *
2195584Sanholt * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2295584Sanholt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395584Sanholt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495584Sanholt * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2595584Sanholt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2695584Sanholt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2795584Sanholt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2895584Sanholt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2995584Sanholt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3095584Sanholt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3195584Sanholt * SUCH DAMAGE.
3295584Sanholt */
3395584Sanholt
34152909Sanholt#ifdef HAVE_CONFIG_H
35152909Sanholt#include <config.h>
36152909SanholtRCSID("$Id: getdtablesize.c 14773 2005-04-12 11:29:18Z lha $");
3795584Sanholt#endif
38112015Sanholt
3995746Sanholt#include "roken.h"
4095584Sanholt
41145132Sanholt#ifdef HAVE_SYS_TYPES_H
4295584Sanholt#include <sys/types.h>
43145132Sanholt#endif
44145132Sanholt#ifdef TIME_WITH_SYS_TIME
45145132Sanholt#include <sys/time.h>
46145132Sanholt#include <time.h>
4795584Sanholt#elif defined(HAVE_SYS_TIME_H)
48182080Srnoland#include <sys/time.h>
49145132Sanholt#else
50183573Srnoland#include <time.h>
51183573Srnoland#endif
52183573Srnoland#ifdef HAVE_SYS_PARAM_H
53145132Sanholt#include <sys/param.h>
54183573Srnoland#endif
55189130Srnoland#ifdef HAVE_UNISTD_H
56183573Srnoland#include <unistd.h>
57183573Srnoland#endif
58183573Srnoland
59183573Srnoland#ifdef HAVE_SYS_RESOURCE_H
60183573Srnoland#include <sys/resource.h>
61183573Srnoland#endif
62183573Srnoland
63183573Srnoland#ifdef HAVE_SYS_SYSCTL_H
64183573Srnoland#include <sys/sysctl.h>
65183573Srnoland#endif
66145132Sanholt
67183573Srnolandint ROKEN_LIB_FUNCTION
68183573Srnolandgetdtablesize(void)
69145132Sanholt{
70183573Srnoland  int files = -1;
71183573Srnoland#if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
72183573Srnoland  files = sysconf(_SC_OPEN_MAX);
73183573Srnoland#else /* !defined(HAVE_SYSCONF) */
74183573Srnoland#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
75183573Srnoland  struct rlimit res;
76145132Sanholt  if (getrlimit(RLIMIT_NOFILE, &res) == 0)
77145132Sanholt    files = res.rlim_cur;
78145132Sanholt#else /* !definded(HAVE_GETRLIMIT) */
79189563Srnoland#if defined(HAVE_SYSCTL) && defined(CTL_KERN) && defined(KERN_MAXFILES)
80145132Sanholt  int mib[2];
81189563Srnoland  size_t len;
82145132Sanholt
83145132Sanholt  mib[0] = CTL_KERN;
84145132Sanholt  mib[1] = KERN_MAXFILES;
85189563Srnoland  len = sizeof(files);
86145132Sanholt  sysctl(&mib, 2, &files, sizeof(files), NULL, 0);
87189563Srnoland#endif /* defined(HAVE_SYSCTL) */
88145132Sanholt#endif /* !definded(HAVE_GETRLIMIT) */
89183833Srnoland#endif /* !defined(HAVE_SYSCONF) */
90183604Srnoland
91183604Srnoland#ifdef OPEN_MAX
92145132Sanholt  if (files < 0)
93183573Srnoland    files = OPEN_MAX;
94189563Srnoland#endif
95145132Sanholt
96145132Sanholt#ifdef NOFILE
97189130Srnoland  if (files < 0)
98189130Srnoland    files = NOFILE;
99189130Srnoland#endif
100189130Srnoland
101189130Srnoland  return files;
102183573Srnoland}
103189563Srnoland