Mercurial > trustbridge > nss-cmake-static
comparison nspr/pr/src/misc/prsystem.c @ 0:1e5118fa0cb1
This is NSS with a Cmake Buildsyste
To compile a static NSS library for Windows we've used the
Chromium-NSS fork and added a Cmake buildsystem to compile
it statically for Windows. See README.chromium for chromium
changes and README.trustbridge for our modifications.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 28 Jul 2014 10:47:06 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e5118fa0cb1 |
---|---|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | |
2 /* This Source Code Form is subject to the terms of the Mozilla Public | |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
5 | |
6 #include "primpl.h" | |
7 #include "prsystem.h" | |
8 #include "prprf.h" | |
9 #include "prlong.h" | |
10 | |
11 #if defined(BEOS) | |
12 #include <kernel/OS.h> | |
13 #endif | |
14 | |
15 #if defined(OS2) | |
16 #define INCL_DOS | |
17 #define INCL_DOSMISC | |
18 #include <os2.h> | |
19 /* define the required constant if it is not already defined in the headers */ | |
20 #ifndef QSV_NUMPROCESSORS | |
21 #define QSV_NUMPROCESSORS 26 | |
22 #endif | |
23 #endif | |
24 | |
25 /* BSD-derived systems use sysctl() to get the number of processors */ | |
26 #if defined(BSDI) || defined(FREEBSD) || defined(NETBSD) \ | |
27 || defined(OPENBSD) || defined(DARWIN) | |
28 #define _PR_HAVE_SYSCTL | |
29 #include <sys/param.h> | |
30 #include <sys/sysctl.h> | |
31 #endif | |
32 | |
33 #if defined(DARWIN) | |
34 #include <mach/mach_init.h> | |
35 #include <mach/mach_host.h> | |
36 #endif | |
37 | |
38 #if defined(HPUX) | |
39 #include <sys/mpctl.h> | |
40 #include <sys/pstat.h> | |
41 #endif | |
42 | |
43 #if defined(XP_UNIX) | |
44 #include <unistd.h> | |
45 #include <sys/utsname.h> | |
46 #endif | |
47 | |
48 #if defined(LINUX) | |
49 #include <string.h> | |
50 #include <ctype.h> | |
51 #define MAX_LINE 512 | |
52 #endif | |
53 | |
54 #if defined(AIX) | |
55 #include <cf.h> | |
56 #include <sys/cfgodm.h> | |
57 #endif | |
58 | |
59 PR_IMPLEMENT(char) PR_GetDirectorySeparator(void) | |
60 { | |
61 return PR_DIRECTORY_SEPARATOR; | |
62 } /* PR_GetDirectorySeparator */ | |
63 | |
64 /* | |
65 ** OBSOLETE -- the function name is misspelled. | |
66 */ | |
67 PR_IMPLEMENT(char) PR_GetDirectorySepartor(void) | |
68 { | |
69 #if defined(DEBUG) | |
70 static PRBool warn = PR_TRUE; | |
71 if (warn) { | |
72 warn = _PR_Obsolete("PR_GetDirectorySepartor()", | |
73 "PR_GetDirectorySeparator()"); | |
74 } | |
75 #endif | |
76 return PR_GetDirectorySeparator(); | |
77 } /* PR_GetDirectorySepartor */ | |
78 | |
79 PR_IMPLEMENT(char) PR_GetPathSeparator(void) | |
80 { | |
81 return PR_PATH_SEPARATOR; | |
82 } /* PR_GetPathSeparator */ | |
83 | |
84 PR_IMPLEMENT(PRStatus) PR_GetSystemInfo(PRSysInfo cmd, char *buf, PRUint32 buflen) | |
85 { | |
86 PRUintn len = 0; | |
87 | |
88 if (!_pr_initialized) _PR_ImplicitInitialization(); | |
89 | |
90 switch(cmd) | |
91 { | |
92 case PR_SI_HOSTNAME: | |
93 case PR_SI_HOSTNAME_UNTRUNCATED: | |
94 if (PR_FAILURE == _PR_MD_GETHOSTNAME(buf, (PRUintn)buflen)) | |
95 return PR_FAILURE; | |
96 | |
97 if (cmd == PR_SI_HOSTNAME_UNTRUNCATED) | |
98 break; | |
99 /* | |
100 * On some platforms a system does not have a hostname and | |
101 * its IP address is returned instead. The following code | |
102 * should be skipped on those platforms. | |
103 */ | |
104 #ifndef _PR_GET_HOST_ADDR_AS_NAME | |
105 /* Return the unqualified hostname */ | |
106 while (buf[len] && (len < buflen)) { | |
107 if (buf[len] == '.') { | |
108 buf[len] = '\0'; | |
109 break; | |
110 } | |
111 len += 1; | |
112 } | |
113 #endif | |
114 break; | |
115 | |
116 case PR_SI_SYSNAME: | |
117 /* Return the operating system name */ | |
118 #if defined(XP_UNIX) || defined(WIN32) | |
119 if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen)) | |
120 return PR_FAILURE; | |
121 #else | |
122 (void)PR_snprintf(buf, buflen, _PR_SI_SYSNAME); | |
123 #endif | |
124 break; | |
125 | |
126 case PR_SI_RELEASE: | |
127 /* Return the version of the operating system */ | |
128 #if defined(XP_UNIX) || defined(WIN32) | |
129 if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen)) | |
130 return PR_FAILURE; | |
131 #endif | |
132 #if defined(XP_OS2) | |
133 { | |
134 ULONG os2ver[2] = {0}; | |
135 DosQuerySysInfo(QSV_VERSION_MINOR, QSV_VERSION_REVISION, | |
136 &os2ver, sizeof(os2ver)); | |
137 /* Formatting for normal usage (2.11, 3.0, 4.0, 4.5); officially, | |
138 Warp 4 is version 2.40.00, WSeB 2.45.00 */ | |
139 if (os2ver[0] < 30) | |
140 (void)PR_snprintf(buf, buflen, "%s%lu", | |
141 "2.", os2ver[0]); | |
142 else if (os2ver[0] < 45) | |
143 (void)PR_snprintf(buf, buflen, "%lu%s%lu", | |
144 os2ver[0]/10, ".", os2ver[1]); | |
145 else | |
146 (void)PR_snprintf(buf, buflen, "%.1f", | |
147 os2ver[0]/10.0); | |
148 } | |
149 #endif /* OS2 */ | |
150 break; | |
151 | |
152 case PR_SI_ARCHITECTURE: | |
153 /* Return the architecture of the machine (ie. x86, mips, alpha, ...)*/ | |
154 (void)PR_snprintf(buf, buflen, _PR_SI_ARCHITECTURE); | |
155 break; | |
156 default: | |
157 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); | |
158 return PR_FAILURE; | |
159 } | |
160 return PR_SUCCESS; | |
161 } | |
162 | |
163 /* | |
164 ** PR_GetNumberOfProcessors() | |
165 ** | |
166 ** Implementation notes: | |
167 ** Every platform does it a bit different. | |
168 ** numCpus is the returned value. | |
169 ** for each platform's "if defined" section | |
170 ** declare your local variable | |
171 ** do your thing, assign to numCpus | |
172 ** order of the if defined()s may be important, | |
173 ** especially for unix variants. Do platform | |
174 ** specific implementations before XP_UNIX. | |
175 ** | |
176 */ | |
177 PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void ) | |
178 { | |
179 PRInt32 numCpus; | |
180 #if defined(WIN32) | |
181 SYSTEM_INFO info; | |
182 | |
183 GetSystemInfo( &info ); | |
184 numCpus = info.dwNumberOfProcessors; | |
185 #elif defined(BEOS) | |
186 system_info sysInfo; | |
187 | |
188 get_system_info(&sysInfo); | |
189 numCpus = sysInfo.cpu_count; | |
190 #elif defined(OS2) | |
191 DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus)); | |
192 #elif defined(_PR_HAVE_SYSCTL) | |
193 int mib[2]; | |
194 int rc; | |
195 size_t len = sizeof(numCpus); | |
196 | |
197 mib[0] = CTL_HW; | |
198 mib[1] = HW_NCPU; | |
199 rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 ); | |
200 if ( -1 == rc ) { | |
201 numCpus = -1; /* set to -1 for return value on error */ | |
202 _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); | |
203 } | |
204 #elif defined(HPUX) | |
205 numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 ); | |
206 if ( numCpus < 1 ) { | |
207 numCpus = -1; /* set to -1 for return value on error */ | |
208 _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); | |
209 } | |
210 #elif defined(IRIX) | |
211 numCpus = sysconf( _SC_NPROC_ONLN ); | |
212 #elif defined(RISCOS) || defined(SYMBIAN) | |
213 numCpus = 1; | |
214 #elif defined(LINUX) | |
215 /* for the benefit of devices with advanced power-saving, that | |
216 actually hotplug their cpus in heavy load, try to figure out | |
217 the real number of CPUs */ | |
218 char buf[MAX_LINE]; | |
219 FILE *fin; | |
220 const char *cpu_present = "/sys/devices/system/cpu/present"; | |
221 size_t strsize; | |
222 numCpus = 0; | |
223 fin = fopen(cpu_present, "r"); | |
224 if (fin != NULL) { | |
225 if (fgets(buf, MAX_LINE, fin) != NULL) { | |
226 /* check that the format is what we expect */ | |
227 if (buf[0] == '0') { | |
228 strsize = strlen(buf); | |
229 if (strsize == 1) { | |
230 /* single core */ | |
231 numCpus = 1; | |
232 } else if (strsize >= 3 && strsize <= 5) { | |
233 /* should be of the form 0-999 */ | |
234 /* parse the part after the 0-, note count is 0-based */ | |
235 if (buf[1] == '-' && isdigit(buf[2])) { | |
236 numCpus = 1 + atoi(buf + 2); | |
237 } | |
238 } | |
239 } | |
240 } | |
241 fclose(fin); | |
242 } | |
243 /* if that fails, fall back to more standard methods */ | |
244 if (!numCpus) { | |
245 numCpus = sysconf( _SC_NPROCESSORS_CONF ); | |
246 } | |
247 #elif defined(XP_UNIX) | |
248 numCpus = sysconf( _SC_NPROCESSORS_CONF ); | |
249 #else | |
250 #error "An implementation is required" | |
251 #endif | |
252 return(numCpus); | |
253 } /* end PR_GetNumberOfProcessors() */ | |
254 | |
255 /* | |
256 ** PR_GetPhysicalMemorySize() | |
257 ** | |
258 ** Implementation notes: | |
259 ** Every platform does it a bit different. | |
260 ** bytes is the returned value. | |
261 ** for each platform's "if defined" section | |
262 ** declare your local variable | |
263 ** do your thing, assign to bytes. | |
264 ** | |
265 */ | |
266 PR_IMPLEMENT(PRUint64) PR_GetPhysicalMemorySize(void) | |
267 { | |
268 PRUint64 bytes = 0; | |
269 | |
270 #if defined(LINUX) || defined(SOLARIS) | |
271 | |
272 long pageSize = sysconf(_SC_PAGESIZE); | |
273 long pageCount = sysconf(_SC_PHYS_PAGES); | |
274 if (pageSize >= 0 && pageCount >= 0) | |
275 bytes = (PRUint64) pageSize * pageCount; | |
276 | |
277 #elif defined(NETBSD) || defined(OPENBSD) | |
278 | |
279 int mib[2]; | |
280 int rc; | |
281 uint64_t memSize; | |
282 size_t len = sizeof(memSize); | |
283 | |
284 mib[0] = CTL_HW; | |
285 mib[1] = HW_PHYSMEM64; | |
286 rc = sysctl(mib, 2, &memSize, &len, NULL, 0); | |
287 if (-1 != rc) { | |
288 bytes = memSize; | |
289 } | |
290 | |
291 #elif defined(HPUX) | |
292 | |
293 struct pst_static info; | |
294 int result = pstat_getstatic(&info, sizeof(info), 1, 0); | |
295 if (result == 1) | |
296 bytes = (PRUint64) info.physical_memory * info.page_size; | |
297 | |
298 #elif defined(DARWIN) | |
299 | |
300 struct host_basic_info hInfo; | |
301 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; | |
302 | |
303 int result = host_info(mach_host_self(), | |
304 HOST_BASIC_INFO, | |
305 (host_info_t) &hInfo, | |
306 &count); | |
307 if (result == KERN_SUCCESS) | |
308 bytes = hInfo.max_mem; | |
309 | |
310 #elif defined(WIN32) | |
311 | |
312 MEMORYSTATUSEX memStat; | |
313 memStat.dwLength = sizeof(memStat); | |
314 if (GlobalMemoryStatusEx(&memStat)) | |
315 bytes = memStat.ullTotalPhys; | |
316 | |
317 #elif defined(OS2) | |
318 | |
319 ULONG ulPhysMem; | |
320 DosQuerySysInfo(QSV_TOTPHYSMEM, | |
321 QSV_TOTPHYSMEM, | |
322 &ulPhysMem, | |
323 sizeof(ulPhysMem)); | |
324 bytes = ulPhysMem; | |
325 | |
326 #elif defined(AIX) | |
327 | |
328 if (odm_initialize() == 0) { | |
329 int how_many; | |
330 struct CuAt *obj = getattr("sys0", "realmem", 0, &how_many); | |
331 if (obj != NULL) { | |
332 bytes = (PRUint64) atoi(obj->value) * 1024; | |
333 free(obj); | |
334 } | |
335 odm_terminate(); | |
336 } | |
337 | |
338 #else | |
339 | |
340 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
341 | |
342 #endif | |
343 | |
344 return bytes; | |
345 } /* end PR_GetPhysicalMemorySize() */ |