2
0

oslib-posix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 <glib/gprintf.h>
  31. #include "qemu-common.h"
  32. #include "sysemu/sysemu.h"
  33. #include "trace.h"
  34. #include "qapi/error.h"
  35. #include "qemu/sockets.h"
  36. #include "qemu/thread.h"
  37. #include <libgen.h>
  38. #include <sys/signal.h>
  39. #include "qemu/cutils.h"
  40. #ifdef CONFIG_LINUX
  41. #include <sys/syscall.h>
  42. #endif
  43. #ifdef __FreeBSD__
  44. #include <sys/sysctl.h>
  45. #include <sys/user.h>
  46. #include <libutil.h>
  47. #endif
  48. #ifdef __NetBSD__
  49. #include <sys/sysctl.h>
  50. #endif
  51. #include "qemu/mmap-alloc.h"
  52. #ifdef CONFIG_DEBUG_STACK_USAGE
  53. #include "qemu/error-report.h"
  54. #endif
  55. #define MAX_MEM_PREALLOC_THREAD_COUNT 16
  56. struct MemsetThread {
  57. char *addr;
  58. size_t numpages;
  59. size_t hpagesize;
  60. QemuThread pgthread;
  61. sigjmp_buf env;
  62. };
  63. typedef struct MemsetThread MemsetThread;
  64. static MemsetThread *memset_thread;
  65. static int memset_num_threads;
  66. static bool memset_thread_failed;
  67. int qemu_get_thread_id(void)
  68. {
  69. #if defined(__linux__)
  70. return syscall(SYS_gettid);
  71. #else
  72. return getpid();
  73. #endif
  74. }
  75. int qemu_daemon(int nochdir, int noclose)
  76. {
  77. return daemon(nochdir, noclose);
  78. }
  79. bool qemu_write_pidfile(const char *path, Error **errp)
  80. {
  81. int fd;
  82. char pidstr[32];
  83. while (1) {
  84. struct stat a, b;
  85. struct flock lock = {
  86. .l_type = F_WRLCK,
  87. .l_whence = SEEK_SET,
  88. .l_len = 0,
  89. };
  90. fd = qemu_open(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
  91. if (fd == -1) {
  92. error_setg_errno(errp, errno, "Cannot open pid file");
  93. return false;
  94. }
  95. if (fstat(fd, &b) < 0) {
  96. error_setg_errno(errp, errno, "Cannot stat file");
  97. goto fail_close;
  98. }
  99. if (fcntl(fd, F_SETLK, &lock)) {
  100. error_setg_errno(errp, errno, "Cannot lock pid file");
  101. goto fail_close;
  102. }
  103. /*
  104. * Now make sure the path we locked is the same one that now
  105. * exists on the filesystem.
  106. */
  107. if (stat(path, &a) < 0) {
  108. /*
  109. * PID file disappeared, someone else must be racing with
  110. * us, so try again.
  111. */
  112. close(fd);
  113. continue;
  114. }
  115. if (a.st_ino == b.st_ino) {
  116. break;
  117. }
  118. /*
  119. * PID file was recreated, someone else must be racing with
  120. * us, so try again.
  121. */
  122. close(fd);
  123. }
  124. if (ftruncate(fd, 0) < 0) {
  125. error_setg_errno(errp, errno, "Failed to truncate pid file");
  126. goto fail_unlink;
  127. }
  128. snprintf(pidstr, sizeof(pidstr), FMT_pid "\n", getpid());
  129. if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
  130. error_setg(errp, "Failed to write pid file");
  131. goto fail_unlink;
  132. }
  133. return true;
  134. fail_unlink:
  135. unlink(path);
  136. fail_close:
  137. close(fd);
  138. return false;
  139. }
  140. void *qemu_oom_check(void *ptr)
  141. {
  142. if (ptr == NULL) {
  143. fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
  144. abort();
  145. }
  146. return ptr;
  147. }
  148. void *qemu_try_memalign(size_t alignment, size_t size)
  149. {
  150. void *ptr;
  151. if (alignment < sizeof(void*)) {
  152. alignment = sizeof(void*);
  153. }
  154. #if defined(CONFIG_POSIX_MEMALIGN)
  155. int ret;
  156. ret = posix_memalign(&ptr, alignment, size);
  157. if (ret != 0) {
  158. errno = ret;
  159. ptr = NULL;
  160. }
  161. #elif defined(CONFIG_BSD)
  162. ptr = valloc(size);
  163. #else
  164. ptr = memalign(alignment, size);
  165. #endif
  166. trace_qemu_memalign(alignment, size, ptr);
  167. return ptr;
  168. }
  169. void *qemu_memalign(size_t alignment, size_t size)
  170. {
  171. return qemu_oom_check(qemu_try_memalign(alignment, size));
  172. }
  173. /* alloc shared memory pages */
  174. void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared)
  175. {
  176. size_t align = QEMU_VMALLOC_ALIGN;
  177. void *ptr = qemu_ram_mmap(-1, size, align, shared, false);
  178. if (ptr == MAP_FAILED) {
  179. return NULL;
  180. }
  181. if (alignment) {
  182. *alignment = align;
  183. }
  184. trace_qemu_anon_ram_alloc(size, ptr);
  185. return ptr;
  186. }
  187. void qemu_vfree(void *ptr)
  188. {
  189. trace_qemu_vfree(ptr);
  190. free(ptr);
  191. }
  192. void qemu_anon_ram_free(void *ptr, size_t size)
  193. {
  194. trace_qemu_anon_ram_free(ptr, size);
  195. qemu_ram_munmap(-1, ptr, size);
  196. }
  197. void qemu_set_block(int fd)
  198. {
  199. int f;
  200. f = fcntl(fd, F_GETFL);
  201. assert(f != -1);
  202. f = fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
  203. assert(f != -1);
  204. }
  205. void qemu_set_nonblock(int fd)
  206. {
  207. int f;
  208. f = fcntl(fd, F_GETFL);
  209. assert(f != -1);
  210. f = fcntl(fd, F_SETFL, f | O_NONBLOCK);
  211. #ifdef __OpenBSD__
  212. if (f == -1) {
  213. /*
  214. * Previous to OpenBSD 6.3, fcntl(F_SETFL) is not permitted on
  215. * memory devices and sets errno to ENODEV.
  216. * It's OK if we fail to set O_NONBLOCK on devices like /dev/null,
  217. * because they will never block anyway.
  218. */
  219. assert(errno == ENODEV);
  220. }
  221. #else
  222. assert(f != -1);
  223. #endif
  224. }
  225. int socket_set_fast_reuse(int fd)
  226. {
  227. int val = 1, ret;
  228. ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
  229. (const char *)&val, sizeof(val));
  230. assert(ret == 0);
  231. return ret;
  232. }
  233. void qemu_set_cloexec(int fd)
  234. {
  235. int f;
  236. f = fcntl(fd, F_GETFD);
  237. assert(f != -1);
  238. f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
  239. assert(f != -1);
  240. }
  241. /*
  242. * Creates a pipe with FD_CLOEXEC set on both file descriptors
  243. */
  244. int qemu_pipe(int pipefd[2])
  245. {
  246. int ret;
  247. #ifdef CONFIG_PIPE2
  248. ret = pipe2(pipefd, O_CLOEXEC);
  249. if (ret != -1 || errno != ENOSYS) {
  250. return ret;
  251. }
  252. #endif
  253. ret = pipe(pipefd);
  254. if (ret == 0) {
  255. qemu_set_cloexec(pipefd[0]);
  256. qemu_set_cloexec(pipefd[1]);
  257. }
  258. return ret;
  259. }
  260. char *
  261. qemu_get_local_state_pathname(const char *relative_pathname)
  262. {
  263. return g_strdup_printf("%s/%s", CONFIG_QEMU_LOCALSTATEDIR,
  264. relative_pathname);
  265. }
  266. void qemu_set_tty_echo(int fd, bool echo)
  267. {
  268. struct termios tty;
  269. tcgetattr(fd, &tty);
  270. if (echo) {
  271. tty.c_lflag |= ECHO | ECHONL | ICANON | IEXTEN;
  272. } else {
  273. tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
  274. }
  275. tcsetattr(fd, TCSANOW, &tty);
  276. }
  277. static char exec_dir[PATH_MAX];
  278. void qemu_init_exec_dir(const char *argv0)
  279. {
  280. char *dir;
  281. char *p = NULL;
  282. char buf[PATH_MAX];
  283. assert(!exec_dir[0]);
  284. #if defined(__linux__)
  285. {
  286. int len;
  287. len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  288. if (len > 0) {
  289. buf[len] = 0;
  290. p = buf;
  291. }
  292. }
  293. #elif defined(__FreeBSD__) \
  294. || (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME))
  295. {
  296. #if defined(__FreeBSD__)
  297. static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
  298. #else
  299. static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
  300. #endif
  301. size_t len = sizeof(buf) - 1;
  302. *buf = '\0';
  303. if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
  304. *buf) {
  305. buf[sizeof(buf) - 1] = '\0';
  306. p = buf;
  307. }
  308. }
  309. #endif
  310. /* If we don't have any way of figuring out the actual executable
  311. location then try argv[0]. */
  312. if (!p) {
  313. if (!argv0) {
  314. return;
  315. }
  316. p = realpath(argv0, buf);
  317. if (!p) {
  318. return;
  319. }
  320. }
  321. dir = g_path_get_dirname(p);
  322. pstrcpy(exec_dir, sizeof(exec_dir), dir);
  323. g_free(dir);
  324. }
  325. char *qemu_get_exec_dir(void)
  326. {
  327. return g_strdup(exec_dir);
  328. }
  329. static void sigbus_handler(int signal)
  330. {
  331. int i;
  332. if (memset_thread) {
  333. for (i = 0; i < memset_num_threads; i++) {
  334. if (qemu_thread_is_self(&memset_thread[i].pgthread)) {
  335. siglongjmp(memset_thread[i].env, 1);
  336. }
  337. }
  338. }
  339. }
  340. static void *do_touch_pages(void *arg)
  341. {
  342. MemsetThread *memset_args = (MemsetThread *)arg;
  343. sigset_t set, oldset;
  344. /* unblock SIGBUS */
  345. sigemptyset(&set);
  346. sigaddset(&set, SIGBUS);
  347. pthread_sigmask(SIG_UNBLOCK, &set, &oldset);
  348. if (sigsetjmp(memset_args->env, 1)) {
  349. memset_thread_failed = true;
  350. } else {
  351. char *addr = memset_args->addr;
  352. size_t numpages = memset_args->numpages;
  353. size_t hpagesize = memset_args->hpagesize;
  354. size_t i;
  355. for (i = 0; i < numpages; i++) {
  356. /*
  357. * Read & write back the same value, so we don't
  358. * corrupt existing user/app data that might be
  359. * stored.
  360. *
  361. * 'volatile' to stop compiler optimizing this away
  362. * to a no-op
  363. *
  364. * TODO: get a better solution from kernel so we
  365. * don't need to write at all so we don't cause
  366. * wear on the storage backing the region...
  367. */
  368. *(volatile char *)addr = *addr;
  369. addr += hpagesize;
  370. }
  371. }
  372. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  373. return NULL;
  374. }
  375. static inline int get_memset_num_threads(int smp_cpus)
  376. {
  377. long host_procs = sysconf(_SC_NPROCESSORS_ONLN);
  378. int ret = 1;
  379. if (host_procs > 0) {
  380. ret = MIN(MIN(host_procs, MAX_MEM_PREALLOC_THREAD_COUNT), smp_cpus);
  381. }
  382. /* In case sysconf() fails, we fall back to single threaded */
  383. return ret;
  384. }
  385. static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages,
  386. int smp_cpus)
  387. {
  388. size_t numpages_per_thread;
  389. size_t size_per_thread;
  390. char *addr = area;
  391. int i = 0;
  392. memset_thread_failed = false;
  393. memset_num_threads = get_memset_num_threads(smp_cpus);
  394. memset_thread = g_new0(MemsetThread, memset_num_threads);
  395. numpages_per_thread = (numpages / memset_num_threads);
  396. size_per_thread = (hpagesize * numpages_per_thread);
  397. for (i = 0; i < memset_num_threads; i++) {
  398. memset_thread[i].addr = addr;
  399. memset_thread[i].numpages = (i == (memset_num_threads - 1)) ?
  400. numpages : numpages_per_thread;
  401. memset_thread[i].hpagesize = hpagesize;
  402. qemu_thread_create(&memset_thread[i].pgthread, "touch_pages",
  403. do_touch_pages, &memset_thread[i],
  404. QEMU_THREAD_JOINABLE);
  405. addr += size_per_thread;
  406. numpages -= numpages_per_thread;
  407. }
  408. for (i = 0; i < memset_num_threads; i++) {
  409. qemu_thread_join(&memset_thread[i].pgthread);
  410. }
  411. g_free(memset_thread);
  412. memset_thread = NULL;
  413. return memset_thread_failed;
  414. }
  415. void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
  416. Error **errp)
  417. {
  418. int ret;
  419. struct sigaction act, oldact;
  420. size_t hpagesize = qemu_fd_getpagesize(fd);
  421. size_t numpages = DIV_ROUND_UP(memory, hpagesize);
  422. memset(&act, 0, sizeof(act));
  423. act.sa_handler = &sigbus_handler;
  424. act.sa_flags = 0;
  425. ret = sigaction(SIGBUS, &act, &oldact);
  426. if (ret) {
  427. error_setg_errno(errp, errno,
  428. "os_mem_prealloc: failed to install signal handler");
  429. return;
  430. }
  431. /* touch pages simultaneously */
  432. if (touch_all_pages(area, hpagesize, numpages, smp_cpus)) {
  433. error_setg(errp, "os_mem_prealloc: Insufficient free host memory "
  434. "pages available to allocate guest RAM");
  435. }
  436. ret = sigaction(SIGBUS, &oldact, NULL);
  437. if (ret) {
  438. /* Terminate QEMU since it can't recover from error */
  439. perror("os_mem_prealloc: failed to reinstall signal handler");
  440. exit(1);
  441. }
  442. }
  443. char *qemu_get_pid_name(pid_t pid)
  444. {
  445. char *name = NULL;
  446. #if defined(__FreeBSD__)
  447. /* BSDs don't have /proc, but they provide a nice substitute */
  448. struct kinfo_proc *proc = kinfo_getproc(pid);
  449. if (proc) {
  450. name = g_strdup(proc->ki_comm);
  451. free(proc);
  452. }
  453. #else
  454. /* Assume a system with reasonable procfs */
  455. char *pid_path;
  456. size_t len;
  457. pid_path = g_strdup_printf("/proc/%d/cmdline", pid);
  458. g_file_get_contents(pid_path, &name, &len, NULL);
  459. g_free(pid_path);
  460. #endif
  461. return name;
  462. }
  463. pid_t qemu_fork(Error **errp)
  464. {
  465. sigset_t oldmask, newmask;
  466. struct sigaction sig_action;
  467. int saved_errno;
  468. pid_t pid;
  469. /*
  470. * Need to block signals now, so that child process can safely
  471. * kill off caller's signal handlers without a race.
  472. */
  473. sigfillset(&newmask);
  474. if (pthread_sigmask(SIG_SETMASK, &newmask, &oldmask) != 0) {
  475. error_setg_errno(errp, errno,
  476. "cannot block signals");
  477. return -1;
  478. }
  479. pid = fork();
  480. saved_errno = errno;
  481. if (pid < 0) {
  482. /* attempt to restore signal mask, but ignore failure, to
  483. * avoid obscuring the fork failure */
  484. (void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
  485. error_setg_errno(errp, saved_errno,
  486. "cannot fork child process");
  487. errno = saved_errno;
  488. return -1;
  489. } else if (pid) {
  490. /* parent process */
  491. /* Restore our original signal mask now that the child is
  492. * safely running. Only documented failures are EFAULT (not
  493. * possible, since we are using just-grabbed mask) or EINVAL
  494. * (not possible, since we are using correct arguments). */
  495. (void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
  496. } else {
  497. /* child process */
  498. size_t i;
  499. /* Clear out all signal handlers from parent so nothing
  500. * unexpected can happen in our child once we unblock
  501. * signals */
  502. sig_action.sa_handler = SIG_DFL;
  503. sig_action.sa_flags = 0;
  504. sigemptyset(&sig_action.sa_mask);
  505. for (i = 1; i < NSIG; i++) {
  506. /* Only possible errors are EFAULT or EINVAL The former
  507. * won't happen, the latter we expect, so no need to check
  508. * return value */
  509. (void)sigaction(i, &sig_action, NULL);
  510. }
  511. /* Unmask all signals in child, since we've no idea what the
  512. * caller's done with their signal mask and don't want to
  513. * propagate that to children */
  514. sigemptyset(&newmask);
  515. if (pthread_sigmask(SIG_SETMASK, &newmask, NULL) != 0) {
  516. Error *local_err = NULL;
  517. error_setg_errno(&local_err, errno,
  518. "cannot unblock signals");
  519. error_report_err(local_err);
  520. _exit(1);
  521. }
  522. }
  523. return pid;
  524. }
  525. void *qemu_alloc_stack(size_t *sz)
  526. {
  527. void *ptr, *guardpage;
  528. int flags;
  529. #ifdef CONFIG_DEBUG_STACK_USAGE
  530. void *ptr2;
  531. #endif
  532. size_t pagesz = qemu_real_host_page_size;
  533. #ifdef _SC_THREAD_STACK_MIN
  534. /* avoid stacks smaller than _SC_THREAD_STACK_MIN */
  535. long min_stack_sz = sysconf(_SC_THREAD_STACK_MIN);
  536. *sz = MAX(MAX(min_stack_sz, 0), *sz);
  537. #endif
  538. /* adjust stack size to a multiple of the page size */
  539. *sz = ROUND_UP(*sz, pagesz);
  540. /* allocate one extra page for the guard page */
  541. *sz += pagesz;
  542. flags = MAP_PRIVATE | MAP_ANONYMOUS;
  543. #if defined(MAP_STACK) && defined(__OpenBSD__)
  544. /* Only enable MAP_STACK on OpenBSD. Other OS's such as
  545. * Linux/FreeBSD/NetBSD have a flag with the same name
  546. * but have differing functionality. OpenBSD will SEGV
  547. * if it spots execution with a stack pointer pointing
  548. * at memory that was not allocated with MAP_STACK.
  549. */
  550. flags |= MAP_STACK;
  551. #endif
  552. ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);
  553. if (ptr == MAP_FAILED) {
  554. perror("failed to allocate memory for stack");
  555. abort();
  556. }
  557. #if defined(HOST_IA64)
  558. /* separate register stack */
  559. guardpage = ptr + (((*sz - pagesz) / 2) & ~pagesz);
  560. #elif defined(HOST_HPPA)
  561. /* stack grows up */
  562. guardpage = ptr + *sz - pagesz;
  563. #else
  564. /* stack grows down */
  565. guardpage = ptr;
  566. #endif
  567. if (mprotect(guardpage, pagesz, PROT_NONE) != 0) {
  568. perror("failed to set up stack guard page");
  569. abort();
  570. }
  571. #ifdef CONFIG_DEBUG_STACK_USAGE
  572. for (ptr2 = ptr + pagesz; ptr2 < ptr + *sz; ptr2 += sizeof(uint32_t)) {
  573. *(uint32_t *)ptr2 = 0xdeadbeaf;
  574. }
  575. #endif
  576. return ptr;
  577. }
  578. #ifdef CONFIG_DEBUG_STACK_USAGE
  579. static __thread unsigned int max_stack_usage;
  580. #endif
  581. void qemu_free_stack(void *stack, size_t sz)
  582. {
  583. #ifdef CONFIG_DEBUG_STACK_USAGE
  584. unsigned int usage;
  585. void *ptr;
  586. for (ptr = stack + qemu_real_host_page_size; ptr < stack + sz;
  587. ptr += sizeof(uint32_t)) {
  588. if (*(uint32_t *)ptr != 0xdeadbeaf) {
  589. break;
  590. }
  591. }
  592. usage = sz - (uintptr_t) (ptr - stack);
  593. if (usage > max_stack_usage) {
  594. error_report("thread %d max stack usage increased from %u to %u",
  595. qemu_get_thread_id(), max_stack_usage, usage);
  596. max_stack_usage = usage;
  597. }
  598. #endif
  599. munmap(stack, sz);
  600. }
  601. void sigaction_invoke(struct sigaction *action,
  602. struct qemu_signalfd_siginfo *info)
  603. {
  604. siginfo_t si = {};
  605. si.si_signo = info->ssi_signo;
  606. si.si_errno = info->ssi_errno;
  607. si.si_code = info->ssi_code;
  608. /* Convert the minimal set of fields defined by POSIX.
  609. * Positive si_code values are reserved for kernel-generated
  610. * signals, where the valid siginfo fields are determined by
  611. * the signal number. But according to POSIX, it is unspecified
  612. * whether SI_USER and SI_QUEUE have values less than or equal to
  613. * zero.
  614. */
  615. if (info->ssi_code == SI_USER || info->ssi_code == SI_QUEUE ||
  616. info->ssi_code <= 0) {
  617. /* SIGTERM, etc. */
  618. si.si_pid = info->ssi_pid;
  619. si.si_uid = info->ssi_uid;
  620. } else if (info->ssi_signo == SIGILL || info->ssi_signo == SIGFPE ||
  621. info->ssi_signo == SIGSEGV || info->ssi_signo == SIGBUS) {
  622. si.si_addr = (void *)(uintptr_t)info->ssi_addr;
  623. } else if (info->ssi_signo == SIGCHLD) {
  624. si.si_pid = info->ssi_pid;
  625. si.si_status = info->ssi_status;
  626. si.si_uid = info->ssi_uid;
  627. }
  628. action->sa_sigaction(info->ssi_signo, &si, NULL);
  629. }