shim_fork.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_fork.c
  17. *
  18. * Implementation of system call "fork".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_ipc.h>
  24. #include <shim_profile.h>
  25. #include <shim_checkpoint.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <errno.h>
  29. #include <sys/syscall.h>
  30. #include <sys/mman.h>
  31. #include <asm/prctl.h>
  32. #include <linux/futex.h>
  33. int migrate_fork (struct shim_cp_store * store,
  34. struct shim_thread * thread,
  35. struct shim_process * process, va_list ap)
  36. {
  37. BEGIN_MIGRATION_DEF(fork, struct shim_thread * thread,
  38. struct shim_process * process)
  39. {
  40. DEFINE_MIGRATE(process, process, sizeof(struct shim_process));
  41. DEFINE_MIGRATE(all_mounts, NULL, 0);
  42. DEFINE_MIGRATE(all_vmas, NULL, 0);
  43. DEFINE_MIGRATE(running_thread, thread, sizeof(struct shim_thread));
  44. DEFINE_MIGRATE(handle_map, thread->handle_map,
  45. sizeof (struct shim_handle_map));
  46. DEFINE_MIGRATE(brk, NULL, 0);
  47. DEFINE_MIGRATE(loaded_libraries, NULL, 0);
  48. #ifdef DEBUG
  49. DEFINE_MIGRATE(gdb_map, NULL, 0);
  50. #endif
  51. DEFINE_MIGRATE(migratable, NULL, 0);
  52. }
  53. END_MIGRATION_DEF(fork)
  54. int ret = START_MIGRATE(store, fork, thread, process);
  55. thread->in_vm = false;
  56. if (thread->exec) {
  57. put_handle(thread->exec);
  58. thread->exec = NULL;
  59. }
  60. return ret;
  61. }
  62. int shim_do_fork (void)
  63. {
  64. int ret = 0;
  65. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  66. if ((ret = prepare_ns_leaders()) < 0)
  67. return ret;
  68. struct shim_thread * cur_thread = get_cur_thread();
  69. struct shim_thread * new_thread = get_new_thread(0);
  70. if (!new_thread)
  71. return -ENOMEM;
  72. new_thread->tcb = cur_thread->tcb;
  73. new_thread->user_tcb = cur_thread->user_tcb;
  74. new_thread->tgid = new_thread->tid;
  75. new_thread->in_vm = false;
  76. new_thread->is_alive = true;
  77. add_thread(new_thread);
  78. set_as_child(cur_thread, new_thread);
  79. if ((ret = do_migrate_process(&migrate_fork, NULL, NULL, new_thread)) < 0) {
  80. put_thread(new_thread);
  81. return ret;
  82. }
  83. lock(new_thread->lock);
  84. struct shim_handle_map * handle_map = new_thread->handle_map;
  85. new_thread->handle_map = NULL;
  86. unlock(new_thread->lock);
  87. if (handle_map)
  88. put_handle_map(handle_map);
  89. IDTYPE tid = new_thread->tid;
  90. put_thread(new_thread);
  91. return tid;
  92. }