platform-nix.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <errno.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <stdlib.h>
  20. #include <dlfcn.h>
  21. #include <unistd.h>
  22. #include <time.h>
  23. #include "dstr.h"
  24. #include "platform.h"
  25. void *os_dlopen(const char *path)
  26. {
  27. struct dstr dylib_name;
  28. if (!path)
  29. return NULL;
  30. dstr_init_copy(&dylib_name, path);
  31. if (!dstr_find(&dylib_name, ".so"))
  32. dstr_cat(&dylib_name, ".so");
  33. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  34. if (!res)
  35. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  36. path, dylib_name.array, dlerror());
  37. dstr_free(&dylib_name);
  38. return res;
  39. }
  40. void *os_dlsym(void *module, const char *func)
  41. {
  42. return dlsym(module, func);
  43. }
  44. void os_dlclose(void *module)
  45. {
  46. dlclose(module);
  47. }
  48. bool os_sleepto_ns(uint64_t time_target)
  49. {
  50. uint64_t current = os_gettime_ns();
  51. if (time_target < current)
  52. return false;
  53. time_target -= current;
  54. struct timespec req, remain;
  55. memset(&req, 0, sizeof(req));
  56. memset(&remain, 0, sizeof(remain));
  57. req.tv_sec = time_target/1000000000;
  58. req.tv_nsec = time_target%1000000000;
  59. while (nanosleep(&req, &remain)) {
  60. req = remain;
  61. memset(&remain, 0, sizeof(remain));
  62. }
  63. return true;
  64. }
  65. void os_sleep_ms(uint32_t duration)
  66. {
  67. usleep(duration*1000);
  68. }
  69. uint64_t os_gettime_ns(void)
  70. {
  71. struct timespec ts;
  72. clock_gettime(CLOCK_MONOTONIC, &ts);
  73. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  74. }
  75. /* should return $HOME/.[name] */
  76. char *os_get_config_path(const char *name)
  77. {
  78. char *path_ptr = getenv("HOME");
  79. if (path_ptr == NULL)
  80. bcrash("Could not get $HOME\n");
  81. struct dstr path;
  82. dstr_init_copy(&path, path_ptr);
  83. dstr_cat(&path, "/.");
  84. dstr_cat(&path, name);
  85. return path.array;
  86. }
  87. bool os_file_exists(const char *path)
  88. {
  89. return access(path, F_OK) == 0;
  90. }
  91. int os_mkdir(const char *path)
  92. {
  93. int errorcode = mkdir(path, S_IRWXU);
  94. if (errorcode == 0)
  95. return MKDIR_SUCCESS;
  96. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  97. }