obs-nix.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <sys/sysinfo.h>
  22. #include <sys/utsname.h>
  23. #include <inttypes.h>
  24. #include "util/dstr.h"
  25. #include "obs-internal.h"
  26. const char *get_module_extension(void)
  27. {
  28. return ".so";
  29. }
  30. #ifdef __LP64__
  31. #define BIT_STRING "64bit"
  32. #else
  33. #define BIT_STRING "32bit"
  34. #endif
  35. static const char *module_bin[] = {
  36. "../../obs-plugins/" BIT_STRING,
  37. OBS_INSTALL_PREFIX "/" OBS_PLUGIN_DESTINATION
  38. };
  39. static const char *module_data[] = {
  40. OBS_DATA_PATH "/obs-plugins/%module%",
  41. OBS_INSTALL_DATA_PATH "/obs-plugins/%module%",
  42. };
  43. static const int module_patterns_size =
  44. sizeof(module_bin)/sizeof(module_bin[0]);
  45. void add_default_module_paths(void)
  46. {
  47. for (int i = 0; i < module_patterns_size; i++)
  48. obs_add_module_path(module_bin[i], module_data[i]);
  49. }
  50. /*
  51. * /usr/local/share/libobs
  52. * /usr/share/libobs
  53. */
  54. char *find_libobs_data_file(const char *file)
  55. {
  56. struct dstr output;
  57. dstr_init(&output);
  58. if (check_path(file, OBS_DATA_PATH "/libobs/", &output))
  59. return output.array;
  60. if (OBS_INSTALL_PREFIX [0] != 0) {
  61. if (check_path(file, OBS_INSTALL_DATA_PATH "/libobs/",
  62. &output))
  63. return output.array;
  64. }
  65. dstr_free(&output);
  66. return NULL;
  67. }
  68. static void log_processor_cores(void)
  69. {
  70. blog(LOG_INFO, "Processor: %lu logical cores",
  71. sysconf(_SC_NPROCESSORS_ONLN));
  72. }
  73. static void log_processor_info(void)
  74. {
  75. FILE *fp;
  76. int physical_id = -1;
  77. int last_physical_id = -1;
  78. char *line = NULL;
  79. size_t linecap = 0;
  80. struct dstr processor;
  81. fp = fopen("/proc/cpuinfo", "r");
  82. if (!fp)
  83. return;
  84. dstr_init(&processor);
  85. while (getline(&line, &linecap, fp) != -1) {
  86. if (!strncmp(line, "model name", 10)) {
  87. char *start = strchr(line, ':');
  88. if (!start || *(++start) == '\0')
  89. continue;
  90. dstr_copy(&processor, start);
  91. dstr_resize(&processor, processor.len - 1);
  92. dstr_depad(&processor);
  93. }
  94. if (!strncmp(line, "physical id", 11)) {
  95. char *start = strchr(line, ':');
  96. if (!start || *(++start) == '\0')
  97. continue;
  98. physical_id = atoi(start);
  99. }
  100. if (*line == '\n' && physical_id != last_physical_id) {
  101. last_physical_id = physical_id;
  102. blog(LOG_INFO, "Processor: %s", processor.array);
  103. }
  104. }
  105. fclose(fp);
  106. dstr_free(&processor);
  107. free(line);
  108. }
  109. static void log_memory_info(void)
  110. {
  111. struct sysinfo info;
  112. if (sysinfo(&info) < 0)
  113. return;
  114. blog(LOG_INFO, "Physical Memory: %"PRIu64"MB Total",
  115. (uint64_t)info.totalram * info.mem_unit / 1024 / 1024);
  116. }
  117. static void log_kernel_version(void)
  118. {
  119. struct utsname info;
  120. if (uname(&info) < 0)
  121. return;
  122. blog(LOG_INFO, "Kernel Version: %s %s", info.sysname, info.release);
  123. }
  124. static void log_distribution_info(void)
  125. {
  126. FILE *fp;
  127. char *line = NULL;
  128. size_t linecap = 0;
  129. struct dstr distro;
  130. struct dstr version;
  131. fp = fopen("/etc/os-release", "r");
  132. if (!fp) {
  133. blog(LOG_INFO, "Distribution: Missing /etc/os-release !");
  134. return;
  135. }
  136. dstr_init_copy(&distro, "Unknown");
  137. dstr_init_copy(&version, "Unknown");
  138. while (getline(&line, &linecap, fp) != -1) {
  139. if (!strncmp(line, "NAME", 4)) {
  140. char *start = strchr(line, '=');
  141. if (!start || *(++start) == '\0')
  142. continue;
  143. dstr_copy(&distro, start);
  144. dstr_resize(&distro, distro.len - 1);
  145. }
  146. if (!strncmp(line, "VERSION_ID", 10)) {
  147. char *start = strchr(line, '=');
  148. if (!start || *(++start) == '\0')
  149. continue;
  150. dstr_copy(&version, start);
  151. dstr_resize(&version, version.len - 1);
  152. }
  153. }
  154. blog(LOG_INFO, "Distribution: %s %s", distro.array, version.array);
  155. fclose(fp);
  156. dstr_free(&version);
  157. dstr_free(&distro);
  158. free(line);
  159. }
  160. void log_system_info(void)
  161. {
  162. log_processor_cores();
  163. log_processor_info();
  164. log_memory_info();
  165. log_kernel_version();
  166. log_distribution_info();
  167. }