Upload Tizen:Base source
[external/eglibc.git] / nptl / tst-exec1.c
1 /* Simple exec test, only a thread in the parent.
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   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.  */
60   if (sigignore (SIGPIPE) != 0)
61     {
62       puts ("sigignore failed");
63       exit (1);
64     }
65
66   posix_spawn_file_actions_t a;
67   if (posix_spawn_file_actions_init (&a) != 0)
68     {
69       puts ("spawn_file_actions_init failed");
70       exit (1);
71     }
72
73   if (posix_spawn_file_actions_adddup2 (&a, fd[1], STDOUT_FILENO) != 0)
74     {
75       puts ("spawn_file_actions_adddup2 failed");
76       exit (1);
77     }
78
79   if (posix_spawn_file_actions_addclose (&a, fd[0]) != 0)
80     {
81       puts ("spawn_file_actions_addclose");
82       exit (1);
83     }
84
85   pthread_t th;
86   if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
87     {
88       puts ("create failed");
89       exit (1);
90     }
91
92   pid_t pid;
93   char *argv[] = { (char *) _PATH_BSHELL, (char *) "-c", (char *) "echo $$",
94                    NULL };
95   if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, argv, NULL) != 0)
96     {
97       puts ("spawn failed");
98       exit (1);
99     }
100
101   close (fd[1]);
102
103   char buf[200];
104   ssize_t n;
105   bool seen_pid = false;
106   while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
107     {
108       /* We only expect to read the PID.  */
109       char *endp;
110       long int rpid = strtol (buf, &endp, 10);
111
112       if (*endp != '\n')
113         {
114           printf ("didn't parse whole line: \"%s\"\n", buf);
115           exit (1);
116         }
117       if (endp == buf)
118         {
119           puts ("read empty line");
120           exit (1);
121         }
122
123       if (rpid != pid)
124         {
125           printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
126           exit (1);
127         }
128
129       if (seen_pid)
130         {
131           puts ("found more than one PID line");
132           exit (1);
133         }
134
135       seen_pid = true;
136     }
137
138   close (fd[0]);
139
140   int status;
141   int err = waitpid (pid, &status, 0);
142   if (err != pid)
143     {
144       puts ("waitpid failed");
145       exit (1);
146     }
147
148   if (!seen_pid)
149     {
150       puts ("didn't get PID");
151       exit (1);
152     }
153
154   puts ("read correct PID");
155
156   return 0;
157 }
158
159 #define TEST_FUNCTION do_test ()
160 #include "../test-skeleton.c"