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 
29 #include <ctype.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <limits.h>
34 #include <pthread.h>
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 
41 #include "private/bionic_tls.h"
42 #include "private/ScopedReaddir.h"
43 
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
52 #define  SYSTEM_SEM_VALUE_MAX   0x3fffffff  /* see bionic/semaphore.c */
53 #define  SYSTEM_SIGQUEUE_MAX    32
54 #define  SYSTEM_TIMER_MAX       32
55 #define  SYSTEM_LOGIN_NAME_MAX  256
56 #define  SYSTEM_TTY_NAME_MAX    32
57 
58 /* the following depends on our implementation */
59 #define  SYSTEM_ATEXIT_MAX          65536    /* our implementation is unlimited */
60 #define  SYSTEM_THREAD_THREADS_MAX  2048     /* really unlimited */
61 
62 #define  SYSTEM_2_C_BIND     _POSIX_VERSION  /* Posix C binding version */
63 #define  SYSTEM_2_C_VER      _POSIX2_C_VERSION
64 #define  SYSTEM_2_C_DEV      -1       /* Posix C development tools unsupported on the device */
65 #define  SYSTEM_2_FORT_DEV   -1       /* Fortran development unsupported */
66 #define  SYSTEM_2_FORT_RUN   -1       /* Fortran runtime unsupported */
67 #define  SYSTEM_2_SW_DEV     -1       /* posix software dev utilities unsupported */
68 #define  SYSTEM_2_LOCALEDEF  -1       /* localedef() unimplemented */
69 #define  SYSTEM_2_UPE        -1       /* No UPE for you ! (User Portability Utilities) */
70 #define  SYSTEM_2_VERSION    -1       /* No posix command-line tools */
71 
__matches_cpuN(const char * s)72 static bool __matches_cpuN(const char* s) {
73   // The %c trick is to ensure that we have the anchored match "^cpu[0-9]+$".
74   unsigned cpu;
75   char dummy;
76   return (sscanf(s, "cpu%u%c", &cpu, &dummy) == 1);
77 }
78 
__sysconf_nprocessors_conf()79 static int __sysconf_nprocessors_conf() {
80   // On x86 kernels you can use /proc/cpuinfo for this, but on ARM kernels offline CPUs disappear
81   // from there. This method works on both.
82   ScopedReaddir reader("/sys/devices/system/cpu");
83   if (reader.IsBad()) {
84     return 1;
85   }
86 
87   int result = 0;
88   dirent* entry;
89   while ((entry = reader.ReadEntry()) != NULL) {
90     if (entry->d_type == DT_DIR && __matches_cpuN(entry->d_name)) {
91       ++result;
92     }
93   }
94   return result;
95 }
96 
__sysconf_nprocessors_onln()97 static int __sysconf_nprocessors_onln() {
98   FILE* fp = fopen("/proc/stat", "r");
99   if (fp == NULL) {
100     return 1;
101   }
102 
103   int result = 0;
104   char buf[256];
105   while (fgets(buf, sizeof(buf), fp) != NULL) {
106     // Extract just the first word from the line.
107     // 'cpu0 7976751 1364388 3116842 469770388 8629405 0 49047 0 0 0'
108     char* p = strchr(buf, ' ');
109     if (p != NULL) {
110       *p = 0;
111     }
112     if (__matches_cpuN(buf)) {
113       ++result;
114     }
115   }
116   fclose(fp);
117   return result;
118 }
119 
__get_meminfo(const char * pattern)120 static int __get_meminfo(const char* pattern) {
121   FILE* fp = fopen("/proc/meminfo", "r");
122   if (fp == NULL) {
123     return -1;
124   }
125 
126   int result = -1;
127   char buf[256];
128   while (fgets(buf, sizeof(buf), fp) != NULL) {
129     long total;
130     if (sscanf(buf, pattern, &total) == 1) {
131       result = (int) (total / (PAGE_SIZE/1024));
132       break;
133     }
134   }
135   fclose(fp);
136   return result;
137 }
138 
__sysconf_phys_pages()139 static int __sysconf_phys_pages() {
140   return __get_meminfo("MemTotal: %ld kB");
141 }
142 
__sysconf_avphys_pages()143 static int __sysconf_avphys_pages() {
144   return __get_meminfo("MemFree: %ld kB");
145 }
146 
__sysconf_monotonic_clock()147 static int __sysconf_monotonic_clock() {
148   timespec t;
149   int rc = clock_getres(CLOCK_MONOTONIC, &t);
150   return (rc == -1) ? -1 : _POSIX_VERSION;
151 }
152 
sysconf(int name)153 long sysconf(int name) {
154     switch (name) {
155 #ifdef _POSIX_ARG_MAX
156     case _SC_ARG_MAX:           return _POSIX_ARG_MAX;
157 #endif
158 #ifdef _POSIX2_BC_BASE_MAX
159     case _SC_BC_BASE_MAX:       return _POSIX2_BC_BASE_MAX;
160 #endif
161 #ifdef _POSIX2_BC_DIM_MAX
162     case _SC_BC_DIM_MAX:        return _POSIX2_BC_DIM_MAX;
163 #endif
164 #ifdef _POSIX2_BC_SCALE_MAX
165     case _SC_BC_SCALE_MAX:      return _POSIX2_BC_SCALE_MAX;
166 #endif
167 #ifdef _POSIX2_BC_STRING_MAX
168     case _SC_BC_STRING_MAX:     return _POSIX2_BC_STRING_MAX;
169 #endif
170     case _SC_CHILD_MAX:         return CHILD_MAX;
171     case _SC_CLK_TCK:           return SYSTEM_CLK_TCK;
172 #ifdef _POSIX2_COLL_WEIGHTS_MASK
173     case _SC_COLL_WEIGHTS_MAX:  return _POSIX2_COLL_WEIGHTS_MASK;
174 #endif
175 #ifdef _POSIX2_EXPR_NEST_MAX
176     case _SC_EXPR_NEST_MAX:    return _POSIX2_EXPR_NEST_MAX;
177 #endif
178 #ifdef _POSIX2_LINE_MAX
179     case _SC_LINE_MAX:          return _POSIX2_LINE_MAX;
180 #endif
181     case _SC_NGROUPS_MAX:       return NGROUPS_MAX;
182     case _SC_OPEN_MAX:          return OPEN_MAX;
183     //case _SC_PASS_MAX:          return PASS_MAX;
184     case _SC_2_C_BIND:          return SYSTEM_2_C_BIND;
185     case _SC_2_C_DEV:           return SYSTEM_2_C_DEV;
186     case _SC_2_C_VERSION:       return SYSTEM_2_C_VER;
187     //case _SC_2_CHAR_TERM:         return ;
188     case _SC_2_FORT_DEV:        return SYSTEM_2_FORT_DEV;
189     case _SC_2_FORT_RUN:        return SYSTEM_2_FORT_RUN;
190     case _SC_2_LOCALEDEF:       return SYSTEM_2_LOCALEDEF;
191     case _SC_2_SW_DEV:          return SYSTEM_2_SW_DEV;
192     case _SC_2_UPE:             return SYSTEM_2_UPE;
193     case _SC_2_VERSION:         return SYSTEM_2_VERSION;
194 #ifdef _POSIX_JOB_CONTROL
195     case _SC_JOB_CONTROL:       return _POSIX_JOB_CONTROL;
196 #endif
197 #ifdef _POSIX_SAVED_IDS
198     case _SC_SAVED_IDS:         return _POSIX_SAVED_IDS;
199 #endif
200 #ifdef _POSIX_VERSION
201     case _SC_VERSION:           return _POSIX_VERSION;
202 #endif
203     //case _SC_RE_DUP_<AX:        return ;
204     case _SC_STREAM_MAX:        return FOPEN_MAX;
205     //case _SC_TZNAME_MAX:        return ;
206 #if _XOPEN_CRYPT
207     case _SC_XOPEN_CRYPT:       return _XOPEN_CRYPT;
208 #endif
209 #ifdef _XOPEN_ENH_I18N
210     case _SC_XOPEN_ENH_I18N:    return _XOPEN_ENH_I18N;
211 #endif
212 #ifdef _XOPEN_SHM
213     case _SC_XOPEN_SHM:         return _XOPEN_SHM;
214 #endif
215 #ifdef _XOPEN_VERSION
216     case _SC_XOPEN_VERSION:     return _XOPEN_VERSION;
217 #endif
218 #ifdef _XOPEN_XCU_VERSION
219     case _SC_XOPEN_XCU_VERSION: return _XOPEN_XCU_VERSION;
220 #endif
221 #ifdef _XOPEN_REALTIME
222     case _SC_XOPEN_REALTIME:    return _XOPEN_REALTIME;
223 #endif
224 #ifdef _XOPEN_REALTIME_THREADS
225     case _SC_XOPEN_REALTIME_THREADS: return _XOPEN_REALTIME_THREADS;
226 #endif
227 #ifdef _XOPEN_LEGACY
228     case _SC_XOPEN_LEGACY:      return _XOPEN_LEGACY;
229 #endif
230     case _SC_ATEXIT_MAX:        return SYSTEM_ATEXIT_MAX;
231     case _SC_IOV_MAX:           return SYSTEM_IOV_MAX;
232 
233     case _SC_PAGESIZE:
234     case _SC_PAGE_SIZE:
235         return PAGE_SIZE;
236 
237 #ifdef _XOPEN_UNIX
238     case _SC_XOPEN_UNIX:        return _XOPEN_UNIX;
239 #endif
240 
241     // XXX: TODO: XBS5 nonsense
242 
243 #ifdef AIO_LISTIO_MAX
244     case _SC_AIO_LISTIO_MAX:       return AIO_LISTIO_MAX;
245 #endif
246 #ifdef AIO_MAX
247     case _SC_AIO_MAX:              return AIO_MAX;
248 #endif
249 #ifdef AIO_PRIO_DELTA_MAX
250     case _SC_AIO_PRIO_DELTA_MAX:   return AIO_PRIO_DELTA_MAX;
251 #endif
252     case _SC_DELAYTIMER_MAX:    return SYSTEM_DELAYTIMER_MAX;
253     case _SC_MQ_OPEN_MAX:       return SYSTEM_MQ_OPEN_MAX;
254     case _SC_MQ_PRIO_MAX:       return SYSTEM_MQ_PRIO_MAX;
255     case _SC_RTSIG_MAX:         return RTSIG_MAX;
256     case _SC_SEM_NSEMS_MAX:     return SYSTEM_SEM_NSEMS_MAX;
257     case _SC_SEM_VALUE_MAX:     return SYSTEM_SEM_VALUE_MAX;
258     case _SC_SIGQUEUE_MAX:      return SYSTEM_SIGQUEUE_MAX;
259     case _SC_TIMER_MAX:         return SYSTEM_TIMER_MAX;
260 #ifdef _POSIX_ASYNCHRONOUS_IO
261     case _SC_ASYNCHRONOUS_IO:   return _POSIX_ASYNCHRONOUS_IO;
262 #endif
263 #ifdef _POSIX_FSYNC
264     case _SC_FSYNC:             return _POSIX_FSYNC;
265 #endif
266 #ifdef _POSIX_MAPPED_FILES
267     case _SC_MAPPED_FILES:      return _POSIX_MAPPED_FILES;
268 #endif
269 #ifdef _POSIX_MEMLOCK
270     case _SC_MEMLOCK:           return _POSIX_MEMLOCK;
271 #endif
272 #ifdef _POSIX_MEMLOCK_RANGE
273     case _SC_MEMLOCK_RANGE:     return _POSIX_MEMLOCK_RANGE
274 #endif
275 #ifdef _POSIX_MEMORY_PROTECTION
276     case _SC_MEMORY_PROTECTION: return _POSIX_MEMORY_PROTECTION;
277 #endif
278 #ifdef _POSIX_MESSAGE_PASSING
279     case _SC_MESSAGE_PASSING:   return _POSIX_MESSAGE_PASSING;
280 #endif
281 #ifdef _POSIX_PRIORITIZED_IO
282     case _SC_PRIORITIZED_IO:    return _POSIX_PRIORITIZED_IO;
283 #endif
284 #ifdef _POSIX_PRIORITY_SCHEDULING
285     case _SC_PRIORITY_SCHEDULING:  return _POSIX_PRIORITY_SCHEDULING;
286 #endif
287 #ifdef _POSIX_REALTIME_SIGNALS
288     case _SC_REALTIME_SIGNALS:  return _POSIX_REALTIME_SIGNALS;
289 #endif
290 #ifdef _POSIX_SEMAPHORES
291     case _SC_SEMAPHORES:        return _POSIX_SEMAPHORES;
292 #endif
293 #ifdef _POSIX_SHARED_MEMORY_OBJECTS
294     case _SC_SHARED_MEMORY_OBJECTS:  return _POSIX_SHARED_MEMORY_OBJECTS;
295 #endif
296 #ifdef _POSIX_SYNCHRONIZED_IO
297     case _SC_SYNCHRONIZED_IO:   return _POSIX_SYNCHRONIZED_IO;
298 #endif
299 #ifdef _POSIX_TIMERS
300     case _SC_TIMERS:            return _POSIX_TIMERS;
301 #endif
302 
303     case _SC_GETGR_R_SIZE_MAX: return 1024;
304     case _SC_GETPW_R_SIZE_MAX: return 1024;
305 
306     case _SC_LOGIN_NAME_MAX:    return SYSTEM_LOGIN_NAME_MAX;
307 
308     case _SC_THREAD_DESTRUCTOR_ITERATIONS:
309       return _POSIX_THREAD_DESTRUCTOR_ITERATIONS;
310 
311     case _SC_THREAD_KEYS_MAX:
312       return (BIONIC_TLS_SLOTS - TLS_SLOT_FIRST_USER_SLOT - BIONIC_TLS_RESERVED_SLOTS);
313 
314     case _SC_THREAD_STACK_MIN:    return PTHREAD_STACK_MIN;
315     case _SC_THREAD_THREADS_MAX:  return SYSTEM_THREAD_THREADS_MAX;
316     case _SC_TTY_NAME_MAX:        return SYSTEM_TTY_NAME_MAX;
317 #ifdef _POSIX_THREADS
318     case _SC_THREADS:             return _POSIX_THREADS;
319 #endif
320 
321     case _SC_THREAD_ATTR_STACKADDR:   return -1; // Removed in POSIX 2008
322     case _SC_THREAD_ATTR_STACKSIZE:   return -1; // Removed in POSIX 2008
323 
324 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
325     case _SC_THREAD_PRIORITY_SCHEDULING:  return _POSIX_THREAD_PRIORITY_SCHEDULING;
326 #endif
327 #ifdef _POSIX_THREAD_PRIO_INHERIT
328     case _SC_THREAD_PRIO_INHERIT:         return _POSIX_THREAD_PRIO_INHERIT;
329 #endif
330 #ifdef _POSIX_THREAD_PRIO_PROTECT
331     case _SC_THREAD_PRIO_PROTECT: return _POSIX_THREAD_PRIO_PROTECT;
332 #endif
333 #ifdef _POSIX_THREAD_SAFE_FUNCTIONS
334     case _SC_THREAD_SAFE_FUNCTIONS:  return _POSIX_THREAD_SAFE_FUNCTIONS
335 #endif
336 
337     case _SC_MONOTONIC_CLOCK:   return __sysconf_monotonic_clock();
338     case _SC_NPROCESSORS_CONF:  return __sysconf_nprocessors_conf();
339     case _SC_NPROCESSORS_ONLN:  return __sysconf_nprocessors_onln();
340     case _SC_PHYS_PAGES:        return __sysconf_phys_pages();
341     case _SC_AVPHYS_PAGES:      return __sysconf_avphys_pages();
342 
343     default:
344        /* Posix says EINVAL is the only error that shall be returned,
345         * but GLibc uses ENOSYS */
346         errno = ENOSYS;
347         return -1;
348     }
349 }
350