1 /* Test for vfork functions.
2 Copyright (C) 2007-2015 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/>. */
28 static int do_test (void);
29 static void do_prepare (void);
32 #define TEST_FUNCTION do_test ()
33 #define PREPARE(argc, argv) do_prepare ()
34 #include "../test-skeleton.c"
41 const char *path = getenv ("PATH");
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");
53 char *argv[3] = { (char *) "script1.sh", (char *) "1", NULL };
54 for (i = 0; i < 5; i++)
59 printf ("vfork failed: %m\n");
64 execvp ("script1.sh", argv);
68 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
70 puts ("waitpid failed");
75 if (WIFEXITED (status))
76 printf ("script1.sh failed with status %d\n",
77 WEXITSTATUS (status));
79 printf ("script1.sh kill by signal %d\n",
85 argv[0] = (char *) "script2.sh";
86 argv[1] = (char *) "2";
87 for (i = 0; i < 5; i++)
92 printf ("vfork failed: %m\n");
97 execvp ("script2.sh", argv);
101 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
103 puts ("waitpid failed");
106 else if (status != 0)
108 printf ("script2.sh failed with status %d\n", status);
113 for (i = 0; i < 5; i++)
115 pid_t pid = vfork ();
118 printf ("vfork failed: %m\n");
123 execlp ("script2.sh", "script2.sh", "3", NULL);
127 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
129 puts ("waitpid failed");
132 else if (status != 0)
134 printf ("script2.sh failed with status %d\n", status);
140 argv[0] = (char *) "echo";
141 argv[1] = (char *) "script 4";
142 for (i = 0; i < 5; i++)
144 pid_t pid = vfork ();
147 printf ("vfork failed: %m\n");
152 execvp ("echo", argv);
156 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
158 puts ("waitpid failed");
161 else if (status != 0)
163 printf ("echo failed with status %d\n", status);
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");
183 strcpy (stpcpy (tmpdirname, test_dir), "/tst-vfork3.XXXXXX");
185 tmpdirname = mkdtemp (tmpdirname);
186 if (tmpdirname == NULL)
188 puts ("could not create temporary directory");
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);
203 || TEMP_FAILURE_RETRY (write (fd, content1, sizeof content1))
205 || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
207 printf ("Could not write %s\n", script1);
212 const char content2[] = "echo script $1\n";
213 fd = open (script2, O_WRONLY | O_CREAT, 0700);
215 || TEMP_FAILURE_RETRY (write (fd, content2, sizeof content2))
217 || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
219 printf ("Could not write %s\n", script2);