1  /* arch_fork definition for Linux fork implementation.
       2     Copyright (C) 2014-2023 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  #ifndef __ARCH_FORK_H
      20  #define __ARCH_FORK_H
      21  
      22  #include <sysdep.h>
      23  #include <sched.h>
      24  #include <signal.h>
      25  #include <unistd.h>
      26  
      27  /* Call the clone syscall with fork semantic.  The CTID address is used
      28     to store the child thread ID at its locationm, to erase it in child memory
      29     when the child exits, and do a wakeup on the futex at that address.
      30  
      31     The architecture with non-default kernel abi semantic should correctly
      32     override it with one of the supported calling convention (check generic
      33     kernel-features.h for the clone abi variants).  */
      34  static inline pid_t
      35  arch_fork (void *ctid)
      36  {
      37    const int flags = CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD;
      38    long int ret;
      39  #ifdef __ASSUME_CLONE_BACKWARDS
      40  # ifdef INLINE_CLONE_SYSCALL
      41    ret = INLINE_CLONE_SYSCALL (flags, 0, NULL, 0, ctid);
      42  # else
      43    ret = INLINE_SYSCALL_CALL (clone, flags, 0, NULL, 0, ctid);
      44  # endif
      45  #elif defined(__ASSUME_CLONE_BACKWARDS2)
      46    ret = INLINE_SYSCALL_CALL (clone, 0, flags, NULL, ctid, 0);
      47  #elif defined(__ASSUME_CLONE_BACKWARDS3)
      48    ret = INLINE_SYSCALL_CALL (clone, flags, 0, 0, NULL, ctid, 0);
      49  #elif defined(__ASSUME_CLONE2)
      50    ret = INLINE_SYSCALL_CALL (clone2, flags, 0, 0, NULL, ctid, 0);
      51  #elif defined(__ASSUME_CLONE_DEFAULT)
      52    ret = INLINE_SYSCALL_CALL (clone, flags, 0, NULL, ctid, 0);
      53  #else
      54  # error "Undefined clone variant"
      55  #endif
      56    return ret;
      57  }
      58  
      59  #endif /* __ARCH_FORK_H  */