shim_wait.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_wait.c
  15. *
  16. * Implementation of system call "wait4".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_utils.h>
  20. #include <shim_table.h>
  21. #include <shim_thread.h>
  22. #include <shim_profile.h>
  23. #include <pal.h>
  24. #include <pal_error.h>
  25. #include <sys/syscall.h>
  26. #include <sys/mman.h>
  27. #include <asm/prctl.h>
  28. #include <linux/wait.h>
  29. #include <errno.h>
  30. DEFINE_PROFILE_CATEGORY(wait, );
  31. DEFINE_PROFILE_INTERVAL(child_exit_notification, wait);
  32. pid_t shim_do_wait4 (pid_t pid, int * status, int option,
  33. struct __kernel_rusage * ru)
  34. {
  35. struct shim_thread * cur = get_cur_thread();
  36. struct shim_thread * thread = NULL;
  37. int ret = 0;
  38. __UNUSED(ru);
  39. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  40. if (pid > 0) {
  41. if (!(thread = lookup_thread(pid)))
  42. return -ECHILD;
  43. if (!(option & WNOHANG)) {
  44. block_pid:
  45. object_wait_with_retry(thread->exit_event);
  46. }
  47. lock(&thread->lock);
  48. if (thread->is_alive) {
  49. unlock(&thread->lock);
  50. if (!(option & WNOHANG))
  51. goto block_pid;
  52. put_thread(thread);
  53. return 0;
  54. }
  55. if (!LIST_EMPTY(thread, siblings)) {
  56. debug("reaping thread %p\n", thread);
  57. struct shim_thread * parent = thread->parent;
  58. assert(parent);
  59. lock(&parent->lock);
  60. /* DEP 5/15/17: These threads are exited */
  61. assert(!thread->is_alive);
  62. LISTP_DEL_INIT(thread, &thread->parent->exited_children, siblings);
  63. unlock(&parent->lock);
  64. put_thread(parent);
  65. put_thread(thread);
  66. thread->parent = NULL;
  67. }
  68. unlock(&thread->lock);
  69. goto found;
  70. }
  71. lock(&cur->lock);
  72. if (LISTP_EMPTY(&cur->children) &&
  73. LISTP_EMPTY(&cur->exited_children)) {
  74. unlock(&cur->lock);
  75. return -ECHILD;
  76. }
  77. if (!(option & WNOHANG)) {
  78. block:
  79. if (cur->child_exit_event)
  80. while (LISTP_EMPTY(&cur->exited_children)) {
  81. unlock(&cur->lock);
  82. object_wait_with_retry(cur->child_exit_event);
  83. lock(&cur->lock);
  84. }
  85. }
  86. if (pid == 0 || pid < -1) {
  87. if (pid == 0)
  88. pid = -cur->pgid;
  89. LISTP_FOR_EACH_ENTRY(thread, &cur->exited_children, siblings)
  90. if (thread->pgid == (IDTYPE) -pid)
  91. goto found_child;
  92. if (!(option & WNOHANG))
  93. goto block;
  94. } else {
  95. if (!LISTP_EMPTY(&cur->exited_children)) {
  96. thread = LISTP_FIRST_ENTRY(&cur->exited_children,
  97. struct shim_thread, siblings);
  98. goto found_child;
  99. }
  100. }
  101. unlock(&cur->lock);
  102. return 0;
  103. found_child:
  104. LISTP_DEL_INIT(thread, &cur->exited_children, siblings);
  105. put_thread(cur);
  106. thread->parent = NULL;
  107. if (LISTP_EMPTY(&cur->exited_children))
  108. DkEventClear(cur->child_exit_event);
  109. unlock(&cur->lock);
  110. found:
  111. if (status) {
  112. /* Bits 0--7 are for the signal, if any.
  113. * Bits 8--15 are for the exit code */
  114. *status = thread->term_signal;
  115. *status |= ((thread->exit_code & 0xff) << 8);
  116. }
  117. ret = thread->tid;
  118. SAVE_PROFILE_INTERVAL_SINCE(child_exit_notification, thread->exit_time);
  119. del_thread(thread);
  120. put_thread(thread);
  121. return ret;
  122. }