platform-nix.c 12 KB

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