blob: 2df6accc5e6dd96abf9f774db294425c96911189 [file] [log] [blame]
Elliott Hughesa55f6302013-01-02 14:23:43 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughesa55f6302013-01-02 14:23:43 -080029#include <errno.h>
30#include <fcntl.h>
31#include <limits.h>
Elliott Hughesc03e1e72013-07-29 16:51:45 -070032#include <pthread.h>
Elliott Hughesa55f6302013-01-02 14:23:43 -080033#include <stdio.h> // For FOPEN_MAX.
Elliott Hughesa55f6302013-01-02 14:23:43 -080034#include <sys/sysconf.h>
Yabin Cui9d939862014-11-14 15:51:58 -080035#include <sys/sysinfo.h>
Elliott Hughesa55f6302013-01-02 14:23:43 -080036#include <time.h>
37#include <unistd.h>
38
Elliott Hugheseb847bc2013-10-09 15:50:50 -070039#include "private/bionic_tls.h"
Elliott Hughes701bec22013-02-25 13:14:31 -080040
Elliott Hughesa55f6302013-01-02 14:23:43 -080041/* seems to be the default on Linux, per the GLibc sources and my own digging */
42
43#define SYSTEM_CLK_TCK 100
44#define SYSTEM_IOV_MAX 1024
45#define SYSTEM_DELAYTIMER_MAX 2147483647
46#define SYSTEM_MQ_OPEN_MAX 8
47#define SYSTEM_MQ_PRIO_MAX 32768
48#define SYSTEM_SEM_NSEMS_MAX 256
Elliott Hughesa55f6302013-01-02 14:23:43 -080049#define SYSTEM_SIGQUEUE_MAX 32
50#define SYSTEM_TIMER_MAX 32
51#define SYSTEM_LOGIN_NAME_MAX 256
52#define SYSTEM_TTY_NAME_MAX 32
53
54/* the following depends on our implementation */
55#define SYSTEM_ATEXIT_MAX 65536 /* our implementation is unlimited */
Elliott Hughesa55f6302013-01-02 14:23:43 -080056#define SYSTEM_THREAD_THREADS_MAX 2048 /* really unlimited */
57
58#define SYSTEM_2_C_BIND _POSIX_VERSION /* Posix C binding version */
59#define SYSTEM_2_C_VER _POSIX2_C_VERSION
60#define SYSTEM_2_C_DEV -1 /* Posix C development tools unsupported on the device */
61#define SYSTEM_2_FORT_DEV -1 /* Fortran development unsupported */
62#define SYSTEM_2_FORT_RUN -1 /* Fortran runtime unsupported */
63#define SYSTEM_2_SW_DEV -1 /* posix software dev utilities unsupported */
64#define SYSTEM_2_LOCALEDEF -1 /* localedef() unimplemented */
65#define SYSTEM_2_UPE -1 /* No UPE for you ! (User Portability Utilities) */
66#define SYSTEM_2_VERSION -1 /* No posix command-line tools */
67
Elliott Hughesa55f6302013-01-02 14:23:43 -080068static int __sysconf_monotonic_clock() {
69 timespec t;
70 int rc = clock_getres(CLOCK_MONOTONIC, &t);
71 return (rc == -1) ? -1 : _POSIX_VERSION;
72}
73
Elliott Hughes60d84af2014-11-14 15:14:44 -080074long sysconf(int name) {
Elliott Hughes04303f52014-09-18 16:11:59 -070075 switch (name) {
Elliott Hughesa55f6302013-01-02 14:23:43 -080076 case _SC_ARG_MAX: return _POSIX_ARG_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -080077 case _SC_CHILD_MAX: return CHILD_MAX;
78 case _SC_CLK_TCK: return SYSTEM_CLK_TCK;
Elliott Hughesa55f6302013-01-02 14:23:43 -080079 case _SC_LINE_MAX: return _POSIX2_LINE_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -080080 case _SC_NGROUPS_MAX: return NGROUPS_MAX;
81 case _SC_OPEN_MAX: return OPEN_MAX;
Elliott Hughesa186b2e2014-09-22 14:49:07 -070082 case _SC_PASS_MAX: return PASS_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -080083 case _SC_2_C_BIND: return SYSTEM_2_C_BIND;
84 case _SC_2_C_DEV: return SYSTEM_2_C_DEV;
85 case _SC_2_C_VERSION: return SYSTEM_2_C_VER;
86 //case _SC_2_CHAR_TERM: return ;
87 case _SC_2_FORT_DEV: return SYSTEM_2_FORT_DEV;
88 case _SC_2_FORT_RUN: return SYSTEM_2_FORT_RUN;
89 case _SC_2_LOCALEDEF: return SYSTEM_2_LOCALEDEF;
90 case _SC_2_SW_DEV: return SYSTEM_2_SW_DEV;
91 case _SC_2_UPE: return SYSTEM_2_UPE;
92 case _SC_2_VERSION: return SYSTEM_2_VERSION;
Elliott Hughesa55f6302013-01-02 14:23:43 -080093 case _SC_JOB_CONTROL: return _POSIX_JOB_CONTROL;
Elliott Hughesa55f6302013-01-02 14:23:43 -080094 case _SC_SAVED_IDS: return _POSIX_SAVED_IDS;
Elliott Hughesa55f6302013-01-02 14:23:43 -080095 case _SC_VERSION: return _POSIX_VERSION;
Elliott Hughes04303f52014-09-18 16:11:59 -070096 case _SC_RE_DUP_MAX: return _POSIX_RE_DUP_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -080097 case _SC_STREAM_MAX: return FOPEN_MAX;
Elliott Hughes04303f52014-09-18 16:11:59 -070098 case _SC_TZNAME_MAX: return _POSIX_TZNAME_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -080099 case _SC_XOPEN_CRYPT: return _XOPEN_CRYPT;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800100 case _SC_XOPEN_ENH_I18N: return _XOPEN_ENH_I18N;
Elliott Hughes04303f52014-09-18 16:11:59 -0700101 //case _SC_XOPEN_SHM: return _XOPEN_SHM;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800102 case _SC_XOPEN_VERSION: return _XOPEN_VERSION;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800103 case _SC_XOPEN_XCU_VERSION: return _XOPEN_XCU_VERSION;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800104 case _SC_XOPEN_REALTIME: return _XOPEN_REALTIME;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800105 case _SC_XOPEN_REALTIME_THREADS: return _XOPEN_REALTIME_THREADS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800106 case _SC_XOPEN_LEGACY: return _XOPEN_LEGACY;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800107 case _SC_ATEXIT_MAX: return SYSTEM_ATEXIT_MAX;
108 case _SC_IOV_MAX: return SYSTEM_IOV_MAX;
Elliott Hughes0e44bc32014-02-24 15:55:31 -0800109
110 case _SC_PAGESIZE:
111 case _SC_PAGE_SIZE:
112 return PAGE_SIZE;
113
Elliott Hughesa55f6302013-01-02 14:23:43 -0800114 case _SC_XOPEN_UNIX: return _XOPEN_UNIX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800115
116 // XXX: TODO: XBS5 nonsense
117
Elliott Hughes04303f52014-09-18 16:11:59 -0700118 //case _SC_AIO_LISTIO_MAX: return AIO_LISTIO_MAX;
119 //case _SC_AIO_MAX: return AIO_MAX;
120 //case _SC_AIO_PRIO_DELTA_MAX: return AIO_PRIO_DELTA_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800121 case _SC_DELAYTIMER_MAX: return SYSTEM_DELAYTIMER_MAX;
122 case _SC_MQ_OPEN_MAX: return SYSTEM_MQ_OPEN_MAX;
123 case _SC_MQ_PRIO_MAX: return SYSTEM_MQ_PRIO_MAX;
124 case _SC_RTSIG_MAX: return RTSIG_MAX;
125 case _SC_SEM_NSEMS_MAX: return SYSTEM_SEM_NSEMS_MAX;
Elliott Hughes04303f52014-09-18 16:11:59 -0700126 case _SC_SEM_VALUE_MAX: return SEM_VALUE_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800127 case _SC_SIGQUEUE_MAX: return SYSTEM_SIGQUEUE_MAX;
128 case _SC_TIMER_MAX: return SYSTEM_TIMER_MAX;
Elliott Hughes04303f52014-09-18 16:11:59 -0700129 //case _SC_ASYNCHRONOUS_IO: return _POSIX_ASYNCHRONOUS_IO;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800130 case _SC_FSYNC: return _POSIX_FSYNC;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800131 case _SC_MAPPED_FILES: return _POSIX_MAPPED_FILES;
Elliott Hughes04303f52014-09-18 16:11:59 -0700132 //case _SC_MEMLOCK: return _POSIX_MEMLOCK;
133 //case _SC_MEMLOCK_RANGE: return _POSIX_MEMLOCK_RANGE;
134 //case _SC_MEMORY_PROTECTION: return _POSIX_MEMORY_PROTECTION;
135 //case _SC_MESSAGE_PASSING: return _POSIX_MESSAGE_PASSING;
136 //case _SC_PRIORITIZED_IO: return _POSIX_PRIORITIZED_IO;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800137 case _SC_PRIORITY_SCHEDULING: return _POSIX_PRIORITY_SCHEDULING;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800138 case _SC_REALTIME_SIGNALS: return _POSIX_REALTIME_SIGNALS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800139 case _SC_SEMAPHORES: return _POSIX_SEMAPHORES;
Elliott Hughes04303f52014-09-18 16:11:59 -0700140 //case _SC_SHARED_MEMORY_OBJECTS: return _POSIX_SHARED_MEMORY_OBJECTS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800141 case _SC_SYNCHRONIZED_IO: return _POSIX_SYNCHRONIZED_IO;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800142 case _SC_TIMERS: return _POSIX_TIMERS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800143
Elliott Hughesd35106f2013-05-14 17:20:34 -0700144 case _SC_GETGR_R_SIZE_MAX: return 1024;
145 case _SC_GETPW_R_SIZE_MAX: return 1024;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800146
147 case _SC_LOGIN_NAME_MAX: return SYSTEM_LOGIN_NAME_MAX;
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000148
149 case _SC_THREAD_DESTRUCTOR_ITERATIONS:
150 return _POSIX_THREAD_DESTRUCTOR_ITERATIONS;
151
152 case _SC_THREAD_KEYS_MAX:
Christopher Ferris72bbd422014-05-08 11:14:03 -0700153 return (BIONIC_TLS_SLOTS - TLS_SLOT_FIRST_USER_SLOT - BIONIC_TLS_RESERVED_SLOTS);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000154
Elliott Hughesc03e1e72013-07-29 16:51:45 -0700155 case _SC_THREAD_STACK_MIN: return PTHREAD_STACK_MIN;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800156 case _SC_THREAD_THREADS_MAX: return SYSTEM_THREAD_THREADS_MAX;
157 case _SC_TTY_NAME_MAX: return SYSTEM_TTY_NAME_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800158 case _SC_THREADS: return _POSIX_THREADS;
Calin Juravlea0ca2092014-03-10 18:25:36 +0000159
160 case _SC_THREAD_ATTR_STACKADDR: return -1; // Removed in POSIX 2008
161 case _SC_THREAD_ATTR_STACKSIZE: return -1; // Removed in POSIX 2008
162
Elliott Hughes04303f52014-09-18 16:11:59 -0700163 //case _SC_THREAD_PRIORITY_SCHEDULING: return _POSIX_THREAD_PRIORITY_SCHEDULING;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800164 case _SC_THREAD_PRIO_INHERIT: return _POSIX_THREAD_PRIO_INHERIT;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800165 case _SC_THREAD_PRIO_PROTECT: return _POSIX_THREAD_PRIO_PROTECT;
Elliott Hughes04303f52014-09-18 16:11:59 -0700166 //case _SC_THREAD_SAFE_FUNCTIONS: return _POSIX_THREAD_SAFE_FUNCTIONS
Elliott Hughesa55f6302013-01-02 14:23:43 -0800167
168 case _SC_MONOTONIC_CLOCK: return __sysconf_monotonic_clock();
Yabin Cui9d939862014-11-14 15:51:58 -0800169 case _SC_NPROCESSORS_CONF: return get_nprocs_conf();
170 case _SC_NPROCESSORS_ONLN: return get_nprocs();
171 case _SC_PHYS_PAGES: return get_phys_pages();
172 case _SC_AVPHYS_PAGES: return get_avphys_pages();
Elliott Hughesa55f6302013-01-02 14:23:43 -0800173
174 default:
Elliott Hughes04303f52014-09-18 16:11:59 -0700175 // Posix says EINVAL is the only error that shall be returned,
176 // but glibc uses ENOSYS.
177 errno = ENOSYS;
178 return -1;
179 }
Elliott Hughesa55f6302013-01-02 14:23:43 -0800180}