]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/tst-clone2.c
0da9e54ba25259952c80e8e05da266cef1ca098f
1 /* Test if CLONE_VM does not change pthread pid/tid field (BZ #19957)
2 Copyright (C) 2016 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/>. */
29 #include <sys/types.h>
31 #include <sys/syscall.h>
33 #include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */
35 static int do_test (void);
37 #define TEST_FUNCTION do_test ()
38 #include <test-skeleton.c>
48 pid_t ppid
= getppid ();
49 pid_t pid
= getpid ();
50 pid_t tid
= syscall (__NR_gettid
);
52 if (write (pipefd
[1], &ppid
, sizeof ppid
) != sizeof (ppid
))
53 FAIL_EXIT1 ("write ppid failed\n");
54 if (write (pipefd
[1], &pid
, sizeof pid
) != sizeof (pid
))
55 FAIL_EXIT1 ("write pid failed\n");
56 if (write (pipefd
[1], &tid
, sizeof tid
) != sizeof (tid
))
57 FAIL_EXIT1 ("write tid failed\n");
70 if (sigprocmask (SIG_BLOCK
, &ss
, NULL
) != 0)
71 FAIL_EXIT1 ("sigprocmask failed: %m");
73 if (pipe2 (pipefd
, O_CLOEXEC
))
74 FAIL_EXIT1 ("pipe failed: %m");
78 extern int __clone2 (int (*__fn
) (void *__arg
), void *__child_stack_base
,
79 size_t __child_stack_size
, int __flags
,
81 char st
[256 * 1024] __attribute__ ((aligned
));
82 pid_t p
= __clone2 (f
, st
, sizeof (st
), clone_flags
, 0);
84 char st
[128 * 1024] __attribute__ ((aligned
));
86 pid_t p
= clone (f
, st
+ sizeof (st
), clone_flags
, 0);
88 pid_t p
= clone (f
, st
, clone_flags
, 0);
90 #error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
97 FAIL_EXIT1("clone failed: %m");
100 if (read (pipefd
[0], &ppid
, sizeof pid
) != sizeof pid
)
103 FAIL_EXIT1 ("read ppid failed: %m");
105 if (read (pipefd
[0], &pid
, sizeof pid
) != sizeof pid
)
108 FAIL_EXIT1 ("read pid failed: %m");
110 if (read (pipefd
[0], &tid
, sizeof tid
) != sizeof tid
)
113 FAIL_EXIT1 ("read tid failed: %m");
120 pid_t own_pid
= getpid ();
121 pid_t own_tid
= syscall (__NR_gettid
);
123 /* Some sanity checks for clone syscall: returned ppid should be current
124 pid and both returned tid/pid should be different from current one. */
125 if ((ppid
!= own_pid
) || (pid
== own_pid
) || (tid
== own_tid
))
126 FAIL_RET ("ppid=%i pid=%i tid=%i | own_pid=%i own_tid=%i",
127 (int)ppid
, (int)pid
, (int)tid
, (int)own_pid
, (int)own_tid
);
130 if (waitpid (p
, &e
, __WCLONE
) != p
)
133 FAIL_EXIT1 ("waitpid failed");
138 printf ("died from signal %s\n", strsignal (WTERMSIG (e
)));
140 puts ("did not terminate correctly");
143 if (WEXITSTATUS (e
) != 0)
144 FAIL_EXIT1 ("exit code %d", WEXITSTATUS (e
));