4 #include <sys/resource.h>
6 #include <sys/sysinfo.h>
10 #define JT(x) (-256|(x))
12 #define JT_ARG_MAX JT(2)
13 #define JT_MQ_PRIO_MAX JT(3)
14 #define JT_PAGE_SIZE JT(4)
15 #define JT_SEM_VALUE_MAX JT(5)
16 #define JT_NPROCESSORS_CONF JT(6)
17 #define JT_NPROCESSORS_ONLN JT(7)
18 #define JT_PHYS_PAGES JT(8)
19 #define JT_AVPHYS_PAGES JT(9)
20 #define JT_ZERO JT(10)
22 #define RLIM(x) (-32768|(RLIMIT_ ## x))
24 long sysconf(int name
)
26 static const short values
[] = {
27 [_SC_ARG_MAX
] = JT_ARG_MAX
,
28 [_SC_CHILD_MAX
] = RLIM(NPROC
),
30 [_SC_NGROUPS_MAX
] = 32,
31 [_SC_OPEN_MAX
] = RLIM(NOFILE
),
32 [_SC_STREAM_MAX
] = -1,
33 [_SC_TZNAME_MAX
] = TZNAME_MAX
,
34 [_SC_JOB_CONTROL
] = 1,
36 [_SC_REALTIME_SIGNALS
] = 1,
37 [_SC_PRIORITY_SCHEDULING
] = -1,
39 [_SC_ASYNCHRONOUS_IO
] = VER
,
40 [_SC_PRIORITIZED_IO
] = -1,
41 [_SC_SYNCHRONIZED_IO
] = -1,
43 [_SC_MAPPED_FILES
] = VER
,
45 [_SC_MEMLOCK_RANGE
] = VER
,
46 [_SC_MEMORY_PROTECTION
] = VER
,
47 [_SC_MESSAGE_PASSING
] = VER
,
48 [_SC_SEMAPHORES
] = VER
,
49 [_SC_SHARED_MEMORY_OBJECTS
] = VER
,
50 [_SC_AIO_LISTIO_MAX
] = -1,
52 [_SC_AIO_PRIO_DELTA_MAX
] = JT_ZERO
, /* ?? */
53 [_SC_DELAYTIMER_MAX
] = _POSIX_DELAYTIMER_MAX
,
54 [_SC_MQ_OPEN_MAX
] = -1,
55 [_SC_MQ_PRIO_MAX
] = JT_MQ_PRIO_MAX
,
57 [_SC_PAGE_SIZE
] = JT_PAGE_SIZE
,
58 [_SC_RTSIG_MAX
] = _NSIG
- 1 - 31 - 3,
59 [_SC_SEM_NSEMS_MAX
] = SEM_NSEMS_MAX
,
60 [_SC_SEM_VALUE_MAX
] = JT_SEM_VALUE_MAX
,
61 [_SC_SIGQUEUE_MAX
] = -1,
63 [_SC_BC_BASE_MAX
] = _POSIX2_BC_BASE_MAX
,
64 [_SC_BC_DIM_MAX
] = _POSIX2_BC_DIM_MAX
,
65 [_SC_BC_SCALE_MAX
] = _POSIX2_BC_SCALE_MAX
,
66 [_SC_BC_STRING_MAX
] = _POSIX2_BC_STRING_MAX
,
67 [_SC_COLL_WEIGHTS_MAX
] = COLL_WEIGHTS_MAX
,
68 [_SC_EXPR_NEST_MAX
] = -1,
70 [_SC_RE_DUP_MAX
] = RE_DUP_MAX
,
71 [_SC_2_VERSION
] = VER
,
74 [_SC_2_FORT_DEV
] = -1,
75 [_SC_2_FORT_RUN
] = -1,
77 [_SC_2_LOCALEDEF
] = -1,
78 [_SC_IOV_MAX
] = IOV_MAX
,
80 [_SC_THREAD_SAFE_FUNCTIONS
] = VER
,
81 [_SC_GETGR_R_SIZE_MAX
] = -1,
82 [_SC_GETPW_R_SIZE_MAX
] = -1,
83 [_SC_LOGIN_NAME_MAX
] = 256,
84 [_SC_TTY_NAME_MAX
] = TTY_NAME_MAX
,
85 [_SC_THREAD_DESTRUCTOR_ITERATIONS
] = PTHREAD_DESTRUCTOR_ITERATIONS
,
86 [_SC_THREAD_KEYS_MAX
] = PTHREAD_KEYS_MAX
,
87 [_SC_THREAD_STACK_MIN
] = PTHREAD_STACK_MIN
,
88 [_SC_THREAD_THREADS_MAX
] = -1,
89 [_SC_THREAD_ATTR_STACKADDR
] = VER
,
90 [_SC_THREAD_ATTR_STACKSIZE
] = VER
,
91 [_SC_THREAD_PRIORITY_SCHEDULING
] = VER
,
92 [_SC_THREAD_PRIO_INHERIT
] = -1,
93 [_SC_THREAD_PRIO_PROTECT
] = -1,
94 [_SC_THREAD_PROCESS_SHARED
] = VER
,
95 [_SC_NPROCESSORS_CONF
] = JT_NPROCESSORS_CONF
,
96 [_SC_NPROCESSORS_ONLN
] = JT_NPROCESSORS_ONLN
,
97 [_SC_PHYS_PAGES
] = JT_PHYS_PAGES
,
98 [_SC_AVPHYS_PAGES
] = JT_AVPHYS_PAGES
,
99 [_SC_ATEXIT_MAX
] = -1,
101 [_SC_XOPEN_VERSION
] = _XOPEN_VERSION
,
102 [_SC_XOPEN_XCU_VERSION
] = _XOPEN_VERSION
,
103 [_SC_XOPEN_UNIX
] = 1,
104 [_SC_XOPEN_CRYPT
] = -1,
105 [_SC_XOPEN_ENH_I18N
] = 1,
107 [_SC_2_CHAR_TERM
] = -1,
109 [_SC_XOPEN_XPG2
] = -1,
110 [_SC_XOPEN_XPG3
] = -1,
111 [_SC_XOPEN_XPG4
] = -1,
113 [_SC_XBS5_ILP32_OFF32
] = -1,
114 [_SC_XBS5_ILP32_OFFBIG
] = sizeof(long)==4 ? 1 : JT_ZERO
,
115 [_SC_XBS5_LP64_OFF64
] = sizeof(long)==8 ? 1 : JT_ZERO
,
116 [_SC_XBS5_LPBIG_OFFBIG
] = -1,
117 [_SC_XOPEN_LEGACY
] = -1,
118 [_SC_XOPEN_REALTIME
] = -1,
119 [_SC_XOPEN_REALTIME_THREADS
] = -1,
120 [_SC_ADVISORY_INFO
] = VER
,
121 [_SC_BARRIERS
] = VER
,
122 [_SC_CLOCK_SELECTION
] = VER
,
124 [_SC_THREAD_CPUTIME
] = VER
,
125 [_SC_MONOTONIC_CLOCK
] = VER
,
126 [_SC_READER_WRITER_LOCKS
] = VER
,
127 [_SC_SPIN_LOCKS
] = VER
,
131 [_SC_SPORADIC_SERVER
] = -1,
132 [_SC_THREAD_SPORADIC_SERVER
] = -1,
133 [_SC_TIMEOUTS
] = VER
,
134 [_SC_TYPED_MEMORY_OBJECTS
] = -1,
136 [_SC_2_PBS_ACCOUNTING
] = -1,
137 [_SC_2_PBS_LOCATE
] = -1,
138 [_SC_2_PBS_MESSAGE
] = -1,
139 [_SC_2_PBS_TRACK
] = -1,
140 [_SC_SYMLOOP_MAX
] = SYMLOOP_MAX
,
141 [_SC_STREAMS
] = JT_ZERO
,
142 [_SC_2_PBS_CHECKPOINT
] = -1,
143 [_SC_V6_ILP32_OFF32
] = -1,
144 [_SC_V6_ILP32_OFFBIG
] = sizeof(long)==4 ? 1 : JT_ZERO
,
145 [_SC_V6_LP64_OFF64
] = sizeof(long)==8 ? 1 : JT_ZERO
,
146 [_SC_V6_LPBIG_OFFBIG
] = -1,
147 [_SC_HOST_NAME_MAX
] = HOST_NAME_MAX
,
149 [_SC_TRACE_EVENT_FILTER
] = -1,
150 [_SC_TRACE_INHERIT
] = -1,
151 [_SC_TRACE_LOG
] = -1,
154 [_SC_RAW_SOCKETS
] = VER
,
155 [_SC_V7_ILP32_OFF32
] = -1,
156 [_SC_V7_ILP32_OFFBIG
] = sizeof(long)==4 ? 1 : JT_ZERO
,
157 [_SC_V7_LP64_OFF64
] = sizeof(long)==8 ? 1 : JT_ZERO
,
158 [_SC_V7_LPBIG_OFFBIG
] = -1,
159 [_SC_SS_REPL_MAX
] = -1,
160 [_SC_TRACE_EVENT_NAME_MAX
] = -1,
161 [_SC_TRACE_NAME_MAX
] = -1,
162 [_SC_TRACE_SYS_MAX
] = -1,
163 [_SC_TRACE_USER_EVENT_MAX
] = -1,
164 [_SC_XOPEN_STREAMS
] = JT_ZERO
,
165 [_SC_THREAD_ROBUST_PRIO_INHERIT
] = -1,
166 [_SC_THREAD_ROBUST_PRIO_PROTECT
] = -1,
169 if (name
>= sizeof(values
)/sizeof(values
[0]) || !values
[name
]) {
172 } else if (values
[name
] >= -1) {
174 } else if (values
[name
] < -256) {
176 getrlimit(values
[name
]&16383, &lim
);
177 return lim
.rlim_cur
> LONG_MAX
? LONG_MAX
: lim
.rlim_cur
;
180 switch ((unsigned char)values
[name
]) {
182 return _POSIX_VERSION
;
183 case JT_ARG_MAX
& 255:
185 case JT_MQ_PRIO_MAX
& 255:
187 case JT_PAGE_SIZE
& 255:
189 case JT_SEM_VALUE_MAX
& 255:
190 return SEM_VALUE_MAX
;
191 case JT_NPROCESSORS_CONF
& 255:
192 case JT_NPROCESSORS_ONLN
& 255: ;
193 unsigned char set
[128] = {1};
195 __syscall(SYS_sched_getaffinity
, 0, sizeof set
, set
);
196 for (i
=cnt
=0; i
<sizeof set
; i
++)
197 for (; set
[i
]; set
[i
]&=set
[i
]-1, cnt
++);
199 case JT_PHYS_PAGES
& 255:
200 case JT_AVPHYS_PAGES
& 255: ;
201 unsigned long long mem
;
202 int __lsysinfo(struct sysinfo
*);
205 if (!si
.mem_unit
) si
.mem_unit
= 1;
206 if (name
==_SC_PHYS_PAGES
) mem
= si
.totalram
;
207 else mem
= si
.freeram
+ si
.bufferram
;
210 return (mem
> LONG_MAX
) ? LONG_MAX
: mem
;