LoongArch: Use "$fcsr0" instead of "$r0" in _FPU_{GET,SET}CW
[glibc.git] / sysdeps / pthread / tst-exec2.c
blobc86df185f8973a99aa2d45b5bdd90d51218e31b2
1 /* Thread with running thread calls exec.
2 Copyright (C) 2002-2024 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 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <paths.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <spawn.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/wait.h>
29 #include <support/xsignal.h>
32 static void *
33 tf (void *arg)
35 pthread_t th = (pthread_t) arg;
37 if (pthread_join (th, NULL) == 0)
39 puts ("thread in parent joined!?");
40 exit (1);
43 puts ("join in thread in parent returned!?");
44 exit (1);
48 static int
49 do_test (void)
51 int fd[2];
52 if (pipe (fd) != 0)
54 puts ("pipe failed");
55 exit (1);
58 /* Not interested in knowing when the pipe is closed. */
59 xsignal (SIGPIPE, SIG_IGN);
61 pid_t pid = fork ();
62 if (pid == -1)
64 puts ("fork failed");
65 exit (1);
68 if (pid == 0)
70 /* Use the fd for stdout. This is kind of ugly because it
71 substitutes the fd of stdout but we know what we are doing
72 here... */
73 if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO)
75 puts ("dup2 failed");
76 exit (1);
79 close (fd[0]);
81 pthread_t th;
82 if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
84 puts ("create failed");
85 exit (1);
88 execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL);
90 puts ("execl failed");
91 exit (1);
94 close (fd[1]);
96 char buf[200];
97 ssize_t n;
98 bool seen_pid = false;
99 while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
101 /* We only expect to read the PID. */
102 char *endp;
103 long int rpid = strtol (buf, &endp, 10);
105 if (*endp != '\n')
107 printf ("didn't parse whole line: \"%s\"\n", buf);
108 exit (1);
110 if (endp == buf)
112 puts ("read empty line");
113 exit (1);
116 if (rpid != pid)
118 printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
119 exit (1);
122 if (seen_pid)
124 puts ("found more than one PID line");
125 exit (1);
127 seen_pid = true;
130 close (fd[0]);
132 int status;
133 int err = waitpid (pid, &status, 0);
134 if (err != pid)
136 puts ("waitpid failed");
137 exit (1);
140 if (!seen_pid)
142 puts ("didn't get PID");
143 exit (1);
146 return 0;
149 #define TEST_FUNCTION do_test ()
150 #include "../test-skeleton.c"