Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 29 | #include <ctype.h> |
| 30 | #include <dirent.h> |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <limits.h> |
Elliott Hughes | c03e1e7 | 2013-07-29 16:51:45 -0700 | [diff] [blame] | 34 | #include <pthread.h> |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 35 | #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 Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 41 | #include "private/bionic_tls.h" |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 42 | #include "private/ScopedReaddir.h" |
| 43 | |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 44 | /* 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 Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 52 | #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 Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 59 | #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 | |
| 71 | static 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 | |
| 78 | static 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 Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 81 | ScopedReaddir reader("/sys/devices/system/cpu"); |
| 82 | if (reader.IsBad()) { |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | int result = 0; |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 87 | dirent* entry; |
| 88 | while ((entry = reader.ReadEntry()) != NULL) { |
| 89 | if (entry->d_type == DT_DIR && __matches_cpuN(entry->d_name)) { |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 90 | ++result; |
| 91 | } |
| 92 | } |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 93 | return result; |
| 94 | } |
| 95 | |
| 96 | static int __sysconf_nprocessors_onln() { |
Elliott Hughes | c674edb | 2014-08-26 15:56:54 -0700 | [diff] [blame] | 97 | FILE* fp = fopen("/proc/stat", "re"); |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 98 | 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 | |
| 119 | static int __get_meminfo(const char* pattern) { |
Elliott Hughes | c674edb | 2014-08-26 15:56:54 -0700 | [diff] [blame] | 120 | FILE* fp = fopen("/proc/meminfo", "re"); |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 121 | 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 | |
| 138 | static int __sysconf_phys_pages() { |
| 139 | return __get_meminfo("MemTotal: %ld kB"); |
| 140 | } |
| 141 | |
| 142 | static int __sysconf_avphys_pages() { |
| 143 | return __get_meminfo("MemFree: %ld kB"); |
| 144 | } |
| 145 | |
| 146 | static int __sysconf_monotonic_clock() { |
| 147 | timespec t; |
| 148 | int rc = clock_getres(CLOCK_MONOTONIC, &t); |
| 149 | return (rc == -1) ? -1 : _POSIX_VERSION; |
| 150 | } |
| 151 | |
| 152 | int sysconf(int name) { |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 153 | switch (name) { |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 154 | case _SC_ARG_MAX: return _POSIX_ARG_MAX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 155 | case _SC_CHILD_MAX: return CHILD_MAX; |
| 156 | case _SC_CLK_TCK: return SYSTEM_CLK_TCK; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 157 | case _SC_LINE_MAX: return _POSIX2_LINE_MAX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 158 | case _SC_NGROUPS_MAX: return NGROUPS_MAX; |
| 159 | case _SC_OPEN_MAX: return OPEN_MAX; |
Elliott Hughes | a186b2e | 2014-09-22 14:49:07 -0700 | [diff] [blame] | 160 | case _SC_PASS_MAX: return PASS_MAX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 161 | 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 Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 171 | case _SC_JOB_CONTROL: return _POSIX_JOB_CONTROL; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 172 | case _SC_SAVED_IDS: return _POSIX_SAVED_IDS; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 173 | case _SC_VERSION: return _POSIX_VERSION; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 174 | case _SC_RE_DUP_MAX: return _POSIX_RE_DUP_MAX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 175 | case _SC_STREAM_MAX: return FOPEN_MAX; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 176 | case _SC_TZNAME_MAX: return _POSIX_TZNAME_MAX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 177 | case _SC_XOPEN_CRYPT: return _XOPEN_CRYPT; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 178 | case _SC_XOPEN_ENH_I18N: return _XOPEN_ENH_I18N; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 179 | //case _SC_XOPEN_SHM: return _XOPEN_SHM; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 180 | case _SC_XOPEN_VERSION: return _XOPEN_VERSION; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 181 | case _SC_XOPEN_XCU_VERSION: return _XOPEN_XCU_VERSION; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 182 | case _SC_XOPEN_REALTIME: return _XOPEN_REALTIME; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 183 | case _SC_XOPEN_REALTIME_THREADS: return _XOPEN_REALTIME_THREADS; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 184 | case _SC_XOPEN_LEGACY: return _XOPEN_LEGACY; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 185 | case _SC_ATEXIT_MAX: return SYSTEM_ATEXIT_MAX; |
| 186 | case _SC_IOV_MAX: return SYSTEM_IOV_MAX; |
Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 187 | |
| 188 | case _SC_PAGESIZE: |
| 189 | case _SC_PAGE_SIZE: |
| 190 | return PAGE_SIZE; |
| 191 | |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 192 | case _SC_XOPEN_UNIX: return _XOPEN_UNIX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 193 | |
| 194 | // XXX: TODO: XBS5 nonsense |
| 195 | |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 196 | //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 Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 199 | 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 Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 204 | case _SC_SEM_VALUE_MAX: return SEM_VALUE_MAX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 205 | case _SC_SIGQUEUE_MAX: return SYSTEM_SIGQUEUE_MAX; |
| 206 | case _SC_TIMER_MAX: return SYSTEM_TIMER_MAX; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 207 | //case _SC_ASYNCHRONOUS_IO: return _POSIX_ASYNCHRONOUS_IO; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 208 | case _SC_FSYNC: return _POSIX_FSYNC; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 209 | case _SC_MAPPED_FILES: return _POSIX_MAPPED_FILES; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 210 | //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 Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 215 | case _SC_PRIORITY_SCHEDULING: return _POSIX_PRIORITY_SCHEDULING; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 216 | case _SC_REALTIME_SIGNALS: return _POSIX_REALTIME_SIGNALS; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 217 | case _SC_SEMAPHORES: return _POSIX_SEMAPHORES; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 218 | //case _SC_SHARED_MEMORY_OBJECTS: return _POSIX_SHARED_MEMORY_OBJECTS; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 219 | case _SC_SYNCHRONIZED_IO: return _POSIX_SYNCHRONIZED_IO; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 220 | case _SC_TIMERS: return _POSIX_TIMERS; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 221 | |
Elliott Hughes | d35106f | 2013-05-14 17:20:34 -0700 | [diff] [blame] | 222 | case _SC_GETGR_R_SIZE_MAX: return 1024; |
| 223 | case _SC_GETPW_R_SIZE_MAX: return 1024; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 224 | |
| 225 | case _SC_LOGIN_NAME_MAX: return SYSTEM_LOGIN_NAME_MAX; |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 226 | |
| 227 | case _SC_THREAD_DESTRUCTOR_ITERATIONS: |
| 228 | return _POSIX_THREAD_DESTRUCTOR_ITERATIONS; |
| 229 | |
| 230 | case _SC_THREAD_KEYS_MAX: |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 231 | return (BIONIC_TLS_SLOTS - TLS_SLOT_FIRST_USER_SLOT - BIONIC_TLS_RESERVED_SLOTS); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 232 | |
Elliott Hughes | c03e1e7 | 2013-07-29 16:51:45 -0700 | [diff] [blame] | 233 | case _SC_THREAD_STACK_MIN: return PTHREAD_STACK_MIN; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 234 | case _SC_THREAD_THREADS_MAX: return SYSTEM_THREAD_THREADS_MAX; |
| 235 | case _SC_TTY_NAME_MAX: return SYSTEM_TTY_NAME_MAX; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 236 | case _SC_THREADS: return _POSIX_THREADS; |
Calin Juravle | a0ca209 | 2014-03-10 18:25:36 +0000 | [diff] [blame] | 237 | |
| 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 Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 241 | //case _SC_THREAD_PRIORITY_SCHEDULING: return _POSIX_THREAD_PRIORITY_SCHEDULING; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 242 | case _SC_THREAD_PRIO_INHERIT: return _POSIX_THREAD_PRIO_INHERIT; |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 243 | case _SC_THREAD_PRIO_PROTECT: return _POSIX_THREAD_PRIO_PROTECT; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 244 | //case _SC_THREAD_SAFE_FUNCTIONS: return _POSIX_THREAD_SAFE_FUNCTIONS |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 245 | |
| 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 Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 253 | // Posix says EINVAL is the only error that shall be returned, |
| 254 | // but glibc uses ENOSYS. |
| 255 | errno = ENOSYS; |
| 256 | return -1; |
| 257 | } |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 258 | } |