]>
Commit | Line | Data |
---|---|---|
76a50749 | 1 | /* Simple exec test, only a thread in the parent. |
2b778ceb | 2 | Copyright (C) 2002-2021 Free Software Foundation, Inc. |
76a50749 UD |
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 | |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
76a50749 UD |
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> | |
02802faf | 30 | #include <support/xsignal.h> |
76a50749 UD |
31 | |
32 | ||
33 | static void * | |
34 | tf (void *arg) | |
35 | { | |
36 | pthread_t th = (pthread_t) arg; | |
37 | ||
38 | if (pthread_join (th, NULL) == 0) | |
39 | { | |
40 | puts ("thread in parent joined!?"); | |
41 | exit (1); | |
42 | } | |
43 | ||
44 | puts ("join in thread in parent returned!?"); | |
45 | exit (1); | |
46 | } | |
47 | ||
48 | ||
49 | static int | |
50 | do_test (void) | |
51 | { | |
52 | int fd[2]; | |
53 | if (pipe (fd) != 0) | |
54 | { | |
55 | puts ("pipe failed"); | |
56 | exit (1); | |
57 | } | |
58 | ||
59 | /* Not interested in knowing when the pipe is closed. */ | |
02802faf | 60 | xsignal (SIGPIPE, SIG_IGN); |
76a50749 UD |
61 | |
62 | posix_spawn_file_actions_t a; | |
63 | if (posix_spawn_file_actions_init (&a) != 0) | |
64 | { | |
65 | puts ("spawn_file_actions_init failed"); | |
66 | exit (1); | |
67 | } | |
68 | ||
69 | if (posix_spawn_file_actions_adddup2 (&a, fd[1], STDOUT_FILENO) != 0) | |
70 | { | |
71 | puts ("spawn_file_actions_adddup2 failed"); | |
72 | exit (1); | |
73 | } | |
74 | ||
75 | if (posix_spawn_file_actions_addclose (&a, fd[0]) != 0) | |
76 | { | |
77 | puts ("spawn_file_actions_addclose"); | |
78 | exit (1); | |
79 | } | |
80 | ||
81 | pthread_t th; | |
82 | if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0) | |
83 | { | |
84 | puts ("create failed"); | |
85 | exit (1); | |
86 | } | |
87 | ||
88 | pid_t pid; | |
89 | char *argv[] = { (char *) _PATH_BSHELL, (char *) "-c", (char *) "echo $$", | |
90 | NULL }; | |
91 | if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, argv, NULL) != 0) | |
92 | { | |
93 | puts ("spawn failed"); | |
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 | ||
131 | seen_pid = true; | |
132 | } | |
133 | ||
134 | close (fd[0]); | |
135 | ||
136 | int status; | |
137 | int err = waitpid (pid, &status, 0); | |
138 | if (err != pid) | |
139 | { | |
140 | puts ("waitpid failed"); | |
141 | exit (1); | |
142 | } | |
143 | ||
144 | if (!seen_pid) | |
145 | { | |
146 | puts ("didn't get PID"); | |
147 | exit (1); | |
148 | } | |
149 | ||
150 | puts ("read correct PID"); | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
155 | #define TEST_FUNCTION do_test () | |
156 | #include "../test-skeleton.c" |