• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef __LITEOS_A__
28 	return syscall(SYS_sysconf, name);
29 #else
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,
156 
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,
171 	};
172 
173 	if (name >= sizeof(values)/sizeof(values[0]) || !values[name]) {
174 		errno = EINVAL;
175 		return -1;
176 	} else if (values[name] >= -1) {
177 		return values[name];
178 	} else if (values[name] < -256) {
179 		struct rlimit lim;
180 		getrlimit(values[name]&16383, &lim);
181 		if (lim.rlim_cur == RLIM_INFINITY)
182 			return -1;
183 		return lim.rlim_cur > LONG_MAX ? LONG_MAX : lim.rlim_cur;
184 	}
185 
186 	switch ((unsigned char)values[name]) {
187 	case VER & 255:
188 		return _POSIX_VERSION;
189 	case JT_ARG_MAX & 255:
190 		return ARG_MAX;
191 	case JT_MQ_PRIO_MAX & 255:
192 		return MQ_PRIO_MAX;
193 	case JT_PAGE_SIZE & 255:
194 		return PAGE_SIZE;
195 	case JT_SEM_VALUE_MAX & 255:
196 		return SEM_VALUE_MAX;
197 	case JT_DELAYTIMER_MAX & 255:
198 		return DELAYTIMER_MAX;
199 	case JT_NPROCESSORS_CONF & 255:
200 		return get_nprocs_conf();
201 	case JT_NPROCESSORS_ONLN & 255:
202 		return get_nprocs();
203 	case JT_PHYS_PAGES & 255:
204 	case JT_AVPHYS_PAGES & 255: ;
205 		unsigned long long mem;
206 		struct sysinfo si;
207 		__lsysinfo(&si);
208 		if (!si.mem_unit) si.mem_unit = 1;
209 		if (name==_SC_PHYS_PAGES) mem = si.totalram;
210 		else mem = si.freeram + si.bufferram;
211 		mem *= si.mem_unit;
212 		mem /= PAGE_SIZE;
213 		return (mem > LONG_MAX) ? LONG_MAX : mem;
214 	case JT_ZERO & 255:
215 		return 0;
216 	}
217 	return values[name];
218 #endif
219 }
220