obs-nix.c 5.6 KB

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