oslib-posix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #include "qemu/osdep.h"
  29. #include <termios.h>
  30. #include <termios.h>
  31. #include <glib/gprintf.h>
  32. #include "sysemu/sysemu.h"
  33. #include "trace.h"
  34. #include "qapi/error.h"
  35. #include "qemu/sockets.h"
  36. #include <libgen.h>
  37. #include <sys/signal.h>
  38. #include "qemu/cutils.h"
  39. #ifdef CONFIG_LINUX
  40. #include <sys/syscall.h>
  41. #endif
  42. #ifdef __FreeBSD__
  43. #include <sys/sysctl.h>
  44. #endif
  45. #include <qemu/mmap-alloc.h>
  46. int qemu_get_thread_id(void)
  47. {
  48. #if defined(__linux__)
  49. return syscall(SYS_gettid);
  50. #else
  51. return getpid();
  52. #endif
  53. }
  54. int qemu_daemon(int nochdir, int noclose)
  55. {
  56. return daemon(nochdir, noclose);
  57. }
  58. void *qemu_oom_check(void *ptr)
  59. {
  60. if (ptr == NULL) {
  61. fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
  62. abort();
  63. }
  64. return ptr;
  65. }
  66. void *qemu_try_memalign(size_t alignment, size_t size)
  67. {
  68. void *ptr;
  69. if (alignment < sizeof(void*)) {
  70. alignment = sizeof(void*);
  71. }
  72. #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
  73. int ret;
  74. ret = posix_memalign(&ptr, alignment, size);
  75. if (ret != 0) {
  76. errno = ret;
  77. ptr = NULL;
  78. }
  79. #elif defined(CONFIG_BSD)
  80. ptr = valloc(size);
  81. #else
  82. ptr = memalign(alignment, size);
  83. #endif
  84. trace_qemu_memalign(alignment, size, ptr);
  85. return ptr;
  86. }
  87. void *qemu_memalign(size_t alignment, size_t size)
  88. {
  89. return qemu_oom_check(qemu_try_memalign(alignment, size));
  90. }
  91. /* alloc shared memory pages */
  92. void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment)
  93. {
  94. size_t align = QEMU_VMALLOC_ALIGN;
  95. void *ptr = qemu_ram_mmap(-1, size, align, false);
  96. if (ptr == MAP_FAILED) {
  97. return NULL;
  98. }
  99. if (alignment) {
  100. *alignment = align;
  101. }
  102. trace_qemu_anon_ram_alloc(size, ptr);
  103. return ptr;
  104. }
  105. void qemu_vfree(void *ptr)
  106. {
  107. trace_qemu_vfree(ptr);
  108. free(ptr);
  109. }
  110. void qemu_anon_ram_free(void *ptr, size_t size)
  111. {
  112. trace_qemu_anon_ram_free(ptr, size);
  113. qemu_ram_munmap(ptr, size);
  114. }
  115. void qemu_set_block(int fd)
  116. {
  117. int f;
  118. f = fcntl(fd, F_GETFL);
  119. fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
  120. }
  121. void qemu_set_nonblock(int fd)
  122. {
  123. int f;
  124. f = fcntl(fd, F_GETFL);
  125. fcntl(fd, F_SETFL, f | O_NONBLOCK);
  126. }
  127. int socket_set_fast_reuse(int fd)
  128. {
  129. int val = 1, ret;
  130. ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
  131. (const char *)&val, sizeof(val));
  132. assert(ret == 0);
  133. return ret;
  134. }
  135. void qemu_set_cloexec(int fd)
  136. {
  137. int f;
  138. f = fcntl(fd, F_GETFD);
  139. fcntl(fd, F_SETFD, f | FD_CLOEXEC);
  140. }
  141. /*
  142. * Creates a pipe with FD_CLOEXEC set on both file descriptors
  143. */
  144. int qemu_pipe(int pipefd[2])
  145. {
  146. int ret;
  147. #ifdef CONFIG_PIPE2
  148. ret = pipe2(pipefd, O_CLOEXEC);
  149. if (ret != -1 || errno != ENOSYS) {
  150. return ret;
  151. }
  152. #endif
  153. ret = pipe(pipefd);
  154. if (ret == 0) {
  155. qemu_set_cloexec(pipefd[0]);
  156. qemu_set_cloexec(pipefd[1]);
  157. }
  158. return ret;
  159. }
  160. int qemu_utimens(const char *path, const struct timespec *times)
  161. {
  162. struct timeval tv[2], tv_now;
  163. struct stat st;
  164. int i;
  165. #ifdef CONFIG_UTIMENSAT
  166. int ret;
  167. ret = utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW);
  168. if (ret != -1 || errno != ENOSYS) {
  169. return ret;
  170. }
  171. #endif
  172. /* Fallback: use utimes() instead of utimensat() */
  173. /* happy if special cases */
  174. if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) {
  175. return 0;
  176. }
  177. if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) {
  178. return utimes(path, NULL);
  179. }
  180. /* prepare for hard cases */
  181. if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) {
  182. gettimeofday(&tv_now, NULL);
  183. }
  184. if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) {
  185. stat(path, &st);
  186. }
  187. for (i = 0; i < 2; i++) {
  188. if (times[i].tv_nsec == UTIME_NOW) {
  189. tv[i].tv_sec = tv_now.tv_sec;
  190. tv[i].tv_usec = tv_now.tv_usec;
  191. } else if (times[i].tv_nsec == UTIME_OMIT) {
  192. tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
  193. tv[i].tv_usec = 0;
  194. } else {
  195. tv[i].tv_sec = times[i].tv_sec;
  196. tv[i].tv_usec = times[i].tv_nsec / 1000;
  197. }
  198. }
  199. return utimes(path, &tv[0]);
  200. }
  201. char *
  202. qemu_get_local_state_pathname(const char *relative_pathname)
  203. {
  204. return g_strdup_printf("%s/%s", CONFIG_QEMU_LOCALSTATEDIR,
  205. relative_pathname);
  206. }
  207. void qemu_set_tty_echo(int fd, bool echo)
  208. {
  209. struct termios tty;
  210. tcgetattr(fd, &tty);
  211. if (echo) {
  212. tty.c_lflag |= ECHO | ECHONL | ICANON | IEXTEN;
  213. } else {
  214. tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
  215. }
  216. tcsetattr(fd, TCSANOW, &tty);
  217. }
  218. static char exec_dir[PATH_MAX];
  219. void qemu_init_exec_dir(const char *argv0)
  220. {
  221. char *dir;
  222. char *p = NULL;
  223. char buf[PATH_MAX];
  224. assert(!exec_dir[0]);
  225. #if defined(__linux__)
  226. {
  227. int len;
  228. len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  229. if (len > 0) {
  230. buf[len] = 0;
  231. p = buf;
  232. }
  233. }
  234. #elif defined(__FreeBSD__)
  235. {
  236. static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
  237. size_t len = sizeof(buf) - 1;
  238. *buf = '\0';
  239. if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
  240. *buf) {
  241. buf[sizeof(buf) - 1] = '\0';
  242. p = buf;
  243. }
  244. }
  245. #endif
  246. /* If we don't have any way of figuring out the actual executable
  247. location then try argv[0]. */
  248. if (!p) {
  249. if (!argv0) {
  250. return;
  251. }
  252. p = realpath(argv0, buf);
  253. if (!p) {
  254. return;
  255. }
  256. }
  257. dir = dirname(p);
  258. pstrcpy(exec_dir, sizeof(exec_dir), dir);
  259. }
  260. char *qemu_get_exec_dir(void)
  261. {
  262. return g_strdup(exec_dir);
  263. }
  264. static sigjmp_buf sigjump;
  265. static void sigbus_handler(int signal)
  266. {
  267. siglongjmp(sigjump, 1);
  268. }
  269. void os_mem_prealloc(int fd, char *area, size_t memory)
  270. {
  271. int ret;
  272. struct sigaction act, oldact;
  273. sigset_t set, oldset;
  274. memset(&act, 0, sizeof(act));
  275. act.sa_handler = &sigbus_handler;
  276. act.sa_flags = 0;
  277. ret = sigaction(SIGBUS, &act, &oldact);
  278. if (ret) {
  279. perror("os_mem_prealloc: failed to install signal handler");
  280. exit(1);
  281. }
  282. /* unblock SIGBUS */
  283. sigemptyset(&set);
  284. sigaddset(&set, SIGBUS);
  285. pthread_sigmask(SIG_UNBLOCK, &set, &oldset);
  286. if (sigsetjmp(sigjump, 1)) {
  287. fprintf(stderr, "os_mem_prealloc: Insufficient free host memory "
  288. "pages available to allocate guest RAM\n");
  289. exit(1);
  290. } else {
  291. int i;
  292. size_t hpagesize = qemu_fd_getpagesize(fd);
  293. size_t numpages = DIV_ROUND_UP(memory, hpagesize);
  294. /* MAP_POPULATE silently ignores failures */
  295. for (i = 0; i < numpages; i++) {
  296. memset(area + (hpagesize * i), 0, 1);
  297. }
  298. ret = sigaction(SIGBUS, &oldact, NULL);
  299. if (ret) {
  300. perror("os_mem_prealloc: failed to reinstall signal handler");
  301. exit(1);
  302. }
  303. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  304. }
  305. }
  306. static struct termios oldtty;
  307. static void term_exit(void)
  308. {
  309. tcsetattr(0, TCSANOW, &oldtty);
  310. }
  311. static void term_init(void)
  312. {
  313. struct termios tty;
  314. tcgetattr(0, &tty);
  315. oldtty = tty;
  316. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  317. |INLCR|IGNCR|ICRNL|IXON);
  318. tty.c_oflag |= OPOST;
  319. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  320. tty.c_cflag &= ~(CSIZE|PARENB);
  321. tty.c_cflag |= CS8;
  322. tty.c_cc[VMIN] = 1;
  323. tty.c_cc[VTIME] = 0;
  324. tcsetattr(0, TCSANOW, &tty);
  325. atexit(term_exit);
  326. }
  327. int qemu_read_password(char *buf, int buf_size)
  328. {
  329. uint8_t ch;
  330. int i, ret;
  331. printf("password: ");
  332. fflush(stdout);
  333. term_init();
  334. i = 0;
  335. for (;;) {
  336. ret = read(0, &ch, 1);
  337. if (ret == -1) {
  338. if (errno == EAGAIN || errno == EINTR) {
  339. continue;
  340. } else {
  341. break;
  342. }
  343. } else if (ret == 0) {
  344. ret = -1;
  345. break;
  346. } else {
  347. if (ch == '\r' ||
  348. ch == '\n') {
  349. ret = 0;
  350. break;
  351. }
  352. if (i < (buf_size - 1)) {
  353. buf[i++] = ch;
  354. }
  355. }
  356. }
  357. term_exit();
  358. buf[i] = '\0';
  359. printf("\n");
  360. return ret;
  361. }
  362. pid_t qemu_fork(Error **errp)
  363. {
  364. sigset_t oldmask, newmask;
  365. struct sigaction sig_action;
  366. int saved_errno;
  367. pid_t pid;
  368. /*
  369. * Need to block signals now, so that child process can safely
  370. * kill off caller's signal handlers without a race.
  371. */
  372. sigfillset(&newmask);
  373. if (pthread_sigmask(SIG_SETMASK, &newmask, &oldmask) != 0) {
  374. error_setg_errno(errp, errno,
  375. "cannot block signals");
  376. return -1;
  377. }
  378. pid = fork();
  379. saved_errno = errno;
  380. if (pid < 0) {
  381. /* attempt to restore signal mask, but ignore failure, to
  382. * avoid obscuring the fork failure */
  383. (void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
  384. error_setg_errno(errp, saved_errno,
  385. "cannot fork child process");
  386. errno = saved_errno;
  387. return -1;
  388. } else if (pid) {
  389. /* parent process */
  390. /* Restore our original signal mask now that the child is
  391. * safely running. Only documented failures are EFAULT (not
  392. * possible, since we are using just-grabbed mask) or EINVAL
  393. * (not possible, since we are using correct arguments). */
  394. (void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
  395. } else {
  396. /* child process */
  397. size_t i;
  398. /* Clear out all signal handlers from parent so nothing
  399. * unexpected can happen in our child once we unblock
  400. * signals */
  401. sig_action.sa_handler = SIG_DFL;
  402. sig_action.sa_flags = 0;
  403. sigemptyset(&sig_action.sa_mask);
  404. for (i = 1; i < NSIG; i++) {
  405. /* Only possible errors are EFAULT or EINVAL The former
  406. * won't happen, the latter we expect, so no need to check
  407. * return value */
  408. (void)sigaction(i, &sig_action, NULL);
  409. }
  410. /* Unmask all signals in child, since we've no idea what the
  411. * caller's done with their signal mask and don't want to
  412. * propagate that to children */
  413. sigemptyset(&newmask);
  414. if (pthread_sigmask(SIG_SETMASK, &newmask, NULL) != 0) {
  415. Error *local_err = NULL;
  416. error_setg_errno(&local_err, errno,
  417. "cannot unblock signals");
  418. error_report_err(local_err);
  419. _exit(1);
  420. }
  421. }
  422. return pid;
  423. }