platform-nix.c 2.6 KB

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