Consolidate alphasort{64} and versionsort{64} implementation
[glibc.git] / sysdeps / unix / sysv / linux / tst-clone3.c
blob784ce18f5343ec72191f551eb1ddb53dea032a2b
1 /* Check if clone (CLONE_THREAD) does not call exit_group (BZ #21512)
2 Copyright (C) 2017-2018 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 <http://www.gnu.org/licenses/>. */
19 #include <string.h>
20 #include <sched.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <sys/syscall.h>
24 #include <sys/wait.h>
25 #include <sys/types.h>
26 #include <linux/futex.h>
28 #include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */
29 #include <support/check.h>
31 /* Test if clone call with CLONE_THREAD does not call exit_group. The 'f'
32 function returns '1', which will be used by clone thread to call the
33 'exit' syscall directly. If _exit is used instead, exit_group will be
34 used and thus the thread group will finish with return value of '1'
35 (where '2' from main thread is expected. */
37 static int
38 f (void *a)
40 return 1;
43 /* Futex wait for TID argument, similar to pthread_join internal
44 implementation. */
45 #define wait_tid(tid) \
46 do { \
47 __typeof (tid) __tid; \
48 while ((__tid = (tid)) != 0) \
49 futex_wait (&(tid), __tid); \
50 } while (0)
52 static inline int
53 futex_wait (int *futexp, int val)
55 return syscall (__NR_futex, futexp, FUTEX_WAIT, val);
58 static int
59 do_test (void)
61 char st[1024] __attribute__ ((aligned));
62 int clone_flags = CLONE_THREAD;
63 /* Minimum required flags to used along with CLONE_THREAD. */
64 clone_flags |= CLONE_VM | CLONE_SIGHAND;
65 /* We will used ctid to call on futex to wait for thread exit. */
66 clone_flags |= CLONE_CHILD_CLEARTID;
67 pid_t ctid, tid;
69 #ifdef __ia64__
70 extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
71 size_t __child_stack_size, int __flags,
72 void *__arg, ...);
73 tid = __clone2 (f, st, sizeof (st), clone_flags, NULL, /* ptid */ NULL,
74 /* tls */ NULL, &ctid);
75 #else
76 #if _STACK_GROWS_DOWN
77 tid = clone (f, st + sizeof (st), clone_flags, NULL, /* ptid */ NULL,
78 /* tls */ NULL, &ctid);
79 #elif _STACK_GROWS_UP
80 tid = clone (f, st, clone_flags, NULL, /* ptid */ NULL, /* tls */ NULL,
81 &ctid);
82 #else
83 #error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
84 #endif
85 #endif
86 if (tid == -1)
87 FAIL_EXIT1 ("clone failed: %m");
89 ctid = tid;
90 wait_tid (ctid);
92 return 2;
95 #define EXPECTED_STATUS 2
96 #include <support/test-driver.c>