Add MREMAP_DONTUNMAP from Linux 5.7
[glibc.git] / sysdeps / pthread / tst-exec3.c
blobb849d3aef7782d42502de3e2c316157181aceb87
1 /* Thread calls exec.
2 Copyright (C) 2002-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <paths.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <spawn.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/wait.h>
30 #include <support/xsignal.h>
33 static void *
34 tf (void *arg)
36 execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL);
38 puts ("execl failed");
39 exit (1);
43 static int
44 do_test (void)
46 int fd[2];
47 if (pipe (fd) != 0)
49 puts ("pipe failed");
50 exit (1);
53 /* Not interested in knowing when the pipe is closed. */
54 xsignal (SIGPIPE, SIG_IGN);
56 pid_t pid = fork ();
57 if (pid == -1)
59 puts ("fork failed");
60 exit (1);
63 if (pid == 0)
65 /* Use the fd for stdout. This is kind of ugly because it
66 substitutes the fd of stdout but we know what we are doing
67 here... */
68 if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO)
70 puts ("dup2 failed");
71 exit (1);
74 close (fd[0]);
76 pthread_t th;
77 if (pthread_create (&th, NULL, tf, NULL) != 0)
79 puts ("create failed");
80 exit (1);
83 if (pthread_join (th, NULL) == 0)
85 puts ("join succeeded!?");
86 exit (1);
89 puts ("join returned!?");
90 exit (1);
93 close (fd[1]);
95 char buf[200];
96 ssize_t n;
97 bool seen_pid = false;
98 while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
100 /* We only expect to read the PID. */
101 char *endp;
102 long int rpid = strtol (buf, &endp, 10);
104 if (*endp != '\n')
106 printf ("didn't parse whole line: \"%s\"\n", buf);
107 exit (1);
109 if (endp == buf)
111 puts ("read empty line");
112 exit (1);
115 if (rpid != pid)
117 printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
118 exit (1);
121 if (seen_pid)
123 puts ("found more than one PID line");
124 exit (1);
126 seen_pid = true;
129 close (fd[0]);
131 int status;
132 int err = waitpid (pid, &status, 0);
133 if (err != pid)
135 puts ("waitpid failed");
136 exit (1);
139 if (!seen_pid)
141 puts ("didn't get PID");
142 exit (1);
145 return 0;
148 #define TEST_FUNCTION do_test ()
149 #include "../test-skeleton.c"