oslib-posix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * os-posix-lib.c
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. *
  7. * QEMU library functions on POSIX which are shared between QEMU and
  8. * the QEMU tools.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. /* The following block of code temporarily renames the daemon() function so the
  29. compiler does not see the warning associated with it in stdlib.h on OSX */
  30. #ifdef __APPLE__
  31. #define daemon qemu_fake_daemon_function
  32. #include <stdlib.h>
  33. #undef daemon
  34. extern int daemon(int, int);
  35. #endif
  36. #if defined(__linux__) && defined(__x86_64__)
  37. /* Use 2 MiB alignment so transparent hugepages can be used by KVM.
  38. Valgrind does not support alignments larger than 1 MiB,
  39. therefore we need special code which handles running on Valgrind. */
  40. # define QEMU_VMALLOC_ALIGN (512 * 4096)
  41. # define CONFIG_VALGRIND
  42. #else
  43. # define QEMU_VMALLOC_ALIGN getpagesize()
  44. #endif
  45. #include "config-host.h"
  46. #include "sysemu.h"
  47. #include "trace.h"
  48. #include "qemu_socket.h"
  49. #if defined(CONFIG_VALGRIND)
  50. static int running_on_valgrind = -1;
  51. #else
  52. # define running_on_valgrind 0
  53. #endif
  54. #ifdef CONFIG_LINUX
  55. #include <sys/syscall.h>
  56. #endif
  57. #ifdef CONFIG_EVENTFD
  58. #include <sys/eventfd.h>
  59. #endif
  60. int qemu_get_thread_id(void)
  61. {
  62. #if defined(__linux__)
  63. return syscall(SYS_gettid);
  64. #else
  65. return getpid();
  66. #endif
  67. }
  68. int qemu_daemon(int nochdir, int noclose)
  69. {
  70. return daemon(nochdir, noclose);
  71. }
  72. void *qemu_oom_check(void *ptr)
  73. {
  74. if (ptr == NULL) {
  75. fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
  76. abort();
  77. }
  78. return ptr;
  79. }
  80. void *qemu_memalign(size_t alignment, size_t size)
  81. {
  82. void *ptr;
  83. #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
  84. int ret;
  85. ret = posix_memalign(&ptr, alignment, size);
  86. if (ret != 0) {
  87. fprintf(stderr, "Failed to allocate %zu B: %s\n",
  88. size, strerror(ret));
  89. abort();
  90. }
  91. #elif defined(CONFIG_BSD)
  92. ptr = qemu_oom_check(valloc(size));
  93. #else
  94. ptr = qemu_oom_check(memalign(alignment, size));
  95. #endif
  96. trace_qemu_memalign(alignment, size, ptr);
  97. return ptr;
  98. }
  99. /* alloc shared memory pages */
  100. void *qemu_vmalloc(size_t size)
  101. {
  102. void *ptr;
  103. size_t align = QEMU_VMALLOC_ALIGN;
  104. #if defined(CONFIG_VALGRIND)
  105. if (running_on_valgrind < 0) {
  106. /* First call, test whether we are running on Valgrind.
  107. This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */
  108. const char *ld = getenv("LD_PRELOAD");
  109. running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload"));
  110. }
  111. #endif
  112. if (size < align || running_on_valgrind) {
  113. align = getpagesize();
  114. }
  115. ptr = qemu_memalign(align, size);
  116. trace_qemu_vmalloc(size, ptr);
  117. return ptr;
  118. }
  119. void qemu_vfree(void *ptr)
  120. {
  121. trace_qemu_vfree(ptr);
  122. free(ptr);
  123. }
  124. void socket_set_block(int fd)
  125. {
  126. int f;
  127. f = fcntl(fd, F_GETFL);
  128. fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
  129. }
  130. void socket_set_nonblock(int fd)
  131. {
  132. int f;
  133. f = fcntl(fd, F_GETFL);
  134. fcntl(fd, F_SETFL, f | O_NONBLOCK);
  135. }
  136. void qemu_set_cloexec(int fd)
  137. {
  138. int f;
  139. f = fcntl(fd, F_GETFD);
  140. fcntl(fd, F_SETFD, f | FD_CLOEXEC);
  141. }
  142. /*
  143. * Creates a pipe with FD_CLOEXEC set on both file descriptors
  144. */
  145. int qemu_pipe(int pipefd[2])
  146. {
  147. int ret;
  148. #ifdef CONFIG_PIPE2
  149. ret = pipe2(pipefd, O_CLOEXEC);
  150. if (ret != -1 || errno != ENOSYS) {
  151. return ret;
  152. }
  153. #endif
  154. ret = pipe(pipefd);
  155. if (ret == 0) {
  156. qemu_set_cloexec(pipefd[0]);
  157. qemu_set_cloexec(pipefd[1]);
  158. }
  159. return ret;
  160. }
  161. /*
  162. * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
  163. */
  164. int qemu_eventfd(int fds[2])
  165. {
  166. #ifdef CONFIG_EVENTFD
  167. int ret;
  168. ret = eventfd(0, 0);
  169. if (ret >= 0) {
  170. fds[0] = ret;
  171. fds[1] = dup(ret);
  172. if (fds[1] == -1) {
  173. close(ret);
  174. return -1;
  175. }
  176. qemu_set_cloexec(ret);
  177. qemu_set_cloexec(fds[1]);
  178. return 0;
  179. }
  180. if (errno != ENOSYS) {
  181. return -1;
  182. }
  183. #endif
  184. return qemu_pipe(fds);
  185. }
  186. int qemu_utimens(const char *path, const struct timespec *times)
  187. {
  188. struct timeval tv[2], tv_now;
  189. struct stat st;
  190. int i;
  191. #ifdef CONFIG_UTIMENSAT
  192. int ret;
  193. ret = utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW);
  194. if (ret != -1 || errno != ENOSYS) {
  195. return ret;
  196. }
  197. #endif
  198. /* Fallback: use utimes() instead of utimensat() */
  199. /* happy if special cases */
  200. if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) {
  201. return 0;
  202. }
  203. if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) {
  204. return utimes(path, NULL);
  205. }
  206. /* prepare for hard cases */
  207. if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) {
  208. gettimeofday(&tv_now, NULL);
  209. }
  210. if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) {
  211. stat(path, &st);
  212. }
  213. for (i = 0; i < 2; i++) {
  214. if (times[i].tv_nsec == UTIME_NOW) {
  215. tv[i].tv_sec = tv_now.tv_sec;
  216. tv[i].tv_usec = tv_now.tv_usec;
  217. } else if (times[i].tv_nsec == UTIME_OMIT) {
  218. tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
  219. tv[i].tv_usec = 0;
  220. } else {
  221. tv[i].tv_sec = times[i].tv_sec;
  222. tv[i].tv_usec = times[i].tv_nsec / 1000;
  223. }
  224. }
  225. return utimes(path, &tv[0]);
  226. }