OSDN Git Service

* Makefile.in (DLL_OFILES): Add posix_ipc.o.
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / sysconf.cc
1 /* sysconf.cc
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4    2006, 2007 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 <time.h>
15 #include <limits.h>
16 #include <ntdef.h>
17 #include "cygerrno.h"
18 #include "security.h"
19 #include "path.h"
20 #include "fhandler.h"
21 #include "dtable.h"
22 #include "cygheap.h"
23 #include "ntdll.h"
24
25 static long
26 get_open_max (int in)
27 {
28   long max = getdtablesize ();
29   if (max < OPEN_MAX)
30     max = OPEN_MAX;
31   return max;
32 }
33
34 static long
35 get_page_size (int in)
36 {
37   return getpagesize ();
38 }
39
40 static long
41 get_nproc_values (int in)
42 {
43   switch (in)
44     {
45     case _SC_NPROCESSORS_CONF:
46     case _SC_NPROCESSORS_ONLN:
47       if (!wincap.supports_smp ())
48         return 1;
49       /*FALLTHRU*/
50     case _SC_PHYS_PAGES:
51       if (wincap.supports_smp ())
52         {
53           NTSTATUS ret;
54           SYSTEM_BASIC_INFORMATION sbi;
55           if ((ret = NtQuerySystemInformation (SystemBasicInformation,
56                                                  (PVOID) &sbi,
57                                                sizeof sbi, NULL))
58                 != STATUS_SUCCESS)
59             {
60               __seterrno_from_nt_status (ret);
61               debug_printf ("NtQuerySystemInformation: ret %d, Dos(ret) %E",
62                             ret);
63               return -1;
64             }
65           switch (in)
66             {
67             case _SC_NPROCESSORS_CONF:
68              return sbi.NumberProcessors;
69             case _SC_NPROCESSORS_ONLN:
70              {
71                int i = 0;
72                do
73                  if (sbi.ActiveProcessors & 1)
74                    i++;
75                while (sbi.ActiveProcessors >>= 1);
76                return i;
77              }
78             case _SC_PHYS_PAGES:
79               return sbi.NumberOfPhysicalPages
80                      / (getpagesize () / getsystempagesize ());
81             }
82         }
83     }
84   return -1;
85 }
86
87 static long
88 get_avphys (int in)
89 {
90   if (wincap.supports_smp ())
91     {
92       NTSTATUS ret;
93       SYSTEM_PERFORMANCE_INFORMATION spi;
94       if ((ret = NtQuerySystemInformation (SystemPerformanceInformation,
95                                              (PVOID) &spi,
96                                            sizeof spi, NULL))
97             != STATUS_SUCCESS)
98         {
99           __seterrno_from_nt_status (ret);
100           debug_printf ("NtQuerySystemInformation: ret %d, Dos(ret) %E",
101                         ret);
102           return -1;
103         }
104       return spi.AvailablePages / (getpagesize () / getsystempagesize ());
105     }
106   return -1;
107 }
108
109 enum sc_type { nsup, cons, func };
110
111 static struct
112 {
113   sc_type type;
114   union
115     {
116       long c;
117       long (*f)(int);
118     };
119 } sca[] =
120 {
121   {cons, {c:ARG_MAX}},                  /*   0, _SC_ARG_MAX */
122   {cons, {c:CHILD_MAX}},                /*   1, _SC_CHILD_MAX */
123   {cons, {c:CLOCKS_PER_SEC}},           /*   2, _SC_CLK_TCK */
124   {cons, {c:NGROUPS_MAX}},              /*   3, _SC_NGROUPS_MAX */
125   {func, {f:get_open_max}},             /*   4, _SC_OPEN_MAX */
126   {cons, {c:_POSIX_JOB_CONTROL}},       /*   5, _SC_JOB_CONTROL */
127   {cons, {c:_POSIX_SAVED_IDS}},         /*   6, _SC_SAVED_IDS */
128   {cons, {c:_POSIX_VERSION}},           /*   7, _SC_VERSION */
129   {func, {f:get_page_size}},            /*   8, _SC_PAGESIZE */
130   {func, {f:get_nproc_values}},         /*   9, _SC_NPROCESSORS_CONF */
131   {func, {f:get_nproc_values}},         /*  10, _SC_NPROCESSORS_ONLN */
132   {func, {f:get_nproc_values}},         /*  11, _SC_PHYS_PAGES */
133   {func, {f:get_avphys}},               /*  12, _SC_AVPHYS_PAGES */
134   {cons, {c:MQ_OPEN_MAX}},              /*  13, _SC_MQ_OPEN_MAX */
135   {cons, {c:MQ_PRIO_MAX}},              /*  14, _SC_MQ_PRIO_MAX */
136   {cons, {c:RTSIG_MAX}},                /*  15, _SC_RTSIG_MAX */
137   {cons, {c:-1L}},                      /*  16, _SC_SEM_NSEMS_MAX */
138   {cons, {c:SEM_VALUE_MAX}},            /*  17, _SC_SEM_VALUE_MAX */
139   {cons, {c:SIGQUEUE_MAX}},             /*  18, _SC_SIGQUEUE_MAX */
140   {cons, {c:TIMER_MAX}},                /*  19, _SC_TIMER_MAX */
141   {nsup, {c:0}},                        /*  20, _SC_TZNAME_MAX */
142   {cons, {c:-1L}},                      /*  21, _SC_ASYNCHRONOUS_IO */
143   {cons, {c:_POSIX_FSYNC}},             /*  22, _SC_FSYNC */
144   {cons, {c:_POSIX_MAPPED_FILES}},      /*  23, _SC_MAPPED_FILES */
145   {cons, {c:-1L}},                      /*  24, _SC_MEMLOCK */
146   {cons, {c:_POSIX_MEMLOCK_RANGE}},     /*  25, _SC_MEMLOCK_RANGE */
147   {cons, {c:_POSIX_MEMORY_PROTECTION}}, /*  26, _SC_MEMORY_PROTECTION */
148   {cons, {c:_POSIX_MESSAGE_PASSING}},   /*  27, _SC_MESSAGE_PASSING */
149   {cons, {c:-1L}},                      /*  28, _SC_PRIORITIZED_IO */
150   {cons, {c:_POSIX_REALTIME_SIGNALS}},  /*  29, _SC_REALTIME_SIGNALS */
151   {cons, {c:_POSIX_SEMAPHORES}},        /*  30, _SC_SEMAPHORES */
152   {cons, {c:_POSIX_SHARED_MEMORY_OBJECTS}},     /*  31, _SC_SHARED_MEMORY_OBJECTS */
153   {cons, {c:_POSIX_SYNCHRONIZED_IO}},   /*  32, _SC_SYNCHRONIZED_IO */
154   {cons, {c:_POSIX_TIMERS}},            /*  33, _SC_TIMERS */
155   {nsup, {c:0}},                        /*  34, _SC_AIO_LISTIO_MAX */
156   {nsup, {c:0}},                        /*  35, _SC_AIO_MAX */
157   {nsup, {c:0}},                        /*  36, _SC_AIO_PRIO_DELTA_MAX */
158   {nsup, {c:0}},                        /*  37, _SC_DELAYTIMER_MAX */
159   {cons, {c:PTHREAD_KEYS_MAX}},         /*  38, _SC_THREAD_KEYS_MAX */
160   {cons, {c:PTHREAD_STACK_MIN}},        /*  39, _SC_THREAD_STACK_MIN */
161   {cons, {c:-1L}},                      /*  40, _SC_THREAD_THREADS_MAX */
162   {cons, {c:TTY_NAME_MAX}},             /*  41, _SC_TTY_NAME_MAX */
163   {cons, {c:_POSIX_THREADS}},           /*  42, _SC_THREADS */
164   {cons, {c:-1L}},                      /*  43, _SC_THREAD_ATTR_STACKADDR */
165   {cons, {c:_POSIX_THREAD_ATTR_STACKSIZE}},     /*  44, _SC_THREAD_ATTR_STACKSIZE */
166   {cons, {c:_POSIX_THREAD_PRIORITY_SCHEDULING}},        /*  45, _SC_THREAD_PRIORITY_SCHEDULING */
167   {cons, {c:-1L}},                      /*  46, _SC_THREAD_PRIO_INHERIT */
168   {cons, {c:-1L}},                      /*  47, _SC_THREAD_PRIO_PROTECT */
169   {cons, {c:_POSIX_THREAD_PROCESS_SHARED}},     /*  48, _SC_THREAD_PROCESS_SHARED */
170   {cons, {c:_POSIX_THREAD_SAFE_FUNCTIONS}},     /*  49, _SC_THREAD_SAFE_FUNCTIONS */
171   {cons, {c:16384L}},                   /*  50, _SC_GETGR_R_SIZE_MAX */
172   {cons, {c:16384L}},                   /*  51, _SC_GETPW_R_SIZE_MAX */
173   {cons, {c:LOGIN_NAME_MAX}},           /*  52, _SC_LOGIN_NAME_MAX */
174   {cons, {c:PTHREAD_DESTRUCTOR_ITERATIONS}},    /*  53, _SC_THREAD_DESTRUCTOR_ITERATIONS */
175   {cons, {c:_POSIX_ADVISORY_INFO}},     /*  54, _SC_ADVISORY_INFO */
176   {cons, {c:ATEXIT_MAX}},               /*  55, _SC_ATEXIT_MAX */
177   {cons, {c:-1L}},                      /*  56, _SC_BARRIERS */
178   {cons, {c:BC_BASE_MAX}},              /*  57, _SC_BC_BASE_MAX */
179   {cons, {c:BC_DIM_MAX}},               /*  58, _SC_BC_DIM_MAX */
180   {cons, {c:BC_SCALE_MAX}},             /*  59, _SC_BC_SCALE_MAX */
181   {cons, {c:BC_STRING_MAX}},            /*  60, _SC_BC_STRING_MAX */
182   {cons, {c:-1L}},                      /*  61, _SC_CLOCK_SELECTION */
183   {nsup, {c:0}},                        /*  62, _SC_COLL_WEIGHTS_MAX */
184   {cons, {c:-1L}},                      /*  63, _SC_CPUTIME */
185   {cons, {c:EXPR_NEST_MAX}},            /*  64, _SC_EXPR_NEST_MAX */
186   {cons, {c:HOST_NAME_MAX}},            /*  65, _SC_HOST_NAME_MAX */
187   {cons, {c:IOV_MAX}},                  /*  66, _SC_IOV_MAX */
188   {cons, {c:_POSIX_IPV6}},              /*  67, _SC_IPV6 */
189   {cons, {c:LINE_MAX}},                 /*  68, _SC_LINE_MAX */
190   {cons, {c:-1L}},                      /*  69, _SC_MONOTONIC_CLOCK */
191   {cons, {c:_POSIX_RAW_SOCKETS}},       /*  70, _SC_RAW_SOCKETS */
192   {cons, {c:_POSIX_READER_WRITER_LOCKS}},       /*  71, _SC_READER_WRITER_LOCKS */
193   {cons, {c:_POSIX_REGEXP}},            /*  72, _SC_REGEXP */
194   {cons, {c:RE_DUP_MAX}},               /*  73, _SC_RE_DUP_MAX */
195   {cons, {c:_POSIX_SHELL}},             /*  74, _SC_SHELL */
196   {cons, {c:-1L}},                      /*  75, _SC_SPAWN */
197   {cons, {c:-1L}},                      /*  76, _SC_SPIN_LOCKS */
198   {cons, {c:-1L}},                      /*  77, _SC_SPORADIC_SERVER */
199   {nsup, {c:0}},                        /*  78, _SC_SS_REPL_MAX */
200   {cons, {c:SYMLOOP_MAX}},              /*  79, _SC_SYMLOOP_MAX */
201   {cons, {c:-1L}},                      /*  80, _SC_THREAD_CPUTIME */
202   {cons, {c:-1L}},                      /*  81, _SC_THREAD_SPORADIC_SERVER */
203   {cons, {c:-1L}},                      /*  82, _SC_TIMEOUTS */
204   {cons, {c:-1L}},                      /*  83, _SC_TRACE */
205   {cons, {c:-1L}},                      /*  84, _SC_TRACE_EVENT_FILTER */
206   {nsup, {c:0}},                        /*  85, _SC_TRACE_EVENT_NAME_MAX */
207   {cons, {c:-1L}},                      /*  86, _SC_TRACE_INHERIT */
208   {cons, {c:-1L}},                      /*  87, _SC_TRACE_LOG */
209   {nsup, {c:0}},                        /*  88, _SC_TRACE_NAME_MAX */
210   {nsup, {c:0}},                        /*  89, _SC_TRACE_SYS_MAX */
211   {nsup, {c:0}},                        /*  90, _SC_TRACE_USER_EVENT_MAX */
212   {cons, {c:-1L}},                      /*  91, _SC_TYPED_MEMORY_OBJECTS */
213   {cons, {c:-1L}},                      /*  92, _SC_V6_ILP32_OFF32 */
214   {cons, {c:_POSIX_V6_ILP32_OFFBIG}},   /*  93, _SC_V6_ILP32_OFFBIG */
215   {cons, {c:-1L}},                      /*  94, _SC_V6_LP64_OFF64 */
216   {cons, {c:-1L}},                      /*  95, _SC_V6_LPBIG_OFFBIG */
217   {cons, {c:_XOPEN_CRYPT}},             /*  96, _SC_XOPEN_CRYPT */
218   {cons, {c:_XOPEN_ENH_I18N}},          /*  97, _SC_XOPEN_ENH_I18N */
219   {cons, {c:-1L}},                      /*  98, _SC_XOPEN_LEGACY */
220   {cons, {c:-1L}},                      /*  99, _SC_XOPEN_REALTIME */
221   {cons, {c:STREAM_MAX}},               /* 100, _SC_STREAM_MAX */
222   {cons, {c:_POSIX_PRIORITY_SCHEDULING}},       /* 101, _SC_PRIORITY_SCHEDULING */
223   {cons, {c:-1L}},                      /* 102, _SC_XOPEN_REALTIME_THREADS */
224   {cons, {c:_XOPEN_SHM}},               /* 103, _SC_XOPEN_SHM */
225   {cons, {c:-1L}},                      /* 104, _SC_XOPEN_STREAMS */
226   {cons, {c:-1L}},                      /* 105, _SC_XOPEN_UNIX */
227   {cons, {c:_XOPEN_VERSION}},           /* 106, _SC_XOPEN_VERSION */
228   {cons, {c:_POSIX2_CHAR_TERM}},        /* 107, _SC_2_CHAR_TERM */
229   {cons, {c:_POSIX2_C_BIND}},           /* 108, _SC_2_C_BIND */
230   {cons, {c:_POSIX2_C_BIND}},           /* 109, _SC_2_C_DEV */
231   {cons, {c:-1L}},                      /* 110, _SC_2_FORT_DEV */
232   {cons, {c:-1L}},                      /* 111, _SC_2_FORT_RUN */
233   {cons, {c:-1L}},                      /* 112, _SC_2_LOCALEDEF */
234   {cons, {c:-1L}},                      /* 113, _SC_2_PBS */
235   {cons, {c:-1L}},                      /* 114, _SC_2_PBS_ACCOUNTING */
236   {cons, {c:-1L}},                      /* 115, _SC_2_PBS_CHECKPOINT */
237   {cons, {c:-1L}},                      /* 116, _SC_2_PBS_LOCATE */
238   {cons, {c:-1L}},                      /* 117, _SC_2_PBS_MESSAGE */
239   {cons, {c:-1L}},                      /* 118, _SC_2_PBS_TRACK */
240   {cons, {c:_POSIX2_SW_DEV}},           /* 119, _SC_2_SW_DEV */
241   {cons, {c:_POSIX2_UPE}},              /* 120, _SC_2_UPE */
242   {cons, {c:_POSIX2_VERSION}},          /* 121, _SC_2_VERSION */
243 };
244
245 #define SC_MIN _SC_ARG_MAX
246 #define SC_MAX _SC_2_VERSION
247
248 /* sysconf: POSIX 4.8.1.1 */
249 /* Allows a portable app to determine quantities of resources or
250    presence of an option at execution time. */
251 long int
252 sysconf (int in)
253 {
254   if (in >= SC_MIN && in <= SC_MAX)
255     {
256       switch (sca[in].type)
257         {
258         case nsup:
259           break;
260         case cons:
261           return sca[in].c;
262         case func:
263           return sca[in].f (in);
264         }
265     }
266   /* Unimplemented sysconf name or invalid option value. */
267   set_errno (EINVAL);
268   return -1L;
269 }