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

1/* Simple exec test, only a thread in the parent.
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5
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.
10
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.
15
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 <http://www.gnu.org/licenses/>. */
19
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
31
32static void *
33tf (void *arg)
34{
35 pthread_t th = (pthread_t) arg;
36
37 if (pthread_join (th, NULL) == 0)
38 {
39 puts ("thread in parent joined!?");
40 exit (1);
41 }
42
43 puts ("join in thread in parent returned!?");
44 exit (1);
45}
46
47
48static int
49do_test (void)
50{
51 int fd[2];
52 if (pipe (fd) != 0)
53 {
54 puts ("pipe failed");
55 exit (1);
56 }
57
58 /* Not interested in knowing when the pipe is closed. */
59 if (sigignore (SIGPIPE) != 0)
60 {
61 puts ("sigignore failed");
62 exit (1);
63 }
64
65 posix_spawn_file_actions_t a;
66 if (posix_spawn_file_actions_init (&a) != 0)
67 {
68 puts ("spawn_file_actions_init failed");
69 exit (1);
70 }
71
72 if (posix_spawn_file_actions_adddup2 (&a, fd[1], STDOUT_FILENO) != 0)
73 {
74 puts ("spawn_file_actions_adddup2 failed");
75 exit (1);
76 }
77
78 if (posix_spawn_file_actions_addclose (&a, fd[0]) != 0)
79 {
80 puts ("spawn_file_actions_addclose");
81 exit (1);
82 }
83
84 pthread_t th;
85 if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
86 {
87 puts ("create failed");
88 exit (1);
89 }
90
91 pid_t pid;
92 char *argv[] = { (char *) _PATH_BSHELL, (char *) "-c", (char *) "echo $$",
93 NULL };
94 if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, argv, NULL) != 0)
95 {
96 puts ("spawn failed");
97 exit (1);
98 }
99
100 close (fd[1]);
101
102 char buf[200];
103 ssize_t n;
104 bool seen_pid = false;
105 while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
106 {
107 /* We only expect to read the PID. */
108 char *endp;
109 long int rpid = strtol (buf, &endp, 10);
110
111 if (*endp != '\n')
112 {
113 printf ("didn't parse whole line: \"%s\"\n", buf);
114 exit (1);
115 }
116 if (endp == buf)
117 {
118 puts ("read empty line");
119 exit (1);
120 }
121
122 if (rpid != pid)
123 {
124 printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
125 exit (1);
126 }
127
128 if (seen_pid)
129 {
130 puts ("found more than one PID line");
131 exit (1);
132 }
133
134 seen_pid = true;
135 }
136
137 close (fd[0]);
138
139 int status;
140 int err = waitpid (pid, &status, 0);
141 if (err != pid)
142 {
143 puts ("waitpid failed");
144 exit (1);
145 }
146
147 if (!seen_pid)
148 {
149 puts ("didn't get PID");
150 exit (1);
151 }
152
153 puts ("read correct PID");
154
155 return 0;
156}
157
158#define TEST_FUNCTION do_test ()
159#include "../test-skeleton.c"
160

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