1 /* Copyright (C) 1991,1993,1995-1997,2000
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 see <http://www.gnu.org/licenses/>. */
19 #define _XOPEN_SOURCE 500
31 #include <sys/syscall.h>
32 #include <sys/sysinfo.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #ifdef __UCLIBC_HAS_REGEX__
38 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
41 #include <sys/resource.h>
44 #include "internal/parse_config.h"
46 long int get_phys_pages(void)
49 int ps
= getpagesize();;
53 if (ps
>= si
.mem_unit
)
54 return si
.totalram
/ (ps
/ si
.mem_unit
);
56 return si
.totalram
* (si
.mem_unit
/ ps
);
59 long int get_avphys_pages(void)
62 int ps
= getpagesize();;
66 if (ps
>= si
.mem_unit
)
67 return si
.freeram
/ (ps
/ si
.mem_unit
);
69 return si
.freeram
* (si
.mem_unit
/ ps
);
72 static int nprocessors_onln(void)
75 parser_t
*p
= config_open("/proc/stat");
79 while (config_read(p
, &l
, 2, 1, " ", 0))
85 } else if ((p
= config_open("/proc/cpuinfo"))) {
87 while (config_read(p
, &l
, 2, 2, "\0:", PARSE_NORMAL
))
88 if (strncmp("ncpus active", l
[0], 12) == 0) {
93 while (config_read(p
, &l
, 2, 2, "\0:\t", PARSE_NORMAL
))
94 if (strcmp("processor", l
[0]) == 0)
99 return ret
!= 0 ? ret
: 1;
102 #if defined __UCLIBC__ && !defined __UCLIBC_HAS_LFS__
103 # define readdir64 readdir
104 # define dirent64 dirent
106 static int nprocessors_conf(void)
109 DIR *dir
= opendir("/sys/devices/system/cpu");
114 while ((dp
= readdir64(dir
))) {
115 if (dp
->d_type
== DT_DIR
116 && dp
->d_name
[0] == 'c'
117 && dp
->d_name
[1] == 'p'
118 && dp
->d_name
[2] == 'u'
119 && isdigit(dp
->d_name
[3]))
125 #if defined __sparc__
127 parser_t
*p
= config_open("/proc/stat");
128 while (config_read(p
, &l
, 2, 2, "\0:", PARSE_NORMAL
))
129 if (strncmp("ncpus probed", l
[0], 13) == 0) {
135 ret
= nprocessors_onln();
138 return ret
!= 0 ? ret
: 1;
142 #ifndef __UCLIBC_CLK_TCK_CONST
143 #error __UCLIBC_CLK_TCK_CONST not defined!
146 /***********************************************************************/
148 * Manuel Novoa III Jan 2001
150 * On i386, the switch-based implementation generates 796 bytes of code.
151 * However, many of the return values are repeats. By collecting these
152 * repeats and moving to a table-based implementation, we generate 283
153 * bytes on i386 (-Os -fomit-frame-pointer).
156 #ifdef _UCLIBC_GENERATE_SYSCONF_ARCH
158 * Set some errno's so the auto-gen code knows what it is dealing with.
159 * 1) ENOSYS signifies that we're returning a default value.
160 * This is just extra info for development.
161 * 2) EISNAM signifies that the value returned varies at runtime.
163 * Option: GETPAGESIZE_IS_DYNAMIC
164 * The current implementation of getpagesize in uClibc returns
165 * a constant. The pagesize on the target arch should not vary,
166 * so it should be safe to set this as 0.
168 #define RETURN_NEG_1 __set_errno(ENOSYS); return -1
169 #define RETURN_FUNCTION(f) __set_errno(EISNAM); return (long int) #f
170 #define GETPAGESIZE_IS_DYNAMIC 0
172 #define RETURN_NEG_1 return -1
173 #define RETURN_FUNCTION(f) return f;
174 #endif /* _UCLIBC_GENERATE_SYSCONF_ARCH */
176 /* Legacy value of ARG_MAX. The macro is now not defined since the
177 actual value varies based on the stack size. */
178 #define legacy_ARG_MAX 131072
180 /* Get the value of the system variable NAME. */
181 long int sysconf(int name
)
183 struct rlimit rlimit
;
192 /* Use getrlimit to get the stack limit. */
193 if (getrlimit (RLIMIT_STACK
, &rlimit
) == 0)
194 return MAX (legacy_ARG_MAX
, rlimit
.rlim_cur
/ 4);
198 return legacy_ARG_MAX
;
209 /* Can't use CLK_TCK here since that calls __sysconf(_SC_CLK_TCK) */
210 return __UCLIBC_CLK_TCK_CONST
;
212 case _SC_NGROUPS_MAX
:
220 RETURN_FUNCTION(getdtablesize());
230 return _POSIX_TZNAME_MAX
;
232 case _SC_JOB_CONTROL
:
233 #ifdef _POSIX_JOB_CONTROL
240 #ifdef _POSIX_SAVED_IDS
246 case _SC_REALTIME_SIGNALS
:
247 #ifdef _POSIX_REALTIME_SIGNALS
253 case _SC_PRIORITY_SCHEDULING
:
254 #ifdef _POSIX_PRIORITY_SCHEDULING
267 case _SC_ASYNCHRONOUS_IO
:
268 #ifdef _POSIX_ASYNCHRONOUS_IO
274 case _SC_PRIORITIZED_IO
:
275 #ifdef _POSIX_PRIORITIZED_IO
281 case _SC_SYNCHRONIZED_IO
:
282 #ifdef _POSIX_SYNCHRONIZED_IO
295 case _SC_MAPPED_FILES
:
296 #ifdef _POSIX_MAPPED_FILES
303 #ifdef _POSIX_MEMLOCK
309 case _SC_MEMLOCK_RANGE
:
310 #ifdef _POSIX_MEMLOCK_RANGE
316 case _SC_MEMORY_PROTECTION
:
317 #ifdef _POSIX_MEMORY_PROTECTION
323 case _SC_MESSAGE_PASSING
:
324 #ifdef _POSIX_MESSAGE_PASSING
331 #ifdef _POSIX_SEMAPHORES
337 case _SC_SHARED_MEMORY_OBJECTS
:
338 #ifdef _POSIX_SHARED_MEMORY_OBJECTS
345 return _POSIX_VERSION
;
348 #if defined(GETPAGESIZE_IS_DYNAMIC) && (GETPAGESIZE_IS_DYNAMIC == 1)
349 RETURN_FUNCTION(getpagesize());
351 return getpagesize(); /* note: currently this is not dynamic */
354 case _SC_AIO_LISTIO_MAX
:
355 #ifdef AIO_LISTIO_MAX
356 return AIO_LISTIO_MAX
;
368 case _SC_AIO_PRIO_DELTA_MAX
:
369 #ifdef AIO_PRIO_DELTA_MAX
370 return AIO_PRIO_DELTA_MAX
;
375 case _SC_DELAYTIMER_MAX
:
376 #ifdef DELAYTIMER_MAX
377 return DELAYTIMER_MAX
;
382 case _SC_MQ_OPEN_MAX
:
389 case _SC_MQ_PRIO_MAX
:
403 case _SC_SEM_NSEMS_MAX
:
405 return SEM_NSEMS_MAX
;
410 case _SC_SEM_VALUE_MAX
:
412 return SEM_VALUE_MAX
;
417 case _SC_SIGQUEUE_MAX
:
431 case _SC_BC_BASE_MAX
:
445 case _SC_BC_SCALE_MAX
:
452 case _SC_BC_STRING_MAX
:
454 return BC_STRING_MAX
;
459 case _SC_COLL_WEIGHTS_MAX
:
460 #ifdef COLL_WEIGHTS_MAX
461 return COLL_WEIGHTS_MAX
;
466 case _SC_EQUIV_CLASS_MAX
:
467 #ifdef EQUIV_CLASS_MAX
468 return EQUIV_CLASS_MAX
;
473 case _SC_EXPR_NEST_MAX
:
475 return EXPR_NEST_MAX
;
494 case _SC_CHARCLASS_NAME_MAX
:
495 #ifdef CHARCLASS_NAME_MAX
496 return CHARCLASS_NAME_MAX
;
509 #ifdef _POSIX_PII_XTI
516 #ifdef _POSIX_PII_SOCKET
522 case _SC_PII_INTERNET
:
523 #ifdef _POSIX_PII_INTERNET
530 #ifdef _POSIX_PII_OSI
557 case _SC_PII_INTERNET_STREAM
:
558 #ifdef _POSIX_PII_INTERNET_STREAM
564 case _SC_PII_INTERNET_DGRAM
:
565 #ifdef _POSIX_PII_INTERNET_DGRAM
571 case _SC_PII_OSI_COTS
:
572 #ifdef _POSIX_PII_OSI_COTS
578 case _SC_PII_OSI_CLTS
:
579 #ifdef _POSIX_PII_OSI_CLTS
586 #ifdef _POSIX_PII_OSI_M
600 return _POSIX2_VERSION
;
603 #ifdef _POSIX2_C_BIND
604 return _POSIX2_C_BIND
;
611 return _POSIX2_C_DEV
;
616 case _SC_2_C_VERSION
:
617 #ifdef _POSIX2_C_VERSION
618 return _POSIX2_C_VERSION
;
624 #ifdef _POSIX2_FORT_DEV
625 return _POSIX2_FORT_DEV
;
631 #ifdef _POSIX2_FORT_RUN
632 return _POSIX2_FORT_RUN
;
637 case _SC_2_LOCALEDEF
:
638 #ifdef _POSIX2_LOCALEDEF
639 return _POSIX2_LOCALEDEF
;
645 #ifdef _POSIX2_SW_DEV
646 return _POSIX2_SW_DEV
;
651 case _SC_2_CHAR_TERM
:
652 #ifdef _POSIX2_CHAR_TERM
653 return _POSIX2_CHAR_TERM
;
665 /* POSIX 1003.1c (POSIX Threads). */
667 #ifdef __UCLIBC_HAS_THREADS__
673 case _SC_THREAD_SAFE_FUNCTIONS
:
674 #ifdef __UCLIBC_HAS_THREADS__
680 /* If you change these, also change libc/pwd_grp/pwd_grp.c to match */
681 case _SC_GETGR_R_SIZE_MAX
:
682 return __UCLIBC_GRP_BUFFER_SIZE__
;
684 case _SC_GETPW_R_SIZE_MAX
:
685 return __UCLIBC_PWD_BUFFER_SIZE__
;
687 /* getlogin() is a worthless interface. In uClibc we let the user specify
688 * whatever they want via the LOGNAME environment variable, or we return NULL
689 * if getenv() fails to find anything. So this is merely how large a env
690 * variable can be. Lets use 256 */
691 case _SC_LOGIN_NAME_MAX
:
694 /* If you change this, also change _SC_TTY_NAME_MAX in libc/unistd/sysconf.c */
695 #define TTYNAME_BUFLEN 32
696 case _SC_TTY_NAME_MAX
:
697 return TTYNAME_BUFLEN
;
699 case _SC_THREAD_DESTRUCTOR_ITERATIONS
:
700 #ifdef _POSIX_THREAD_DESTRUCTOR_ITERATIONS
701 return _POSIX_THREAD_DESTRUCTOR_ITERATIONS
;
706 case _SC_THREAD_KEYS_MAX
:
707 #ifdef PTHREAD_KEYS_MAX
708 return PTHREAD_KEYS_MAX
;
713 case _SC_THREAD_STACK_MIN
:
714 #ifdef PTHREAD_STACK_MIN
715 return PTHREAD_STACK_MIN
;
720 case _SC_THREAD_THREADS_MAX
:
721 #ifdef PTHREAD_THREADS_MAX
722 return PTHREAD_THREADS_MAX
;
727 case _SC_THREAD_ATTR_STACKADDR
:
728 #ifdef _POSIX_THREAD_ATTR_STACKADDR
734 case _SC_THREAD_ATTR_STACKSIZE
:
735 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
741 case _SC_THREAD_PRIORITY_SCHEDULING
:
742 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
748 case _SC_THREAD_PRIO_INHERIT
:
749 #ifdef _POSIX_THREAD_PRIO_INHERIT
755 case _SC_THREAD_PRIO_PROTECT
:
756 #ifdef _POSIX_THREAD_PRIO_PROTECT
762 case _SC_THREAD_PROCESS_SHARED
:
763 #ifdef _POSIX_THREAD_PROCESS_SHARED
769 case _SC_NPROCESSORS_CONF
:
770 RETURN_FUNCTION(nprocessors_conf());
772 case _SC_NPROCESSORS_ONLN
:
773 RETURN_FUNCTION(nprocessors_onln());
776 RETURN_FUNCTION(get_phys_pages());
778 case _SC_AVPHYS_PAGES
:
779 RETURN_FUNCTION(get_avphys_pages());
782 return __UCLIBC_MAX_ATEXIT
;
785 /* We have no limit but since the return value might be used to
786 allocate a buffer we restrict the value. */
789 case _SC_XOPEN_VERSION
:
790 return _XOPEN_VERSION
;
792 case _SC_XOPEN_XCU_VERSION
:
793 return _XOPEN_XCU_VERSION
;
798 case _SC_XOPEN_CRYPT
:
805 case _SC_XOPEN_ENH_I18N
:
806 #ifdef _XOPEN_ENH_I18N
807 return _XOPEN_ENH_I18N
;
856 return sizeof (long int) * CHAR_BIT
;
859 return sizeof (int) * CHAR_BIT
;
868 return _POSIX_SSIZE_MAX
;
936 case _SC_XBS5_ILP32_OFF32
:
937 #ifdef _XBS5_ILP32_OFF32
938 return _XBS5_ILP32_OFF32
;
942 case _SC_XBS5_ILP32_OFFBIG
:
943 #ifdef _XBS5_ILP32_OFFBIG
944 return _XBS5_ILP32_OFFBIG
;
948 case _SC_XBS5_LP64_OFF64
:
949 #ifdef _XBS5_LP64_OFF64
950 return _XBS5_LP64_OFF64
;
954 case _SC_XBS5_LPBIG_OFFBIG
:
955 #ifdef _XBS5_LPBIG_OFFBIG
956 return _XBS5_LPBIG_OFFBIG
;
960 case _SC_V7_ILP32_OFF32
:
961 #ifdef _POSIX_V7_ILP32_OFF32
962 return _POSIX_V7_ILP32_OFF32
;
966 case _SC_V7_ILP32_OFFBIG
:
967 #ifdef _POSIX_V7_ILP32_OFFBIG
968 return _POSIX_V7_ILP32_OFFBIG
;
972 case _SC_V7_LP64_OFF64
:
973 #ifdef _POSIX_V7_LP64_OFF64
974 return _POSIX_V7_LP64_OFF64
;
978 case _SC_V7_LPBIG_OFFBIG
:
979 #ifdef _POSIX_V7_LPBIG_OFFBIG
980 return _POSIX_V7_LPBIG_OFFBIG
;
985 case _SC_XOPEN_LEGACY
:
986 return _XOPEN_LEGACY
;
988 case _SC_XOPEN_REALTIME
:
989 #ifdef _XOPEN_REALTIME
990 return _XOPEN_REALTIME
;
994 case _SC_XOPEN_REALTIME_THREADS
:
995 #ifdef _XOPEN_REALTIME_THREADS
996 return _XOPEN_REALTIME_THREADS
;
1001 case _SC_MONOTONIC_CLOCK
:
1002 #if defined __UCLIBC_HAS_REALTIME__ && defined __NR_clock_getres
1003 if (clock_getres(CLOCK_MONOTONIC
, NULL
) >= 0)
1004 return _POSIX_VERSION
;
1008 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
1009 case _SC_THREAD_CPUTIME
:
1010 # if _POSIX_THREAD_CPUTIME > 0
1011 return _POSIX_THREAD_CPUTIME
;
1018 libc_hidden_def(sysconf
)