obs-nix.c 4.5 KB

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