source: vendor/glibc-tests/glibc/nptl/tst-exec2.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1/* Thread with running 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
33static void *
34tf (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
49static int
50do_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 pid_t pid = fork ();
67 if (pid == -1)
68 {
69 puts ("fork failed");
70 exit (1);
71 }
72
73 if (pid == 0)
74 {
75 /* Use the fd for stdout. This is kind of ugly because it
76 substitutes the fd of stdout but we know what we are doing
77 here... */
78 if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO)
79 {
80 puts ("dup2 failed");
81 exit (1);
82 }
83
84 close (fd[0]);
85
86 pthread_t th;
87 if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
88 {
89 puts ("create failed");
90 exit (1);
91 }
92
93 execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL);
94
95 puts ("execl failed");
96 exit (1);
97 }
98
99 close (fd[1]);
100
101 char buf[200];
102 ssize_t n;
103 bool seen_pid = false;
104 while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
105 {
106 /* We only expect to read the PID. */
107 char *endp;
108 long int rpid = strtol (buf, &endp, 10);
109
110 if (*endp != '\n')
111 {
112 printf ("didn't parse whole line: \"%s\"\n", buf);
113 exit (1);
114 }
115 if (endp == buf)
116 {
117 puts ("read empty line");
118 exit (1);
119 }
120
121 if (rpid != pid)
122 {
123 printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
124 exit (1);
125 }
126
127 if (seen_pid)
128 {
129 puts ("found more than one PID line");
130 exit (1);
131 }
132 seen_pid = true;
133 }
134
135 close (fd[0]);
136
137 int status;
138 int err = waitpid (pid, &status, 0);
139 if (err != pid)
140 {
141 puts ("waitpid failed");
142 exit (1);
143 }
144
145 if (!seen_pid)
146 {
147 puts ("didn't get PID");
148 exit (1);
149 }
150
151 return 0;
152}
153
154#define TEST_FUNCTION do_test ()
155#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.