shim_exit.c 4.8 KB

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