1 | /* Thread calls exec.
|
---|
2 | Copyright (C) 2002 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, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #include <errno.h>
|
---|
22 | #include <paths.h>
|
---|
23 | #include <pthread.h>
|
---|
24 | #include <signal.h>
|
---|
25 | #include <spawn.h>
|
---|
26 | #include <stdbool.h>
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <unistd.h>
|
---|
30 | #include <sys/wait.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | static void *
|
---|
34 | tf (void *arg)
|
---|
35 | {
|
---|
36 | execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL);
|
---|
37 |
|
---|
38 | puts ("execl failed");
|
---|
39 | exit (1);
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | static int
|
---|
44 | do_test (void)
|
---|
45 | {
|
---|
46 | int fd[2];
|
---|
47 | if (pipe (fd) != 0)
|
---|
48 | {
|
---|
49 | puts ("pipe failed");
|
---|
50 | exit (1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /* Not interested in knowing when the pipe is closed. */
|
---|
54 | if (sigignore (SIGPIPE) != 0)
|
---|
55 | {
|
---|
56 | puts ("sigignore failed");
|
---|
57 | exit (1);
|
---|
58 | }
|
---|
59 |
|
---|
60 | pid_t pid = fork ();
|
---|
61 | if (pid == -1)
|
---|
62 | {
|
---|
63 | puts ("fork failed");
|
---|
64 | exit (1);
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (pid == 0)
|
---|
68 | {
|
---|
69 | /* Use the fd for stdout. This is kind of ugly because it
|
---|
70 | substitutes the fd of stdout but we know what we are doing
|
---|
71 | here... */
|
---|
72 | if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
---|
73 | {
|
---|
74 | puts ("dup2 failed");
|
---|
75 | exit (1);
|
---|
76 | }
|
---|
77 |
|
---|
78 | close (fd[0]);
|
---|
79 |
|
---|
80 | pthread_t th;
|
---|
81 | if (pthread_create (&th, NULL, tf, NULL) != 0)
|
---|
82 | {
|
---|
83 | puts ("create failed");
|
---|
84 | exit (1);
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (pthread_join (th, NULL) == 0)
|
---|
88 | {
|
---|
89 | puts ("join succeeded!?");
|
---|
90 | exit (1);
|
---|
91 | }
|
---|
92 |
|
---|
93 | puts ("join returned!?");
|
---|
94 | exit (1);
|
---|
95 | }
|
---|
96 |
|
---|
97 | close (fd[1]);
|
---|
98 |
|
---|
99 | char buf[200];
|
---|
100 | ssize_t n;
|
---|
101 | bool seen_pid = false;
|
---|
102 | while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
|
---|
103 | {
|
---|
104 | /* We only expect to read the PID. */
|
---|
105 | char *endp;
|
---|
106 | long int rpid = strtol (buf, &endp, 10);
|
---|
107 |
|
---|
108 | if (*endp != '\n')
|
---|
109 | {
|
---|
110 | printf ("didn't parse whole line: \"%s\"\n", buf);
|
---|
111 | exit (1);
|
---|
112 | }
|
---|
113 | if (endp == buf)
|
---|
114 | {
|
---|
115 | puts ("read empty line");
|
---|
116 | exit (1);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (rpid != pid)
|
---|
120 | {
|
---|
121 | printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
|
---|
122 | exit (1);
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (seen_pid)
|
---|
126 | {
|
---|
127 | puts ("found more than one PID line");
|
---|
128 | exit (1);
|
---|
129 | }
|
---|
130 | seen_pid = true;
|
---|
131 | }
|
---|
132 |
|
---|
133 | close (fd[0]);
|
---|
134 |
|
---|
135 | int status;
|
---|
136 | int err = waitpid (pid, &status, 0);
|
---|
137 | if (err != pid)
|
---|
138 | {
|
---|
139 | puts ("waitpid failed");
|
---|
140 | exit (1);
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (!seen_pid)
|
---|
144 | {
|
---|
145 | puts ("didn't get PID");
|
---|
146 | exit (1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | return 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 | #define TEST_FUNCTION do_test ()
|
---|
153 | #include "../test-skeleton.c"
|
---|