]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/sysconf.cc
73c44fe362f690657b476fd27476d19823d31e63
[newlib-cygwin.git] / winsup / cygwin / sysconf.cc
1 /* sysconf.cc
2
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2009, 2010, 2011 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #include "winsup.h"
13 #include <unistd.h>
14 #include <sys/sysinfo.h>
15 #include "cygerrno.h"
16 #include "security.h"
17 #include "path.h"
18 #include "fhandler.h"
19 #include "dtable.h"
20 #include "pinfo.h"
21 #include "ntdll.h"
22
23 static long
24 get_open_max (int in)
25 {
26 long max = getdtablesize ();
27 if (max < OPEN_MAX)
28 max = OPEN_MAX;
29 return max;
30 }
31
32 static long
33 get_page_size (int in)
34 {
35 return getpagesize ();
36 }
37
38 static long
39 get_nproc_values (int in)
40 {
41 NTSTATUS ret;
42 SYSTEM_BASIC_INFORMATION sbi;
43 if ((ret = NtQuerySystemInformation (SystemBasicInformation, (PVOID) &sbi,
44 sizeof sbi, NULL)) != STATUS_SUCCESS)
45 {
46 __seterrno_from_nt_status (ret);
47 debug_printf ("NtQuerySystemInformation: ret %d, Dos(ret) %E",
48 ret);
49 return -1;
50 }
51 switch (in)
52 {
53 case _SC_NPROCESSORS_CONF:
54 return sbi.NumberProcessors;
55 case _SC_NPROCESSORS_ONLN:
56 {
57 int i = 0;
58 do
59 if (sbi.ActiveProcessors & 1)
60 i++;
61 while (sbi.ActiveProcessors >>= 1);
62 return i;
63 }
64 case _SC_PHYS_PAGES:
65 return sbi.NumberOfPhysicalPages
66 / (getpagesize () / getsystempagesize ());
67 }
68 return -1;
69 }
70
71 static long
72 get_avphys (int in)
73 {
74 NTSTATUS ret;
75 SYSTEM_PERFORMANCE_INFORMATION spi;
76 if ((ret = NtQuerySystemInformation (SystemPerformanceInformation,
77 (PVOID) &spi, sizeof spi, NULL))
78 != STATUS_SUCCESS)
79 {
80 __seterrno_from_nt_status (ret);
81 debug_printf ("NtQuerySystemInformation: ret %d, Dos(ret) %E",
82 ret);
83 return -1;
84 }
85 return spi.AvailablePages / (getpagesize () / getsystempagesize ());
86 }
87
88 enum sc_type { nsup, cons, func };
89
90 static struct
91 {
92 sc_type type;
93 union
94 {
95 long c;
96 long (*f)(int);
97 };
98 } sca[] =
99 {
100 {cons, {c:ARG_MAX}}, /* 0, _SC_ARG_MAX */
101 {cons, {c:CHILD_MAX}}, /* 1, _SC_CHILD_MAX */
102 {cons, {c:CLOCKS_PER_SEC}}, /* 2, _SC_CLK_TCK */
103 {cons, {c:NGROUPS_MAX}}, /* 3, _SC_NGROUPS_MAX */
104 {func, {f:get_open_max}}, /* 4, _SC_OPEN_MAX */
105 {cons, {c:_POSIX_JOB_CONTROL}}, /* 5, _SC_JOB_CONTROL */
106 {cons, {c:_POSIX_SAVED_IDS}}, /* 6, _SC_SAVED_IDS */
107 {cons, {c:_POSIX_VERSION}}, /* 7, _SC_VERSION */
108 {func, {f:get_page_size}}, /* 8, _SC_PAGESIZE */
109 {func, {f:get_nproc_values}}, /* 9, _SC_NPROCESSORS_CONF */
110 {func, {f:get_nproc_values}}, /* 10, _SC_NPROCESSORS_ONLN */
111 {func, {f:get_nproc_values}}, /* 11, _SC_PHYS_PAGES */
112 {func, {f:get_avphys}}, /* 12, _SC_AVPHYS_PAGES */
113 {cons, {c:MQ_OPEN_MAX}}, /* 13, _SC_MQ_OPEN_MAX */
114 {cons, {c:MQ_PRIO_MAX}}, /* 14, _SC_MQ_PRIO_MAX */
115 {cons, {c:RTSIG_MAX}}, /* 15, _SC_RTSIG_MAX */
116 {cons, {c:-1L}}, /* 16, _SC_SEM_NSEMS_MAX */
117 {cons, {c:SEM_VALUE_MAX}}, /* 17, _SC_SEM_VALUE_MAX */
118 {cons, {c:SIGQUEUE_MAX}}, /* 18, _SC_SIGQUEUE_MAX */
119 {cons, {c:TIMER_MAX}}, /* 19, _SC_TIMER_MAX */
120 {nsup, {c:0}}, /* 20, _SC_TZNAME_MAX */
121 {cons, {c:-1L}}, /* 21, _SC_ASYNCHRONOUS_IO */
122 {cons, {c:_POSIX_FSYNC}}, /* 22, _SC_FSYNC */
123 {cons, {c:_POSIX_MAPPED_FILES}}, /* 23, _SC_MAPPED_FILES */
124 {cons, {c:-1L}}, /* 24, _SC_MEMLOCK */
125 {cons, {c:_POSIX_MEMLOCK_RANGE}}, /* 25, _SC_MEMLOCK_RANGE */
126 {cons, {c:_POSIX_MEMORY_PROTECTION}}, /* 26, _SC_MEMORY_PROTECTION */
127 {cons, {c:_POSIX_MESSAGE_PASSING}}, /* 27, _SC_MESSAGE_PASSING */
128 {cons, {c:-1L}}, /* 28, _SC_PRIORITIZED_IO */
129 {cons, {c:_POSIX_REALTIME_SIGNALS}}, /* 29, _SC_REALTIME_SIGNALS */
130 {cons, {c:_POSIX_SEMAPHORES}}, /* 30, _SC_SEMAPHORES */
131 {cons, {c:_POSIX_SHARED_MEMORY_OBJECTS}}, /* 31, _SC_SHARED_MEMORY_OBJECTS */
132 {cons, {c:_POSIX_SYNCHRONIZED_IO}}, /* 32, _SC_SYNCHRONIZED_IO */
133 {cons, {c:_POSIX_TIMERS}}, /* 33, _SC_TIMERS */
134 {nsup, {c:0}}, /* 34, _SC_AIO_LISTIO_MAX */
135 {nsup, {c:0}}, /* 35, _SC_AIO_MAX */
136 {nsup, {c:0}}, /* 36, _SC_AIO_PRIO_DELTA_MAX */
137 {nsup, {c:0}}, /* 37, _SC_DELAYTIMER_MAX */
138 {cons, {c:PTHREAD_KEYS_MAX}}, /* 38, _SC_THREAD_KEYS_MAX */
139 {cons, {c:PTHREAD_STACK_MIN}}, /* 39, _SC_THREAD_STACK_MIN */
140 {cons, {c:-1L}}, /* 40, _SC_THREAD_THREADS_MAX */
141 {cons, {c:TTY_NAME_MAX}}, /* 41, _SC_TTY_NAME_MAX */
142 {cons, {c:_POSIX_THREADS}}, /* 42, _SC_THREADS */
143 {cons, {c:_POSIX_THREAD_ATTR_STACKADDR}},/* 43, _SC_THREAD_ATTR_STACKADDR */
144 {cons, {c:_POSIX_THREAD_ATTR_STACKSIZE}},/* 44, _SC_THREAD_ATTR_STACKSIZE */
145 {cons, {c:_POSIX_THREAD_PRIORITY_SCHEDULING}}, /* 45, _SC_THREAD_PRIORITY_SCHEDULING */
146 {cons, {c:-1L}}, /* 46, _SC_THREAD_PRIO_INHERIT */
147 {cons, {c:-1L}}, /* 47, _SC_THREAD_PRIO_PROTECT */
148 {cons, {c:_POSIX_THREAD_PROCESS_SHARED}}, /* 48, _SC_THREAD_PROCESS_SHARED */
149 {cons, {c:_POSIX_THREAD_SAFE_FUNCTIONS}}, /* 49, _SC_THREAD_SAFE_FUNCTIONS */
150 {cons, {c:16384L}}, /* 50, _SC_GETGR_R_SIZE_MAX */
151 {cons, {c:16384L}}, /* 51, _SC_GETPW_R_SIZE_MAX */
152 {cons, {c:LOGIN_NAME_MAX}}, /* 52, _SC_LOGIN_NAME_MAX */
153 {cons, {c:PTHREAD_DESTRUCTOR_ITERATIONS}}, /* 53, _SC_THREAD_DESTRUCTOR_ITERATIONS */
154 {cons, {c:_POSIX_ADVISORY_INFO}}, /* 54, _SC_ADVISORY_INFO */
155 {cons, {c:ATEXIT_MAX}}, /* 55, _SC_ATEXIT_MAX */
156 {cons, {c:-1L}}, /* 56, _SC_BARRIERS */
157 {cons, {c:BC_BASE_MAX}}, /* 57, _SC_BC_BASE_MAX */
158 {cons, {c:BC_DIM_MAX}}, /* 58, _SC_BC_DIM_MAX */
159 {cons, {c:BC_SCALE_MAX}}, /* 59, _SC_BC_SCALE_MAX */
160 {cons, {c:BC_STRING_MAX}}, /* 60, _SC_BC_STRING_MAX */
161 {cons, {c:-1L}}, /* 61, _SC_CLOCK_SELECTION */
162 {nsup, {c:0}}, /* 62, _SC_COLL_WEIGHTS_MAX */
163 {cons, {c:-1L}}, /* 63, _SC_CPUTIME */
164 {cons, {c:EXPR_NEST_MAX}}, /* 64, _SC_EXPR_NEST_MAX */
165 {cons, {c:HOST_NAME_MAX}}, /* 65, _SC_HOST_NAME_MAX */
166 {cons, {c:IOV_MAX}}, /* 66, _SC_IOV_MAX */
167 {cons, {c:_POSIX_IPV6}}, /* 67, _SC_IPV6 */
168 {cons, {c:LINE_MAX}}, /* 68, _SC_LINE_MAX */
169 {cons, {c:_POSIX_MONOTONIC_CLOCK}}, /* 69, _SC_MONOTONIC_CLOCK */
170 {cons, {c:_POSIX_RAW_SOCKETS}}, /* 70, _SC_RAW_SOCKETS */
171 {cons, {c:_POSIX_READER_WRITER_LOCKS}}, /* 71, _SC_READER_WRITER_LOCKS */
172 {cons, {c:_POSIX_REGEXP}}, /* 72, _SC_REGEXP */
173 {cons, {c:RE_DUP_MAX}}, /* 73, _SC_RE_DUP_MAX */
174 {cons, {c:_POSIX_SHELL}}, /* 74, _SC_SHELL */
175 {cons, {c:-1L}}, /* 75, _SC_SPAWN */
176 {cons, {c:_POSIX_SPIN_LOCKS}}, /* 76, _SC_SPIN_LOCKS */
177 {cons, {c:-1L}}, /* 77, _SC_SPORADIC_SERVER */
178 {nsup, {c:0}}, /* 78, _SC_SS_REPL_MAX */
179 {cons, {c:SYMLOOP_MAX}}, /* 79, _SC_SYMLOOP_MAX */
180 {cons, {c:-1L}}, /* 80, _SC_THREAD_CPUTIME */
181 {cons, {c:-1L}}, /* 81, _SC_THREAD_SPORADIC_SERVER */
182 {cons, {c:-1L}}, /* 82, _SC_TIMEOUTS */
183 {cons, {c:-1L}}, /* 83, _SC_TRACE */
184 {cons, {c:-1L}}, /* 84, _SC_TRACE_EVENT_FILTER */
185 {nsup, {c:0}}, /* 85, _SC_TRACE_EVENT_NAME_MAX */
186 {cons, {c:-1L}}, /* 86, _SC_TRACE_INHERIT */
187 {cons, {c:-1L}}, /* 87, _SC_TRACE_LOG */
188 {nsup, {c:0}}, /* 88, _SC_TRACE_NAME_MAX */
189 {nsup, {c:0}}, /* 89, _SC_TRACE_SYS_MAX */
190 {nsup, {c:0}}, /* 90, _SC_TRACE_USER_EVENT_MAX */
191 {cons, {c:-1L}}, /* 91, _SC_TYPED_MEMORY_OBJECTS */
192 {cons, {c:-1L}}, /* 92, _SC_V6_ILP32_OFF32 */
193 {cons, {c:_POSIX_V6_ILP32_OFFBIG}}, /* 93, _SC_V6_ILP32_OFFBIG */
194 {cons, {c:-1L}}, /* 94, _SC_V6_LP64_OFF64 */
195 {cons, {c:-1L}}, /* 95, _SC_V6_LPBIG_OFFBIG */
196 {cons, {c:_XOPEN_CRYPT}}, /* 96, _SC_XOPEN_CRYPT */
197 {cons, {c:_XOPEN_ENH_I18N}}, /* 97, _SC_XOPEN_ENH_I18N */
198 {cons, {c:-1L}}, /* 98, _SC_XOPEN_LEGACY */
199 {cons, {c:-1L}}, /* 99, _SC_XOPEN_REALTIME */
200 {cons, {c:STREAM_MAX}}, /* 100, _SC_STREAM_MAX */
201 {cons, {c:_POSIX_PRIORITY_SCHEDULING}}, /* 101, _SC_PRIORITY_SCHEDULING */
202 {cons, {c:-1L}}, /* 102, _SC_XOPEN_REALTIME_THREADS */
203 {cons, {c:_XOPEN_SHM}}, /* 103, _SC_XOPEN_SHM */
204 {cons, {c:-1L}}, /* 104, _SC_XOPEN_STREAMS */
205 {cons, {c:-1L}}, /* 105, _SC_XOPEN_UNIX */
206 {cons, {c:_XOPEN_VERSION}}, /* 106, _SC_XOPEN_VERSION */
207 {cons, {c:_POSIX2_CHAR_TERM}}, /* 107, _SC_2_CHAR_TERM */
208 {cons, {c:_POSIX2_C_BIND}}, /* 108, _SC_2_C_BIND */
209 {cons, {c:_POSIX2_C_BIND}}, /* 109, _SC_2_C_DEV */
210 {cons, {c:-1L}}, /* 110, _SC_2_FORT_DEV */
211 {cons, {c:-1L}}, /* 111, _SC_2_FORT_RUN */
212 {cons, {c:-1L}}, /* 112, _SC_2_LOCALEDEF */
213 {cons, {c:-1L}}, /* 113, _SC_2_PBS */
214 {cons, {c:-1L}}, /* 114, _SC_2_PBS_ACCOUNTING */
215 {cons, {c:-1L}}, /* 115, _SC_2_PBS_CHECKPOINT */
216 {cons, {c:-1L}}, /* 116, _SC_2_PBS_LOCATE */
217 {cons, {c:-1L}}, /* 117, _SC_2_PBS_MESSAGE */
218 {cons, {c:-1L}}, /* 118, _SC_2_PBS_TRACK */
219 {cons, {c:_POSIX2_SW_DEV}}, /* 119, _SC_2_SW_DEV */
220 {cons, {c:_POSIX2_UPE}}, /* 120, _SC_2_UPE */
221 {cons, {c:_POSIX2_VERSION}}, /* 121, _SC_2_VERSION */
222 };
223
224 #define SC_MIN _SC_ARG_MAX
225 #define SC_MAX _SC_2_VERSION
226
227 /* sysconf: POSIX 4.8.1.1 */
228 /* Allows a portable app to determine quantities of resources or
229 presence of an option at execution time. */
230 long int
231 sysconf (int in)
232 {
233 if (in >= SC_MIN && in <= SC_MAX)
234 {
235 switch (sca[in].type)
236 {
237 case nsup:
238 break;
239 case cons:
240 return sca[in].c;
241 case func:
242 return sca[in].f (in);
243 }
244 }
245 /* Unimplemented sysconf name or invalid option value. */
246 set_errno (EINVAL);
247 return -1L;
248 }
249
250 #define ls(s) sizeof(s),s
251
252 static struct
253 {
254 size_t l;
255 const char *s;
256 } csa[] =
257 {
258 {ls ("/bin:/usr/bin")}, /* _CS_PATH */
259 {0, NULL}, /* _CS_POSIX_V6_ILP32_OFF32_CFLAGS */
260 {0, NULL}, /* _CS_POSIX_V6_ILP32_OFF32_LDFLAGS */
261 {0, NULL}, /* _CS_POSIX_V6_ILP32_OFF32_LIBS */
262 {0, NULL}, /* _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS */
263 {ls ("")}, /* _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS */
264 {ls ("")}, /* _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS */
265 {ls ("")}, /* _CS_POSIX_V6_ILP32_OFFBIG_LIBS */
266 {ls ("")}, /* _CS_XBS5_ILP32_OFFBIG_LINTFLAGS */
267 {0, NULL}, /* _CS_POSIX_V6_LP64_OFF64_CFLAGS */
268 {0, NULL}, /* _CS_POSIX_V6_LP64_OFF64_LDFLAGS */
269 {0, NULL}, /* _CS_POSIX_V6_LP64_OFF64_LIBS */
270 {0, NULL}, /* _CS_XBS5_LP64_OFF64_LINTFLAGS */
271 {0, NULL}, /* _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS */
272 {0, NULL}, /* _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS */
273 {0, NULL}, /* _CS_POSIX_V6_LPBIG_OFFBIG_LIBS */
274 {0, NULL}, /* _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS */
275 {ls ("POSIX_V6_ILP32_OFFBIG")}, /* _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS */
276 };
277
278 #define CS_MIN _CS_PATH
279 #define CS_MAX _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS
280
281 extern "C" size_t
282 confstr (int in, char *buf, size_t len)
283 {
284 if (in >= CS_MIN && in <= CS_MAX)
285 {
286 if (csa[in].l && len)
287 {
288 buf[0] = 0;
289 strncat (buf, csa[in].s, min (len, csa[in].l) - 1);
290 }
291 return csa[in].l;
292 }
293 /* Invalid option value. */
294 set_errno (EINVAL);
295 return 0;
296 }
297
298 extern "C" int
299 get_nprocs_conf (void)
300 {
301 return get_nproc_values (_SC_NPROCESSORS_CONF);
302 }
303
304 extern "C" int
305 get_nprocs (void)
306 {
307 return get_nproc_values (_SC_NPROCESSORS_ONLN);
308 }
309
310 extern "C" long
311 get_phys_pages (void)
312 {
313 return get_nproc_values (_SC_PHYS_PAGES);
314 }
315
316 extern "C" long
317 get_avphys_pages (void)
318 {
319 return get_avphys (_SC_AVPHYS_PAGES);
320 }
321
322 extern "C" int
323 sysinfo (struct sysinfo *info)
324 {
325 unsigned long long uptime = 0ULL, totalram = 0ULL, freeram = 0ULL,
326 totalswap = 0ULL, freeswap = 0ULL;
327 MEMORYSTATUSEX memory_status;
328 PSYSTEM_PAGEFILE_INFORMATION spi = NULL;
329 ULONG sizeof_spi = 512;
330 PSYSTEM_TIME_OF_DAY_INFORMATION stodi = NULL;
331 ULONG sizeof_stodi = sizeof (SYSTEM_TIME_OF_DAY_INFORMATION);
332 NTSTATUS ret = STATUS_SUCCESS;
333 winpids pids ((DWORD) 0);
334
335 if (!info)
336 {
337 set_errno (EFAULT);
338 return -1;
339 }
340
341 stodi = (PSYSTEM_TIME_OF_DAY_INFORMATION) malloc (sizeof_stodi);
342 ret = NtQuerySystemInformation (SystemTimeOfDayInformation, (PVOID) stodi,
343 sizeof_stodi, NULL);
344 if (NT_SUCCESS (ret))
345 uptime = (stodi->CurrentTime.QuadPart - stodi->BootTime.QuadPart) / 10000000ULL;
346 else
347 {
348 debug_printf ("NtQuerySystemInformation(SystemTimeOfDayInformation), "
349 "status %p", ret);
350 }
351
352 if (stodi)
353 free (stodi);
354
355 memory_status.dwLength = sizeof (MEMORYSTATUSEX);
356 GlobalMemoryStatusEx (&memory_status);
357 totalram = memory_status.ullTotalPhys / getsystempagesize ();
358 freeram = memory_status.ullAvailPhys / getsystempagesize ();
359
360 spi = (PSYSTEM_PAGEFILE_INFORMATION) malloc (sizeof_spi);
361 if (spi)
362 {
363 ret = NtQuerySystemInformation (SystemPagefileInformation, (PVOID) spi,
364 sizeof_spi, &sizeof_spi);
365 if (ret == STATUS_INFO_LENGTH_MISMATCH)
366 {
367 free (spi);
368 spi = (PSYSTEM_PAGEFILE_INFORMATION) malloc (sizeof_spi);
369 if (spi)
370 ret = NtQuerySystemInformation (SystemPagefileInformation,
371 (PVOID) spi, sizeof_spi, &sizeof_spi);
372 }
373 }
374 if (!spi || ret || (!ret && GetLastError () == ERROR_PROC_NOT_FOUND))
375 {
376 debug_printf ("NtQuerySystemInformation(SystemPagefileInformation), "
377 "status %p", ret);
378 totalswap = (memory_status.ullTotalPageFile - memory_status.ullTotalPhys)
379 / getsystempagesize ();
380 freeswap = (memory_status.ullAvailPageFile - memory_status.ullTotalPhys)
381 / getsystempagesize ();
382 }
383 else
384 {
385 PSYSTEM_PAGEFILE_INFORMATION spp = spi;
386 do
387 {
388 totalswap += spp->CurrentSize;
389 freeswap += spp->CurrentSize - spp->TotalUsed;
390 }
391 while (spp->NextEntryOffset
392 && (spp = (PSYSTEM_PAGEFILE_INFORMATION)
393 ((char *) spp + spp->NextEntryOffset)));
394 }
395 if (spi)
396 free (spi);
397
398 info->uptime = (long) uptime;
399 info->totalram = (unsigned long) totalram;
400 info->freeram = (unsigned long) freeram;
401 info->totalswap = (unsigned long) totalswap;
402 info->freeswap = (unsigned long) freeswap;
403 info->procs = (unsigned short) pids.npids;
404 info->mem_unit = (unsigned int) getsystempagesize ();
405
406 /* FIXME: unsupported */
407 info->loads[0] = 0UL;
408 info->loads[1] = 0UL;
409 info->loads[2] = 0UL;
410 info->sharedram = 0UL;
411 info->bufferram = 0UL;
412 info->totalhigh = 0UL;
413 info->freehigh = 0UL;
414
415 return 0;
416 }
This page took 0.058659 seconds and 4 git commands to generate.