Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Check if posix_spawn does not act as a cancellation entrypoint.
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
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
30static int do_test (void);
31#define TEST_FUNCTION do_test ()
32#include <test-skeleton.c>
33
34static pthread_barrier_t b;
35
36static pid_t pid;
37static int pipefd[2];
38
39static void *
40tf (void *arg)
41{
42 xpthread_barrier_wait (&b);
43
44 posix_spawn_file_actions_t a;
45 if (posix_spawn_file_actions_init (&a) != 0)
46 {
47 puts ("error: spawn_file_actions_init failed");
48 exit (1);
49 }
50
51 if (posix_spawn_file_actions_adddup2 (&a, pipefd[1], STDOUT_FILENO) != 0)
52 {
53 puts ("error: spawn_file_actions_adddup2 failed");
54 exit (1);
55 }
56
57 if (posix_spawn_file_actions_addclose (&a, pipefd[0]) != 0)
58 {
59 puts ("error: spawn_file_actions_addclose");
60 exit (1);
61 }
62
63 char *argv[] = { (char *) _PATH_BSHELL, (char *) "-c", (char *) "echo $$",
64 NULL };
65 if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, argv, NULL) != 0)
66 {
67 puts ("error: spawn failed");
68 exit (1);
69 }
70
71 return NULL;
72}
73
74
75static int
76do_test (void)
77{
78 /* The test basically pipe a 'echo $$' created by a thread with a
79 cancellation pending. It then checks if the thread is not cancelled,
80 the process is created and if the output is the expected one. */
81
82 if (pipe (pipefd) != 0)
83 {
84 puts ("error: pipe failed");
85 exit (1);
86 }
87
88 /* Not interested in knowing when the pipe is closed. */
89 if (sigignore (SIGPIPE) != 0)
90 {
91 puts ("error: sigignore failed");
92 exit (1);
93 }
94
95 /* To synchronize with the thread. */
96 if (pthread_barrier_init (&b, NULL, 2) != 0)
97 {
98 puts ("error: pthread_barrier_init failed");
99 exit (1);
100 }
101
102 pthread_t th = xpthread_create (NULL, &tf, NULL);
103
104 if (pthread_cancel (th) != 0)
105 {
106 puts ("error: pthread_cancel failed");
107 return 1;
108 }
109
110 xpthread_barrier_wait (&b);
111
112 if (xpthread_join (th) == PTHREAD_CANCELED)
113 {
114 puts ("error: thread cancelled");
115 exit (1);
116 }
117
118 close (pipefd[1]);
119
120 /* The global 'pid' should be set by thread posix_spawn calling. Check
121 below if it was executed correctly and with expected output. */
122
123 char buf[64];
124 ssize_t n;
125 bool seen_pid = false;
126 while (TEMP_FAILURE_RETRY ((n = read (pipefd[0], buf, sizeof (buf)))) > 0)
127 {
128 /* We only expect to read the PID. */
129 char *endp;
130 long int rpid = strtol (buf, &endp, 10);
131
132 if (*endp != '\n')
133 {
134 printf ("error: didn't parse whole line: \"%s\"\n", buf);
135 exit (1);
136 }
137 if (endp == buf)
138 {
139 puts ("error: read empty line");
140 exit (1);
141 }
142
143 if (rpid != pid)
144 {
145 printf ("error: found \"%s\", expected PID %ld\n", buf,
146 (long int) pid);
147 exit (1);
148 }
149
150 if (seen_pid)
151 {
152 puts ("error: found more than one PID line");
153 exit (1);
154 }
155
156 seen_pid = true;
157 }
158
159 close (pipefd[0]);
160
161 int status;
162 int err = waitpid (pid, &status, 0);
163 if (err != pid)
164 {
165 puts ("errnor: waitpid failed");
166 exit (1);
167 }
168
169 if (!seen_pid)
170 {
171 puts ("error: didn't get PID");
172 exit (1);
173 }
174
175 return 0;
176}
177

Warning: That file was not part of the compilation database. It may have many parsing errors.