Back to index

glibc  2.9
tst-vfork3.c
Go to the documentation of this file.
00001 /* Test for vfork functions.
00002    Copyright (C) 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <mcheck.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/wait.h>
00028 
00029 static int do_test (void);
00030 static void do_prepare (void);
00031 char *tmpdirname;
00032 
00033 #define TEST_FUNCTION do_test ()
00034 #define PREPARE(argc, argv) do_prepare ()
00035 #include "../test-skeleton.c"
00036 
00037 static int
00038 do_test (void)
00039 {
00040   mtrace ();
00041 
00042   const char *path = getenv ("PATH");
00043   if (path == NULL)
00044     path = "/bin";
00045   char pathbuf[strlen (tmpdirname) + 1 + strlen (path) + 1];
00046   strcpy (stpcpy (stpcpy (pathbuf, tmpdirname), ":"), path);
00047   if (setenv ("PATH", pathbuf, 1) < 0)
00048     {
00049       puts ("setenv failed");
00050       return 1;
00051     }
00052 
00053   size_t i;
00054   char *argv[3] = { (char *) "script1.sh", (char *) "1", NULL };
00055   for (i = 0; i < 5; i++)
00056     {
00057       pid_t pid = vfork ();
00058       if (pid < 0)
00059        {
00060          printf ("vfork failed: %m\n");
00061          return 1;
00062        }
00063       else if (pid == 0)
00064        {
00065          execvp ("script1.sh", argv);
00066          _exit (errno);
00067        }
00068       int status;
00069       if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00070        {
00071          puts ("waitpid failed");
00072          return 1;
00073        }
00074       else if (status != 0)
00075        {
00076          if (WIFEXITED (status))
00077            printf ("script1.sh failed with status %d\n",
00078                   WEXITSTATUS (status));
00079          else
00080            printf ("script1.sh kill by signal %d\n",
00081                   WTERMSIG (status));
00082          return 1;
00083        }
00084     }
00085 
00086   argv[0] = (char *) "script2.sh";
00087   argv[1] = (char *) "2";
00088   for (i = 0; i < 5; i++)
00089     {
00090       pid_t pid = vfork ();
00091       if (pid < 0)
00092        {
00093          printf ("vfork failed: %m\n");
00094          return 1;
00095        }
00096       else if (pid == 0)
00097        {
00098          execvp ("script2.sh", argv);
00099          _exit (errno);
00100        }
00101       int status;
00102       if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00103        {
00104          puts ("waitpid failed");
00105          return 1;
00106        }
00107       else if (status != 0)
00108        {
00109          printf ("script2.sh failed with status %d\n", status);
00110          return 1;
00111        }
00112     }
00113 
00114   for (i = 0; i < 5; i++)
00115     {
00116       pid_t pid = vfork ();
00117       if (pid < 0)
00118        {
00119          printf ("vfork failed: %m\n");
00120          return 1;
00121        }
00122       else if (pid == 0)
00123        {
00124          execlp ("script2.sh", "script2.sh", "3", NULL);
00125          _exit (errno);
00126        }
00127       int status;
00128       if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00129        {
00130          puts ("waitpid failed");
00131          return 1;
00132        }
00133       else if (status != 0)
00134        {
00135          printf ("script2.sh failed with status %d\n", status);
00136          return 1;
00137        }
00138     }
00139 
00140   unsetenv ("PATH");
00141   argv[0] = (char *) "echo";
00142   argv[1] = (char *) "script 4";
00143   for (i = 0; i < 5; i++)
00144     {
00145       pid_t pid = vfork ();
00146       if (pid < 0)
00147        {
00148          printf ("vfork failed: %m\n");
00149          return 1;
00150        }
00151       else if (pid == 0)
00152        {
00153          execvp ("echo", argv);
00154          _exit (errno);
00155        }
00156       int status;
00157       if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00158        {
00159          puts ("waitpid failed");
00160          return 1;
00161        }
00162       else if (status != 0)
00163        {
00164          printf ("echo failed with status %d\n", status);
00165          return 1;
00166        }
00167     }
00168 
00169   return 0;
00170 }
00171 
00172 static void
00173 do_prepare (void)
00174 {
00175   size_t len = strlen (test_dir) + sizeof ("/tst-vfork3.XXXXXX");
00176   tmpdirname = malloc (len);
00177   char *script1 = malloc (len + sizeof "/script1.sh");
00178   char *script2 = malloc (len + sizeof "/script2.sh");
00179   if (tmpdirname == NULL || script1 == NULL || script2 == NULL)
00180     {
00181       puts ("out of memory");
00182       exit (1);
00183     }
00184   strcpy (stpcpy (tmpdirname, test_dir), "/tst-vfork3.XXXXXX");
00185 
00186   tmpdirname = mkdtemp (tmpdirname);
00187   if (tmpdirname == NULL)
00188     {
00189       puts ("could not create temporary directory");
00190       exit (1);
00191     }
00192 
00193   strcpy (stpcpy (script1, tmpdirname), "/script1.sh");
00194   strcpy (stpcpy (script2, tmpdirname), "/script2.sh");
00195 
00196   /* Need to make sure tmpdirname is at the end of the linked list.  */
00197   add_temp_file (script1);
00198   add_temp_file (tmpdirname);
00199   add_temp_file (script2);
00200 
00201   const char content1[] = "#!/bin/sh\necho script $1\n";
00202   int fd = open (script1, O_WRONLY | O_CREAT, 0700);
00203   if (fd < 0
00204       || TEMP_FAILURE_RETRY (write (fd, content1, sizeof content1))
00205         != sizeof content1
00206       || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
00207     {
00208       printf ("Could not write %s\n", script1);
00209       exit (1);
00210     }
00211   close (fd);
00212 
00213   const char content2[] = "echo script $1\n";
00214   fd = open (script2, O_WRONLY | O_CREAT, 0700);
00215   if (fd < 0
00216       || TEMP_FAILURE_RETRY (write (fd, content2, sizeof content2))
00217         != sizeof content2
00218       || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
00219     {
00220       printf ("Could not write %s\n", script2);
00221       exit (1);
00222     }
00223   close (fd);
00224 }