shim_fork.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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,
  44. sizeof (struct shim_handle_map));
  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. DEFINE_MIGRATE(migratable, NULL, 0);
  51. }
  52. END_MIGRATION_DEF(fork)
  53. int ret = START_MIGRATE(store, fork, thread, process);
  54. thread->in_vm = false;
  55. if (thread->exec) {
  56. put_handle(thread->exec);
  57. thread->exec = NULL;
  58. }
  59. return ret;
  60. }
  61. int shim_do_fork (void)
  62. {
  63. int ret = 0;
  64. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  65. if ((ret = prepare_ns_leaders()) < 0)
  66. return ret;
  67. struct shim_thread * cur_thread = get_cur_thread();
  68. struct shim_thread * new_thread = get_new_thread(0);
  69. if (!new_thread)
  70. return -ENOMEM;
  71. new_thread->tcb = cur_thread->tcb;
  72. new_thread->user_tcb = cur_thread->user_tcb;
  73. new_thread->tgid = new_thread->tid;
  74. new_thread->in_vm = false;
  75. new_thread->is_alive = true;
  76. add_thread(new_thread);
  77. set_as_child(cur_thread, new_thread);
  78. if ((ret = do_migrate_process(&migrate_fork, NULL, NULL, new_thread)) < 0) {
  79. put_thread(new_thread);
  80. return ret;
  81. }
  82. lock(&new_thread->lock);
  83. struct shim_handle_map * handle_map = new_thread->handle_map;
  84. new_thread->handle_map = NULL;
  85. unlock(&new_thread->lock);
  86. if (handle_map)
  87. put_handle_map(handle_map);
  88. IDTYPE tid = new_thread->tid;
  89. put_thread(new_thread);
  90. return tid;
  91. }