platform-nix.c 12 KB

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