José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
Vinson Lee | fdb1997 | 2020-10-21 15:13:03 -0700 | [diff] [blame] | 3 | * Copyright 2008-2010 VMware, Inc. |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 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 "os_misc.h" |
Jonathan Gray | 033dcb2 | 2019-12-06 01:01:29 +1100 | [diff] [blame] | 30 | #include "os_file.h" |
Jonathan Gray | b30bd6f | 2019-12-06 01:21:07 +1100 | [diff] [blame] | 31 | #include "macros.h" |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 32 | |
| 33 | #include <stdarg.h> |
| 34 | |
| 35 | |
Eric Engestrom | 8c52bca | 2019-08-01 22:49:05 +0100 | [diff] [blame] | 36 | #if DETECT_OS_WINDOWS |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 37 | |
| 38 | #ifndef WIN32_LEAN_AND_MEAN |
| 39 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers |
| 40 | #endif |
| 41 | #include <windows.h> |
| 42 | #include <stdio.h> |
Jose Fonseca | 46f7b36 | 2019-08-27 11:51:00 +0100 | [diff] [blame] | 43 | #include <stdlib.h> |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 44 | |
| 45 | #else |
| 46 | |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
Jonathan Gray | 033dcb2 | 2019-12-06 01:01:29 +1100 | [diff] [blame] | 49 | #include <string.h> |
| 50 | #include <inttypes.h> |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 51 | |
| 52 | #endif |
| 53 | |
| 54 | |
Rob Clark | f9f7cbc | 2019-09-03 11:43:40 -0700 | [diff] [blame] | 55 | #if DETECT_OS_ANDROID |
| 56 | # define LOG_TAG "MESA" |
| 57 | # include <unistd.h> |
| 58 | # include <log/log.h> |
| 59 | #elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 60 | # include <unistd.h> |
Emmanuel Vadot | fe894dc | 2020-10-04 11:00:49 +0200 | [diff] [blame] | 61 | #elif DETECT_OS_OPENBSD || DETECT_OS_FREEBSD |
Jonathan Gray | b30bd6f | 2019-12-06 01:21:07 +1100 | [diff] [blame] | 62 | # include <sys/resource.h> |
| 63 | # include <sys/sysctl.h> |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 64 | #elif DETECT_OS_APPLE || DETECT_OS_BSD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 65 | # include <sys/sysctl.h> |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 66 | #elif DETECT_OS_HAIKU |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 67 | # include <kernel/OS.h> |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 68 | #elif DETECT_OS_WINDOWS |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 69 | # include <windows.h> |
| 70 | #else |
| 71 | #error unexpected platform in os_sysinfo.c |
| 72 | #endif |
| 73 | |
| 74 | |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 75 | void |
| 76 | os_log_message(const char *message) |
| 77 | { |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 78 | /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename, |
| 79 | * write all messages to that file. |
| 80 | */ |
| 81 | static FILE *fout = NULL; |
| 82 | |
| 83 | if (!fout) { |
Brian Paul | bb1292e | 2016-06-14 20:35:53 -0600 | [diff] [blame] | 84 | #ifdef DEBUG |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 85 | /* one-time init */ |
| 86 | const char *filename = os_get_option("GALLIUM_LOG_FILE"); |
Brian Paul | bb1292e | 2016-06-14 20:35:53 -0600 | [diff] [blame] | 87 | if (filename) { |
| 88 | const char *mode = "w"; |
| 89 | if (filename[0] == '+') { |
| 90 | /* If the filename is prefixed with '+' then open the file for |
| 91 | * appending instead of normal writing. |
| 92 | */ |
| 93 | mode = "a"; |
| 94 | filename++; /* skip the '+' */ |
| 95 | } |
| 96 | fout = fopen(filename, mode); |
| 97 | } |
| 98 | #endif |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 99 | if (!fout) |
| 100 | fout = stderr; |
| 101 | } |
| 102 | |
Eric Engestrom | 8c52bca | 2019-08-01 22:49:05 +0100 | [diff] [blame] | 103 | #if DETECT_OS_WINDOWS |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 104 | OutputDebugStringA(message); |
| 105 | if(GetConsoleWindow() && !IsDebuggerPresent()) { |
| 106 | fflush(stdout); |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 107 | fputs(message, fout); |
| 108 | fflush(fout); |
| 109 | } |
| 110 | else if (fout != stderr) { |
| 111 | fputs(message, fout); |
| 112 | fflush(fout); |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 113 | } |
Eric Engestrom | 8c52bca | 2019-08-01 22:49:05 +0100 | [diff] [blame] | 114 | #else /* !DETECT_OS_WINDOWS */ |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 115 | fflush(stdout); |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 116 | fputs(message, fout); |
| 117 | fflush(fout); |
Rob Clark | f9f7cbc | 2019-09-03 11:43:40 -0700 | [diff] [blame] | 118 | # if DETECT_OS_ANDROID |
| 119 | LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", message); |
| 120 | # endif |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 121 | #endif |
| 122 | } |
| 123 | |
| 124 | |
Eric Engestrom | e740e7a | 2019-08-01 21:45:25 +0100 | [diff] [blame] | 125 | #if !defined(EMBEDDED_DEVICE) |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 126 | const char * |
| 127 | os_get_option(const char *name) |
| 128 | { |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 129 | return getenv(name); |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 130 | } |
Eric Engestrom | e740e7a | 2019-08-01 21:45:25 +0100 | [diff] [blame] | 131 | #endif /* !EMBEDDED_DEVICE */ |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 132 | |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Return the size of the total physical memory. |
| 136 | * \param size returns the size of the total physical memory |
| 137 | * \return true for success, or false on failure |
| 138 | */ |
| 139 | bool |
| 140 | os_get_total_physical_memory(uint64_t *size) |
| 141 | { |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 142 | #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 143 | const long phys_pages = sysconf(_SC_PHYS_PAGES); |
| 144 | const long page_size = sysconf(_SC_PAGE_SIZE); |
| 145 | |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 146 | if (phys_pages <= 0 || page_size <= 0) |
| 147 | return false; |
| 148 | |
Axel Davy | 197cdd1 | 2016-10-11 18:57:17 +0200 | [diff] [blame] | 149 | *size = (uint64_t)phys_pages * (uint64_t)page_size; |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 150 | return true; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 151 | #elif DETECT_OS_APPLE || DETECT_OS_BSD |
Jonathan Gray | 7983a3d | 2015-02-22 19:19:25 +1100 | [diff] [blame] | 152 | size_t len = sizeof(*size); |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 153 | int mib[2]; |
| 154 | |
| 155 | mib[0] = CTL_HW; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 156 | #if DETECT_OS_APPLE |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 157 | mib[1] = HW_MEMSIZE; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 158 | #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 159 | mib[1] = HW_PHYSMEM64; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 160 | #elif DETECT_OS_FREEBSD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 161 | mib[1] = HW_REALMEM; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 162 | #elif DETECT_OS_DRAGONFLY |
Vinson Lee | a2fd55c | 2014-10-10 22:40:21 -0700 | [diff] [blame] | 163 | mib[1] = HW_PHYSMEM; |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 164 | #else |
| 165 | #error Unsupported *BSD |
| 166 | #endif |
| 167 | |
Jonathan Gray | 7983a3d | 2015-02-22 19:19:25 +1100 | [diff] [blame] | 168 | return (sysctl(mib, 2, size, &len, NULL, 0) == 0); |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 169 | #elif DETECT_OS_HAIKU |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 170 | system_info info; |
| 171 | status_t ret; |
| 172 | |
| 173 | ret = get_system_info(&info); |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 174 | if (ret != B_OK || info.max_pages <= 0) |
| 175 | return false; |
| 176 | |
Axel Davy | 197cdd1 | 2016-10-11 18:57:17 +0200 | [diff] [blame] | 177 | *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE; |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 178 | return true; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 179 | #elif DETECT_OS_WINDOWS |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 180 | MEMORYSTATUSEX status; |
| 181 | BOOL ret; |
| 182 | |
| 183 | status.dwLength = sizeof(status); |
| 184 | ret = GlobalMemoryStatusEx(&status); |
| 185 | *size = status.ullTotalPhys; |
| 186 | return (ret == TRUE); |
| 187 | #else |
| 188 | #error unexpected platform in os_sysinfo.c |
| 189 | return false; |
| 190 | #endif |
| 191 | } |
Jonathan Gray | 033dcb2 | 2019-12-06 01:01:29 +1100 | [diff] [blame] | 192 | |
| 193 | bool |
| 194 | os_get_available_system_memory(uint64_t *size) |
| 195 | { |
| 196 | #if DETECT_OS_LINUX |
| 197 | char *meminfo = os_read_file("/proc/meminfo", NULL); |
| 198 | if (!meminfo) |
| 199 | return false; |
| 200 | |
| 201 | char *str = strstr(meminfo, "MemAvailable:"); |
| 202 | if (!str) { |
| 203 | free(meminfo); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | uint64_t kb_mem_available; |
| 208 | if (sscanf(str, "MemAvailable: %" PRIx64, &kb_mem_available) == 1) { |
| 209 | free(meminfo); |
| 210 | *size = kb_mem_available << 10; |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | free(meminfo); |
| 215 | return false; |
Emmanuel Vadot | fe894dc | 2020-10-04 11:00:49 +0200 | [diff] [blame] | 216 | #elif DETECT_OS_OPENBSD || DETECT_OS_FREEBSD |
Jonathan Gray | b30bd6f | 2019-12-06 01:21:07 +1100 | [diff] [blame] | 217 | struct rlimit rl; |
Emmanuel Vadot | fe894dc | 2020-10-04 11:00:49 +0200 | [diff] [blame] | 218 | #if DETECT_OS_OPENBSD |
Jonathan Gray | b30bd6f | 2019-12-06 01:21:07 +1100 | [diff] [blame] | 219 | int mib[] = { CTL_HW, HW_USERMEM64 }; |
Emmanuel Vadot | fe894dc | 2020-10-04 11:00:49 +0200 | [diff] [blame] | 220 | #elif DETECT_OS_FREEBSD |
| 221 | int mib[] = { CTL_HW, HW_USERMEM }; |
| 222 | #endif |
Jonathan Gray | b30bd6f | 2019-12-06 01:21:07 +1100 | [diff] [blame] | 223 | int64_t mem_available; |
| 224 | size_t len = sizeof(mem_available); |
| 225 | |
| 226 | /* physmem - wired */ |
| 227 | if (sysctl(mib, 2, &mem_available, &len, NULL, 0) == -1) |
| 228 | return false; |
| 229 | |
| 230 | /* static login.conf limit */ |
| 231 | if (getrlimit(RLIMIT_DATA, &rl) == -1) |
| 232 | return false; |
| 233 | |
| 234 | *size = MIN2(mem_available, rl.rlim_cur); |
| 235 | return true; |
Jonathan Gray | 033dcb2 | 2019-12-06 01:01:29 +1100 | [diff] [blame] | 236 | #else |
| 237 | return false; |
| 238 | #endif |
| 239 | } |