1 /* Check if posix_spawn does not act as a cancellation entrypoint.
2 Copyright (C) 2016-2020 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/>. */
30 static int do_test (void);
31 #define TEST_FUNCTION do_test ()
32 #include <test-skeleton.c>
34 static pthread_barrier_t b
;
42 xpthread_barrier_wait (&b
);
44 posix_spawn_file_actions_t a
;
45 if (posix_spawn_file_actions_init (&a
) != 0)
47 puts ("error: spawn_file_actions_init failed");
51 if (posix_spawn_file_actions_adddup2 (&a
, pipefd
[1], STDOUT_FILENO
) != 0)
53 puts ("error: spawn_file_actions_adddup2 failed");
57 if (posix_spawn_file_actions_addclose (&a
, pipefd
[0]) != 0)
59 puts ("error: spawn_file_actions_addclose");
63 char *argv
[] = { (char *) _PATH_BSHELL
, (char *) "-c", (char *) "echo $$",
65 if (posix_spawn (&pid
, _PATH_BSHELL
, &a
, NULL
, argv
, NULL
) != 0)
67 puts ("error: spawn failed");
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. */
82 if (pipe (pipefd
) != 0)
84 puts ("error: pipe failed");
88 /* Not interested in knowing when the pipe is closed. */
89 xsignal (SIGPIPE
, SIG_IGN
);
91 /* To synchronize with the thread. */
92 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
94 puts ("error: pthread_barrier_init failed");
98 pthread_t th
= xpthread_create (NULL
, &tf
, NULL
);
100 if (pthread_cancel (th
) != 0)
102 puts ("error: pthread_cancel failed");
106 xpthread_barrier_wait (&b
);
108 if (xpthread_join (th
) == PTHREAD_CANCELED
)
110 puts ("error: thread cancelled");
116 /* The global 'pid' should be set by thread posix_spawn calling. Check
117 below if it was executed correctly and with expected output. */
121 bool seen_pid
= false;
122 while (TEMP_FAILURE_RETRY ((n
= read (pipefd
[0], buf
, sizeof (buf
)))) > 0)
124 /* We only expect to read the PID. */
126 long int rpid
= strtol (buf
, &endp
, 10);
130 printf ("error: didn't parse whole line: \"%s\"\n", buf
);
135 puts ("error: read empty line");
141 printf ("error: found \"%s\", expected PID %ld\n", buf
,
148 puts ("error: found more than one PID line");
158 int err
= waitpid (pid
, &status
, 0);
161 puts ("errnor: waitpid failed");
167 puts ("error: didn't get PID");