Back to index

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