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