shim_exit.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. int exit_code = self->exit_code;
  50. self->is_alive = false;
  51. if (IS_INTERNAL(self))
  52. goto out;
  53. struct shim_handle_map * handle_map = self->handle_map;
  54. struct shim_handle * exec = self->exec;
  55. struct shim_thread * parent = self->parent;
  56. self->handle_map = NULL;
  57. self->exec = NULL;
  58. if (parent) {
  59. assert(parent != self);
  60. assert(parent->child_exit_event);
  61. debug("thread exits, notifying thread %d\n", parent->tid);
  62. lock(parent->lock);
  63. list_del_init(&self->siblings);
  64. list_add_tail(&self->siblings, &parent->exited_children);
  65. if (!self->in_vm) {
  66. debug("deliver SIGCHLD (thread = %d, exitval = %d)\n",
  67. self->tid, exit_code);
  68. siginfo_t info;
  69. memset(&info, 0, sizeof(siginfo_t));
  70. info.si_signo = SIGCHLD;
  71. info.si_pid = self->tid;
  72. info.si_uid = self->uid;
  73. info.si_status = (exit_code & 0xff) << 8;
  74. append_signal(parent, SIGCHLD, &info);
  75. }
  76. unlock(parent->lock);
  77. DkEventSet(parent->child_exit_event);
  78. }
  79. struct robust_list_head * robust_list = (void *) self->robust_list;
  80. self->robust_list = NULL;
  81. unlock(self->lock);
  82. if (handle_map)
  83. put_handle_map(handle_map);
  84. if (exec)
  85. put_handle(exec);
  86. if (robust_list)
  87. release_robust_list(robust_list);
  88. if (self->clear_child_tid)
  89. release_clear_child_id (self->clear_child_tid);
  90. DkEventSet(self->exit_event);
  91. return 0;
  92. }
  93. int try_process_exit (int error_code)
  94. {
  95. struct shim_thread * cur_thread = get_cur_thread();
  96. thread_exit(cur_thread, true);
  97. if (check_last_thread(cur_thread))
  98. return 0;
  99. terminate_async_helper();
  100. if (!exit_with_ipc_helper(true))
  101. shim_clean();
  102. return 0;
  103. }
  104. int shim_do_exit_group (int error_code)
  105. {
  106. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  107. struct shim_thread * cur_thread = get_cur_thread();
  108. assert(!IS_INTERNAL(cur_thread));
  109. if (debug_handle)
  110. sysparser_printf("---- shim_exit_group (returning %d)\n", error_code);
  111. if (cur_thread->dummy) {
  112. thread_exit(cur_thread, true);
  113. switch_dummy_thread(cur_thread);
  114. }
  115. debug("now kill other threads in the process\n");
  116. do_kill_proc(cur_thread->tgid, cur_thread->tgid, SIGKILL, false);
  117. debug("now exit the process\n");
  118. try_process_exit(error_code);
  119. #ifdef PROFILE
  120. if (ENTER_TIME)
  121. SAVE_PROFILE_INTERVAL_SINCE(syscall_exit_group, ENTER_TIME);
  122. #endif
  123. DkThreadExit();
  124. return 0;
  125. }
  126. int shim_do_exit (int error_code)
  127. {
  128. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  129. struct shim_thread * cur_thread = get_cur_thread();
  130. assert(!IS_INTERNAL(cur_thread));
  131. if (debug_handle)
  132. sysparser_printf("---- shim_exit (returning %d)\n", error_code);
  133. if (cur_thread->dummy) {
  134. thread_exit(cur_thread, true);
  135. switch_dummy_thread(cur_thread);
  136. }
  137. try_process_exit(error_code);
  138. #ifdef PROFILE
  139. if (ENTER_TIME)
  140. SAVE_PROFILE_INTERVAL_SINCE(syscall_exit, ENTER_TIME);
  141. #endif
  142. DkThreadExit();
  143. return 0;
  144. }