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