shim_exit.c 5.0 KB

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