obs-nix.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 3 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 "util/dstr.h"
  21. #include "obs.h"
  22. static inline bool check_path(const char* data, const char *path,
  23. struct dstr * output)
  24. {
  25. dstr_copy(output, path);
  26. dstr_cat(output, data);
  27. blog(LOG_INFO, "Attempting path: %s\n", output->array);
  28. return access(output->array, R_OK) == 0;
  29. }
  30. static inline bool check_lib_path(const char* data, const char *path,
  31. struct dstr *output)
  32. {
  33. bool result = false;
  34. struct dstr tmp;
  35. dstr_init_copy(&tmp, "lib");
  36. dstr_cat(&tmp, data);
  37. dstr_cat(&tmp, ".so");
  38. result = check_path(tmp.array, path, output);
  39. dstr_free(&tmp);
  40. return result;
  41. }
  42. /*
  43. * /usr/local/lib/obs-plugins
  44. * /usr/lib/obs-plugins
  45. */
  46. char *find_plugin(const char *plugin)
  47. {
  48. struct dstr output;
  49. dstr_init(&output);
  50. if(sizeof(void*) == 4) {
  51. if (check_lib_path(plugin, "../../obs-plugins/32bit/", &output))
  52. return output.array;
  53. } else {
  54. if (check_lib_path(plugin, "../../obs-plugins/64bit/", &output))
  55. return output.array;
  56. }
  57. if (OBS_INSTALL_PREFIX [0] != 0) {
  58. if (check_lib_path(plugin,
  59. OBS_INSTALL_PREFIX "lib/obs-plugins/",
  60. &output))
  61. return output.array;
  62. }
  63. dstr_free(&output);
  64. return NULL;
  65. }
  66. /*
  67. * /usr/local/share/libobs
  68. * /usr/share/libobs
  69. */
  70. char *find_libobs_data_file(const char *file)
  71. {
  72. struct dstr output;
  73. dstr_init(&output);
  74. if (check_path(file, OBS_DATA_PATH "/libobs/", &output))
  75. return output.array;
  76. if (OBS_INSTALL_PREFIX [0] != 0) {
  77. if (check_path(file, OBS_INSTALL_DATA_PATH "/libobs/",
  78. &output))
  79. return output.array;
  80. }
  81. dstr_free(&output);
  82. return NULL;
  83. }
  84. /*
  85. * /usr/local/share/obs-plugins
  86. * /usr/share/obs-plugins
  87. */
  88. char *obs_find_plugin_file(const char *file)
  89. {
  90. struct dstr output;
  91. dstr_init(&output);
  92. if (check_path(file, OBS_DATA_PATH "/obs-plugins/", &output))
  93. return output.array;
  94. if (OBS_INSTALL_PREFIX [0] != 0) {
  95. if (check_path(file, OBS_INSTALL_DATA_PATH "/obs-plugins/",
  96. &output))
  97. return output.array;
  98. }
  99. dstr_free(&output);
  100. return NULL;
  101. }
  102. static void log_processor_info(void)
  103. {
  104. FILE *fp;
  105. int physical_id = -1;
  106. int last_physical_id = -1;
  107. char *line = NULL;
  108. size_t linecap = 0;
  109. struct dstr processor;
  110. blog(LOG_INFO, "Processor: %lu logical cores",
  111. sysconf(_SC_NPROCESSORS_ONLN));
  112. fp = fopen("/proc/cpuinfo", "r");
  113. if (!fp)
  114. return;
  115. dstr_init(&processor);
  116. while (getline(&line, &linecap, fp) != -1) {
  117. if (!strncmp(line, "model name", 10)) {
  118. char *start = strchr(line, ':');
  119. if (!start || *(++start) == '\0')
  120. continue;
  121. dstr_copy(&processor, start);
  122. dstr_resize(&processor, processor.len - 1);
  123. dstr_depad(&processor);
  124. }
  125. if (!strncmp(line, "physical id", 11)) {
  126. char *start = strchr(line, ':');
  127. if (!start || *(++start) == '\0')
  128. continue;
  129. physical_id = atoi(start);
  130. }
  131. if (*line == '\n' && physical_id != last_physical_id) {
  132. last_physical_id = physical_id;
  133. blog(LOG_INFO, "Processor: %s", processor.array);
  134. }
  135. }
  136. fclose(fp);
  137. dstr_free(&processor);
  138. free(line);
  139. }
  140. static void log_memory_info(void)
  141. {
  142. struct sysinfo info;
  143. if (sysinfo(&info) < 0)
  144. return;
  145. blog(LOG_INFO, "Physical Memory: %luMB Total",
  146. info.totalram / 1024 / 1024);
  147. }
  148. static void log_kernel_version(void)
  149. {
  150. struct utsname info;
  151. if (uname(&info) < 0)
  152. return;
  153. blog(LOG_INFO, "Kernel Version: %s %s", info.sysname, info.release);
  154. }
  155. static void log_distribution_info(void)
  156. {
  157. FILE *fp;
  158. char *line = NULL;
  159. size_t linecap = 0;
  160. struct dstr distro;
  161. struct dstr version;
  162. fp = fopen("/etc/os-release", "r");
  163. if (!fp) {
  164. blog(LOG_INFO, "Distribution: Missing /etc/os-release !");
  165. return;
  166. }
  167. dstr_init_copy(&distro, "Unknown");
  168. dstr_init_copy(&version, "Unknown");
  169. while (getline(&line, &linecap, fp) != -1) {
  170. if (!strncmp(line, "NAME", 4)) {
  171. char *start = strchr(line, '=');
  172. if (!start || *(++start) == '\0')
  173. continue;
  174. dstr_copy(&distro, start);
  175. dstr_resize(&distro, distro.len - 1);
  176. }
  177. if (!strncmp(line, "VERSION_ID", 10)) {
  178. char *start = strchr(line, '=');
  179. if (!start || *(++start) == '\0')
  180. continue;
  181. dstr_copy(&version, start);
  182. dstr_resize(&version, version.len - 1);
  183. }
  184. }
  185. blog(LOG_INFO, "Distribution: %s %s", distro.array, version.array);
  186. fclose(fp);
  187. dstr_free(&version);
  188. dstr_free(&distro);
  189. free(line);
  190. }
  191. void log_system_info(void)
  192. {
  193. log_processor_info();
  194. log_memory_info();
  195. log_kernel_version();
  196. log_distribution_info();
  197. }