Wrap too-long log entry.
[glibc.git] / posix / tst-vfork3.c
blob399a7285b4556498b40dca2c556a285779ffbe8f
1 /* Test for vfork functions.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
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.
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.
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/>. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <mcheck.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/wait.h>
28 static int do_test (void);
29 static void do_prepare (void);
30 char *tmpdirname;
32 #define TEST_FUNCTION do_test ()
33 #define PREPARE(argc, argv) do_prepare ()
34 #include "../test-skeleton.c"
36 static int
37 do_test (void)
39 mtrace ();
41 const char *path = getenv ("PATH");
42 if (path == NULL)
43 path = "/bin";
44 char pathbuf[strlen (tmpdirname) + 1 + strlen (path) + 1];
45 strcpy (stpcpy (stpcpy (pathbuf, tmpdirname), ":"), path);
46 if (setenv ("PATH", pathbuf, 1) < 0)
48 puts ("setenv failed");
49 return 1;
52 size_t i;
53 char *argv[3] = { (char *) "script1.sh", (char *) "1", NULL };
54 for (i = 0; i < 5; i++)
56 pid_t pid = vfork ();
57 if (pid < 0)
59 printf ("vfork failed: %m\n");
60 return 1;
62 else if (pid == 0)
64 execvp ("script1.sh", argv);
65 _exit (errno);
67 int status;
68 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
70 puts ("waitpid failed");
71 return 1;
73 else if (status != 0)
75 if (WIFEXITED (status))
76 printf ("script1.sh failed with status %d\n",
77 WEXITSTATUS (status));
78 else
79 printf ("script1.sh kill by signal %d\n",
80 WTERMSIG (status));
81 return 1;
85 argv[0] = (char *) "script2.sh";
86 argv[1] = (char *) "2";
87 for (i = 0; i < 5; i++)
89 pid_t pid = vfork ();
90 if (pid < 0)
92 printf ("vfork failed: %m\n");
93 return 1;
95 else if (pid == 0)
97 execvp ("script2.sh", argv);
98 _exit (errno);
100 int status;
101 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
103 puts ("waitpid failed");
104 return 1;
106 else if (status != 0)
108 printf ("script2.sh failed with status %d\n", status);
109 return 1;
113 for (i = 0; i < 5; i++)
115 pid_t pid = vfork ();
116 if (pid < 0)
118 printf ("vfork failed: %m\n");
119 return 1;
121 else if (pid == 0)
123 execlp ("script2.sh", "script2.sh", "3", NULL);
124 _exit (errno);
126 int status;
127 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
129 puts ("waitpid failed");
130 return 1;
132 else if (status != 0)
134 printf ("script2.sh failed with status %d\n", status);
135 return 1;
139 unsetenv ("PATH");
140 argv[0] = (char *) "echo";
141 argv[1] = (char *) "script 4";
142 for (i = 0; i < 5; i++)
144 pid_t pid = vfork ();
145 if (pid < 0)
147 printf ("vfork failed: %m\n");
148 return 1;
150 else if (pid == 0)
152 execvp ("echo", argv);
153 _exit (errno);
155 int status;
156 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
158 puts ("waitpid failed");
159 return 1;
161 else if (status != 0)
163 printf ("echo failed with status %d\n", status);
164 return 1;
168 return 0;
171 static void
172 do_prepare (void)
174 size_t len = strlen (test_dir) + sizeof ("/tst-vfork3.XXXXXX");
175 tmpdirname = malloc (len);
176 char *script1 = malloc (len + sizeof "/script1.sh");
177 char *script2 = malloc (len + sizeof "/script2.sh");
178 if (tmpdirname == NULL || script1 == NULL || script2 == NULL)
180 puts ("out of memory");
181 exit (1);
183 strcpy (stpcpy (tmpdirname, test_dir), "/tst-vfork3.XXXXXX");
185 tmpdirname = mkdtemp (tmpdirname);
186 if (tmpdirname == NULL)
188 puts ("could not create temporary directory");
189 exit (1);
192 strcpy (stpcpy (script1, tmpdirname), "/script1.sh");
193 strcpy (stpcpy (script2, tmpdirname), "/script2.sh");
195 /* Need to make sure tmpdirname is at the end of the linked list. */
196 add_temp_file (script1);
197 add_temp_file (tmpdirname);
198 add_temp_file (script2);
200 const char content1[] = "#!/bin/sh\necho script $1\n";
201 int fd = open (script1, O_WRONLY | O_CREAT, 0700);
202 if (fd < 0
203 || TEMP_FAILURE_RETRY (write (fd, content1, sizeof content1))
204 != sizeof content1
205 || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
207 printf ("Could not write %s\n", script1);
208 exit (1);
210 close (fd);
212 const char content2[] = "echo script $1\n";
213 fd = open (script2, O_WRONLY | O_CREAT, 0700);
214 if (fd < 0
215 || TEMP_FAILURE_RETRY (write (fd, content2, sizeof content2))
216 != sizeof content2
217 || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
219 printf ("Could not write %s\n", script2);
220 exit (1);
222 close (fd);