obs-nix.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. Copyright (C) 2014 by Zachary Lund <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #ifdef __FreeBSD__
  16. #define _WITH_GETLINE
  17. #endif
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #if defined(__FreeBSD__)
  22. #include <sys/sysctl.h>
  23. #endif
  24. #include <sys/sysinfo.h>
  25. #include <sys/utsname.h>
  26. #include <inttypes.h>
  27. #include "util/dstr.h"
  28. #include "obs-internal.h"
  29. const char *get_module_extension(void)
  30. {
  31. return ".so";
  32. }
  33. #ifdef __LP64__
  34. #define BIT_STRING "64bit"
  35. #else
  36. #define BIT_STRING "32bit"
  37. #endif
  38. static const char *module_bin[] = {
  39. "../../obs-plugins/" BIT_STRING,
  40. OBS_INSTALL_PREFIX "/" OBS_PLUGIN_DESTINATION
  41. };
  42. static const char *module_data[] = {
  43. OBS_DATA_PATH "/obs-plugins/%module%",
  44. OBS_INSTALL_DATA_PATH "/obs-plugins/%module%",
  45. };
  46. static const int module_patterns_size =
  47. sizeof(module_bin)/sizeof(module_bin[0]);
  48. void add_default_module_paths(void)
  49. {
  50. for (int i = 0; i < module_patterns_size; i++)
  51. obs_add_module_path(module_bin[i], module_data[i]);
  52. }
  53. /*
  54. * /usr/local/share/libobs
  55. * /usr/share/libobs
  56. */
  57. char *find_libobs_data_file(const char *file)
  58. {
  59. struct dstr output;
  60. dstr_init(&output);
  61. if (check_path(file, OBS_DATA_PATH "/libobs/", &output))
  62. return output.array;
  63. if (OBS_INSTALL_PREFIX [0] != 0) {
  64. if (check_path(file, OBS_INSTALL_DATA_PATH "/libobs/",
  65. &output))
  66. return output.array;
  67. }
  68. dstr_free(&output);
  69. return NULL;
  70. }
  71. static void log_processor_cores(void)
  72. {
  73. blog(LOG_INFO, "Processor: %lu logical cores",
  74. sysconf(_SC_NPROCESSORS_ONLN));
  75. }
  76. #if defined(__linux__)
  77. static void log_processor_info(void)
  78. {
  79. FILE *fp;
  80. int physical_id = -1;
  81. int last_physical_id = -1;
  82. char *line = NULL;
  83. size_t linecap = 0;
  84. struct dstr processor;
  85. fp = fopen("/proc/cpuinfo", "r");
  86. if (!fp)
  87. return;
  88. dstr_init(&processor);
  89. while (getline(&line, &linecap, fp) != -1) {
  90. if (!strncmp(line, "model name", 10)) {
  91. char *start = strchr(line, ':');
  92. if (!start || *(++start) == '\0')
  93. continue;
  94. dstr_copy(&processor, start);
  95. dstr_resize(&processor, processor.len - 1);
  96. dstr_depad(&processor);
  97. }
  98. if (!strncmp(line, "physical id", 11)) {
  99. char *start = strchr(line, ':');
  100. if (!start || *(++start) == '\0')
  101. continue;
  102. physical_id = atoi(start);
  103. }
  104. if (*line == '\n' && physical_id != last_physical_id) {
  105. last_physical_id = physical_id;
  106. blog(LOG_INFO, "Processor: %s", processor.array);
  107. }
  108. }
  109. fclose(fp);
  110. dstr_free(&processor);
  111. free(line);
  112. }
  113. #elif defined(__FreeBSD__)
  114. static void log_processor_info(void)
  115. {
  116. int mib[2];
  117. size_t len;
  118. char *proc;
  119. mib[0] = CTL_HW;
  120. mib[1] = HW_MODEL;
  121. sysctl(mib, 2, NULL, &len, NULL, 0);
  122. proc = bmalloc(len);
  123. if (!proc)
  124. return;
  125. sysctl(mib, 2, proc, &len, NULL, 0);
  126. blog(LOG_INFO, "Processor: %s", proc);
  127. bfree(proc);
  128. }
  129. #endif
  130. static void log_memory_info(void)
  131. {
  132. struct sysinfo info;
  133. if (sysinfo(&info) < 0)
  134. return;
  135. blog(LOG_INFO, "Physical Memory: %"PRIu64"MB Total",
  136. (uint64_t)info.totalram * info.mem_unit / 1024 / 1024);
  137. }
  138. static void log_kernel_version(void)
  139. {
  140. struct utsname info;
  141. if (uname(&info) < 0)
  142. return;
  143. blog(LOG_INFO, "Kernel Version: %s %s", info.sysname, info.release);
  144. }
  145. #if defined(__linux__)
  146. static void log_distribution_info(void)
  147. {
  148. FILE *fp;
  149. char *line = NULL;
  150. size_t linecap = 0;
  151. struct dstr distro;
  152. struct dstr version;
  153. fp = fopen("/etc/os-release", "r");
  154. if (!fp) {
  155. blog(LOG_INFO, "Distribution: Missing /etc/os-release !");
  156. return;
  157. }
  158. dstr_init_copy(&distro, "Unknown");
  159. dstr_init_copy(&version, "Unknown");
  160. while (getline(&line, &linecap, fp) != -1) {
  161. if (!strncmp(line, "NAME", 4)) {
  162. char *start = strchr(line, '=');
  163. if (!start || *(++start) == '\0')
  164. continue;
  165. dstr_copy(&distro, start);
  166. dstr_resize(&distro, distro.len - 1);
  167. }
  168. if (!strncmp(line, "VERSION_ID", 10)) {
  169. char *start = strchr(line, '=');
  170. if (!start || *(++start) == '\0')
  171. continue;
  172. dstr_copy(&version, start);
  173. dstr_resize(&version, version.len - 1);
  174. }
  175. }
  176. blog(LOG_INFO, "Distribution: %s %s", distro.array, version.array);
  177. fclose(fp);
  178. dstr_free(&version);
  179. dstr_free(&distro);
  180. free(line);
  181. }
  182. #endif
  183. void log_system_info(void)
  184. {
  185. log_processor_cores();
  186. #if defined(__linux__) || defined(__FreeBSD__)
  187. log_processor_info();
  188. #endif
  189. log_memory_info();
  190. log_kernel_version();
  191. #if defined(__linux__)
  192. log_distribution_info();
  193. #endif
  194. }