platform-nix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <dirent.h>
  21. #include <stdlib.h>
  22. #include <dlfcn.h>
  23. #include <unistd.h>
  24. #include <glob.h>
  25. #include <time.h>
  26. #include "obsconfig.h"
  27. #if !defined(__APPLE__)
  28. #include <sys/times.h>
  29. #include <sys/wait.h>
  30. #include <signal.h>
  31. #include <spawn.h>
  32. #endif
  33. #include "darray.h"
  34. #include "dstr.h"
  35. #include "platform.h"
  36. #include "threading.h"
  37. void *os_dlopen(const char *path)
  38. {
  39. struct dstr dylib_name;
  40. if (!path)
  41. return NULL;
  42. dstr_init_copy(&dylib_name, path);
  43. if (!dstr_find(&dylib_name, ".so"))
  44. dstr_cat(&dylib_name, ".so");
  45. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  46. if (!res)
  47. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  48. path, dylib_name.array, dlerror());
  49. dstr_free(&dylib_name);
  50. return res;
  51. }
  52. void *os_dlsym(void *module, const char *func)
  53. {
  54. return dlsym(module, func);
  55. }
  56. void os_dlclose(void *module)
  57. {
  58. dlclose(module);
  59. }
  60. #if !defined(__APPLE__)
  61. struct os_cpu_usage_info {
  62. clock_t last_cpu_time, last_sys_time, last_user_time;
  63. int core_count;
  64. };
  65. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  66. {
  67. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  68. struct tms time_sample;
  69. info->last_cpu_time = times(&time_sample);
  70. info->last_sys_time = time_sample.tms_stime;
  71. info->last_user_time = time_sample.tms_utime;
  72. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  73. return info;
  74. }
  75. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  76. {
  77. struct tms time_sample;
  78. clock_t cur_cpu_time;
  79. double percent;
  80. if (!info)
  81. return 0.0;
  82. cur_cpu_time = times(&time_sample);
  83. if (cur_cpu_time <= info->last_cpu_time ||
  84. time_sample.tms_stime < info->last_sys_time ||
  85. time_sample.tms_utime < info->last_user_time)
  86. return 0.0;
  87. percent = (double)(time_sample.tms_stime - info->last_sys_time +
  88. (time_sample.tms_utime - info->last_user_time));
  89. percent /= (double)(cur_cpu_time - info->last_cpu_time);
  90. percent /= (double)info->core_count;
  91. info->last_cpu_time = cur_cpu_time;
  92. info->last_sys_time = time_sample.tms_stime;
  93. info->last_user_time = time_sample.tms_utime;
  94. return percent * 100.0;
  95. }
  96. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  97. {
  98. if (info)
  99. bfree(info);
  100. }
  101. #endif
  102. bool os_sleepto_ns(uint64_t time_target)
  103. {
  104. uint64_t current = os_gettime_ns();
  105. if (time_target < current)
  106. return false;
  107. time_target -= current;
  108. struct timespec req, remain;
  109. memset(&req, 0, sizeof(req));
  110. memset(&remain, 0, sizeof(remain));
  111. req.tv_sec = time_target/1000000000;
  112. req.tv_nsec = time_target%1000000000;
  113. while (nanosleep(&req, &remain)) {
  114. req = remain;
  115. memset(&remain, 0, sizeof(remain));
  116. }
  117. return true;
  118. }
  119. void os_sleep_ms(uint32_t duration)
  120. {
  121. usleep(duration*1000);
  122. }
  123. #if !defined(__APPLE__)
  124. uint64_t os_gettime_ns(void)
  125. {
  126. struct timespec ts;
  127. clock_gettime(CLOCK_MONOTONIC, &ts);
  128. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  129. }
  130. /* should return $HOME/.[name], or when using XDG,
  131. * should return $HOME/.config/[name] as default */
  132. int os_get_config_path(char *dst, size_t size, const char *name)
  133. {
  134. #ifdef USE_XDG
  135. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  136. // If XDG_CONFIG_HOME is unset,
  137. // we use the default $HOME/.config/[name] instead
  138. if (xdg_ptr == NULL) {
  139. char *home_ptr = getenv("HOME");
  140. if (home_ptr == NULL)
  141. bcrash("Could not get $HOME\n");
  142. if (!name || !*name) {
  143. return snprintf(dst, size, "%s/.config", home_ptr);
  144. } else {
  145. return snprintf(dst, size, "%s/.config/%s", home_ptr,
  146. name);
  147. }
  148. } else {
  149. if (!name || !*name)
  150. return snprintf(dst, size, "%s", xdg_ptr);
  151. else
  152. return snprintf(dst, size, "%s/%s", xdg_ptr, name);
  153. }
  154. #else
  155. char *path_ptr = getenv("HOME");
  156. if (path_ptr == NULL)
  157. bcrash("Could not get $HOME\n");
  158. if (!name || !*name)
  159. return snprintf(dst, size, "%s", path_ptr);
  160. else
  161. return snprintf(dst, size, "%s/.%s", path_ptr, name);
  162. #endif
  163. }
  164. /* should return $HOME/.[name], or when using XDG,
  165. * should return $HOME/.config/[name] as default */
  166. char *os_get_config_path_ptr(const char *name)
  167. {
  168. #ifdef USE_XDG
  169. struct dstr path;
  170. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  171. /* If XDG_CONFIG_HOME is unset,
  172. * we use the default $HOME/.config/[name] instead */
  173. if (xdg_ptr == NULL) {
  174. char *home_ptr = getenv("HOME");
  175. if (home_ptr == NULL)
  176. bcrash("Could not get $HOME\n");
  177. dstr_init_copy(&path, home_ptr);
  178. dstr_cat(&path, "/.config/");
  179. dstr_cat(&path, name);
  180. } else {
  181. dstr_init_copy(&path, xdg_ptr);
  182. dstr_cat(&path, "/");
  183. dstr_cat(&path, name);
  184. }
  185. return path.array;
  186. #else
  187. char *path_ptr = getenv("HOME");
  188. if (path_ptr == NULL)
  189. bcrash("Could not get $HOME\n");
  190. struct dstr path;
  191. dstr_init_copy(&path, path_ptr);
  192. dstr_cat(&path, "/.");
  193. dstr_cat(&path, name);
  194. return path.array;
  195. #endif
  196. }
  197. #endif
  198. bool os_file_exists(const char *path)
  199. {
  200. return access(path, F_OK) == 0;
  201. }
  202. struct os_dir {
  203. const char *path;
  204. DIR *dir;
  205. struct dirent *cur_dirent;
  206. struct os_dirent out;
  207. };
  208. os_dir_t *os_opendir(const char *path)
  209. {
  210. struct os_dir *dir;
  211. DIR *dir_val;
  212. dir_val = opendir(path);
  213. if (!dir_val)
  214. return NULL;
  215. dir = bzalloc(sizeof(struct os_dir));
  216. dir->dir = dir_val;
  217. dir->path = path;
  218. return dir;
  219. }
  220. static inline bool is_dir(const char *path)
  221. {
  222. struct stat stat_info;
  223. if (stat(path, &stat_info) == 0)
  224. return !!S_ISDIR(stat_info.st_mode);
  225. blog(LOG_DEBUG, "is_dir: stat for %s failed, errno: %d", path, errno);
  226. return false;
  227. }
  228. struct os_dirent *os_readdir(os_dir_t *dir)
  229. {
  230. struct dstr file_path = {0};
  231. if (!dir) return NULL;
  232. dir->cur_dirent = readdir(dir->dir);
  233. if (!dir->cur_dirent)
  234. return NULL;
  235. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  236. dstr_copy(&file_path, dir->path);
  237. dstr_cat(&file_path, "/");
  238. dstr_cat(&file_path, dir->out.d_name);
  239. dir->out.directory = is_dir(file_path.array);
  240. dstr_free(&file_path);
  241. return &dir->out;
  242. }
  243. void os_closedir(os_dir_t *dir)
  244. {
  245. if (dir) {
  246. closedir(dir->dir);
  247. bfree(dir);
  248. }
  249. }
  250. struct posix_glob_info {
  251. struct os_glob_info base;
  252. glob_t gl;
  253. };
  254. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  255. {
  256. struct posix_glob_info pgi;
  257. int ret = glob(pattern, 0, NULL, &pgi.gl);
  258. if (ret == 0) {
  259. DARRAY(struct os_globent) list;
  260. da_init(list);
  261. for (size_t i = 0; i < pgi.gl.gl_pathc; i++) {
  262. struct os_globent ent = {0};
  263. ent.path = pgi.gl.gl_pathv[i];
  264. ent.directory = is_dir(ent.path);
  265. da_push_back(list, &ent);
  266. }
  267. pgi.base.gl_pathc = list.num;
  268. pgi.base.gl_pathv = list.array;
  269. *pglob = bmemdup(&pgi, sizeof(pgi));
  270. } else {
  271. *pglob = NULL;
  272. }
  273. UNUSED_PARAMETER(flags);
  274. return ret;
  275. }
  276. void os_globfree(os_glob_t *pglob)
  277. {
  278. if (pglob) {
  279. struct posix_glob_info *pgi = (struct posix_glob_info*)pglob;
  280. globfree(&pgi->gl);
  281. bfree(pgi->base.gl_pathv);
  282. bfree(pgi);
  283. }
  284. }
  285. int os_unlink(const char *path)
  286. {
  287. return unlink(path);
  288. }
  289. int os_rmdir(const char *path)
  290. {
  291. return rmdir(path);
  292. }
  293. int os_mkdir(const char *path)
  294. {
  295. if (mkdir(path, 0755) == 0)
  296. return MKDIR_SUCCESS;
  297. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  298. }
  299. int os_rename(const char *old_path, const char *new_path)
  300. {
  301. return rename(old_path, new_path);
  302. }
  303. #if !defined(__APPLE__)
  304. os_performance_token_t *os_request_high_performance(const char *reason)
  305. {
  306. UNUSED_PARAMETER(reason);
  307. return NULL;
  308. }
  309. void os_end_high_performance(os_performance_token_t *token)
  310. {
  311. UNUSED_PARAMETER(token);
  312. }
  313. #endif
  314. int os_copyfile(const char *file_path_in, const char *file_path_out)
  315. {
  316. FILE *file_out = NULL;
  317. FILE *file_in = NULL;
  318. uint8_t data[4096];
  319. int ret = -1;
  320. size_t size;
  321. if (os_file_exists(file_path_out))
  322. return -1;
  323. file_in = fopen(file_path_in, "rb");
  324. if (!file_in)
  325. return -1;
  326. file_out = fopen(file_path_out, "ab+");
  327. if (!file_out)
  328. goto error;
  329. do {
  330. size = fread(data, 1, sizeof(data), file_in);
  331. if (size)
  332. size = fwrite(data, 1, size, file_out);
  333. } while (size == sizeof(data));
  334. ret = feof(file_in) ? 0 : -1;
  335. error:
  336. if (file_out)
  337. fclose(file_out);
  338. fclose(file_in);
  339. return ret;
  340. }
  341. char *os_getcwd(char *path, size_t size)
  342. {
  343. return getcwd(path, size);
  344. }
  345. int os_chdir(const char *path)
  346. {
  347. return chdir(path);
  348. }
  349. #if !defined(__APPLE__)
  350. #if HAVE_DBUS
  351. struct dbus_sleep_info;
  352. extern struct dbus_sleep_info *dbus_sleep_info_create(void);
  353. extern void dbus_inhibit_sleep(struct dbus_sleep_info *dbus, const char *sleep,
  354. bool active);
  355. extern void dbus_sleep_info_destroy(struct dbus_sleep_info *dbus);
  356. #endif
  357. struct os_inhibit_info {
  358. #if HAVE_DBUS
  359. struct dbus_sleep_info *dbus;
  360. #endif
  361. pthread_t screensaver_thread;
  362. os_event_t *stop_event;
  363. char *reason;
  364. posix_spawnattr_t attr;
  365. bool active;
  366. };
  367. os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  368. {
  369. struct os_inhibit_info *info = bzalloc(sizeof(*info));
  370. sigset_t set;
  371. #if HAVE_DBUS
  372. info->dbus = dbus_sleep_info_create();
  373. #endif
  374. os_event_init(&info->stop_event, OS_EVENT_TYPE_AUTO);
  375. posix_spawnattr_init(&info->attr);
  376. sigemptyset(&set);
  377. posix_spawnattr_setsigmask(&info->attr, &set);
  378. sigaddset(&set, SIGPIPE);
  379. posix_spawnattr_setsigdefault(&info->attr, &set);
  380. posix_spawnattr_setflags(&info->attr,
  381. POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK);
  382. info->reason = bstrdup(reason);
  383. return info;
  384. }
  385. extern char **environ;
  386. static void reset_screensaver(os_inhibit_t *info)
  387. {
  388. char *argv[3] = {(char*)"xdg-screensaver", (char*)"reset", NULL};
  389. pid_t pid;
  390. int err = posix_spawnp(&pid, "xdg-screensaver", NULL, &info->attr,
  391. argv, environ);
  392. if (err == 0) {
  393. int status;
  394. while (waitpid(pid, &status, 0) == -1);
  395. } else {
  396. blog(LOG_WARNING, "Failed to create xdg-screensaver: %d", err);
  397. }
  398. }
  399. static void *screensaver_thread(void *param)
  400. {
  401. os_inhibit_t *info = param;
  402. while (os_event_timedwait(info->stop_event, 30000) == ETIMEDOUT)
  403. reset_screensaver(info);
  404. return NULL;
  405. }
  406. bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  407. {
  408. int ret;
  409. if (!info)
  410. return false;
  411. if (info->active == active)
  412. return false;
  413. #if HAVE_DBUS
  414. if (info->dbus)
  415. dbus_inhibit_sleep(info->dbus, info->reason, active);
  416. #endif
  417. if (!info->stop_event)
  418. return true;
  419. if (active) {
  420. ret = pthread_create(&info->screensaver_thread, NULL,
  421. &screensaver_thread, info);
  422. if (ret < 0) {
  423. blog(LOG_ERROR, "Failed to create screensaver "
  424. "inhibitor thread");
  425. return false;
  426. }
  427. } else {
  428. os_event_signal(info->stop_event);
  429. pthread_join(info->screensaver_thread, NULL);
  430. }
  431. info->active = active;
  432. return true;
  433. }
  434. void os_inhibit_sleep_destroy(os_inhibit_t *info)
  435. {
  436. if (info) {
  437. os_inhibit_sleep_set_active(info, false);
  438. #if HAVE_DBUS
  439. dbus_sleep_info_destroy(info->dbus);
  440. #endif
  441. os_event_destroy(info->stop_event);
  442. posix_spawnattr_destroy(&info->attr);
  443. bfree(info->reason);
  444. bfree(info);
  445. }
  446. }
  447. #endif