No visible API change. Signed-off-by: Chia-I Wu <olvaffe@gmail.com> Reviewed-by: Yiwei Zhang <zzyiwei@chromium.org> Reviewed-by: Ryan Neph <ryanneph@google.com> Acked-by: Gert Wollny <gert.wollny@collabora.com>macos/master
parent
7683d499d5
commit
d32f92c8d1
@ -1,91 +0,0 @@ |
|||||||
/**************************************************************************
|
|
||||||
* |
|
||||||
* 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 "os_misc.h" |
|
||||||
|
|
||||||
#include <stdarg.h> |
|
||||||
|
|
||||||
|
|
||||||
#if defined(PIPE_SUBSYSTEM_WINDOWS_USER) |
|
||||||
|
|
||||||
#ifndef WIN32_LEAN_AND_MEAN |
|
||||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
|
||||||
#endif |
|
||||||
#include <windows.h> |
|
||||||
#include <stdio.h> |
|
||||||
|
|
||||||
#else |
|
||||||
|
|
||||||
#include <stdio.h> |
|
||||||
#include <stdlib.h> |
|
||||||
|
|
||||||
#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) { |
|
||||||
/* one-time init */ |
|
||||||
const char *filename = os_get_option("GALLIUM_LOG_FILE"); |
|
||||||
if (filename) |
|
||||||
fout = fopen(filename, "w"); |
|
||||||
if (!fout) |
|
||||||
fout = stderr; |
|
||||||
} |
|
||||||
|
|
||||||
#if defined(PIPE_SUBSYSTEM_WINDOWS_USER) |
|
||||||
OutputDebugStringA(message); |
|
||||||
if(GetConsoleWindow() && !IsDebuggerPresent()) { |
|
||||||
fflush(stdout); |
|
||||||
fputs(message, fout); |
|
||||||
fflush(fout); |
|
||||||
} |
|
||||||
else if (fout != stderr) { |
|
||||||
fputs(message, fout); |
|
||||||
fflush(fout); |
|
||||||
} |
|
||||||
#else /* !PIPE_SUBSYSTEM_WINDOWS */ |
|
||||||
fflush(stdout); |
|
||||||
fputs(message, fout); |
|
||||||
fflush(fout); |
|
||||||
#endif |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
const char * |
|
||||||
os_get_option(const char *name) |
|
||||||
{ |
|
||||||
return getenv(name); |
|
||||||
} |
|
||||||
|
|
@ -0,0 +1,227 @@ |
|||||||
|
/*
|
||||||
|
* Copyright 2019 Intel Corporation |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "os_file.h" |
||||||
|
#include "detect_os.h" |
||||||
|
|
||||||
|
#include <errno.h> |
||||||
|
#include <fcntl.h> |
||||||
|
#include <stdlib.h> |
||||||
|
#include <sys/stat.h> |
||||||
|
|
||||||
|
#if DETECT_OS_WINDOWS |
||||||
|
#include <io.h> |
||||||
|
#define open _open |
||||||
|
#define fdopen _fdopen |
||||||
|
#define O_CREAT _O_CREAT |
||||||
|
#define O_EXCL _O_EXCL |
||||||
|
#define O_WRONLY _O_WRONLY |
||||||
|
#else |
||||||
|
#include <unistd.h> |
||||||
|
#ifndef F_DUPFD_CLOEXEC |
||||||
|
#define F_DUPFD_CLOEXEC 1030 |
||||||
|
#endif |
||||||
|
#endif |
||||||
|
|
||||||
|
|
||||||
|
FILE * |
||||||
|
os_file_create_unique(const char *filename, int filemode) |
||||||
|
{ |
||||||
|
int fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, filemode); |
||||||
|
if (fd == -1) |
||||||
|
return NULL; |
||||||
|
return fdopen(fd, "w"); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
#if DETECT_OS_WINDOWS |
||||||
|
int |
||||||
|
os_dupfd_cloexec(int fd) |
||||||
|
{ |
||||||
|
/*
|
||||||
|
* On Windows child processes don't inherit handles by default: |
||||||
|
* https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873
|
||||||
|
*/ |
||||||
|
return dup(fd); |
||||||
|
} |
||||||
|
#else |
||||||
|
int |
||||||
|
os_dupfd_cloexec(int fd) |
||||||
|
{ |
||||||
|
int minfd = 3; |
||||||
|
int newfd = fcntl(fd, F_DUPFD_CLOEXEC, minfd); |
||||||
|
|
||||||
|
if (newfd >= 0) |
||||||
|
return newfd; |
||||||
|
|
||||||
|
if (errno != EINVAL) |
||||||
|
return -1; |
||||||
|
|
||||||
|
newfd = fcntl(fd, F_DUPFD, minfd); |
||||||
|
|
||||||
|
if (newfd < 0) |
||||||
|
return -1; |
||||||
|
|
||||||
|
long flags = fcntl(newfd, F_GETFD); |
||||||
|
if (flags == -1) { |
||||||
|
close(newfd); |
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
if (fcntl(newfd, F_SETFD, flags | FD_CLOEXEC) == -1) { |
||||||
|
close(newfd); |
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
return newfd; |
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
#include <fcntl.h> |
||||||
|
#include <sys/stat.h> |
||||||
|
|
||||||
|
#if DETECT_OS_WINDOWS |
||||||
|
typedef ptrdiff_t ssize_t; |
||||||
|
#endif |
||||||
|
|
||||||
|
static ssize_t |
||||||
|
readN(int fd, char *buf, size_t len) |
||||||
|
{ |
||||||
|
/* err was initially set to -ENODATA but in some BSD systems
|
||||||
|
* ENODATA is not defined and ENOATTR is used instead. |
||||||
|
* As err is not returned by any function it can be initialized |
||||||
|
* to -EFAULT that exists everywhere. |
||||||
|
*/ |
||||||
|
int err = -EFAULT; |
||||||
|
size_t total = 0; |
||||||
|
do { |
||||||
|
ssize_t ret = read(fd, buf + total, len - total); |
||||||
|
|
||||||
|
if (ret < 0) |
||||||
|
ret = -errno; |
||||||
|
|
||||||
|
if (ret == -EINTR || ret == -EAGAIN) |
||||||
|
continue; |
||||||
|
|
||||||
|
if (ret <= 0) { |
||||||
|
err = ret; |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
total += ret; |
||||||
|
} while (total != len); |
||||||
|
|
||||||
|
return total ? (ssize_t)total : err; |
||||||
|
} |
||||||
|
|
||||||
|
#ifndef O_BINARY |
||||||
|
/* Unix makes no distinction between text and binary files. */ |
||||||
|
#define O_BINARY 0 |
||||||
|
#endif |
||||||
|
|
||||||
|
char * |
||||||
|
os_read_file(const char *filename, size_t *size) |
||||||
|
{ |
||||||
|
/* Note that this also serves as a slight margin to avoid a 2x grow when
|
||||||
|
* the file is just a few bytes larger when we read it than when we |
||||||
|
* fstat'ed it. |
||||||
|
* The string's NULL terminator is also included in here. |
||||||
|
*/ |
||||||
|
size_t len = 64; |
||||||
|
|
||||||
|
int fd = open(filename, O_RDONLY | O_BINARY); |
||||||
|
if (fd == -1) { |
||||||
|
/* errno set by open() */ |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
/* Pre-allocate a buffer at least the size of the file if we can read
|
||||||
|
* that information. |
||||||
|
*/ |
||||||
|
struct stat stat; |
||||||
|
if (fstat(fd, &stat) == 0) |
||||||
|
len += stat.st_size; |
||||||
|
|
||||||
|
char *buf = malloc(len); |
||||||
|
if (!buf) { |
||||||
|
close(fd); |
||||||
|
errno = -ENOMEM; |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
ssize_t actually_read; |
||||||
|
size_t offset = 0, remaining = len - 1; |
||||||
|
while ((actually_read = readN(fd, buf + offset, remaining)) == (ssize_t)remaining) { |
||||||
|
char *newbuf = realloc(buf, 2 * len); |
||||||
|
if (!newbuf) { |
||||||
|
free(buf); |
||||||
|
close(fd); |
||||||
|
errno = -ENOMEM; |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
buf = newbuf; |
||||||
|
len *= 2; |
||||||
|
offset += actually_read; |
||||||
|
remaining = len - offset - 1; |
||||||
|
} |
||||||
|
|
||||||
|
close(fd); |
||||||
|
|
||||||
|
if (actually_read > 0) |
||||||
|
offset += actually_read; |
||||||
|
|
||||||
|
/* Final resize to actual size */ |
||||||
|
len = offset + 1; |
||||||
|
char *newbuf = realloc(buf, len); |
||||||
|
if (!newbuf) { |
||||||
|
free(buf); |
||||||
|
errno = -ENOMEM; |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
buf = newbuf; |
||||||
|
|
||||||
|
buf[offset] = '\0'; |
||||||
|
|
||||||
|
if (size) |
||||||
|
*size = offset; |
||||||
|
|
||||||
|
return buf; |
||||||
|
} |
||||||
|
|
||||||
|
#if DETECT_OS_LINUX |
||||||
|
|
||||||
|
#include <sys/syscall.h> |
||||||
|
#include <unistd.h> |
||||||
|
|
||||||
|
/* copied from <linux/kcmp.h> */ |
||||||
|
#define KCMP_FILE 0 |
||||||
|
|
||||||
|
int |
||||||
|
os_same_file_description(int fd1, int fd2) |
||||||
|
{ |
||||||
|
pid_t pid = getpid(); |
||||||
|
|
||||||
|
/* Same file descriptor trivially implies same file description */ |
||||||
|
if (fd1 == fd2) |
||||||
|
return 0; |
||||||
|
|
||||||
|
return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2); |
||||||
|
} |
||||||
|
|
||||||
|
#else |
||||||
|
|
||||||
|
int |
||||||
|
os_same_file_description(int fd1, int fd2) |
||||||
|
{ |
||||||
|
/* Same file descriptor trivially implies same file description */ |
||||||
|
if (fd1 == fd2) |
||||||
|
return 0; |
||||||
|
|
||||||
|
/* Otherwise we can't tell */ |
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,59 @@ |
|||||||
|
/*
|
||||||
|
* Copyright 2019 Intel Corporation |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
* |
||||||
|
* File operations helpers |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef _OS_FILE_H_ |
||||||
|
#define _OS_FILE_H_ |
||||||
|
|
||||||
|
#include <stdbool.h> |
||||||
|
#include <stdio.h> |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
extern "C" { |
||||||
|
#endif |
||||||
|
|
||||||
|
/*
|
||||||
|
* Create a new file and opens it for writing-only. |
||||||
|
* If the given filename already exists, nothing is done and NULL is returned. |
||||||
|
* `errno` gets set to the failure reason; if that is not EEXIST, the caller |
||||||
|
* might want to do something other than trying again. |
||||||
|
*/ |
||||||
|
FILE * |
||||||
|
os_file_create_unique(const char *filename, int filemode); |
||||||
|
|
||||||
|
/*
|
||||||
|
* Duplicate a file descriptor, making sure not to keep it open after an exec*() |
||||||
|
*/ |
||||||
|
int |
||||||
|
os_dupfd_cloexec(int fd); |
||||||
|
|
||||||
|
/*
|
||||||
|
* Read a file. |
||||||
|
* Returns a char* that the caller must free(), or NULL and sets errno. |
||||||
|
* If size is not null and no error occurred it's set to the size of the |
||||||
|
* file. |
||||||
|
* Reads files as binary and includes a NUL terminator after the end of the |
||||||
|
* returned buffer. |
||||||
|
*/ |
||||||
|
char * |
||||||
|
os_read_file(const char *filename, size_t *size); |
||||||
|
|
||||||
|
/*
|
||||||
|
* Try to determine if two file descriptors reference the same file description |
||||||
|
* |
||||||
|
* Return values: |
||||||
|
* - 0: They reference the same file description |
||||||
|
* - > 0: They do not reference the same file description |
||||||
|
* - < 0: Unable to determine whether they reference the same file description |
||||||
|
*/ |
||||||
|
int |
||||||
|
os_same_file_description(int fd1, int fd2); |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
#endif /* _OS_FILE_H_ */ |
@ -0,0 +1,361 @@ |
|||||||
|
/**************************************************************************
|
||||||
|
* |
||||||
|
* 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 "os_misc.h" |
||||||
|
#include "os_file.h" |
||||||
|
#include "macros.h" |
||||||
|
|
||||||
|
#include <stdarg.h> |
||||||
|
|
||||||
|
|
||||||
|
#if DETECT_OS_WINDOWS |
||||||
|
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN |
||||||
|
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||||
|
#endif |
||||||
|
#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 |
||||||
|
# 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> |
||||||
|
#elif DETECT_OS_HAIKU |
||||||
|
# include <kernel/OS.h> |
||||||
|
#elif DETECT_OS_WINDOWS |
||||||
|
# include <windows.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) { |
||||||
|
#ifdef DEBUG |
||||||
|
/* one-time init */ |
||||||
|
const char *filename = os_get_option("GALLIUM_LOG_FILE"); |
||||||
|
if (filename) { |
||||||
|
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(GetConsoleWindow() && !IsDebuggerPresent()) { |
||||||
|
fflush(stdout); |
||||||
|
fputs(message, fout); |
||||||
|
fflush(fout); |
||||||
|
} |
||||||
|
else if (fout != stderr) { |
||||||
|
fputs(message, fout); |
||||||
|
fflush(fout); |
||||||
|
} |
||||||
|
#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 "hash_table.h" |
||||||
|
# include "ralloc.h" |
||||||
|
# include "simple_mtx.h" |
||||||
|
|
||||||
|
static struct hash_table *options_tbl; |
||||||
|
|
||||||
|
static void |
||||||
|
options_tbl_fini(void) |
||||||
|
{ |
||||||
|
_mesa_hash_table_destroy(options_tbl, NULL); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) if necessary, prepend "mesa." |
||||||
|
* |
||||||
|
* For example: |
||||||
|
* - MESA_EXTENSION_OVERRIDE -> mesa.extension.override |
||||||
|
* - GALLIUM_HUD -> mesa.gallium.hud |
||||||
|
* |
||||||
|
* Note that we use a hashtable for two purposes: |
||||||
|
* 1) Avoid re-translating the option name on subsequent lookups |
||||||
|
* 2) Avoid leaking memory. Because property_get() returns the |
||||||
|
* property value into a user allocated buffer, we cannot return |
||||||
|
* that directly to the caller, so we need to strdup(). With the |
||||||
|
* hashtable, subsquent lookups can return the existing string. |
||||||
|
*/ |
||||||
|
static const char * |
||||||
|
os_get_android_option(const char *name) |
||||||
|
{ |
||||||
|
if (!options_tbl) { |
||||||
|
options_tbl = _mesa_hash_table_create(NULL, _mesa_hash_string, |
||||||
|
_mesa_key_string_equal); |
||||||
|
atexit(options_tbl_fini); |
||||||
|
} |
||||||
|
|
||||||
|
struct hash_entry *entry = _mesa_hash_table_search(options_tbl, name); |
||||||
|
if (entry) { |
||||||
|
return entry->data; |
||||||
|
} |
||||||
|
|
||||||
|
char 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]); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const char *opt = NULL; |
||||||
|
int len = property_get(key, value, NULL); |
||||||
|
if (len > 1) { |
||||||
|
opt = ralloc_strdup(options_tbl, value); |
||||||
|
} |
||||||
|
|
||||||
|
_mesa_hash_table_insert(options_tbl, name, (void *)opt); |
||||||
|
|
||||||
|
return opt; |
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
|
||||||
|
#if !defined(EMBEDDED_DEVICE) |
||||||
|
const char * |
||||||
|
os_get_option(const char *name) |
||||||
|
{ |
||||||
|
const char *opt = getenv(name); |
||||||
|
#if DETECT_OS_ANDROID |
||||||
|
if (!opt) { |
||||||
|
opt = os_get_android_option(name); |
||||||
|
} |
||||||
|
#endif |
||||||
|
return opt; |
||||||
|
} |
||||||
|
#endif /* !EMBEDDED_DEVICE */ |
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD |
||||||
|
const long phys_pages = sysconf(_SC_PHYS_PAGES); |
||||||
|
const long page_size = sysconf(_SC_PAGE_SIZE); |
||||||
|
|
||||||
|
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); |
||||||
|
#else |
||||||
|
#error unexpected platform in os_sysinfo.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; |
||||||
|
#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 DETECT_OS_UNIX && !DETECT_OS_APPLE && !DETECT_OS_HAIKU |
||||||
|
const long page_size = sysconf(_SC_PAGE_SIZE); |
||||||
|
|
||||||
|
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_t len = sizeof(*size); |
||||||
|
int mib[2]; |
||||||
|
|
||||||
|
mib[0] = CTL_HW; |
||||||
|
mib[1] = HW_PAGESIZE; |
||||||
|
return (sysctl(mib, 2, size, &len, NULL, 0) == 0); |
||||||
|
#else |
||||||
|
#error unexpected platform in os_sysinfo.c |
||||||
|
return false; |
||||||
|
#endif |
||||||
|
} |
Loading…
Reference in new issue