utils.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * utils - misc libubox utility functions
  3. *
  4. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/mman.h>
  19. #include <errno.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "utils.h"
  25. #define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
  26. for (_addr = (_first_addr), _len = (_first_len); \
  27. _addr; \
  28. _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg, size_t) : 0)
  29. #define C_PTR_ALIGN (sizeof(size_t))
  30. #define C_PTR_MASK (-C_PTR_ALIGN)
  31. void *__calloc_a(size_t len, ...)
  32. {
  33. va_list ap, ap1;
  34. void *ret;
  35. void **cur_addr;
  36. size_t cur_len;
  37. int alloc_len = 0;
  38. char *ptr;
  39. va_start(ap, len);
  40. va_copy(ap1, ap);
  41. foreach_arg(ap1, cur_addr, cur_len, &ret, len)
  42. alloc_len += (cur_len + C_PTR_ALIGN - 1 ) & C_PTR_MASK;
  43. va_end(ap1);
  44. ptr = calloc(1, alloc_len);
  45. if (!ptr) {
  46. va_end(ap);
  47. return NULL;
  48. }
  49. alloc_len = 0;
  50. foreach_arg(ap, cur_addr, cur_len, &ret, len) {
  51. *cur_addr = &ptr[alloc_len];
  52. alloc_len += (cur_len + C_PTR_ALIGN - 1) & C_PTR_MASK;
  53. }
  54. va_end(ap);
  55. return ret;
  56. }
  57. #ifdef LIBUBOX_COMPAT_CLOCK_GETTIME
  58. #include <mach/mach_host.h> /* host_get_clock_service() */
  59. #include <mach/mach_port.h> /* mach_port_deallocate() */
  60. #include <mach/mach_init.h> /* mach_host_self(), mach_task_self() */
  61. #include <mach/clock.h> /* clock_get_time() */
  62. static clock_serv_t clock_realtime;
  63. static clock_serv_t clock_monotonic;
  64. static void __constructor clock_name_init(void)
  65. {
  66. mach_port_t host_self = mach_host_self();
  67. host_get_clock_service(host_self, CLOCK_REALTIME, &clock_realtime);
  68. host_get_clock_service(host_self, CLOCK_MONOTONIC, &clock_monotonic);
  69. }
  70. static void __destructor clock_name_dealloc(void)
  71. {
  72. mach_port_t self = mach_task_self();
  73. mach_port_deallocate(self, clock_realtime);
  74. mach_port_deallocate(self, clock_monotonic);
  75. }
  76. int clock_gettime(int type, struct timespec *tv)
  77. {
  78. int retval = -1;
  79. mach_timespec_t mts;
  80. switch (type) {
  81. case CLOCK_REALTIME:
  82. retval = clock_get_time(clock_realtime, &mts);
  83. break;
  84. case CLOCK_MONOTONIC:
  85. retval = clock_get_time(clock_monotonic, &mts);
  86. break;
  87. default:
  88. goto out;
  89. }
  90. tv->tv_sec = mts.tv_sec;
  91. tv->tv_nsec = mts.tv_nsec;
  92. out:
  93. return retval;
  94. }
  95. #endif
  96. void *cbuf_alloc(unsigned int order)
  97. {
  98. char path[] = "/tmp/cbuf-XXXXXX";
  99. unsigned long size = cbuf_size(order);
  100. void *ret = NULL;
  101. int fd;
  102. fd = mkstemp(path);
  103. if (fd < 0)
  104. return NULL;
  105. if (unlink(path))
  106. goto close;
  107. if (ftruncate(fd, cbuf_size(order)))
  108. goto close;
  109. #ifndef MAP_ANONYMOUS
  110. #define MAP_ANONYMOUS MAP_ANON
  111. #endif
  112. ret = mmap(NULL, size * 2, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
  113. if (ret == MAP_FAILED) {
  114. ret = NULL;
  115. goto close;
  116. }
  117. if (mmap(ret, size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED,
  118. fd, 0) != ret ||
  119. mmap(ret + size, size, PROT_READ | PROT_WRITE,
  120. MAP_FIXED | MAP_SHARED, fd, 0) != ret + size) {
  121. munmap(ret, size * 2);
  122. ret = NULL;
  123. }
  124. close:
  125. close(fd);
  126. return ret;
  127. }
  128. void cbuf_free(void *ptr, unsigned int order)
  129. {
  130. munmap(ptr, cbuf_size(order) * 2);
  131. }
  132. int mkdir_p(char *dir, mode_t mask)
  133. {
  134. char *l;
  135. int ret;
  136. ret = mkdir(dir, mask);
  137. if (!ret || errno == EEXIST)
  138. return 0;
  139. if (ret && (errno != ENOENT))
  140. return -1;
  141. l = strrchr(dir, '/');
  142. if (!l || l == dir)
  143. return -1;
  144. *l = '\0';
  145. if (mkdir_p(dir, mask))
  146. return -1;
  147. *l = '/';
  148. ret = mkdir(dir, mask);
  149. if (!ret || errno == EEXIST)
  150. return 0;
  151. else
  152. return -1;
  153. }