1 | /* The internal wrapper of clone and clone3. |
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 <sysdep.h> |
20 | #include <stddef.h> |
21 | #include <errno.h> |
22 | #include <sched.h> |
23 | #include <clone_internal.h> |
24 | #include <libc-pointer-arith.h> /* For cast_to_pointer. */ |
25 | #include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */ |
26 | |
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 */ |
30 | |
31 | #define sizeof_field(TYPE, MEMBER) sizeof ((((TYPE *)0)->MEMBER)) |
32 | #define offsetofend(TYPE, MEMBER) \ |
33 | (offsetof (TYPE, MEMBER) + sizeof_field (TYPE, MEMBER)) |
34 | |
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" ); |
45 | |
46 | int |
47 | __clone_internal (struct clone_args *cl_args, |
48 | int (*func) (void *arg), void *arg) |
49 | { |
50 | int ret; |
51 | #ifdef HAVE_CLONE3_WRAPPER |
52 | /* Try clone3 first. */ |
53 | int saved_errno = errno; |
54 | ret = __clone3 (cl_args, sizeof (*cl_args), func, arg); |
55 | if (ret != -1 || errno != ENOSYS) |
56 | return ret; |
57 | |
58 | /* NB: Restore errno since errno may be checked against non-zero |
59 | return value. */ |
60 | __set_errno (saved_errno); |
61 | #endif |
62 | |
63 | /* Map clone3 arguments to clone arguments. NB: No need to check |
64 | invalid clone3 specific bits in flags nor exit_signal since this |
65 | is an internal function. */ |
66 | int flags = cl_args->flags | cl_args->exit_signal; |
67 | void *stack = cast_to_pointer (cl_args->stack); |
68 | |
69 | #ifdef __ia64__ |
70 | ret = __clone2 (func, stack, cl_args->stack_size, |
71 | flags, arg, |
72 | cast_to_pointer (cl_args->parent_tid), |
73 | cast_to_pointer (cl_args->tls), |
74 | cast_to_pointer (cl_args->child_tid)); |
75 | #else |
76 | # if !_STACK_GROWS_DOWN && !_STACK_GROWS_UP |
77 | # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP" |
78 | # endif |
79 | |
80 | # if _STACK_GROWS_DOWN |
81 | stack += cl_args->stack_size; |
82 | # endif |
83 | ret = __clone (func, stack, flags, arg, |
84 | cast_to_pointer (cl_args->parent_tid), |
85 | cast_to_pointer (cl_args->tls), |
86 | cast_to_pointer (cl_args->child_tid)); |
87 | #endif |
88 | return ret; |
89 | } |
90 | |
91 | libc_hidden_def (__clone_internal) |
92 | |