blob: 951860d48c2b8fb3c7c377233282fc71f6bc1dc7 [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 <ctype.h>
30#include <dirent.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <limits.h>
Elliott Hughesc03e1e72013-07-29 16:51:45 -070034#include <pthread.h>
Elliott Hughesa55f6302013-01-02 14:23:43 -080035#include <stdio.h> // For FOPEN_MAX.
36#include <string.h>
37#include <sys/sysconf.h>
38#include <time.h>
39#include <unistd.h>
40
Elliott Hugheseb847bc2013-10-09 15:50:50 -070041#include "private/bionic_tls.h"
Elliott Hughes701bec22013-02-25 13:14:31 -080042#include "private/ScopedReaddir.h"
43
Elliott Hughesa55f6302013-01-02 14:23:43 -080044/* seems to be the default on Linux, per the GLibc sources and my own digging */
45
46#define SYSTEM_CLK_TCK 100
47#define SYSTEM_IOV_MAX 1024
48#define SYSTEM_DELAYTIMER_MAX 2147483647
49#define SYSTEM_MQ_OPEN_MAX 8
50#define SYSTEM_MQ_PRIO_MAX 32768
51#define SYSTEM_SEM_NSEMS_MAX 256
Elliott Hughesa55f6302013-01-02 14:23:43 -080052#define SYSTEM_SIGQUEUE_MAX 32
53#define SYSTEM_TIMER_MAX 32
54#define SYSTEM_LOGIN_NAME_MAX 256
55#define SYSTEM_TTY_NAME_MAX 32
56
57/* the following depends on our implementation */
58#define SYSTEM_ATEXIT_MAX 65536 /* our implementation is unlimited */
Elliott Hughesa55f6302013-01-02 14:23:43 -080059#define SYSTEM_THREAD_THREADS_MAX 2048 /* really unlimited */
60
61#define SYSTEM_2_C_BIND _POSIX_VERSION /* Posix C binding version */
62#define SYSTEM_2_C_VER _POSIX2_C_VERSION
63#define SYSTEM_2_C_DEV -1 /* Posix C development tools unsupported on the device */
64#define SYSTEM_2_FORT_DEV -1 /* Fortran development unsupported */
65#define SYSTEM_2_FORT_RUN -1 /* Fortran runtime unsupported */
66#define SYSTEM_2_SW_DEV -1 /* posix software dev utilities unsupported */
67#define SYSTEM_2_LOCALEDEF -1 /* localedef() unimplemented */
68#define SYSTEM_2_UPE -1 /* No UPE for you ! (User Portability Utilities) */
69#define SYSTEM_2_VERSION -1 /* No posix command-line tools */
70
71static bool __matches_cpuN(const char* s) {
72 // The %c trick is to ensure that we have the anchored match "^cpu[0-9]+$".
73 unsigned cpu;
74 char dummy;
75 return (sscanf(s, "cpu%u%c", &cpu, &dummy) == 1);
76}
77
78static int __sysconf_nprocessors_conf() {
79 // On x86 kernels you can use /proc/cpuinfo for this, but on ARM kernels offline CPUs disappear
80 // from there. This method works on both.
Elliott Hughes701bec22013-02-25 13:14:31 -080081 ScopedReaddir reader("/sys/devices/system/cpu");
82 if (reader.IsBad()) {
Elliott Hughesa55f6302013-01-02 14:23:43 -080083 return 1;
84 }
85
86 int result = 0;
Elliott Hughes701bec22013-02-25 13:14:31 -080087 dirent* entry;
88 while ((entry = reader.ReadEntry()) != NULL) {
89 if (entry->d_type == DT_DIR && __matches_cpuN(entry->d_name)) {
Elliott Hughesa55f6302013-01-02 14:23:43 -080090 ++result;
91 }
92 }
Elliott Hughesa55f6302013-01-02 14:23:43 -080093 return result;
94}
95
96static int __sysconf_nprocessors_onln() {
Elliott Hughesc674edb2014-08-26 15:56:54 -070097 FILE* fp = fopen("/proc/stat", "re");
Elliott Hughesa55f6302013-01-02 14:23:43 -080098 if (fp == NULL) {
99 return 1;
100 }
101
102 int result = 0;
103 char buf[256];
104 while (fgets(buf, sizeof(buf), fp) != NULL) {
105 // Extract just the first word from the line.
106 // 'cpu0 7976751 1364388 3116842 469770388 8629405 0 49047 0 0 0'
107 char* p = strchr(buf, ' ');
108 if (p != NULL) {
109 *p = 0;
110 }
111 if (__matches_cpuN(buf)) {
112 ++result;
113 }
114 }
115 fclose(fp);
116 return result;
117}
118
119static int __get_meminfo(const char* pattern) {
Elliott Hughesc674edb2014-08-26 15:56:54 -0700120 FILE* fp = fopen("/proc/meminfo", "re");
Elliott Hughesa55f6302013-01-02 14:23:43 -0800121 if (fp == NULL) {
122 return -1;
123 }
124
125 int result = -1;
126 char buf[256];
127 while (fgets(buf, sizeof(buf), fp) != NULL) {
128 long total;
129 if (sscanf(buf, pattern, &total) == 1) {
130 result = (int) (total / (PAGE_SIZE/1024));
131 break;
132 }
133 }
134 fclose(fp);
135 return result;
136}
137
138static int __sysconf_phys_pages() {
139 return __get_meminfo("MemTotal: %ld kB");
140}
141
142static int __sysconf_avphys_pages() {
143 return __get_meminfo("MemFree: %ld kB");
144}
145
146static int __sysconf_monotonic_clock() {
147 timespec t;
148 int rc = clock_getres(CLOCK_MONOTONIC, &t);
149 return (rc == -1) ? -1 : _POSIX_VERSION;
150}
151
152int sysconf(int name) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700153 switch (name) {
Elliott Hughesa55f6302013-01-02 14:23:43 -0800154 case _SC_ARG_MAX: return _POSIX_ARG_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800155 case _SC_CHILD_MAX: return CHILD_MAX;
156 case _SC_CLK_TCK: return SYSTEM_CLK_TCK;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800157 case _SC_LINE_MAX: return _POSIX2_LINE_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800158 case _SC_NGROUPS_MAX: return NGROUPS_MAX;
159 case _SC_OPEN_MAX: return OPEN_MAX;
Elliott Hughesa186b2e2014-09-22 14:49:07 -0700160 case _SC_PASS_MAX: return PASS_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800161 case _SC_2_C_BIND: return SYSTEM_2_C_BIND;
162 case _SC_2_C_DEV: return SYSTEM_2_C_DEV;
163 case _SC_2_C_VERSION: return SYSTEM_2_C_VER;
164 //case _SC_2_CHAR_TERM: return ;
165 case _SC_2_FORT_DEV: return SYSTEM_2_FORT_DEV;
166 case _SC_2_FORT_RUN: return SYSTEM_2_FORT_RUN;
167 case _SC_2_LOCALEDEF: return SYSTEM_2_LOCALEDEF;
168 case _SC_2_SW_DEV: return SYSTEM_2_SW_DEV;
169 case _SC_2_UPE: return SYSTEM_2_UPE;
170 case _SC_2_VERSION: return SYSTEM_2_VERSION;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800171 case _SC_JOB_CONTROL: return _POSIX_JOB_CONTROL;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800172 case _SC_SAVED_IDS: return _POSIX_SAVED_IDS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800173 case _SC_VERSION: return _POSIX_VERSION;
Elliott Hughes04303f52014-09-18 16:11:59 -0700174 case _SC_RE_DUP_MAX: return _POSIX_RE_DUP_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800175 case _SC_STREAM_MAX: return FOPEN_MAX;
Elliott Hughes04303f52014-09-18 16:11:59 -0700176 case _SC_TZNAME_MAX: return _POSIX_TZNAME_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800177 case _SC_XOPEN_CRYPT: return _XOPEN_CRYPT;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800178 case _SC_XOPEN_ENH_I18N: return _XOPEN_ENH_I18N;
Elliott Hughes04303f52014-09-18 16:11:59 -0700179 //case _SC_XOPEN_SHM: return _XOPEN_SHM;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800180 case _SC_XOPEN_VERSION: return _XOPEN_VERSION;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800181 case _SC_XOPEN_XCU_VERSION: return _XOPEN_XCU_VERSION;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800182 case _SC_XOPEN_REALTIME: return _XOPEN_REALTIME;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800183 case _SC_XOPEN_REALTIME_THREADS: return _XOPEN_REALTIME_THREADS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800184 case _SC_XOPEN_LEGACY: return _XOPEN_LEGACY;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800185 case _SC_ATEXIT_MAX: return SYSTEM_ATEXIT_MAX;
186 case _SC_IOV_MAX: return SYSTEM_IOV_MAX;
Elliott Hughes0e44bc32014-02-24 15:55:31 -0800187
188 case _SC_PAGESIZE:
189 case _SC_PAGE_SIZE:
190 return PAGE_SIZE;
191
Elliott Hughesa55f6302013-01-02 14:23:43 -0800192 case _SC_XOPEN_UNIX: return _XOPEN_UNIX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800193
194 // XXX: TODO: XBS5 nonsense
195
Elliott Hughes04303f52014-09-18 16:11:59 -0700196 //case _SC_AIO_LISTIO_MAX: return AIO_LISTIO_MAX;
197 //case _SC_AIO_MAX: return AIO_MAX;
198 //case _SC_AIO_PRIO_DELTA_MAX: return AIO_PRIO_DELTA_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800199 case _SC_DELAYTIMER_MAX: return SYSTEM_DELAYTIMER_MAX;
200 case _SC_MQ_OPEN_MAX: return SYSTEM_MQ_OPEN_MAX;
201 case _SC_MQ_PRIO_MAX: return SYSTEM_MQ_PRIO_MAX;
202 case _SC_RTSIG_MAX: return RTSIG_MAX;
203 case _SC_SEM_NSEMS_MAX: return SYSTEM_SEM_NSEMS_MAX;
Elliott Hughes04303f52014-09-18 16:11:59 -0700204 case _SC_SEM_VALUE_MAX: return SEM_VALUE_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800205 case _SC_SIGQUEUE_MAX: return SYSTEM_SIGQUEUE_MAX;
206 case _SC_TIMER_MAX: return SYSTEM_TIMER_MAX;
Elliott Hughes04303f52014-09-18 16:11:59 -0700207 //case _SC_ASYNCHRONOUS_IO: return _POSIX_ASYNCHRONOUS_IO;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800208 case _SC_FSYNC: return _POSIX_FSYNC;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800209 case _SC_MAPPED_FILES: return _POSIX_MAPPED_FILES;
Elliott Hughes04303f52014-09-18 16:11:59 -0700210 //case _SC_MEMLOCK: return _POSIX_MEMLOCK;
211 //case _SC_MEMLOCK_RANGE: return _POSIX_MEMLOCK_RANGE;
212 //case _SC_MEMORY_PROTECTION: return _POSIX_MEMORY_PROTECTION;
213 //case _SC_MESSAGE_PASSING: return _POSIX_MESSAGE_PASSING;
214 //case _SC_PRIORITIZED_IO: return _POSIX_PRIORITIZED_IO;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800215 case _SC_PRIORITY_SCHEDULING: return _POSIX_PRIORITY_SCHEDULING;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800216 case _SC_REALTIME_SIGNALS: return _POSIX_REALTIME_SIGNALS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800217 case _SC_SEMAPHORES: return _POSIX_SEMAPHORES;
Elliott Hughes04303f52014-09-18 16:11:59 -0700218 //case _SC_SHARED_MEMORY_OBJECTS: return _POSIX_SHARED_MEMORY_OBJECTS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800219 case _SC_SYNCHRONIZED_IO: return _POSIX_SYNCHRONIZED_IO;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800220 case _SC_TIMERS: return _POSIX_TIMERS;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800221
Elliott Hughesd35106f2013-05-14 17:20:34 -0700222 case _SC_GETGR_R_SIZE_MAX: return 1024;
223 case _SC_GETPW_R_SIZE_MAX: return 1024;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800224
225 case _SC_LOGIN_NAME_MAX: return SYSTEM_LOGIN_NAME_MAX;
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000226
227 case _SC_THREAD_DESTRUCTOR_ITERATIONS:
228 return _POSIX_THREAD_DESTRUCTOR_ITERATIONS;
229
230 case _SC_THREAD_KEYS_MAX:
Christopher Ferris72bbd422014-05-08 11:14:03 -0700231 return (BIONIC_TLS_SLOTS - TLS_SLOT_FIRST_USER_SLOT - BIONIC_TLS_RESERVED_SLOTS);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000232
Elliott Hughesc03e1e72013-07-29 16:51:45 -0700233 case _SC_THREAD_STACK_MIN: return PTHREAD_STACK_MIN;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800234 case _SC_THREAD_THREADS_MAX: return SYSTEM_THREAD_THREADS_MAX;
235 case _SC_TTY_NAME_MAX: return SYSTEM_TTY_NAME_MAX;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800236 case _SC_THREADS: return _POSIX_THREADS;
Calin Juravlea0ca2092014-03-10 18:25:36 +0000237
238 case _SC_THREAD_ATTR_STACKADDR: return -1; // Removed in POSIX 2008
239 case _SC_THREAD_ATTR_STACKSIZE: return -1; // Removed in POSIX 2008
240
Elliott Hughes04303f52014-09-18 16:11:59 -0700241 //case _SC_THREAD_PRIORITY_SCHEDULING: return _POSIX_THREAD_PRIORITY_SCHEDULING;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800242 case _SC_THREAD_PRIO_INHERIT: return _POSIX_THREAD_PRIO_INHERIT;
Elliott Hughesa55f6302013-01-02 14:23:43 -0800243 case _SC_THREAD_PRIO_PROTECT: return _POSIX_THREAD_PRIO_PROTECT;
Elliott Hughes04303f52014-09-18 16:11:59 -0700244 //case _SC_THREAD_SAFE_FUNCTIONS: return _POSIX_THREAD_SAFE_FUNCTIONS
Elliott Hughesa55f6302013-01-02 14:23:43 -0800245
246 case _SC_MONOTONIC_CLOCK: return __sysconf_monotonic_clock();
247 case _SC_NPROCESSORS_CONF: return __sysconf_nprocessors_conf();
248 case _SC_NPROCESSORS_ONLN: return __sysconf_nprocessors_onln();
249 case _SC_PHYS_PAGES: return __sysconf_phys_pages();
250 case _SC_AVPHYS_PAGES: return __sysconf_avphys_pages();
251
252 default:
Elliott Hughes04303f52014-09-18 16:11:59 -0700253 // Posix says EINVAL is the only error that shall be returned,
254 // but glibc uses ENOSYS.
255 errno = ENOSYS;
256 return -1;
257 }
Elliott Hughesa55f6302013-01-02 14:23:43 -0800258}