1 /**************************************************************************
2 *
3 * Copyright 2008-2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "hash_table.h"
30 #include "macros.h"
31 #include "os_misc.h"
32 #include "os_file.h"
33 #include "ralloc.h"
34 #include "simple_mtx.h"
35
36 #include <stdarg.h>
37
38
39 #if DETECT_OS_WINDOWS
40
41 #include <windows.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 #else
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <inttypes.h>
51
52 #endif
53
54
55 #if DETECT_OS_ANDROID
56 # define LOG_TAG "MESA"
57 # include <unistd.h>
58 # include <log/log.h>
59 # include <cutils/properties.h>
60 #elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD || DETECT_OS_MANAGARM
61 # include <unistd.h>
62 #elif DETECT_OS_OPENBSD || DETECT_OS_FREEBSD
63 # include <sys/resource.h>
64 # include <sys/sysctl.h>
65 #elif DETECT_OS_APPLE || DETECT_OS_BSD
66 # include <sys/sysctl.h>
67 #elif DETECT_OS_HAIKU
68 # include <kernel/OS.h>
69 #elif DETECT_OS_WINDOWS
70 # include <windows.h>
71 #else
72 #error unexpected platform in os_sysinfo.c
73 #endif
74
75
76 void
os_log_message(const char * message)77 os_log_message(const char *message)
78 {
79 /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
80 * write all messages to that file.
81 */
82 static FILE *fout = NULL;
83
84 if (!fout) {
85 #ifdef DEBUG
86 /* one-time init */
87 const char *filename = os_get_option("GALLIUM_LOG_FILE");
88 if (filename) {
89 if (strcmp(filename, "stdout") == 0) {
90 fout = stdout;
91 } else {
92 const char *mode = "w";
93 if (filename[0] == '+') {
94 /* If the filename is prefixed with '+' then open the file for
95 * appending instead of normal writing.
96 */
97 mode = "a";
98 filename++; /* skip the '+' */
99 }
100 fout = fopen(filename, mode);
101 }
102 }
103 #endif
104 if (!fout)
105 fout = stderr;
106 }
107
108 #if DETECT_OS_WINDOWS
109 OutputDebugStringA(message);
110 #if !defined(_GAMING_XBOX)
111 if(GetConsoleWindow() && !IsDebuggerPresent()) {
112 fflush(stdout);
113 fputs(message, fout);
114 fflush(fout);
115 }
116 else if (fout != stderr) {
117 fputs(message, fout);
118 fflush(fout);
119 }
120 #endif
121 #else /* !DETECT_OS_WINDOWS */
122 fflush(stdout);
123 fputs(message, fout);
124 fflush(fout);
125 # if DETECT_OS_ANDROID
126 LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", message);
127 # endif
128 #endif
129 }
130
131 #if DETECT_OS_ANDROID
132 # include <ctype.h>
133 # include "c11/threads.h"
134
135 /**
136 * Get an option value from android's property system, as a fallback to
137 * getenv() (which is generally less useful on android due to processes
138 * typically being forked from the zygote.
139 *
140 * The option name used for getenv is translated into a property name
141 * by:
142 *
143 * 1) convert to lowercase
144 * 2) replace '_' with '.'
145 * 3) if necessary, prepend "mesa."
146 *
147 * For example:
148 * - MESA_EXTENSION_OVERRIDE -> mesa.extension.override
149 * - GALLIUM_HUD -> mesa.gallium.hud
150 *
151 */
152 static char *
os_get_android_option(const char * name)153 os_get_android_option(const char *name)
154 {
155 static thread_local char os_android_option_value[PROPERTY_VALUE_MAX];
156 char key[PROPERTY_KEY_MAX];
157 char *p = key, *end = key + PROPERTY_KEY_MAX;
158 /* add "mesa." prefix if necessary: */
159 if (strstr(name, "MESA_") != name)
160 p += strlcpy(p, "mesa.", end - p);
161 p += strlcpy(p, name, end - p);
162 for (int i = 0; key[i]; i++) {
163 if (key[i] == '_') {
164 key[i] = '.';
165 } else {
166 key[i] = tolower(key[i]);
167 }
168 }
169
170 int len = property_get(key, os_android_option_value, NULL);
171 if (len > 1) {
172 return os_android_option_value;
173 }
174 return NULL;
175 }
176 #endif
177
178 #if DETECT_OS_WINDOWS
179
180 /* getenv doesn't necessarily reflect changes to the environment
181 * that have been made during the process lifetime, if either the
182 * setter uses a different CRT (e.g. due to static linking) or the
183 * setter used the Win32 API directly. */
184 const char *
os_get_option(const char * name)185 os_get_option(const char *name)
186 {
187 static thread_local char value[_MAX_ENV];
188 DWORD size = GetEnvironmentVariableA(name, value, _MAX_ENV);
189 return (size > 0 && size < _MAX_ENV) ? value : NULL;
190 }
191
192 #else
193
194 const char *
os_get_option(const char * name)195 os_get_option(const char *name)
196 {
197 const char *opt = getenv(name);
198 #if DETECT_OS_ANDROID
199 if (!opt) {
200 opt = os_get_android_option(name);
201 }
202 #endif
203 return opt;
204 }
205
206 #endif
207
208 static struct hash_table *options_tbl;
209 static bool options_tbl_exited = false;
210 static simple_mtx_t options_tbl_mtx = SIMPLE_MTX_INITIALIZER;
211
212 /**
213 * NOTE: The strings that allocated with ralloc_strdup(options_tbl, ...)
214 * are freed by _mesa_hash_table_destroy automatically
215 */
216 static void
options_tbl_fini(void)217 options_tbl_fini(void)
218 {
219 simple_mtx_lock(&options_tbl_mtx);
220 _mesa_hash_table_destroy(options_tbl, NULL);
221 options_tbl = NULL;
222 options_tbl_exited = true;
223 simple_mtx_unlock(&options_tbl_mtx);
224 }
225
226 const char *
os_get_option_cached(const char * name)227 os_get_option_cached(const char *name)
228 {
229 const char *opt = NULL;
230 simple_mtx_lock(&options_tbl_mtx);
231 if (options_tbl_exited) {
232 opt = os_get_option(name);
233 goto exit_mutex;
234 }
235
236 if (!options_tbl) {
237 options_tbl = _mesa_hash_table_create(NULL, _mesa_hash_string,
238 _mesa_key_string_equal);
239 if (options_tbl == NULL) {
240 goto exit_mutex;
241 }
242 atexit(options_tbl_fini);
243 }
244 struct hash_entry *entry = _mesa_hash_table_search(options_tbl, name);
245 if (entry) {
246 opt = entry->data;
247 goto exit_mutex;
248 }
249
250 char *name_dup = ralloc_strdup(options_tbl, name);
251 if (name_dup == NULL) {
252 goto exit_mutex;
253 }
254 opt = ralloc_strdup(options_tbl, os_get_option(name));
255 _mesa_hash_table_insert(options_tbl, name_dup, (void *)opt);
256 exit_mutex:
257 simple_mtx_unlock(&options_tbl_mtx);
258 return opt;
259 }
260
261 /**
262 * Return the size of the total physical memory.
263 * \param size returns the size of the total physical memory
264 * \return true for success, or false on failure
265 */
266 bool
os_get_total_physical_memory(uint64_t * size)267 os_get_total_physical_memory(uint64_t *size)
268 {
269 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD || DETECT_OS_MANAGARM
270 const long phys_pages = sysconf(_SC_PHYS_PAGES);
271 const long page_size = sysconf(_SC_PAGE_SIZE);
272
273 if (phys_pages <= 0 || page_size <= 0)
274 return false;
275
276 *size = (uint64_t)phys_pages * (uint64_t)page_size;
277 return true;
278 #elif DETECT_OS_APPLE || DETECT_OS_BSD
279 size_t len = sizeof(*size);
280 int mib[2];
281
282 mib[0] = CTL_HW;
283 #if DETECT_OS_APPLE
284 mib[1] = HW_MEMSIZE;
285 #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
286 mib[1] = HW_PHYSMEM64;
287 #elif DETECT_OS_FREEBSD
288 mib[1] = HW_REALMEM;
289 #elif DETECT_OS_DRAGONFLY
290 mib[1] = HW_PHYSMEM;
291 #else
292 #error Unsupported *BSD
293 #endif
294
295 return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
296 #elif DETECT_OS_HAIKU
297 system_info info;
298 status_t ret;
299
300 ret = get_system_info(&info);
301 if (ret != B_OK || info.max_pages <= 0)
302 return false;
303
304 *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE;
305 return true;
306 #elif DETECT_OS_WINDOWS
307 MEMORYSTATUSEX status;
308 BOOL ret;
309
310 status.dwLength = sizeof(status);
311 ret = GlobalMemoryStatusEx(&status);
312 *size = status.ullTotalPhys;
313 return (ret == true);
314 #else
315 #error unexpected platform in os_misc.c
316 return false;
317 #endif
318 }
319
320 bool
os_get_available_system_memory(uint64_t * size)321 os_get_available_system_memory(uint64_t *size)
322 {
323 #if DETECT_OS_LINUX
324 char *meminfo = os_read_file("/proc/meminfo", NULL);
325 if (!meminfo)
326 return false;
327
328 char *str = strstr(meminfo, "MemAvailable:");
329 if (!str) {
330 free(meminfo);
331 return false;
332 }
333
334 uint64_t kb_mem_available;
335 if (sscanf(str, "MemAvailable: %" PRIu64, &kb_mem_available) == 1) {
336 free(meminfo);
337 *size = kb_mem_available << 10;
338 return true;
339 }
340
341 free(meminfo);
342 return false;
343 #elif DETECT_OS_OPENBSD || DETECT_OS_FREEBSD
344 struct rlimit rl;
345 #if DETECT_OS_OPENBSD
346 int mib[] = { CTL_HW, HW_USERMEM64 };
347 #elif DETECT_OS_FREEBSD
348 int mib[] = { CTL_HW, HW_USERMEM };
349 #endif
350 int64_t mem_available;
351 size_t len = sizeof(mem_available);
352
353 /* physmem - wired */
354 if (sysctl(mib, 2, &mem_available, &len, NULL, 0) == -1)
355 return false;
356
357 /* static login.conf limit */
358 if (getrlimit(RLIMIT_DATA, &rl) == -1)
359 return false;
360
361 *size = MIN2(mem_available, rl.rlim_cur);
362 return true;
363 #elif DETECT_OS_WINDOWS
364 MEMORYSTATUSEX status;
365 BOOL ret;
366
367 status.dwLength = sizeof(status);
368 ret = GlobalMemoryStatusEx(&status);
369 *size = status.ullAvailPhys;
370 return (ret == true);
371 #else
372 return false;
373 #endif
374 }
375
376 /**
377 * Return the size of a page
378 * \param size returns the size of a page
379 * \return true for success, or false on failure
380 */
381 bool
os_get_page_size(uint64_t * size)382 os_get_page_size(uint64_t *size)
383 {
384 #if DETECT_OS_UNIX && !DETECT_OS_APPLE && !DETECT_OS_HAIKU
385 const long page_size = sysconf(_SC_PAGE_SIZE);
386
387 if (page_size <= 0)
388 return false;
389
390 *size = (uint64_t)page_size;
391 return true;
392 #elif DETECT_OS_HAIKU
393 *size = (uint64_t)B_PAGE_SIZE;
394 return true;
395 #elif DETECT_OS_WINDOWS
396 SYSTEM_INFO SysInfo;
397
398 GetSystemInfo(&SysInfo);
399 *size = SysInfo.dwPageSize;
400 return true;
401 #elif DETECT_OS_APPLE
402 size_t len = sizeof(*size);
403 int mib[2];
404
405 mib[0] = CTL_HW;
406 mib[1] = HW_PAGESIZE;
407 return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
408 #else
409 #error unexpected platform in os_sysinfo.c
410 return false;
411 #endif
412 }
413