sysconf.c 7.7 KB
Newer Older
R
Rich Felker 已提交
1 2 3
#include <unistd.h>
#include <limits.h>
#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"
R
Rich Felker 已提交
9

10 11 12 13 14 15 16 17
#define JT(x) (-256|(x))
#define VER JT(1)
#define JT_ARG_MAX JT(2)
#define JT_MQ_PRIO_MAX JT(3)
#define JT_PAGE_SIZE JT(4)
#define JT_SEM_VALUE_MAX JT(5)
#define JT_NPROCESSORS_CONF JT(6)
#define JT_NPROCESSORS_ONLN JT(7)
18 19
#define JT_PHYS_PAGES JT(8)
#define JT_AVPHYS_PAGES JT(9)
20
#define JT_ZERO JT(10)
21

22
#define RLIM(x) (-32768|(RLIMIT_ ## x))
R
Rich Felker 已提交
23 24 25 26

long sysconf(int name)
{
	static const short values[] = {
27
		[_SC_ARG_MAX] = JT_ARG_MAX,
28
		[_SC_CHILD_MAX] = RLIM(NPROC),
R
Rich Felker 已提交
29 30
		[_SC_CLK_TCK] = 100,
		[_SC_NGROUPS_MAX] = 32,
31
		[_SC_OPEN_MAX] = RLIM(NOFILE),
R
Rich Felker 已提交
32 33 34 35 36 37 38 39 40 41
		[_SC_STREAM_MAX] = -1,
		[_SC_TZNAME_MAX] = TZNAME_MAX,
		[_SC_JOB_CONTROL] = 1,
		[_SC_SAVED_IDS] = 1,
		[_SC_REALTIME_SIGNALS] = 1,
		[_SC_PRIORITY_SCHEDULING] = -1,
		[_SC_TIMERS] = VER,
		[_SC_ASYNCHRONOUS_IO] = VER,
		[_SC_PRIORITIZED_IO] = -1,
		[_SC_SYNCHRONIZED_IO] = -1,
42
		[_SC_FSYNC] = VER,
R
Rich Felker 已提交
43 44 45 46
		[_SC_MAPPED_FILES] = VER,
		[_SC_MEMLOCK] = VER,
		[_SC_MEMLOCK_RANGE] = VER,
		[_SC_MEMORY_PROTECTION] = VER,
47
		[_SC_MESSAGE_PASSING] = VER,
R
Rich Felker 已提交
48
		[_SC_SEMAPHORES] = VER,
49
		[_SC_SHARED_MEMORY_OBJECTS] = VER,
R
Rich Felker 已提交
50 51
		[_SC_AIO_LISTIO_MAX] = -1,
		[_SC_AIO_MAX] = -1,
52
		[_SC_AIO_PRIO_DELTA_MAX] = JT_ZERO, /* ?? */
R
Rich Felker 已提交
53 54
		[_SC_DELAYTIMER_MAX] = _POSIX_DELAYTIMER_MAX,
		[_SC_MQ_OPEN_MAX] = -1,
55
		[_SC_MQ_PRIO_MAX] = JT_MQ_PRIO_MAX,
R
Rich Felker 已提交
56
		[_SC_VERSION] = VER,
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,
R
Rich Felker 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		[_SC_SIGQUEUE_MAX] = -1,
		[_SC_TIMER_MAX] = -1,
		[_SC_BC_BASE_MAX] = _POSIX2_BC_BASE_MAX,
		[_SC_BC_DIM_MAX] = _POSIX2_BC_DIM_MAX,
		[_SC_BC_SCALE_MAX] = _POSIX2_BC_SCALE_MAX,
		[_SC_BC_STRING_MAX] = _POSIX2_BC_STRING_MAX,
		[_SC_COLL_WEIGHTS_MAX] = COLL_WEIGHTS_MAX,
		[_SC_EQUIV_CLASS_MAX] = -1, /* ?? */
		[_SC_EXPR_NEST_MAX] = -1,
		[_SC_LINE_MAX] = -1,
		[_SC_RE_DUP_MAX] = RE_DUP_MAX,
		[_SC_CHARCLASS_NAME_MAX] = -1, /* ?? */
		[_SC_2_VERSION] = VER,
		[_SC_2_C_BIND] = VER,
		[_SC_2_C_DEV] = -1,
		[_SC_2_FORT_DEV] = -1,
		[_SC_2_FORT_RUN] = -1,
		[_SC_2_SW_DEV] = -1,
		[_SC_2_LOCALEDEF] = -1,
		[_SC_PII] = -1, /* ????????? */
		[_SC_PII_XTI] = -1,
		[_SC_PII_SOCKET] = -1,
		[_SC_PII_INTERNET] = -1,
		[_SC_PII_OSI] = -1,
		[_SC_POLL] = 1,
		[_SC_SELECT] = 1,
		[_SC_IOV_MAX] = IOV_MAX,
		[_SC_PII_INTERNET_STREAM] = -1,
		[_SC_PII_INTERNET_DGRAM] = -1,
		[_SC_PII_OSI_COTS] = -1,
		[_SC_PII_OSI_CLTS] = -1,
		[_SC_PII_OSI_M] = -1,
		[_SC_T_IOV_MAX] = -1,
		[_SC_THREADS] = VER,
		[_SC_THREAD_SAFE_FUNCTIONS] = VER,
		[_SC_GETGR_R_SIZE_MAX] = -1,
		[_SC_GETPW_R_SIZE_MAX] = -1,
		[_SC_LOGIN_NAME_MAX] = 256,
		[_SC_TTY_NAME_MAX] = TTY_NAME_MAX,
100
		[_SC_THREAD_DESTRUCTOR_ITERATIONS] = PTHREAD_DESTRUCTOR_ITERATIONS,
R
Rich Felker 已提交
101
		[_SC_THREAD_KEYS_MAX] = PTHREAD_KEYS_MAX,
102
		[_SC_THREAD_STACK_MIN] = PTHREAD_STACK_MIN,
R
Rich Felker 已提交
103
		[_SC_THREAD_THREADS_MAX] = -1,
104
		[_SC_THREAD_ATTR_STACKADDR] = VER,
R
Rich Felker 已提交
105
		[_SC_THREAD_ATTR_STACKSIZE] = VER,
106
		[_SC_THREAD_PRIORITY_SCHEDULING] = VER,
R
Rich Felker 已提交
107 108 109
		[_SC_THREAD_PRIO_INHERIT] = -1,
		[_SC_THREAD_PRIO_PROTECT] = -1,
		[_SC_THREAD_PROCESS_SHARED] = VER,
110 111
		[_SC_NPROCESSORS_CONF] = JT_NPROCESSORS_CONF,
		[_SC_NPROCESSORS_ONLN] = JT_NPROCESSORS_ONLN,
112 113
		[_SC_PHYS_PAGES] = JT_PHYS_PAGES,
		[_SC_AVPHYS_PAGES] = JT_AVPHYS_PAGES,
R
Rich Felker 已提交
114 115 116 117
		[_SC_ATEXIT_MAX] = -1,
		[_SC_PASS_MAX] = -1,
		[_SC_XOPEN_VERSION] = _XOPEN_VERSION,
		[_SC_XOPEN_XCU_VERSION] = _XOPEN_VERSION,
118
		[_SC_XOPEN_UNIX] = 1,
R
Rich Felker 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		[_SC_XOPEN_CRYPT] = -1,
		[_SC_XOPEN_ENH_I18N] = 1,
		[_SC_XOPEN_SHM] = 1,
		[_SC_2_CHAR_TERM] = -1,
		[_SC_2_C_VERSION] = -1,
		[_SC_2_UPE] = -1,
		[_SC_XOPEN_XPG2] = -1,
		[_SC_XOPEN_XPG3] = -1,
		[_SC_XOPEN_XPG4] = -1,
		[_SC_CHAR_BIT] = -1,
		[_SC_CHAR_MAX] = -1,
		[_SC_CHAR_MIN] = -1,
		[_SC_INT_MAX] = -1,
		[_SC_INT_MIN] = -1,
		[_SC_LONG_BIT] = -1,
		[_SC_WORD_BIT] = -1,
		[_SC_MB_LEN_MAX] = -1,
		[_SC_NZERO] = NZERO,
		[_SC_SSIZE_MAX] = -1,
		[_SC_SCHAR_MAX] = -1,
		[_SC_SCHAR_MIN] = -1,
		[_SC_SHRT_MAX] = -1,
		[_SC_SHRT_MIN] = -1,
		[_SC_UCHAR_MAX] = -1,
		[_SC_UINT_MAX] = -1,
		[_SC_ULONG_MAX] = -1,
		[_SC_USHRT_MAX] = -1,
		[_SC_NL_ARGMAX] = -1,
		[_SC_NL_LANGMAX] = -1,
		[_SC_NL_MSGMAX] = -1,
		[_SC_NL_NMAX] = -1,
		[_SC_NL_SETMAX] = -1,
		[_SC_NL_TEXTMAX] = -1,
		[_SC_XBS5_ILP32_OFF32] = -1,
153 154
		[_SC_XBS5_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : JT_ZERO,
		[_SC_XBS5_LP64_OFF64] = sizeof(long)==8 ? 1 : JT_ZERO,
155
		[_SC_XBS5_LPBIG_OFFBIG] = -1,
R
Rich Felker 已提交
156 157 158
		[_SC_XOPEN_LEGACY] = -1,
		[_SC_XOPEN_REALTIME] = -1,
		[_SC_XOPEN_REALTIME_THREADS] = -1,
159
		[_SC_ADVISORY_INFO] = VER,
R
Rich Felker 已提交
160 161 162 163 164 165
		[_SC_BARRIERS] = VER,
		[_SC_BASE] = -1,
		[_SC_C_LANG_SUPPORT] = -1,
		[_SC_C_LANG_SUPPORT_R] = -1,
		[_SC_CLOCK_SELECTION] = VER,
		[_SC_CPUTIME] = VER,
166
		[_SC_THREAD_CPUTIME] = VER,
R
Rich Felker 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		[_SC_DEVICE_IO] = -1,
		[_SC_DEVICE_SPECIFIC] = -1,
		[_SC_DEVICE_SPECIFIC_R] = -1,
		[_SC_FD_MGMT] = -1,
		[_SC_FIFO] = -1,
		[_SC_PIPE] = -1,
		[_SC_FILE_ATTRIBUTES] = -1,
		[_SC_FILE_LOCKING] = -1,
		[_SC_FILE_SYSTEM] = -1,
		[_SC_MONOTONIC_CLOCK] = VER,
		[_SC_MULTI_PROCESS] = -1,
		[_SC_SINGLE_PROCESS] = -1,
		[_SC_NETWORKING] = -1,
		[_SC_READER_WRITER_LOCKS] = VER,
		[_SC_SPIN_LOCKS] = VER,
		[_SC_REGEXP] = 1,
		[_SC_REGEX_VERSION] = -1,
		[_SC_SHELL] = 1,
		[_SC_SIGNALS] = -1,
186
		[_SC_SPAWN] = VER,
R
Rich Felker 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200
		[_SC_SPORADIC_SERVER] = -1,
		[_SC_THREAD_SPORADIC_SERVER] = -1,
		[_SC_SYSTEM_DATABASE] = -1,
		[_SC_SYSTEM_DATABASE_R] = -1,
		[_SC_TIMEOUTS] = VER,
		[_SC_TYPED_MEMORY_OBJECTS] = -1,
		[_SC_USER_GROUPS] = -1,
		[_SC_USER_GROUPS_R] = -1,
		[_SC_2_PBS] = -1,
		[_SC_2_PBS_ACCOUNTING] = -1,
		[_SC_2_PBS_LOCATE] = -1,
		[_SC_2_PBS_MESSAGE] = -1,
		[_SC_2_PBS_TRACK] = -1,
		[_SC_SYMLOOP_MAX] = SYMLOOP_MAX,
201
		[_SC_STREAMS] = JT_ZERO,
R
Rich Felker 已提交
202 203
		[_SC_2_PBS_CHECKPOINT] = -1,
		[_SC_V6_ILP32_OFF32] = -1,
204 205
		[_SC_V6_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : JT_ZERO,
		[_SC_V6_LP64_OFF64] = sizeof(long)==8 ? 1 : JT_ZERO,
206
		[_SC_V6_LPBIG_OFFBIG] = -1,
R
Rich Felker 已提交
207 208 209 210 211 212 213 214 215
		[_SC_HOST_NAME_MAX] = HOST_NAME_MAX,
		[_SC_TRACE] = -1,
		[_SC_TRACE_EVENT_FILTER] = -1,
		[_SC_TRACE_INHERIT] = -1,
		[_SC_TRACE_LOG] = -1,

		[_SC_IPV6] = VER,
		[_SC_RAW_SOCKETS] = VER,
		[_SC_V7_ILP32_OFF32] = -1,
216 217
		[_SC_V7_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : JT_ZERO,
		[_SC_V7_LP64_OFF64] = sizeof(long)==8 ? 1 : JT_ZERO,
218
		[_SC_V7_LPBIG_OFFBIG] = -1,
R
Rich Felker 已提交
219 220 221 222 223
		[_SC_SS_REPL_MAX] = -1,
		[_SC_TRACE_EVENT_NAME_MAX] = -1,
		[_SC_TRACE_NAME_MAX] = -1,
		[_SC_TRACE_SYS_MAX] = -1,
		[_SC_TRACE_USER_EVENT_MAX] = -1,
224
		[_SC_XOPEN_STREAMS] = JT_ZERO,
R
Rich Felker 已提交
225 226 227
		[_SC_THREAD_ROBUST_PRIO_INHERIT] = -1,
		[_SC_THREAD_ROBUST_PRIO_PROTECT] = -1,
	};
228

229
	if (name >= sizeof(values)/sizeof(values[0]) || !values[name]) {
R
Rich Felker 已提交
230 231
		errno = EINVAL;
		return -1;
232 233 234
	} else if (values[name] >= -1) {
		return values[name];
	} else if (values[name] < -256) {
235 236 237
		struct rlimit lim;
		getrlimit(values[name]&16383, &lim);
		return lim.rlim_cur > LONG_MAX ? LONG_MAX : lim.rlim_cur;
238 239 240 241
	}

	switch ((unsigned char)values[name]) {
	case VER & 255:
R
Rich Felker 已提交
242
		return _POSIX_VERSION;
243 244 245 246 247
	case JT_ARG_MAX & 255:
		return ARG_MAX;
	case JT_MQ_PRIO_MAX & 255:
		return MQ_PRIO_MAX;
	case JT_PAGE_SIZE & 255:
248
		return PAGE_SIZE;
249 250 251 252
	case JT_SEM_VALUE_MAX & 255:
		return SEM_VALUE_MAX;
	case JT_NPROCESSORS_CONF & 255:
	case JT_NPROCESSORS_ONLN & 255: ;
253 254 255 256 257 258
		unsigned char set[128] = {1};
		int i, cnt;
		__syscall(SYS_sched_getaffinity, 0, sizeof set, set);
		for (i=cnt=0; i<sizeof set; i++)
			for (; set[i]; set[i]&=set[i]-1, cnt++);
		return cnt;
259 260 261 262 263 264 265 266 267 268 269 270
	case JT_PHYS_PAGES & 255:
	case JT_AVPHYS_PAGES & 255: ;
		unsigned long long mem;
		int __lsysinfo(struct sysinfo *);
		struct sysinfo si;
		__lsysinfo(&si);
		if (!si.mem_unit) si.mem_unit = 1;
		if (name==_SC_PHYS_PAGES) mem = si.totalram;
		else mem = si.freeram + si.bufferram;
		mem *= si.mem_unit;
		mem /= PAGE_SIZE;
		return (mem > LONG_MAX) ? LONG_MAX : mem;
271 272
	case JT_ZERO & 255:
		return 0;
R
Rich Felker 已提交
273
	}
274
	return values[name];
R
Rich Felker 已提交
275
}