shim_fork.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_fork.c
  15. *
  16. * Implementation of system call "fork".
  17. */
  18. #include <asm/prctl.h>
  19. #include <errno.h>
  20. #include <linux/futex.h>
  21. #include <sys/mman.h>
  22. #include <sys/syscall.h>
  23. #include <pal.h>
  24. #include <pal_error.h>
  25. #include <shim_checkpoint.h>
  26. #include <shim_internal.h>
  27. #include <shim_ipc.h>
  28. #include <shim_profile.h>
  29. #include <shim_table.h>
  30. #include <shim_thread.h>
  31. int migrate_fork(struct shim_cp_store* store, struct shim_thread* thread,
  32. struct shim_process* process, va_list ap) {
  33. __UNUSED(ap);
  34. BEGIN_MIGRATION_DEF(fork, struct shim_thread* thread, struct shim_process* process) {
  35. DEFINE_MIGRATE(process, process, sizeof(struct shim_process));
  36. DEFINE_MIGRATE(all_mounts, NULL, 0);
  37. DEFINE_MIGRATE(all_vmas, NULL, 0);
  38. DEFINE_MIGRATE(running_thread, thread, sizeof(struct shim_thread));
  39. DEFINE_MIGRATE(handle_map, thread->handle_map, sizeof(struct shim_handle_map));
  40. DEFINE_MIGRATE(migratable, NULL, 0);
  41. DEFINE_MIGRATE(brk, NULL, 0);
  42. DEFINE_MIGRATE(loaded_libraries, NULL, 0);
  43. #ifdef DEBUG
  44. DEFINE_MIGRATE(gdb_map, NULL, 0);
  45. #endif
  46. }
  47. END_MIGRATION_DEF(fork)
  48. int ret = START_MIGRATE(store, fork, thread, process);
  49. thread->in_vm = false;
  50. if (thread->exec) {
  51. put_handle(thread->exec);
  52. thread->exec = NULL;
  53. }
  54. return ret;
  55. }
  56. int shim_do_fork(void) {
  57. int ret = 0;
  58. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  59. if ((ret = prepare_ns_leaders()) < 0)
  60. return ret;
  61. struct shim_thread* cur_thread = get_cur_thread();
  62. struct shim_thread* new_thread = get_new_thread(0);
  63. if (!new_thread)
  64. return -ENOMEM;
  65. new_thread->shim_tcb = cur_thread->shim_tcb;
  66. new_thread->tgid = new_thread->tid;
  67. new_thread->in_vm = false;
  68. new_thread->is_alive = true;
  69. add_thread(new_thread);
  70. set_as_child(cur_thread, new_thread);
  71. if ((ret = do_migrate_process(&migrate_fork, NULL, NULL, new_thread)) < 0) {
  72. put_thread(new_thread);
  73. return ret;
  74. }
  75. lock(&new_thread->lock);
  76. struct shim_handle_map* handle_map = new_thread->handle_map;
  77. new_thread->handle_map = NULL;
  78. new_thread->shim_tcb = NULL;
  79. unlock(&new_thread->lock);
  80. if (handle_map)
  81. put_handle_map(handle_map);
  82. IDTYPE tid = new_thread->tid;
  83. put_thread(new_thread);
  84. return tid;
  85. }