Back to index

glibc  2.9
tst-vfork1.c
Go to the documentation of this file.
00001 /* Test for vfork functions.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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 <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <sys/types.h>
00026 #include <sys/wait.h>
00027 
00028 /* This test relies on non-POSIX functionality since the child
00029    processes call write and getpid.  */
00030 static int
00031 do_test (void)
00032 {
00033   int result = 0;
00034   int fd[2];
00035 
00036   if (pipe (fd) == -1)
00037     {
00038       puts ("pipe failed");
00039       return 1;
00040     }
00041 
00042   /* First vfork() without previous getpid().  */
00043   pid_t p1;
00044   if ((p1 = vfork ()) == 0)
00045     {
00046       pid_t p = getpid ();
00047       _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
00048     }
00049   else if (p1 == -1)
00050     {
00051       puts ("1st vfork failed");
00052       result = 1;
00053     }
00054 
00055   pid_t p2 = 0;
00056   if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
00057     {
00058       puts ("1st read failed");
00059       result = 1;
00060     }
00061   int r;
00062   if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
00063     {
00064       puts ("1st waitpid failed");
00065       result = 1;
00066     }
00067   else if (r != 0)
00068     {
00069       puts ("write in 1st child failed");
00070       result = 1;
00071     }
00072 
00073   /* Main process' ID.  */
00074   pid_t p0 = getpid ();
00075 
00076   /* vfork() again, but after a getpid() in the main process.  */
00077   pid_t p3;
00078   if ((p3 = vfork ()) == 0)
00079     {
00080       pid_t p = getpid ();
00081       _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
00082     }
00083   else if (p1 == -1)
00084     {
00085       puts ("2nd vfork failed");
00086       result = 1;
00087     }
00088 
00089   pid_t p4;
00090   if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
00091     {
00092       puts ("2nd read failed");
00093       result = 1;
00094     }
00095   if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
00096     {
00097       puts ("2nd waitpid failed");
00098       result = 1;
00099     }
00100   else if (r != 0)
00101     {
00102       puts ("write in 2nd child failed");
00103       result = 1;
00104     }
00105 
00106   /* And getpid in the main process again.  */
00107   pid_t p5 = getpid ();
00108 
00109   /* Analysis of the results.  */
00110   if (p0 != p5)
00111     {
00112       printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
00113       result = 1;
00114     }
00115 
00116   if (p0 == p1)
00117     {
00118       printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
00119       result = 1;
00120     }
00121 
00122   if (p1 != p2)
00123     {
00124       printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
00125       result = 1;
00126     }
00127 
00128   if (p0 == p3)
00129     {
00130       printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
00131       result = 1;
00132     }
00133 
00134   if (p3 != p4)
00135     {
00136       printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
00137       result = 1;
00138     }
00139 
00140   close (fd[0]);
00141   close (fd[1]);
00142 
00143   if (result == 0)
00144     puts ("All OK");
00145 
00146   return result;
00147 }
00148 
00149 #define TEST_FUNCTION do_test ()
00150 #include "../test-skeleton.c"