2
0

oslib-posix.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #elif defined(__linux__) && defined(__s390x__)
  43. /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */
  44. # define QEMU_VMALLOC_ALIGN (256 * 4096)
  45. #else
  46. # define QEMU_VMALLOC_ALIGN getpagesize()
  47. #endif
  48. #include "config-host.h"
  49. #include "sysemu.h"
  50. #include "trace.h"
  51. #include "qemu_socket.h"
  52. #if defined(CONFIG_VALGRIND)
  53. static int running_on_valgrind = -1;
  54. #else
  55. # define running_on_valgrind 0
  56. #endif
  57. #ifdef CONFIG_LINUX
  58. #include <sys/syscall.h>
  59. #endif
  60. #ifdef CONFIG_EVENTFD
  61. #include <sys/eventfd.h>
  62. #endif
  63. int qemu_get_thread_id(void)
  64. {
  65. #if defined(__linux__)
  66. return syscall(SYS_gettid);
  67. #else
  68. return getpid();
  69. #endif
  70. }
  71. int qemu_daemon(int nochdir, int noclose)
  72. {
  73. return daemon(nochdir, noclose);
  74. }
  75. void *qemu_oom_check(void *ptr)
  76. {
  77. if (ptr == NULL) {
  78. fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
  79. abort();
  80. }
  81. return ptr;
  82. }
  83. void *qemu_memalign(size_t alignment, size_t size)
  84. {
  85. void *ptr;
  86. #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
  87. int ret;
  88. ret = posix_memalign(&ptr, alignment, size);
  89. if (ret != 0) {
  90. fprintf(stderr, "Failed to allocate %zu B: %s\n",
  91. size, strerror(ret));
  92. abort();
  93. }
  94. #elif defined(CONFIG_BSD)
  95. ptr = qemu_oom_check(valloc(size));
  96. #else
  97. ptr = qemu_oom_check(memalign(alignment, size));
  98. #endif
  99. trace_qemu_memalign(alignment, size, ptr);
  100. return ptr;
  101. }
  102. /* conflicts with qemu_vmalloc in bsd-user/mmap.c */
  103. #if !defined(CONFIG_BSD_USER)
  104. /* alloc shared memory pages */
  105. void *qemu_vmalloc(size_t size)
  106. {
  107. void *ptr;
  108. size_t align = QEMU_VMALLOC_ALIGN;
  109. #if defined(CONFIG_VALGRIND)
  110. if (running_on_valgrind < 0) {
  111. /* First call, test whether we are running on Valgrind.
  112. This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */
  113. const char *ld = getenv("LD_PRELOAD");
  114. running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload"));
  115. }
  116. #endif
  117. if (size < align || running_on_valgrind) {
  118. align = getpagesize();
  119. }
  120. ptr = qemu_memalign(align, size);
  121. trace_qemu_vmalloc(size, ptr);
  122. return ptr;
  123. }
  124. #endif
  125. void qemu_vfree(void *ptr)
  126. {
  127. trace_qemu_vfree(ptr);
  128. free(ptr);
  129. }
  130. void socket_set_block(int fd)
  131. {
  132. int f;
  133. f = fcntl(fd, F_GETFL);
  134. fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
  135. }
  136. void socket_set_nonblock(int fd)
  137. {
  138. int f;
  139. f = fcntl(fd, F_GETFL);
  140. fcntl(fd, F_SETFL, f | O_NONBLOCK);
  141. }
  142. void qemu_set_cloexec(int fd)
  143. {
  144. int f;
  145. f = fcntl(fd, F_GETFD);
  146. fcntl(fd, F_SETFD, f | FD_CLOEXEC);
  147. }
  148. /*
  149. * Creates a pipe with FD_CLOEXEC set on both file descriptors
  150. */
  151. int qemu_pipe(int pipefd[2])
  152. {
  153. int ret;
  154. #ifdef CONFIG_PIPE2
  155. ret = pipe2(pipefd, O_CLOEXEC);
  156. if (ret != -1 || errno != ENOSYS) {
  157. return ret;
  158. }
  159. #endif
  160. ret = pipe(pipefd);
  161. if (ret == 0) {
  162. qemu_set_cloexec(pipefd[0]);
  163. qemu_set_cloexec(pipefd[1]);
  164. }
  165. return ret;
  166. }
  167. /*
  168. * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
  169. */
  170. int qemu_eventfd(int fds[2])
  171. {
  172. #ifdef CONFIG_EVENTFD
  173. int ret;
  174. ret = eventfd(0, 0);
  175. if (ret >= 0) {
  176. fds[0] = ret;
  177. fds[1] = dup(ret);
  178. if (fds[1] == -1) {
  179. close(ret);
  180. return -1;
  181. }
  182. qemu_set_cloexec(ret);
  183. qemu_set_cloexec(fds[1]);
  184. return 0;
  185. }
  186. if (errno != ENOSYS) {
  187. return -1;
  188. }
  189. #endif
  190. return qemu_pipe(fds);
  191. }
  192. int qemu_utimens(const char *path, const struct timespec *times)
  193. {
  194. struct timeval tv[2], tv_now;
  195. struct stat st;
  196. int i;
  197. #ifdef CONFIG_UTIMENSAT
  198. int ret;
  199. ret = utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW);
  200. if (ret != -1 || errno != ENOSYS) {
  201. return ret;
  202. }
  203. #endif
  204. /* Fallback: use utimes() instead of utimensat() */
  205. /* happy if special cases */
  206. if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) {
  207. return 0;
  208. }
  209. if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) {
  210. return utimes(path, NULL);
  211. }
  212. /* prepare for hard cases */
  213. if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) {
  214. gettimeofday(&tv_now, NULL);
  215. }
  216. if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) {
  217. stat(path, &st);
  218. }
  219. for (i = 0; i < 2; i++) {
  220. if (times[i].tv_nsec == UTIME_NOW) {
  221. tv[i].tv_sec = tv_now.tv_sec;
  222. tv[i].tv_usec = tv_now.tv_usec;
  223. } else if (times[i].tv_nsec == UTIME_OMIT) {
  224. tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
  225. tv[i].tv_usec = 0;
  226. } else {
  227. tv[i].tv_sec = times[i].tv_sec;
  228. tv[i].tv_usec = times[i].tv_nsec / 1000;
  229. }
  230. }
  231. return utimes(path, &tv[0]);
  232. }