shim_wait.c 4.3 KB

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