Path: blob/main/crypto/heimdal/lib/roken/getdtablesize.c
39507 views
/*1* Copyright (c) 1995-2001 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include "roken.h"3637#ifdef HAVE_SYS_TYPES_H38#include <sys/types.h>39#endif40#ifdef TIME_WITH_SYS_TIME41#include <sys/time.h>42#include <time.h>43#elif defined(HAVE_SYS_TIME_H)44#include <sys/time.h>45#else46#include <time.h>47#endif48#ifdef HAVE_SYS_PARAM_H49#include <sys/param.h>50#endif51#ifdef HAVE_UNISTD_H52#include <unistd.h>53#endif5455#ifdef HAVE_SYS_RESOURCE_H56#include <sys/resource.h>57#endif5859#ifdef HAVE_SYS_SYSCTL_H60#include <sys/sysctl.h>61#endif6263ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL64getdtablesize(void)65{66int files = -1;67#if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)68files = sysconf(_SC_OPEN_MAX);69#else /* !defined(HAVE_SYSCONF) */70#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)71struct rlimit res;72if (getrlimit(RLIMIT_NOFILE, &res) == 0)73files = res.rlim_cur;74#else /* !definded(HAVE_GETRLIMIT) */75#if defined(HAVE_SYSCTL) && defined(CTL_KERN) && defined(KERN_MAXFILES)76int mib[2];77size_t len;7879mib[0] = CTL_KERN;80mib[1] = KERN_MAXFILES;81len = sizeof(files);82sysctl(&mib, 2, &files, sizeof(files), NULL, 0);83#endif /* defined(HAVE_SYSCTL) */84#endif /* !definded(HAVE_GETRLIMIT) */85#endif /* !defined(HAVE_SYSCONF) */8687#ifdef OPEN_MAX88if (files < 0)89files = OPEN_MAX;90#endif9192#ifdef NOFILE93if (files < 0)94files = NOFILE;95#endif9697return files;98}99100101