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