1 /* _Fork implementation. Linux version. 2 Copyright (C) 2021 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 4 5 The GNU C Library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 The GNU C Library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with the GNU C Library; if not, see 17 <https://www.gnu.org/licenses/>. */ 18 19 #include <arch-fork.h> 20 #include <pthreadP.h> 21 22 /* Pointer to the fork generation counter in the thread library. */ 23 extern unsigned long int *__fork_generation_pointer attribute_hidden; 24 25 pid_t _Fork(void)26_Fork (void) 27 { 28 pid_t pid = arch_fork (&THREAD_SELF->tid); 29 if (pid == 0) 30 { 31 struct pthread *self = THREAD_SELF; 32 33 /* Initialize the robust mutex list setting in the kernel which has 34 been reset during the fork. We do not check for errors because if 35 it fails here, it must have failed at process startup as well and 36 nobody could have used robust mutexes. 37 Before we do that, we have to clear the list of robust mutexes 38 because we do not inherit ownership of mutexes from the parent. 39 We do not have to set self->robust_head.futex_offset since we do 40 inherit the correct value from the parent. We do not need to clear 41 the pending operation because it must have been zero when fork was 42 called. */ 43 #if __PTHREAD_MUTEX_HAVE_PREV 44 self->robust_prev = &self->robust_head; 45 #endif 46 self->robust_head.list = &self->robust_head; 47 INTERNAL_SYSCALL_CALL (set_robust_list, &self->robust_head, 48 sizeof (struct robust_list_head)); 49 } 50 return pid; 51 } 52 libc_hidden_def (_Fork) 53