shim_fork.c 3.1 KB

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