Back to index

glibc  2.9
tst-exec1.c
Go to the documentation of this file.
00001 /* Simple exec test, only a thread in the parent.
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   posix_spawn_file_actions_t a;
00067   if (posix_spawn_file_actions_init (&a) != 0)
00068     {
00069       puts ("spawn_file_actions_init failed");
00070       exit (1);
00071     }
00072 
00073   if (posix_spawn_file_actions_adddup2 (&a, fd[1], STDOUT_FILENO) != 0)
00074     {
00075       puts ("spawn_file_actions_adddup2 failed");
00076       exit (1);
00077     }
00078 
00079   if (posix_spawn_file_actions_addclose (&a, fd[0]) != 0)
00080     {
00081       puts ("spawn_file_actions_addclose");
00082       exit (1);
00083     }
00084 
00085   pthread_t th;
00086   if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
00087     {
00088       puts ("create failed");
00089       exit (1);
00090     }
00091 
00092   pid_t pid;
00093   char *argv[] = { (char *) _PATH_BSHELL, (char *) "-c", (char *) "echo $$",
00094                  NULL };
00095   if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, argv, NULL) != 0)
00096     {
00097       puts ("spawn failed");
00098       exit (1);
00099     }
00100 
00101   close (fd[1]);
00102 
00103   char buf[200];
00104   ssize_t n;
00105   bool seen_pid = false;
00106   while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
00107     {
00108       /* We only expect to read the PID.  */
00109       char *endp;
00110       long int rpid = strtol (buf, &endp, 10);
00111 
00112       if (*endp != '\n')
00113        {
00114          printf ("didn't parse whole line: \"%s\"\n", buf);
00115          exit (1);
00116        }
00117       if (endp == buf)
00118        {
00119          puts ("read empty line");
00120          exit (1);
00121        }
00122 
00123       if (rpid != pid)
00124        {
00125          printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
00126          exit (1);
00127        }
00128 
00129       if (seen_pid)
00130        {
00131          puts ("found more than one PID line");
00132          exit (1);
00133        }
00134 
00135       seen_pid = true;
00136     }
00137 
00138   close (fd[0]);
00139 
00140   int status;
00141   int err = waitpid (pid, &status, 0);
00142   if (err != pid)
00143     {
00144       puts ("waitpid failed");
00145       exit (1);
00146     }
00147 
00148   if (!seen_pid)
00149     {
00150       puts ("didn't get PID");
00151       exit (1);
00152     }
00153 
00154   puts ("read correct PID");
00155 
00156   return 0;
00157 }
00158 
00159 #define TEST_FUNCTION do_test ()
00160 #include "../test-skeleton.c"