2
0

oslib-posix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "config-host.h"
  37. #include "sysemu.h"
  38. #include "trace.h"
  39. #include "qemu_socket.h"
  40. int qemu_daemon(int nochdir, int noclose)
  41. {
  42. return daemon(nochdir, noclose);
  43. }
  44. void *qemu_oom_check(void *ptr)
  45. {
  46. if (ptr == NULL) {
  47. fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
  48. abort();
  49. }
  50. return ptr;
  51. }
  52. void *qemu_memalign(size_t alignment, size_t size)
  53. {
  54. void *ptr;
  55. #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
  56. int ret;
  57. ret = posix_memalign(&ptr, alignment, size);
  58. if (ret != 0) {
  59. fprintf(stderr, "Failed to allocate %zu B: %s\n",
  60. size, strerror(ret));
  61. abort();
  62. }
  63. #elif defined(CONFIG_BSD)
  64. ptr = qemu_oom_check(valloc(size));
  65. #else
  66. ptr = qemu_oom_check(memalign(alignment, size));
  67. #endif
  68. trace_qemu_memalign(alignment, size, ptr);
  69. return ptr;
  70. }
  71. /* alloc shared memory pages */
  72. void *qemu_vmalloc(size_t size)
  73. {
  74. return qemu_memalign(getpagesize(), size);
  75. }
  76. void qemu_vfree(void *ptr)
  77. {
  78. trace_qemu_vfree(ptr);
  79. free(ptr);
  80. }
  81. void socket_set_nonblock(int fd)
  82. {
  83. int f;
  84. f = fcntl(fd, F_GETFL);
  85. fcntl(fd, F_SETFL, f | O_NONBLOCK);
  86. }
  87. void qemu_set_cloexec(int fd)
  88. {
  89. int f;
  90. f = fcntl(fd, F_GETFD);
  91. fcntl(fd, F_SETFD, f | FD_CLOEXEC);
  92. }
  93. /*
  94. * Creates a pipe with FD_CLOEXEC set on both file descriptors
  95. */
  96. int qemu_pipe(int pipefd[2])
  97. {
  98. int ret;
  99. #ifdef CONFIG_PIPE2
  100. ret = pipe2(pipefd, O_CLOEXEC);
  101. if (ret != -1 || errno != ENOSYS) {
  102. return ret;
  103. }
  104. #endif
  105. ret = pipe(pipefd);
  106. if (ret == 0) {
  107. qemu_set_cloexec(pipefd[0]);
  108. qemu_set_cloexec(pipefd[1]);
  109. }
  110. return ret;
  111. }
  112. int qemu_utimensat(int dirfd, const char *path, const struct timespec *times,
  113. int flags)
  114. {
  115. struct timeval tv[2], tv_now;
  116. struct stat st;
  117. int i;
  118. #ifdef CONFIG_UTIMENSAT
  119. int ret;
  120. ret = utimensat(dirfd, path, times, flags);
  121. if (ret != -1 || errno != ENOSYS) {
  122. return ret;
  123. }
  124. #endif
  125. /* Fallback: use utimes() instead of utimensat() */
  126. /* happy if special cases */
  127. if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) {
  128. return 0;
  129. }
  130. if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) {
  131. return utimes(path, NULL);
  132. }
  133. /* prepare for hard cases */
  134. if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) {
  135. gettimeofday(&tv_now, NULL);
  136. }
  137. if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) {
  138. stat(path, &st);
  139. }
  140. for (i = 0; i < 2; i++) {
  141. if (times[i].tv_nsec == UTIME_NOW) {
  142. tv[i].tv_sec = tv_now.tv_sec;
  143. tv[i].tv_usec = tv_now.tv_usec;
  144. } else if (times[i].tv_nsec == UTIME_OMIT) {
  145. tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
  146. tv[i].tv_usec = 0;
  147. } else {
  148. tv[i].tv_sec = times[i].tv_sec;
  149. tv[i].tv_usec = times[i].tv_nsec / 1000;
  150. }
  151. }
  152. return utimes(path, &tv[0]);
  153. }