synccall: add separate exit_sem to fix thread release logic bug
[musl.git] / src / conf / sysconf.c
blob60d3e745350dc4b3a1ef298c94d4ae1f54392745
1 #include <unistd.h>
2 #include <limits.h>
3 #include <errno.h>
4 #include <sys/resource.h>
5 #include <signal.h>
6 #include <sys/sysinfo.h>
7 #include <sys/auxv.h>
8 #include "syscall.h"
9 #include "libc.h"
11 #define JT(x) (-256|(x))
12 #define VER JT(1)
13 #define JT_ARG_MAX JT(2)
14 #define JT_MQ_PRIO_MAX JT(3)
15 #define JT_PAGE_SIZE JT(4)
16 #define JT_SEM_VALUE_MAX JT(5)
17 #define JT_NPROCESSORS_CONF JT(6)
18 #define JT_NPROCESSORS_ONLN JT(7)
19 #define JT_PHYS_PAGES JT(8)
20 #define JT_AVPHYS_PAGES JT(9)
21 #define JT_ZERO JT(10)
22 #define JT_DELAYTIMER_MAX JT(11)
23 #define JT_MINSIGSTKSZ JT(12)
24 #define JT_SIGSTKSZ JT(13)
26 #define RLIM(x) (-32768|(RLIMIT_ ## x))
28 long sysconf(int name)
30 static const short values[] = {
31 [_SC_ARG_MAX] = JT_ARG_MAX,
32 [_SC_CHILD_MAX] = RLIM(NPROC),
33 [_SC_CLK_TCK] = 100,
34 [_SC_NGROUPS_MAX] = 32,
35 [_SC_OPEN_MAX] = RLIM(NOFILE),
36 [_SC_STREAM_MAX] = -1,
37 [_SC_TZNAME_MAX] = TZNAME_MAX,
38 [_SC_JOB_CONTROL] = 1,
39 [_SC_SAVED_IDS] = 1,
40 [_SC_REALTIME_SIGNALS] = VER,
41 [_SC_PRIORITY_SCHEDULING] = -1,
42 [_SC_TIMERS] = VER,
43 [_SC_ASYNCHRONOUS_IO] = VER,
44 [_SC_PRIORITIZED_IO] = -1,
45 [_SC_SYNCHRONIZED_IO] = -1,
46 [_SC_FSYNC] = VER,
47 [_SC_MAPPED_FILES] = VER,
48 [_SC_MEMLOCK] = VER,
49 [_SC_MEMLOCK_RANGE] = VER,
50 [_SC_MEMORY_PROTECTION] = VER,
51 [_SC_MESSAGE_PASSING] = VER,
52 [_SC_SEMAPHORES] = VER,
53 [_SC_SHARED_MEMORY_OBJECTS] = VER,
54 [_SC_AIO_LISTIO_MAX] = -1,
55 [_SC_AIO_MAX] = -1,
56 [_SC_AIO_PRIO_DELTA_MAX] = JT_ZERO, /* ?? */
57 [_SC_DELAYTIMER_MAX] = JT_DELAYTIMER_MAX,
58 [_SC_MQ_OPEN_MAX] = -1,
59 [_SC_MQ_PRIO_MAX] = JT_MQ_PRIO_MAX,
60 [_SC_VERSION] = VER,
61 [_SC_PAGE_SIZE] = JT_PAGE_SIZE,
62 [_SC_RTSIG_MAX] = _NSIG - 1 - 31 - 3,
63 [_SC_SEM_NSEMS_MAX] = SEM_NSEMS_MAX,
64 [_SC_SEM_VALUE_MAX] = JT_SEM_VALUE_MAX,
65 [_SC_SIGQUEUE_MAX] = -1,
66 [_SC_TIMER_MAX] = -1,
67 [_SC_BC_BASE_MAX] = _POSIX2_BC_BASE_MAX,
68 [_SC_BC_DIM_MAX] = _POSIX2_BC_DIM_MAX,
69 [_SC_BC_SCALE_MAX] = _POSIX2_BC_SCALE_MAX,
70 [_SC_BC_STRING_MAX] = _POSIX2_BC_STRING_MAX,
71 [_SC_COLL_WEIGHTS_MAX] = COLL_WEIGHTS_MAX,
72 [_SC_EXPR_NEST_MAX] = -1,
73 [_SC_LINE_MAX] = -1,
74 [_SC_RE_DUP_MAX] = RE_DUP_MAX,
75 [_SC_2_VERSION] = VER,
76 [_SC_2_C_BIND] = VER,
77 [_SC_2_C_DEV] = -1,
78 [_SC_2_FORT_DEV] = -1,
79 [_SC_2_FORT_RUN] = -1,
80 [_SC_2_SW_DEV] = -1,
81 [_SC_2_LOCALEDEF] = -1,
82 [_SC_IOV_MAX] = IOV_MAX,
83 [_SC_THREADS] = VER,
84 [_SC_THREAD_SAFE_FUNCTIONS] = VER,
85 [_SC_GETGR_R_SIZE_MAX] = -1,
86 [_SC_GETPW_R_SIZE_MAX] = -1,
87 [_SC_LOGIN_NAME_MAX] = 256,
88 [_SC_TTY_NAME_MAX] = TTY_NAME_MAX,
89 [_SC_THREAD_DESTRUCTOR_ITERATIONS] = PTHREAD_DESTRUCTOR_ITERATIONS,
90 [_SC_THREAD_KEYS_MAX] = PTHREAD_KEYS_MAX,
91 [_SC_THREAD_STACK_MIN] = PTHREAD_STACK_MIN,
92 [_SC_THREAD_THREADS_MAX] = -1,
93 [_SC_THREAD_ATTR_STACKADDR] = VER,
94 [_SC_THREAD_ATTR_STACKSIZE] = VER,
95 [_SC_THREAD_PRIORITY_SCHEDULING] = VER,
96 [_SC_THREAD_PRIO_INHERIT] = -1,
97 [_SC_THREAD_PRIO_PROTECT] = -1,
98 [_SC_THREAD_PROCESS_SHARED] = VER,
99 [_SC_NPROCESSORS_CONF] = JT_NPROCESSORS_CONF,
100 [_SC_NPROCESSORS_ONLN] = JT_NPROCESSORS_ONLN,
101 [_SC_PHYS_PAGES] = JT_PHYS_PAGES,
102 [_SC_AVPHYS_PAGES] = JT_AVPHYS_PAGES,
103 [_SC_ATEXIT_MAX] = -1,
104 [_SC_PASS_MAX] = -1,
105 [_SC_XOPEN_VERSION] = _XOPEN_VERSION,
106 [_SC_XOPEN_XCU_VERSION] = _XOPEN_VERSION,
107 [_SC_XOPEN_UNIX] = 1,
108 [_SC_XOPEN_CRYPT] = -1,
109 [_SC_XOPEN_ENH_I18N] = 1,
110 [_SC_XOPEN_SHM] = 1,
111 [_SC_2_CHAR_TERM] = -1,
112 [_SC_2_UPE] = -1,
113 [_SC_XOPEN_XPG2] = -1,
114 [_SC_XOPEN_XPG3] = -1,
115 [_SC_XOPEN_XPG4] = -1,
116 [_SC_NZERO] = NZERO,
117 [_SC_XBS5_ILP32_OFF32] = -1,
118 [_SC_XBS5_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1,
119 [_SC_XBS5_LP64_OFF64] = sizeof(long)==8 ? 1 : -1,
120 [_SC_XBS5_LPBIG_OFFBIG] = -1,
121 [_SC_XOPEN_LEGACY] = -1,
122 [_SC_XOPEN_REALTIME] = -1,
123 [_SC_XOPEN_REALTIME_THREADS] = -1,
124 [_SC_ADVISORY_INFO] = VER,
125 [_SC_BARRIERS] = VER,
126 [_SC_CLOCK_SELECTION] = VER,
127 [_SC_CPUTIME] = VER,
128 [_SC_THREAD_CPUTIME] = VER,
129 [_SC_MONOTONIC_CLOCK] = VER,
130 [_SC_READER_WRITER_LOCKS] = VER,
131 [_SC_SPIN_LOCKS] = VER,
132 [_SC_REGEXP] = 1,
133 [_SC_SHELL] = 1,
134 [_SC_SPAWN] = VER,
135 [_SC_SPORADIC_SERVER] = -1,
136 [_SC_THREAD_SPORADIC_SERVER] = -1,
137 [_SC_TIMEOUTS] = VER,
138 [_SC_TYPED_MEMORY_OBJECTS] = -1,
139 [_SC_2_PBS] = -1,
140 [_SC_2_PBS_ACCOUNTING] = -1,
141 [_SC_2_PBS_LOCATE] = -1,
142 [_SC_2_PBS_MESSAGE] = -1,
143 [_SC_2_PBS_TRACK] = -1,
144 [_SC_SYMLOOP_MAX] = SYMLOOP_MAX,
145 [_SC_STREAMS] = JT_ZERO,
146 [_SC_2_PBS_CHECKPOINT] = -1,
147 [_SC_V6_ILP32_OFF32] = -1,
148 [_SC_V6_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1,
149 [_SC_V6_LP64_OFF64] = sizeof(long)==8 ? 1 : -1,
150 [_SC_V6_LPBIG_OFFBIG] = -1,
151 [_SC_HOST_NAME_MAX] = HOST_NAME_MAX,
152 [_SC_TRACE] = -1,
153 [_SC_TRACE_EVENT_FILTER] = -1,
154 [_SC_TRACE_INHERIT] = -1,
155 [_SC_TRACE_LOG] = -1,
157 [_SC_IPV6] = VER,
158 [_SC_RAW_SOCKETS] = VER,
159 [_SC_V7_ILP32_OFF32] = -1,
160 [_SC_V7_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1,
161 [_SC_V7_LP64_OFF64] = sizeof(long)==8 ? 1 : -1,
162 [_SC_V7_LPBIG_OFFBIG] = -1,
163 [_SC_SS_REPL_MAX] = -1,
164 [_SC_TRACE_EVENT_NAME_MAX] = -1,
165 [_SC_TRACE_NAME_MAX] = -1,
166 [_SC_TRACE_SYS_MAX] = -1,
167 [_SC_TRACE_USER_EVENT_MAX] = -1,
168 [_SC_XOPEN_STREAMS] = JT_ZERO,
169 [_SC_THREAD_ROBUST_PRIO_INHERIT] = -1,
170 [_SC_THREAD_ROBUST_PRIO_PROTECT] = -1,
172 [_SC_MINSIGSTKSZ] = JT_MINSIGSTKSZ,
173 [_SC_SIGSTKSZ] = JT_SIGSTKSZ,
176 if (name >= sizeof(values)/sizeof(values[0]) || !values[name]) {
177 errno = EINVAL;
178 return -1;
179 } else if (values[name] >= -1) {
180 return values[name];
181 } else if (values[name] < -256) {
182 struct rlimit lim;
183 getrlimit(values[name]&16383, &lim);
184 if (lim.rlim_cur == RLIM_INFINITY)
185 return -1;
186 return lim.rlim_cur > LONG_MAX ? LONG_MAX : lim.rlim_cur;
189 switch ((unsigned char)values[name]) {
190 case VER & 255:
191 return _POSIX_VERSION;
192 case JT_ARG_MAX & 255:
193 return ARG_MAX;
194 case JT_MQ_PRIO_MAX & 255:
195 return MQ_PRIO_MAX;
196 case JT_PAGE_SIZE & 255:
197 return PAGE_SIZE;
198 case JT_SEM_VALUE_MAX & 255:
199 return SEM_VALUE_MAX;
200 case JT_DELAYTIMER_MAX & 255:
201 return DELAYTIMER_MAX;
202 case JT_NPROCESSORS_CONF & 255:
203 case JT_NPROCESSORS_ONLN & 255: ;
204 unsigned char set[128] = {1};
205 int i, cnt;
206 __syscall(SYS_sched_getaffinity, 0, sizeof set, set);
207 for (i=cnt=0; i<sizeof set; i++)
208 for (; set[i]; set[i]&=set[i]-1, cnt++);
209 return cnt;
210 case JT_PHYS_PAGES & 255:
211 case JT_AVPHYS_PAGES & 255: ;
212 unsigned long long mem;
213 struct sysinfo si;
214 __lsysinfo(&si);
215 if (!si.mem_unit) si.mem_unit = 1;
216 if (name==_SC_PHYS_PAGES) mem = si.totalram;
217 else mem = si.freeram + si.bufferram;
218 mem *= si.mem_unit;
219 mem /= PAGE_SIZE;
220 return (mem > LONG_MAX) ? LONG_MAX : mem;
221 case JT_MINSIGSTKSZ & 255:
222 case JT_SIGSTKSZ & 255: ;
223 long val = __getauxval(AT_MINSIGSTKSZ);
224 if (val < MINSIGSTKSZ) val = MINSIGSTKSZ;
225 if (values[name] == JT_SIGSTKSZ)
226 val += SIGSTKSZ - MINSIGSTKSZ;
227 return val;
228 case JT_ZERO & 255:
229 return 0;
231 return values[name];