Installed-header hygiene (BZ#20366): obsolete BSD u_* types.
[glibc.git] / sysdeps / unix / sysv / linux / tst-clone2.c
blob68a7e6d6e22d8ca1d9584efa67f4ef4d039deb32
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/>. */
19 #include <sched.h>
20 #include <signal.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stddef.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
32 #include <tls.h> /* for THREAD_* macros. */
34 static int sig;
35 static int pipefd[2];
37 static int
38 f (void *a)
40 close (pipefd[0]);
42 pid_t pid = THREAD_GETMEM (THREAD_SELF, pid);
43 pid_t tid = THREAD_GETMEM (THREAD_SELF, tid);
45 while (write (pipefd[1], &pid, sizeof pid) < 0)
46 continue;
47 while (write (pipefd[1], &tid, sizeof tid) < 0)
48 continue;
50 return 0;
54 static int
55 clone_test (int clone_flags)
57 sig = SIGRTMIN;
58 sigset_t ss;
59 sigemptyset (&ss);
60 sigaddset (&ss, sig);
61 if (sigprocmask (SIG_BLOCK, &ss, NULL) != 0)
63 printf ("sigprocmask failed: %m\n");
64 return 1;
67 if (pipe2 (pipefd, O_CLOEXEC))
69 printf ("sigprocmask failed: %m\n");
70 return 1;
73 pid_t ppid = getpid ();
75 #ifdef __ia64__
76 extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
77 size_t __child_stack_size, int __flags,
78 void *__arg, ...);
79 char st[256 * 1024] __attribute__ ((aligned));
80 pid_t p = __clone2 (f, st, sizeof (st), clone_flags, 0);
81 #else
82 char st[128 * 1024] __attribute__ ((aligned));
83 #if _STACK_GROWS_DOWN
84 pid_t p = clone (f, st + sizeof (st), clone_flags, 0);
85 #elif _STACK_GROWS_UP
86 pid_t p = clone (f, st, clone_flags, 0);
87 #else
88 #error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
89 #endif
90 #endif
91 close (pipefd[1]);
93 if (p == -1)
95 printf ("clone failed: %m\n");
96 return 1;
99 pid_t pid, tid;
100 if (read (pipefd[0], &pid, sizeof pid) != sizeof pid)
102 printf ("read pid failed: %m\n");
103 kill (p, SIGKILL);
104 return 1;
106 if (read (pipefd[0], &tid, sizeof tid) != sizeof tid)
108 printf ("read pid failed: %m\n");
109 kill (p, SIGKILL);
110 return 1;
113 close (pipefd[0]);
115 int ret = 0;
117 /* For CLONE_VM glibc clone implementation does not change the pthread
118 pid/tid field. */
119 if ((clone_flags & CLONE_VM) == CLONE_VM)
121 if ((ppid != pid) || (ppid != tid))
123 printf ("parent pid (%i) != received pid/tid (%i/%i)\n",
124 (int)ppid, (int)pid, (int)tid);
125 ret = 1;
128 /* For any other flag clone updates the new pthread pid and tid with
129 the clone return value. */
130 else
132 if ((p != pid) || (p != tid))
134 printf ("child pid (%i) != received pid/tid (%i/%i)\n",
135 (int)p, (int)pid, (int)tid);
136 ret = 1;
140 int e;
141 if (waitpid (p, &e, __WCLONE) != p)
143 puts ("waitpid failed");
144 kill (p, SIGKILL);
145 return 1;
147 if (!WIFEXITED (e))
149 if (WIFSIGNALED (e))
150 printf ("died from signal %s\n", strsignal (WTERMSIG (e)));
151 else
152 puts ("did not terminate correctly");
153 return 1;
155 if (WEXITSTATUS (e) != 0)
157 printf ("exit code %d\n", WEXITSTATUS (e));
158 return 1;
161 return ret;
165 do_test (void)
167 /* First, check that the clone implementation, without any flag, updates
168 the struct pthread to contain the new PID and TID. */
169 int ret = clone_test (0);
170 /* Second, check that with CLONE_VM the struct pthread PID and TID fields
171 remain unmodified after the clone. Any modifications would cause problem
172 for the parent as described in bug 19957. */
173 ret += clone_test (CLONE_VM);
174 return ret;
177 #define TEST_FUNCTION do_test ()
178 #include "../test-skeleton.c"