shim_wait.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. 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. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  39. if (pid > 0) {
  40. if (!(thread = lookup_thread(pid)))
  41. return -ECHILD;
  42. if (!(option & WNOHANG)) {
  43. block_pid:
  44. DkObjectsWaitAny(1, &thread->exit_event,
  45. NO_TIMEOUT);
  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. struct shim_thread * parent = thread->parent;
  57. assert(parent);
  58. lock(parent->lock);
  59. list_del_init(&thread->siblings);
  60. unlock(parent->lock);
  61. put_thread(parent);
  62. put_thread(thread);
  63. thread->parent = NULL;
  64. }
  65. unlock(thread->lock);
  66. goto found;
  67. }
  68. lock(cur->lock);
  69. if (list_empty(&cur->children) &&
  70. list_empty(&cur->exited_children)) {
  71. unlock(cur->lock);
  72. return -ECHILD;
  73. }
  74. if (!(option & WNOHANG)) {
  75. block:
  76. if (cur->child_exit_event)
  77. while (list_empty(&cur->exited_children)) {
  78. unlock(cur->lock);
  79. DkObjectsWaitAny(1, &cur->child_exit_event, NO_TIMEOUT);
  80. lock(cur->lock);
  81. }
  82. }
  83. if (pid == 0 || pid < -1) {
  84. if (pid == 0)
  85. pid = -cur->pgid;
  86. list_for_each_entry(thread, &cur->exited_children, siblings)
  87. if (thread->pgid == -pid)
  88. goto found_child;
  89. if (!(option & WNOHANG))
  90. goto block;
  91. } else {
  92. if (!list_empty(&cur->exited_children)) {
  93. thread = list_first_entry(&cur->exited_children,
  94. struct shim_thread, siblings);
  95. goto found_child;
  96. }
  97. }
  98. unlock(cur->lock);
  99. return 0;
  100. found_child:
  101. list_del_init(&thread->siblings);
  102. put_thread(cur);
  103. thread->parent = NULL;
  104. if (list_empty(&cur->exited_children))
  105. DkEventClear(cur->child_exit_event);
  106. unlock(cur->lock);
  107. found:
  108. if (status)
  109. *status = (thread->exit_code & 0xff) << 8;
  110. ret = thread->tid;
  111. del_thread(thread);
  112. put_thread(thread);
  113. return ret;
  114. }