libc/unistd: Initial, minimal implementation of sysconf(). Only sufficient to support the functionality of less standard getdtabilesize().

This commit is contained in:
Michael Jung 2018-05-10 14:08:34 -06:00 committed by Gregory Nutt
parent 50106f0d2a
commit 29af41a85f
5 changed files with 381 additions and 5 deletions

View File

@ -166,7 +166,7 @@
* See imx_iomuxc.h for detailed content.
*/
#define GPIO_IOMUX_SHIFT (0) /* Bits 9-15: IOMUX pin configuration */
#define GPIO_IOMUX_SHIFT (0) /* Bits 0-15: IOMUX pin configuration */
#define GPIO_IOMUX_MASK (0xffff << GPIO_IOMUX_SHIFT)
/************************************************************************************

View File

@ -178,7 +178,7 @@
* See imxrt_iomuxc.h for detailed content.
*/
#define GPIO_IOMUX_SHIFT (0) /* Bits 9-15: IOMUX pin configuration */
#define GPIO_IOMUX_SHIFT (0) /* Bits 0-15: IOMUX pin configuration */
#define GPIO_IOMUX_MASK (0xffff << GPIO_IOMUX_SHIFT)
/* Helper addressing macros */

View File

@ -108,9 +108,146 @@
#undef _POSIX_ASYNC_IO
#undef _POSIX_PRIO_IO
#define fdatasync(f) fsync(f)
/* Constants used with POSIX sysconf().
* Most of these will return -1 and ENOSYS
*/
#define HOST_NAME_MAX 32
#define _SC_2_C_BIND 0x0001
#define _SC_2_C_DEV 0x0002
#define _SC_2_CHAR_TERM 0x0003
#define _SC_2_FORT_DEV 0x0004
#define _SC_2_FORT_RUN 0x0005
#define _SC_2_LOCALEDEF 0x0006
#define _SC_2_PBS 0x0007
#define _SC_2_PBS_ACCOUNTING 0x0008
#define _SC_2_PBS_CHECKPOINT 0x0009
#define _SC_2_PBS_LOCATE 0x000a
#define _SC_2_PBS_MESSAGE 0x000b
#define _SC_2_PBS_TRACK 0x000c
#define _SC_2_SW_DEV 0x000d
#define _SC_2_UPE 0x000e
#define _SC_2_VERSION 0x000f
#define _SC_ADVISORY_INFO 0x0010
#define _SC_AIO_LISTIO_MAX 0x0011
#define _SC_AIO_MAX 0x0012
#define _SC_AIO_PRIO_DELTA_MAX 0x0013
#define _SC_ARG_MAX 0x0014
#define _SC_ASYNCHRONOUS_IO 0x0015
#define _SC_ATEXIT_MAX 0x0016
#define _SC_BARRIERS 0x0017
#define _SC_BC_BASE_MAX 0x0018
#define _SC_BC_DIM_MAX 0x0019
#define _SC_BC_SCALE_MAX 0x001a
#define _SC_BC_STRING_MAX 0x001b
#define _SC_CHILD_MAX 0x001c
#define _SC_CLK_TCK 0x001d
#define _SC_CLOCK_SELECTION 0x001e
#define _SC_COLL_WEIGHTS_MAX 0x001f
#define _SC_CPUTIME 0x0020
#define _SC_DELAYTIMER_MAX 0x0021
#define _SC_EXPR_NEST_MAX 0x0022
#define _SC_FSYNC 0x0023
#define _SC_GETGR_R_SIZE_MAX 0x0024
#define _SC_GETPW_R_SIZE_MAX 0x0025
#define _SC_HOST_NAME_MAX 0x0026
#define _SC_IOV_MAX 0x0027
#define _SC_IPV6 0x0028
#define _SC_JOB_CONTROL 0x0029
#define _SC_LINE_MAX 0x002a
#define _SC_LOGIN_NAME_MAX 0x002b
#define _SC_MAPPED_FILES 0x002c
#define _SC_MEMLOCK 0x002d
#define _SC_MEMLOCK_RANGE 0x002e
#define _SC_MEMORY_PROTECTION 0x002f
#define _SC_MESSAGE_PASSING 0x0030
#define _SC_MONOTONIC_CLOCK 0x0031
#define _SC_MQ_OPEN_MAX 0x0032
#define _SC_MQ_PRIO_MAX 0x0033
#define _SC_NGROUPS_MAX 0x0034
#define _SC_OPEN_MAX 0x0035
#define _SC_PAGE_SIZE 0x0036
#define _SC_PAGESIZE _SC_PAGE_SIZE
#define _SC_PRIORITIZED_IO 0x0037
#define _SC_PRIORITY_SCHEDULING 0x0038
#define _SC_RAW_SOCKETS 0x0039
#define _SC_RE_DUP_MAX 0x003a
#define _SC_READER_WRITER_LOCKS 0x003b
#define _SC_REALTIME_SIGNALS 0x003c
#define _SC_REGEXP 0x003d
#define _SC_RTSIG_MAX 0x003e
#define _SC_SAVED_IDS 0x003f
#define _SC_SEM_NSEMS_MAX 0x0040
#define _SC_SEM_VALUE_MAX 0x0041
#define _SC_SEMAPHORES 0x0042
#define _SC_SHARED_MEMORY_OBJECTS 0x0043
#define _SC_SHELL 0x0044
#define _SC_SIGQUEUE_MAX 0x0045
#define _SC_SPAWN 0x0046
#define _SC_SPIN_LOCKS 0x0047
#define _SC_SPORADIC_SERVER 0x0048
#define _SC_SS_REPL_MAX 0x0049
#define _SC_STREAM_MAX 0x004a
#define _SC_SYMLOOP_MAX 0x004b
#define _SC_SYNCHRONIZED_IO 0x004c
#define _SC_THREAD_ATTR_STACKADDR 0x004d
#define _SC_THREAD_ATTR_STACKSIZE 0x004e
#define _SC_THREAD_CPUTIME 0x004f
#define _SC_THREAD_DESTRUCTOR_ITERATIONS 0x0050
#define _SC_THREAD_KEYS_MAX 0x0051
#define _SC_THREAD_PRIO_INHERIT 0x0052
#define _SC_THREAD_PRIO_PROTECT 0x0053
#define _SC_THREAD_PRIORITY_SCHEDULING 0x0054
#define _SC_THREAD_PROCESS_SHARED 0x0055
#define _SC_THREAD_SAFE_FUNCTIONS 0x0056
#define _SC_THREAD_SPORADIC_SERVER 0x0057
#define _SC_THREAD_STACK_MIN 0x0058
#define _SC_THREAD_THREADS_MAX 0x0059
#define _SC_THREADS 0x005a
#define _SC_TIMEOUTS 0x005b
#define _SC_TIMER_MAX 0x005c
#define _SC_TIMERS 0x005d
#define _SC_TRACE 0x005e
#define _SC_TRACE_EVENT_FILTER 0x005f
#define _SC_TRACE_EVENT_NAME_MAX 0x0060
#define _SC_TRACE_INHERIT 0x0061
#define _SC_TRACE_LOG 0x0062
#define _SC_TRACE_NAME_MAX 0x0063
#define _SC_TRACE_SYS_MAX 0x0064
#define _SC_TRACE_USER_EVENT_MAX 0x0065
#define _SC_TTY_NAME_MAX 0x0066
#define _SC_TYPED_MEMORY_OBJECTS 0x0067
#define _SC_TZNAME_MAX 0x0068
#define _SC_V6_ILP32_OFF32 0x0069
#define _SC_V6_ILP32_OFFBIG 0x006a
#define _SC_V6_LP64_OFF64 0x006b
#define _SC_V6_LPBIG_OFFBIG 0x006c
#define _SC_VERSION 0x006d
#define _SC_XBS5_ILP32_OFF32 0x006e /* (LEGACY) */
#define _SC_XBS5_ILP32_OFFBIG 0x006f /* (LEGACY) */
#define _SC_XBS5_LP64_OFF64 0x0070 /* (LEGACY) */
#define _SC_XBS5_LPBIG_OFFBIG 0x0071 /* (LEGACY) */
#define _SC_XOPEN_CRYPT 0x0072
#define _SC_XOPEN_ENH_I18N 0x0073
#define _SC_XOPEN_LEGACY 0x0074
#define _SC_XOPEN_REALTIME 0x0075
#define _SC_XOPEN_REALTIME_THREADS 0x0076
#define _SC_XOPEN_SHM 0x0077
#define _SC_XOPEN_STREAMS 0x0078
#define _SC_XOPEN_UNIX 0x0079
#define _SC_XOPEN_VERSION 0x007a
/* The following symbolic constants must be defined for file streams: */
#define STDERR_FILENO 2 /* File number of stderr */
#define STDIN_FILENO 0 /* File number of stdin */
#define STDOUT_FILENO 1 /* File number of stdout */
#define HOST_NAME_MAX 32
/* Helpers and compatibility definitions */
#define fdatasync(f) fsync(f)
#define getdtablesize(f) ((int)sysconf(_SC_OPEN_MAX))
/****************************************************************************
* Public Data
@ -227,6 +364,10 @@ int gethostname(FAR char *name, size_t size);
int sethostname(FAR const char *name, size_t size);
#endif
/* Get configurable system varaibles */
long sysconf(int name);
#undef EXTERN
#if defined(__cplusplus)
}

View File

@ -35,7 +35,7 @@
# Add the unistd C files to the build
CSRCS += lib_access.c lib_swab.c
CSRCS += lib_access.c lib_swab.c lib_sysconf.c
CSRCS += lib_getopt.c lib_getoptargp.c lib_getoptindp.c lib_getoptoptp.c
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)

235
libc/unistd/lib_sysconf.c Normal file
View File

@ -0,0 +1,235 @@
/****************************************************************************
* lib/unistd/lib_sysconf.c
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Michael Jung <mijung@gmx.net>
* Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sysconf
*
* Description:
* The sysconf() function provides a method for the application to
* determine the current value of a configurable system limit or option
* (variable). The implementation will support all of the variables
* listed in the following table and may support others.
*
* The 'name' argument represents the system variable to be queried. The
* following table lists the minimal set of system variables from
* <limits.h> or <unistd.h> that can be returned by sysconf(), and the
* symbolic constants defined in <unistd.h> that are the corresponding
* values used for name.
*
* Variable Value of Name
*
* {AIO_LISTIO_MAX} _SC_AIO_LISTIO_MAX
* {AIO_MAX} _SC_AIO_MAX
* {AIO_PRIO_DELTA_MAX} _SC_AIO_PRIO_DELTA_MAX
* {ARG_MAX} _SC_ARG_MAX
* {ATEXIT_MAX} _SC_ATEXIT_MAX
* {BC_BASE_MAX} _SC_BC_BASE_MAX
* {BC_DIM_MAX} _SC_BC_DIM_MAX
* {BC_SCALE_MAX} _SC_BC_SCALE_MAX
* {BC_STRING_MAX} _SC_BC_STRING_MAX
* {CHILD_MAX} _SC_CHILD_MAX
* Clock ticks/second _SC_CLK_TCK
* {COLL_WEIGHTS_MAX} _SC_COLL_WEIGHTS_MAX
* {DELAYTIMER_MAX} _SC_DELAYTIMER_MAX
* {EXPR_NEST_MAX} _SC_EXPR_NEST_MAX
* {HOST_NAME_MAX} _SC_HOST_NAME_MAX
* {IOV_MAX} _SC_IOV_MAX
* {LINE_MAX} _SC_LINE_MAX
* {LOGIN_NAME_MAX} _SC_LOGIN_NAME_MAX
* {NGROUPS_MAX} _SC_NGROUPS_MAX
* Maximum size of getgrgid_r() and _SC_GETGR_R_SIZE_MAX
* getgrnam_r() data buffers
* Maximum size of getpwuid_r() and _SC_GETPW_R_SIZE_MAX
* getpwnam_r() data buffers
* {MQ_OPEN_MAX} _SC_MQ_OPEN_MAX
* {MQ_PRIO_MAX} _SC_MQ_PRIO_MAX
* {OPEN_MAX} _SC_OPEN_MAX
* _POSIX_ADVISORY_INFO _SC_ADVISORY_INFO
* _POSIX_BARRIERS _SC_BARRIERS
* _POSIX_ASYNCHRONOUS_IO _SC_ASYNCHRONOUS_IO
* _POSIX_CLOCK_SELECTION _SC_CLOCK_SELECTION
* _POSIX_CPUTIME _SC_CPUTIME
* _POSIX_FSYNC _SC_FSYNC
* _POSIX_IPV6 _SC_IPV6
* _POSIX_JOB_CONTROL _SC_JOB_CONTROL
* _POSIX_MAPPED_FILES _SC_MAPPED_FILES
* _POSIX_MEMLOCK _SC_MEMLOCK
* _POSIX_MEMLOCK_RANGE _SC_MEMLOCK_RANGE
* _POSIX_MEMORY_PROTECTION _SC_MEMORY_PROTECTION
* _POSIX_MESSAGE_PASSING _SC_MESSAGE_PASSING
* _POSIX_MONOTONIC_CLOCK _SC_MONOTONIC_CLOCK
* _POSIX_PRIORITIZED_IO _SC_PRIORITIZED_IO
* _POSIX_PRIORITY_SCHEDULING _SC_PRIORITY_SCHEDULING
* _POSIX_RAW_SOCKETS _SC_RAW_SOCKETS
* _POSIX_READER_WRITER_LOCKS _SC_READER_WRITER_LOCKS
* _POSIX_REALTIME_SIGNALS _SC_REALTIME_SIGNALS
* _POSIX_REGEXP _SC_REGEXP
* _POSIX_SAVED_IDS _SC_SAVED_IDS
* _POSIX_SEMAPHORES _SC_SEMAPHORES
* _POSIX_SHARED_MEMORY_OBJECTS _SC_SHARED_MEMORY_OBJECTS
* _POSIX_SHELL _SC_SHELL
* _POSIX_SPAWN _SC_SPAWN
* _POSIX_SPIN_LOCKS _SC_SPIN_LOCKS
* _POSIX_SPORADIC_SERVER _SC_SPORADIC_SERVER
* _POSIX_SS_REPL_MAX _SC_SS_REPL_MAX
* _POSIX_SYNCHRONIZED_IO _SC_SYNCHRONIZED_IO
* _POSIX_THREAD_ATTR_STACKADDR _SC_THREAD_ATTR_STACKADDR
* _POSIX_THREAD_ATTR_STACKSIZE _SC_THREAD_ATTR_STACKSIZE
* _POSIX_THREAD_CPUTIME _SC_THREAD_CPUTIME
* _POSIX_THREAD_PRIO_INHERIT _SC_THREAD_PRIO_INHERIT
* _POSIX_THREAD_PRIO_PROTECT _SC_THREAD_PRIO_PROTECT
* _POSIX_THREAD_PRIORITY_SCHEDULING _SC_THREAD_PRIORITY_SCHEDULING
* _POSIX_THREAD_PROCESS_SHARED _SC_THREAD_PROCESS_SHARED
* _POSIX_THREAD_SAFE_FUNCTIONS _SC_THREAD_SAFE_FUNCTIONS
* _POSIX_THREAD_SPORADIC_SERVER _SC_THREAD_SPORADIC_SERVER
* _POSIX_THREADS _SC_THREADS
* _POSIX_TIMEOUTS _SC_TIMEOUTS
* _POSIX_TIMERS _SC_TIMERS
* _POSIX_TRACE _SC_TRACE
* _POSIX_TRACE_EVENT_FILTER _SC_TRACE_EVENT_FILTER
* _POSIX_TRACE_EVENT_NAME_MAX _SC_TRACE_EVENT_NAME_MAX
* _POSIX_TRACE_INHERIT _SC_TRACE_INHERIT
* _POSIX_TRACE_LOG _SC_TRACE_LOG
* _POSIX_TRACE_NAME_MAX _SC_TRACE_NAME_MAX
* _POSIX_TRACE_SYS_MAX _SC_TRACE_SYS_MAX
* _POSIX_TRACE_USER_EVENT_MAX _SC_TRACE_USER_EVENT_MAX
* _POSIX_TYPED_MEMORY_OBJECTS _SC_TYPED_MEMORY_OBJECTS
* _POSIX_VERSION _SC_VERSION
* _POSIX_V6_ILP32_OFF32 _SC_V6_ILP32_OFF32
* _POSIX_V6_ILP32_OFFBIG _SC_V6_ILP32_OFFBIG
* _POSIX_V6_LP64_OFF64 _SC_V6_LP64_OFF64
* _POSIX_V6_LPBIG_OFFBIG _SC_V6_LPBIG_OFFBIG
* _POSIX2_C_BIND _SC_2_C_BIND
* _POSIX2_C_DEV _SC_2_C_DEV
* _POSIX2_CHAR_TERM _SC_2_CHAR_TERM
* _POSIX2_FORT_DEV _SC_2_FORT_DEV
* _POSIX2_FORT_RUN _SC_2_FORT_RUN
* _POSIX2_LOCALEDEF _SC_2_LOCALEDEF
* _POSIX2_PBS _SC_2_PBS
* _POSIX2_PBS_ACCOUNTING _SC_2_PBS_ACCOUNTING
* _POSIX2_PBS_CHECKPOINT _SC_2_PBS_CHECKPOINT
* _POSIX2_PBS_LOCATE _SC_2_PBS_LOCATE
* _POSIX2_PBS_MESSAGE _SC_2_PBS_MESSAGE
* _POSIX2_PBS_TRACK _SC_2_PBS_TRACK
* _POSIX2_SW_DEV _SC_2_SW_DEV
* _POSIX2_UPE _SC_2_UPE
* _POSIX2_VERSION _SC_2_VERSION
* {PAGE_SIZE} _SC_PAGE_SIZE
* {PAGESIZE} _SC_PAGESIZE
* {PTHREAD_DESTRUCTOR_ITERATIONS} _SC_THREAD_DESTRUCTOR_ITERATIONS
* {PTHREAD_KEYS_MAX} _SC_THREAD_KEYS_MAX
* {PTHREAD_STACK_MIN} _SC_THREAD_STACK_MIN
* {PTHREAD_THREADS_MAX} _SC_THREAD_THREADS_MAX
* {RE_DUP_MAX} _SC_RE_DUP_MAX
* {RTSIG_MAX} _SC_RTSIG_MAX
* {SEM_NSEMS_MAX} _SC_SEM_NSEMS_MAX
* {SEM_VALUE_MAX} _SC_SEM_VALUE_MAX
* {SIGQUEUE_MAX} _SC_SIGQUEUE_MAX
* {STREAM_MAX} _SC_STREAM_MAX
* {SYMLOOP_MAX} _SC_SYMLOOP_MAX
* {TIMER_MAX} _SC_TIMER_MAX
* {TTY_NAME_MAX} _SC_TTY_NAME_MAX
* {TZNAME_MAX} _SC_TZNAME_MAX
* _XBS5_ILP32_OFF32 (LEGACY) _SC_XBS5_ILP32_OFF32 (LEGACY)
* _XBS5_ILP32_OFFBIG (LEGACY) _SC_XBS5_ILP32_OFFBIG (LEGACY)
* _XBS5_LP64_OFF64 (LEGACY) _SC_XBS5_LP64_OFF64 (LEGACY)
* _XBS5_LPBIG_OFFBIG (LEGACY) _SC_XBS5_LPBIG_OFFBIG (LEGACY)
* _XOPEN_CRYPT _SC_XOPEN_CRYPT
* _XOPEN_ENH_I18N _SC_XOPEN_ENH_I18N
* _XOPEN_LEGACY _SC_XOPEN_LEGACY
* _XOPEN_REALTIME _SC_XOPEN_REALTIME
* _XOPEN_REALTIME_THREADS _SC_XOPEN_REALTIME_THREADS
* _XOPEN_SHM _SC_XOPEN_SHM
* _XOPEN_STREAMS _SC_XOPEN_STREAMS
* _XOPEN_UNIX _SC_XOPEN_UNIX
* _XOPEN_VERSION _SC_XOPEN_VERSION
*
* Returned Value:
* If name is an invalid value, sysconf() will return -1 and set errno to
* EINVAL to indicate the error. If the variable corresponding to name has
* no limit, sysconf() will return -1 without changing the value of errno.
* Note that indefinite limits do not imply infinite limits; see
* <limits.h>.
*
* Otherwise, sysconf() will return the current variable value on the
* system. The value returned will not be more restrictive than the
* corresponding value described to the application when it was compiled
* with the implementation's <limits.h> or <unistd.h>. The value will not
* change during the lifetime of the calling process, except that
* sysconf(_SC_OPEN_MAX) may return different values before and after a
* call to setrlimit() which changes the RLIMIT_NOFILE soft limit.
*
* If the variable corresponding to name is dependent on an unsupported
* option, the results are unspecified.
*
****************************************************************************/
long sysconf(int name)
{
int errcode;
/* NOTE: The initialize implementation of this interface is very sparse.
* It was originally created to support only the functionality of
* getdtablesize() but can be extended to support as much of the standard
* POSIX sysconf() as is necessary.
*/
switch (name)
{
case _SC_OPEN_MAX:
return CONFIG_NFILE_DESCRIPTORS;
default:
errcode = EINVAL;
break;
}
set_errno(errcode);
return ERROR;
}