shim_exit.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_exit.c
  15. *
  16. * Implementation of system call "exit" and "exit_group".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_table.h>
  20. #include <shim_thread.h>
  21. #include <shim_fs.h>
  22. #include <shim_handle.h>
  23. #include <shim_ipc.h>
  24. #include <shim_utils.h>
  25. #include <shim_checkpoint.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <errno.h>
  29. #include <sys/syscall.h>
  30. #include <sys/mman.h>
  31. #include <asm/prctl.h>
  32. #include <linux/futex.h>
  33. void release_robust_list (struct robust_list_head * head);
  34. void release_clear_child_id (int * clear_child_tid);
  35. int thread_exit(struct shim_thread * self, bool send_ipc)
  36. {
  37. /* Chia-Che: Broadcast exit message as early as possible,
  38. so other process can start early on responding. */
  39. if (self->in_vm && send_ipc)
  40. ipc_cld_exit_send(self->ppid, self->tid, self->exit_code, self->term_signal);
  41. lock(&self->lock);
  42. if (!self->is_alive) {
  43. debug("thread %d is dead\n", self->tid);
  44. out:
  45. unlock(&self->lock);
  46. return 0;
  47. }
  48. #ifdef PROFILE
  49. self->exit_time = GET_PROFILE_INTERVAL();
  50. #endif
  51. int exit_code = self->exit_code;
  52. self->is_alive = false;
  53. if (is_internal(self))
  54. goto out;
  55. struct shim_handle_map * handle_map = self->handle_map;
  56. struct shim_handle * exec = self->exec;
  57. struct shim_thread * parent = self->parent;
  58. self->handle_map = NULL;
  59. self->exec = NULL;
  60. if (parent) {
  61. assert(parent != self);
  62. assert(parent->child_exit_event);
  63. debug("thread exits, notifying thread %d\n", parent->tid);
  64. lock(&parent->lock);
  65. LISTP_DEL_INIT(self, &parent->children, siblings);
  66. LISTP_ADD_TAIL(self, &parent->exited_children, siblings);
  67. if (!self->in_vm) {
  68. debug("deliver SIGCHLD (thread = %d, exitval = %d)\n",
  69. self->tid, exit_code);
  70. siginfo_t info;
  71. memset(&info, 0, sizeof(siginfo_t));
  72. info.si_signo = SIGCHLD;
  73. info.si_pid = self->tid;
  74. info.si_uid = self->uid;
  75. info.si_status = (exit_code & 0xff) << 8;
  76. append_signal(parent, SIGCHLD, &info, true);
  77. }
  78. unlock(&parent->lock);
  79. DkEventSet(parent->child_exit_event);
  80. } else {
  81. debug("parent not here, need to tell another process\n");
  82. ipc_cld_exit_send(self->ppid, self->tid, self->exit_code, self->term_signal);
  83. }
  84. struct robust_list_head * robust_list = (void *) self->robust_list;
  85. self->robust_list = NULL;
  86. unlock(&self->lock);
  87. if (handle_map)
  88. put_handle_map(handle_map);
  89. if (exec)
  90. put_handle(exec);
  91. if (robust_list)
  92. release_robust_list(robust_list);
  93. if (self->clear_child_tid)
  94. release_clear_child_id (self->clear_child_tid);
  95. DkEventSet(self->exit_event);
  96. return 0;
  97. }
  98. int try_process_exit (int error_code, int term_signal)
  99. {
  100. struct shim_thread * cur_thread = get_cur_thread();
  101. cur_thread->exit_code = -error_code;
  102. cur_process.exit_code = error_code;
  103. cur_thread->term_signal = term_signal;
  104. if (cur_thread->in_vm)
  105. thread_exit(cur_thread, true);
  106. if (check_last_thread(cur_thread))
  107. return 0;
  108. struct shim_thread * async_thread = terminate_async_helper();
  109. if (async_thread)
  110. /* TODO: wait for the thread to exit in host.
  111. * This is tracked by the following issue.
  112. * https://github.com/oscarlab/graphene/issues/440
  113. */
  114. put_thread(async_thread); /* free resources of the thread */
  115. struct shim_thread * ipc_thread = terminate_ipc_helper();
  116. if (ipc_thread)
  117. /* TODO: wait for the thread to exit in host.
  118. * This is tracked by the following issue.
  119. * https://github.com/oscarlab/graphene/issues/440
  120. */
  121. put_thread(ipc_thread); /* free resources of the thread */
  122. shim_clean(0);
  123. return 0;
  124. }
  125. noreturn int shim_do_exit_group (int error_code)
  126. {
  127. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  128. struct shim_thread * cur_thread = get_cur_thread();
  129. assert(!is_internal(cur_thread));
  130. if (debug_handle)
  131. sysparser_printf("---- shim_exit_group (returning %d)\n", error_code);
  132. if (cur_thread->dummy) {
  133. cur_thread->term_signal = 0;
  134. thread_exit(cur_thread, true);
  135. switch_dummy_thread(cur_thread);
  136. }
  137. debug("now kill other threads in the process\n");
  138. do_kill_proc(cur_thread->tgid, cur_thread->tgid, SIGKILL, false);
  139. debug("now exit the process\n");
  140. try_process_exit(error_code, 0);
  141. #ifdef PROFILE
  142. if (ENTER_TIME)
  143. SAVE_PROFILE_INTERVAL_SINCE(syscall_exit_group, ENTER_TIME);
  144. #endif
  145. DkThreadExit();
  146. }
  147. noreturn int shim_do_exit (int error_code)
  148. {
  149. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  150. struct shim_thread * cur_thread = get_cur_thread();
  151. assert(!is_internal(cur_thread));
  152. if (debug_handle)
  153. sysparser_printf("---- shim_exit (returning %d)\n", error_code);
  154. if (cur_thread->dummy) {
  155. cur_thread->term_signal = 0;
  156. thread_exit(cur_thread, true);
  157. switch_dummy_thread(cur_thread);
  158. }
  159. try_process_exit(error_code, 0);
  160. #ifdef PROFILE
  161. if (ENTER_TIME)
  162. SAVE_PROFILE_INTERVAL_SINCE(syscall_exit, ENTER_TIME);
  163. #endif
  164. DkThreadExit();
  165. }