/* * Copyright (C) 2008 The Android Open Source Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 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. * * 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. */ #include #include #include #include #include #include /* for FOPEN_MAX */ #include #include #include #include /* seems to be the default on Linux, per the GLibc sources and my own digging */ #define SYSTEM_CLK_TCK 100 #define SYSTEM_IOV_MAX 1024 #define SYSTEM_DELAYTIMER_MAX 2147483647 #define SYSTEM_MQ_OPEN_MAX 8 #define SYSTEM_MQ_PRIO_MAX 32768 #define SYSTEM_SEM_NSEMS_MAX 256 #define SYSTEM_SEM_VALUE_MAX (2147483647) #define SYSTEM_SIGQUEUE_MAX 32 #define SYSTEM_TIMER_MAX 32 #define SYSTEM_LOGIN_NAME_MAX 256 #define SYSTEM_TTY_NAME_MAX 32 /* the following depends on our implementation */ #define SYSTEM_ATEXIT_MAX 65536 /* our implementation is unlimited */ #define SYSTEM_THREAD_KEYS_MAX BIONIC_TLS_SLOTS #define SYSTEM_THREAD_STACK_MIN 32768 /* lower values may be possible, but be conservative */ #define SYSTEM_THREAD_THREADS_MAX 2048 /* really unlimited */ #define SYSTEM_2_C_BIND _POSIX_VERSION /* Posix C binding version */ #define SYSTEM_2_C_VER _POSIX2_C_VERSION #define SYSTEM_2_C_DEV -1 /* Posix C development tools unsupported on the device */ #define SYSTEM_2_FORT_DEV -1 /* Fortran development unsupported */ #define SYSTEM_2_FORT_RUN -1 /* Fortran runtime unsupported */ #define SYSTEM_2_SW_DEV -1 /* posix software dev utilities unsupported */ #define SYSTEM_2_LOCALEDEF -1 /* localdef() unimplemented */ #define SYSTEM_2_UPE -1 /* No UPE for you ! (User Portability Utilities) */ #define SYSTEM_2_VERSION -1 /* No posix command-line tools */ static int __get_nproc_conf(void); static int __get_nproc_onln(void); static int __get_phys_pages(void); static int __get_avphys_pages(void); int sysconf( int name ) { switch (name) { #ifdef _POSIX_ARG_MAX case _SC_ARG_MAX: return _POSIX_ARG_MAX; #endif #ifdef _POSIX2_BC_BASE_MAX case _SC_BC_BASE_MAX: return _POSIX2_BC_BASE_MAX; #endif #ifdef _POSIX2_BC_DIM_MAX case _SC_BC_DIM_MAX: return _POSIX2_BC_DIM_MAX; #endif #ifdef _POSIX2_BC_SCALE_MAX case _SC_BC_SCALE_MAX: return _POSIX2_BC_SCALE_MAX; #endif #ifdef _POSIX2_BC_STRING_MAX case _SC_BC_STRING_MAX: return _POSIX2_BC_STRING_MAX; #endif case _SC_CHILD_MAX: return CHILD_MAX; case _SC_CLK_TCK: return SYSTEM_CLK_TCK; #ifdef _POSIX2_COLL_WEIGHTS_MASK case _SC_COLL_WEIGHTS_MAX: return _POSIX2_COLL_WEIGHTS_MASK; #endif #ifdef _POSIX2_EXPR_NEST_MAX case _SC_EXPR_NEXT_MASK: return _POSIX2_EXPR_NEST_MAX; #endif #ifdef _POSIX2_LINE_MAX case _SC_LINE_MAX: return _POSIX2_LINE_MAX; #endif case _SC_NGROUPS_MAX: return NGROUPS_MAX; case _SC_OPEN_MAX: return OPEN_MAX; //case _SC_PASS_MAX: return PASS_MAX; case _SC_2_C_BIND: return SYSTEM_2_C_BIND; case _SC_2_C_DEV: return SYSTEM_2_C_DEV; case _SC_2_C_VERSION: return SYSTEM_2_C_VER; //case _SC_2_CHAR_TERM: return ; case _SC_2_FORT_DEV: return SYSTEM_2_FORT_DEV; case _SC_2_FORT_RUN: return SYSTEM_2_FORT_RUN; case _SC_2_LOCALEDEF: return SYSTEM_2_LOCALEDEF; case _SC_2_SW_DEV: return SYSTEM_2_SW_DEV; case _SC_2_UPE: return SYSTEM_2_UPE; case _SC_2_VERSION: return SYSTEM_2_VERSION; #ifdef _POSIX_JOB_CONTROL case _SC_JOB_CONTROL: return _POSIX_JOB_CONTROL; #endif #ifdef _POSIX_SAVED_IDS case _SC_SAVED_IDS: return _POSIX_SAVED_IDS; #endif #ifdef _POSIX_VERSION case _SC_VERSION: return _POSIX_VERSION; #endif //case _SC_RE_DUP_rpos = 0; p->len = (int)sizeof(p->buff); p->overflow = 0; p->in_len = 0; p->in_pos = 0; p->fd = open( path, O_RDONLY ); return p->fd; } static int line_parser_addc( LineParser* p, int c ) { if (p->overflow) { p->overflow = (c == '\n'); return 0; } if (p->rpos >= p->len) { p->overflow = 1; return 0; } if (c == '\n') { p->buff[p->rpos] = 0; p->rpos = 0; return 1; } p->buff[p->rpos++] = (char) c; return 0; } static int line_parser_getc( LineParser* p ) { if (p->in_len >= p->in_pos) { int ret; p->in_len = p->in_pos = 0; do { ret = read(p->fd, p->input, sizeof(p->input)); } while (ret < 0 && errno == EINTR); if (ret <= 0) return -1; p->in_len = ret; } return p->input[ p->in_pos++ ]; } static const char* line_parser_gets( LineParser* p ) { for (;;) { for (;;) { int c = line_parser_getc(p); if (c < 0) { close(p->fd); p->fd = -1; return NULL; } if (line_parser_addc(p, c)) return p->buff; } } } static void line_parser_done( LineParser* p ) { if (p->fd >= 0) { close(p->fd); p->fd = -1; } } static int __get_nproc_conf(void) { LineParser parser[1]; const char* p; int count = 0; if (line_parser_init(parser, "/proc/cpuinfo") < 0) return 1; while ((p = line_parser_gets(parser))) { if ( !memcmp(p, "processor", 9) ) count += 1; } return (count < 1) ? 1 : count; } static int __get_nproc_onln(void) { LineParser parser[1]; const char* p; int count = 0; if (line_parser_init(parser, "/proc/stat") < 0) return 1; while ((p = line_parser_gets(parser))) { if ( !memcmp(p, "cpu", 3) && isdigit(p[3]) ) count += 1; } return (count < 1) ? 1 : count; } static int __get_phys_pages(void) { LineParser parser[1]; const char* p; if (line_parser_init(parser, "/proc/meminfo") < 0) return -2; /* what ? */ while ((p = line_parser_gets(parser))) { long total; if ( sscanf(p, "MemTotal: %ld kB", &total) == 1 ) { line_parser_done(parser); return (int) (total / (PAGE_SIZE/1024)); } } return -3; } static int __get_avphys_pages(void) { LineParser parser[1]; const char* p; if (line_parser_init(parser, "/proc/meminfo") < 0) return -1; /* what ? */ while ((p = line_parser_gets(parser))) { long total; if ( sscanf(p, "MemFree: %ld kB", &total) == 1 ) { line_parser_done(parser); return (int) (total / (PAGE_SIZE/1024)); } } return -1; }