Back to index

glibc  2.9
tst-exec2.c
Go to the documentation of this file.
00001 /* Thread with running thread calls exec.
00002    Copyright (C) 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <paths.h>
00023 #include <pthread.h>
00024 #include <signal.h>
00025 #include <spawn.h>
00026 #include <stdbool.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <unistd.h>
00030 #include <sys/wait.h>
00031 
00032 
00033 static void *
00034 tf (void *arg)
00035 {
00036   pthread_t th = (pthread_t) arg;
00037 
00038   if (pthread_join (th, NULL) == 0)
00039     {
00040       puts ("thread in parent joined!?");
00041       exit (1);
00042     }
00043 
00044   puts ("join in thread in parent returned!?");
00045   exit (1);
00046 }
00047 
00048 
00049 static int
00050 do_test (void)
00051 {
00052   int fd[2];
00053   if (pipe (fd) != 0)
00054     {
00055       puts ("pipe failed");
00056       exit (1);
00057     }
00058 
00059   /* Not interested in knowing when the pipe is closed.  */
00060   if (sigignore (SIGPIPE) != 0)
00061     {
00062       puts ("sigignore failed");
00063       exit (1);
00064     }
00065 
00066   pid_t pid = fork ();
00067   if (pid == -1)
00068     {
00069       puts ("fork failed");
00070       exit (1);
00071     }
00072 
00073   if (pid == 0)
00074     {
00075       /* Use the fd for stdout.  This is kind of ugly because it
00076         substitutes the fd of stdout but we know what we are doing
00077         here...  */
00078       if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO)
00079        {
00080          puts ("dup2 failed");
00081          exit (1);
00082        }
00083 
00084       close (fd[0]);
00085 
00086       pthread_t th;
00087       if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
00088        {
00089          puts ("create failed");
00090          exit (1);
00091        }
00092 
00093       execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL);
00094 
00095       puts ("execl failed");
00096       exit (1);
00097     }
00098 
00099   close (fd[1]);
00100 
00101   char buf[200];
00102   ssize_t n;
00103   bool seen_pid = false;
00104   while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
00105     {
00106       /* We only expect to read the PID.  */
00107       char *endp;
00108       long int rpid = strtol (buf, &endp, 10);
00109 
00110       if (*endp != '\n')
00111        {
00112          printf ("didn't parse whole line: \"%s\"\n", buf);
00113          exit (1);
00114        }
00115       if (endp == buf)
00116        {
00117          puts ("read empty line");
00118          exit (1);
00119        }
00120 
00121       if (rpid != pid)
00122        {
00123          printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
00124          exit (1);
00125        }
00126 
00127       if (seen_pid)
00128        {
00129          puts ("found more than one PID line");
00130          exit (1);
00131        }
00132       seen_pid = true;
00133     }
00134 
00135   close (fd[0]);
00136 
00137   int status;
00138   int err = waitpid (pid, &status, 0);
00139   if (err != pid)
00140     {
00141       puts ("waitpid failed");
00142       exit (1);
00143     }
00144 
00145   if (!seen_pid)
00146     {
00147       puts ("didn't get PID");
00148       exit (1);
00149     }
00150 
00151   return 0;
00152 }
00153 
00154 #define TEST_FUNCTION do_test ()
00155 #include "../test-skeleton.c"