platform-nix.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #if !defined(__APPLE__)
  27. #include <sys/times.h>
  28. #endif
  29. #include "darray.h"
  30. #include "dstr.h"
  31. #include "platform.h"
  32. void *os_dlopen(const char *path)
  33. {
  34. struct dstr dylib_name;
  35. if (!path)
  36. return NULL;
  37. dstr_init_copy(&dylib_name, path);
  38. if (!dstr_find(&dylib_name, ".so"))
  39. dstr_cat(&dylib_name, ".so");
  40. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  41. if (!res)
  42. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  43. path, dylib_name.array, dlerror());
  44. dstr_free(&dylib_name);
  45. return res;
  46. }
  47. void *os_dlsym(void *module, const char *func)
  48. {
  49. return dlsym(module, func);
  50. }
  51. void os_dlclose(void *module)
  52. {
  53. dlclose(module);
  54. }
  55. #if !defined(__APPLE__)
  56. struct os_cpu_usage_info {
  57. clock_t last_cpu_time, last_sys_time, last_user_time;
  58. int core_count;
  59. };
  60. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  61. {
  62. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  63. struct tms time_sample;
  64. info->last_cpu_time = times(&time_sample);
  65. info->last_sys_time = time_sample.tms_stime;
  66. info->last_user_time = time_sample.tms_utime;
  67. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  68. return info;
  69. }
  70. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  71. {
  72. struct tms time_sample;
  73. clock_t cur_cpu_time;
  74. double percent;
  75. if (!info)
  76. return 0.0;
  77. cur_cpu_time = times(&time_sample);
  78. if (cur_cpu_time <= info->last_cpu_time ||
  79. time_sample.tms_stime < info->last_sys_time ||
  80. time_sample.tms_utime < info->last_user_time)
  81. return 0.0;
  82. percent = (double)(time_sample.tms_stime - info->last_sys_time +
  83. (time_sample.tms_utime - info->last_user_time));
  84. percent /= (double)(cur_cpu_time - info->last_cpu_time);
  85. percent /= (double)info->core_count;
  86. info->last_cpu_time = cur_cpu_time;
  87. info->last_sys_time = time_sample.tms_stime;
  88. info->last_user_time = time_sample.tms_utime;
  89. return percent * 100.0;
  90. }
  91. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  92. {
  93. if (info)
  94. bfree(info);
  95. }
  96. #endif
  97. bool os_sleepto_ns(uint64_t time_target)
  98. {
  99. uint64_t current = os_gettime_ns();
  100. if (time_target < current)
  101. return false;
  102. time_target -= current;
  103. struct timespec req, remain;
  104. memset(&req, 0, sizeof(req));
  105. memset(&remain, 0, sizeof(remain));
  106. req.tv_sec = time_target/1000000000;
  107. req.tv_nsec = time_target%1000000000;
  108. while (nanosleep(&req, &remain)) {
  109. req = remain;
  110. memset(&remain, 0, sizeof(remain));
  111. }
  112. return true;
  113. }
  114. void os_sleep_ms(uint32_t duration)
  115. {
  116. usleep(duration*1000);
  117. }
  118. #if !defined(__APPLE__)
  119. uint64_t os_gettime_ns(void)
  120. {
  121. struct timespec ts;
  122. clock_gettime(CLOCK_MONOTONIC, &ts);
  123. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  124. }
  125. /* should return $HOME/.[name], or when using XDG,
  126. * should return $HOME/.config/[name] as default */
  127. int os_get_config_path(char *dst, size_t size, const char *name)
  128. {
  129. #ifdef USE_XDG
  130. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  131. // If XDG_CONFIG_HOME is unset,
  132. // we use the default $HOME/.config/[name] instead
  133. if (xdg_ptr == NULL) {
  134. char *home_ptr = getenv("HOME");
  135. if (home_ptr == NULL)
  136. bcrash("Could not get $HOME\n");
  137. if (!name || !*name) {
  138. return snprintf(dst, size, "%s/.config", home_ptr);
  139. } else {
  140. return snprintf(dst, size, "%s/.config/%s", home_ptr,
  141. name);
  142. }
  143. } else {
  144. if (!name || !*name)
  145. return snprintf(dst, size, "%s", xdg_ptr);
  146. else
  147. return snprintf(dst, size, "%s/%s", xdg_ptr, name);
  148. }
  149. #else
  150. char *path_ptr = getenv("HOME");
  151. if (path_ptr == NULL)
  152. bcrash("Could not get $HOME\n");
  153. if (!name || !*name)
  154. return snprintf(dst, size, "%s", path_ptr);
  155. else
  156. return snprintf(dst, size, "%s/.%s", path_ptr, name);
  157. #endif
  158. }
  159. /* should return $HOME/.[name], or when using XDG,
  160. * should return $HOME/.config/[name] as default */
  161. char *os_get_config_path_ptr(const char *name)
  162. {
  163. #ifdef USE_XDG
  164. struct dstr path;
  165. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  166. /* If XDG_CONFIG_HOME is unset,
  167. * we use the default $HOME/.config/[name] instead */
  168. if (xdg_ptr == NULL) {
  169. char *home_ptr = getenv("HOME");
  170. if (home_ptr == NULL)
  171. bcrash("Could not get $HOME\n");
  172. dstr_init_copy(&path, home_ptr);
  173. dstr_cat(&path, "/.config/");
  174. dstr_cat(&path, name);
  175. } else {
  176. dstr_init_copy(&path, xdg_ptr);
  177. dstr_cat(&path, "/");
  178. dstr_cat(&path, name);
  179. }
  180. return path.array;
  181. #else
  182. char *path_ptr = getenv("HOME");
  183. if (path_ptr == NULL)
  184. bcrash("Could not get $HOME\n");
  185. struct dstr path;
  186. dstr_init_copy(&path, path_ptr);
  187. dstr_cat(&path, "/.");
  188. dstr_cat(&path, name);
  189. return path.array;
  190. #endif
  191. }
  192. #endif
  193. bool os_file_exists(const char *path)
  194. {
  195. return access(path, F_OK) == 0;
  196. }
  197. struct os_dir {
  198. const char *path;
  199. DIR *dir;
  200. struct dirent *cur_dirent;
  201. struct os_dirent out;
  202. };
  203. os_dir_t *os_opendir(const char *path)
  204. {
  205. struct os_dir *dir;
  206. DIR *dir_val;
  207. dir_val = opendir(path);
  208. if (!dir_val)
  209. return NULL;
  210. dir = bzalloc(sizeof(struct os_dir));
  211. dir->dir = dir_val;
  212. dir->path = path;
  213. return dir;
  214. }
  215. static inline bool is_dir(const char *path)
  216. {
  217. struct stat stat_info;
  218. if (stat(path, &stat_info) == 0)
  219. return !!S_ISDIR(stat_info.st_mode);
  220. blog(LOG_DEBUG, "is_dir: stat for %s failed, errno: %d", path, errno);
  221. return false;
  222. }
  223. struct os_dirent *os_readdir(os_dir_t *dir)
  224. {
  225. struct dstr file_path = {0};
  226. if (!dir) return NULL;
  227. dir->cur_dirent = readdir(dir->dir);
  228. if (!dir->cur_dirent)
  229. return NULL;
  230. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  231. dstr_copy(&file_path, dir->path);
  232. dstr_cat(&file_path, "/");
  233. dstr_cat(&file_path, dir->out.d_name);
  234. dir->out.directory = is_dir(file_path.array);
  235. dstr_free(&file_path);
  236. return &dir->out;
  237. }
  238. void os_closedir(os_dir_t *dir)
  239. {
  240. if (dir) {
  241. closedir(dir->dir);
  242. bfree(dir);
  243. }
  244. }
  245. struct posix_glob_info {
  246. struct os_glob_info base;
  247. glob_t gl;
  248. };
  249. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  250. {
  251. struct posix_glob_info pgi;
  252. int ret = glob(pattern, 0, NULL, &pgi.gl);
  253. if (ret == 0) {
  254. DARRAY(struct os_globent) list;
  255. da_init(list);
  256. for (size_t i = 0; i < pgi.gl.gl_pathc; i++) {
  257. struct os_globent ent = {0};
  258. ent.path = pgi.gl.gl_pathv[i];
  259. ent.directory = is_dir(ent.path);
  260. da_push_back(list, &ent);
  261. }
  262. pgi.base.gl_pathc = list.num;
  263. pgi.base.gl_pathv = list.array;
  264. *pglob = bmemdup(&pgi, sizeof(pgi));
  265. } else {
  266. *pglob = NULL;
  267. }
  268. UNUSED_PARAMETER(flags);
  269. return ret;
  270. }
  271. void os_globfree(os_glob_t *pglob)
  272. {
  273. if (pglob) {
  274. struct posix_glob_info *pgi = (struct posix_glob_info*)pglob;
  275. globfree(&pgi->gl);
  276. bfree(pgi->base.gl_pathv);
  277. bfree(pgi);
  278. }
  279. }
  280. int os_unlink(const char *path)
  281. {
  282. return unlink(path);
  283. }
  284. int os_rmdir(const char *path)
  285. {
  286. return rmdir(path);
  287. }
  288. int os_mkdir(const char *path)
  289. {
  290. if (mkdir(path, 0755) == 0)
  291. return MKDIR_SUCCESS;
  292. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  293. }
  294. int os_rename(const char *old_path, const char *new_path)
  295. {
  296. return rename(old_path, new_path);
  297. }
  298. #if !defined(__APPLE__)
  299. os_performance_token_t *os_request_high_performance(const char *reason)
  300. {
  301. UNUSED_PARAMETER(reason);
  302. return NULL;
  303. }
  304. void os_end_high_performance(os_performance_token_t *token)
  305. {
  306. UNUSED_PARAMETER(token);
  307. }
  308. #endif
  309. int os_copyfile(const char *file_path_in, const char *file_path_out)
  310. {
  311. FILE *file_out = NULL;
  312. FILE *file_in = NULL;
  313. uint8_t data[4096];
  314. int ret = -1;
  315. size_t size;
  316. if (os_file_exists(file_path_out))
  317. return -1;
  318. file_in = fopen(file_path_in, "rb");
  319. if (!file_in)
  320. return -1;
  321. file_out = fopen(file_path_out, "ab+");
  322. if (!file_out)
  323. goto error;
  324. do {
  325. size = fread(data, 1, sizeof(data), file_in);
  326. if (size)
  327. size = fwrite(data, 1, size, file_out);
  328. } while (size == sizeof(data));
  329. ret = feof(file_in) ? 0 : -1;
  330. error:
  331. if (file_out)
  332. fclose(file_out);
  333. fclose(file_in);
  334. return ret;
  335. }
  336. char *os_getcwd(char *path, size_t size)
  337. {
  338. return getcwd(path, size);
  339. }
  340. int os_chdir(const char *path)
  341. {
  342. return chdir(path);
  343. }