shim_exit.c 6.2 KB

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