File: clone.c

package info (click to toggle)
glibc 2.31-13%2Bdeb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 278,368 kB
  • sloc: ansic: 1,025,361; asm: 256,790; makefile: 12,097; sh: 10,548; python: 9,618; cpp: 5,233; awk: 1,956; perl: 514; yacc: 290; pascal: 182; sed: 73
file content (122 lines) | stat: -rw-r--r-- 3,596 bytes parent folder | download | duplicates (31)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Create a thread.
   Copyright (C) 2002 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Bruno Haible <bruno@clisp.org>, 2002.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#define __clone __no_broken_clone_decl
#include <sched.h>
#include <sys/rfork.h>
#include <errno.h>
#include <signal.h>
#include <stddef.h>
#include <getosreldate.h>
#undef __clone

/* __start_thread (flags, child_stack, fn, arg)
   is roughly equivalent to

     int retval = __rfork (flags);
     if (retval == 0)
       {
         // Here we are in the child thread.
         %stackpointer = child_stack;
         _exit (fn (arg));
       }
     return retval;

   but it cannot be done in portable C because it must access fn and arg
   after having replaced the stack pointer.  */

extern int __start_thread (int flags, void *child_stack,
			   int (*fn) (void *), void *arg);

int __clone (int (*fn) (void *), void *child_stack, int flags, void *arg)
{
  int rfork_flags = RFPROC;

  if (fn == NULL || child_stack == NULL)
    {
      __set_errno (EINVAL);
      return -1;
    }

  /* This implementation of clone() does not support all Linux flags.  */
  if (flags & ~(CSIGNAL | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
		| CLONE_VFORK | CLONE_SYSVSEM))
    {
      __set_errno (EINVAL);
      return -1;
    }

  if ((flags & CSIGNAL) != SIGCHLD)
  {
    if (__kernel_getosreldate() >= 802510)
                /* we slightly cheat here, */
                /* the 9.x snapshot prior to r223966 does not support it too */
    {
      if ((flags & CSIGNAL) & ~RFTSIGMASK)
	{
	  __set_errno (EINVAL);
	  return -1;
	}
        rfork_flags |= (RFTSIGZMB | RFTSIGFLAGS(flags & CSIGNAL));
    }
    else
    {
      if ((flags & CSIGNAL) & ~RFTHPNMASK)
	{
	  __set_errno (EINVAL);
	  return -1;
	}
      if ((flags & CSIGNAL) == 0)
        rfork_flags |= (RFLINUXTHPN | ((SIGCHLD) <<  RFTHPNSHIFT));
      else
        rfork_flags |= (RFLINUXTHPN | ((flags & CSIGNAL) <<  RFTHPNSHIFT));
    }
  } 
  if (flags & CLONE_VM)
    rfork_flags |= RFMEM;

  if (flags & CLONE_FS)
    /* Sharing the filesystem related info (umask, cwd, root dir)
       is not supported by rfork.  Ignore this; let's hope programs
       will set their umask and cwd before spawning threads.  */
    ;

  if (flags & CLONE_SYSVSEM)
    /* Ignore this; it has been introduced into linuxthreads in post 2.4 glibc */
    ;

  if (!(flags & CLONE_FILES))
    rfork_flags |= RFFDG;

  if (flags & CLONE_SIGHAND)
    {
      rfork_flags |= RFSIGSHARE;
      /* Also set the undocumented flag RFTHREAD.  It has the effect that when
	 the thread leader exits, all threads belonging to it are killed.  */
      rfork_flags |= RFTHREAD;
    }

  if (flags & CLONE_VFORK)
    rfork_flags |= RFPPWAIT;

  return __start_thread (rfork_flags, child_stack, fn, arg);
}

weak_alias (__clone, clone)