1 /* The internal wrapper of clone and clone3.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
23 #include <clone_internal.h>
24 #include <libc-pointer-arith.h> /* For cast_to_pointer. */
25 #include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */
27 #define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
28 #define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */
29 #define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */
31 #define sizeof_field(TYPE, MEMBER) sizeof ((((TYPE *)0)->MEMBER))
32 #define offsetofend(TYPE, MEMBER) \
33 (offsetof (TYPE, MEMBER) + sizeof_field (TYPE, MEMBER))
35 _Static_assert (__alignof (struct clone_args
) == 8,
36 "__alignof (struct clone_args) != 8");
37 _Static_assert (offsetofend (struct clone_args
, tls
) == CLONE_ARGS_SIZE_VER0
,
38 "offsetofend (struct clone_args, tls) != CLONE_ARGS_SIZE_VER0");
39 _Static_assert (offsetofend (struct clone_args
, set_tid_size
) == CLONE_ARGS_SIZE_VER1
,
40 "offsetofend (struct clone_args, set_tid_size) != CLONE_ARGS_SIZE_VER1");
41 _Static_assert (offsetofend (struct clone_args
, cgroup
) == CLONE_ARGS_SIZE_VER2
,
42 "offsetofend (struct clone_args, cgroup) != CLONE_ARGS_SIZE_VER2");
43 _Static_assert (sizeof (struct clone_args
) == CLONE_ARGS_SIZE_VER2
,
44 "sizeof (struct clone_args) != CLONE_ARGS_SIZE_VER2");
47 __clone_internal_fallback (struct clone_args
*cl_args
,
48 int (*func
) (void *arg
), void *arg
)
50 /* Map clone3 arguments to clone arguments. NB: No need to check
51 invalid clone3 specific bits in flags nor exit_signal since this
52 is an internal function. */
53 int flags
= cl_args
->flags
| cl_args
->exit_signal
;
54 void *stack
= cast_to_pointer (cl_args
->stack
);
57 #if !_STACK_GROWS_DOWN && !_STACK_GROWS_UP
58 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
62 stack
+= cl_args
->stack_size
;
64 ret
= __clone (func
, stack
, flags
, arg
,
65 cast_to_pointer (cl_args
->parent_tid
),
66 cast_to_pointer (cl_args
->tls
),
67 cast_to_pointer (cl_args
->child_tid
));
72 __clone3_internal (struct clone_args
*cl_args
, int (*func
) (void *args
),
75 #ifdef HAVE_CLONE3_WRAPPER
77 return __clone3 (cl_args
, sizeof (*cl_args
), func
, arg
);
79 static int clone3_supported
= 1;
80 if (atomic_load_relaxed (&clone3_supported
) == 1)
82 int ret
= __clone3 (cl_args
, sizeof (*cl_args
), func
, arg
);
83 if (ret
!= -1 || errno
!= ENOSYS
)
86 atomic_store_relaxed (&clone3_supported
, 0);
95 __clone_internal (struct clone_args
*cl_args
,
96 int (*func
) (void *arg
), void *arg
)
98 #ifdef HAVE_CLONE3_WRAPPER
99 int saved_errno
= errno
;
100 int ret
= __clone3_internal (cl_args
, func
, arg
);
101 if (ret
!= -1 || errno
!= ENOSYS
)
104 /* NB: Restore errno since errno may be checked against non-zero
106 __set_errno (saved_errno
);
109 return __clone_internal_fallback (cl_args
, func
, arg
);
112 libc_hidden_def (__clone_internal
)