| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Thread calls exec. | 
 | 2 |    Copyright (C) 2002-2016 Free Software Foundation, Inc. | 
 | 3 |    This file is part of the GNU C Library. | 
 | 4 |    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | 
 | 5 |  | 
 | 6 |    The GNU C Library is free software; you can redistribute it and/or | 
 | 7 |    modify it under the terms of the GNU Lesser General Public | 
 | 8 |    License as published by the Free Software Foundation; either | 
 | 9 |    version 2.1 of the License, or (at your option) any later version. | 
 | 10 |  | 
 | 11 |    The GNU C Library is distributed in the hope that it will be useful, | 
 | 12 |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 13 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
 | 14 |    Lesser General Public License for more details. | 
 | 15 |  | 
 | 16 |    You should have received a copy of the GNU Lesser General Public | 
 | 17 |    License along with the GNU C Library; if not, see | 
 | 18 |    <http://www.gnu.org/licenses/>.  */ | 
 | 19 |  | 
 | 20 | #include <errno.h> | 
 | 21 | #include <paths.h> | 
 | 22 | #include <pthread.h> | 
 | 23 | #include <signal.h> | 
 | 24 | #include <spawn.h> | 
 | 25 | #include <stdbool.h> | 
 | 26 | #include <stdio.h> | 
 | 27 | #include <stdlib.h> | 
 | 28 | #include <unistd.h> | 
 | 29 | #include <sys/wait.h> | 
 | 30 |  | 
 | 31 |  | 
 | 32 | static void * | 
 | 33 | tf (void *arg) | 
 | 34 | { | 
 | 35 |   execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL); | 
 | 36 |  | 
 | 37 |   puts ("execl failed"); | 
 | 38 |   exit (1); | 
 | 39 | } | 
 | 40 |  | 
 | 41 |  | 
 | 42 | static int | 
 | 43 | do_test (void) | 
 | 44 | { | 
 | 45 |   int fd[2]; | 
 | 46 |   if (pipe (fd) != 0) | 
 | 47 |     { | 
 | 48 |       puts ("pipe failed"); | 
 | 49 |       exit (1); | 
 | 50 |     } | 
 | 51 |  | 
 | 52 |   /* Not interested in knowing when the pipe is closed.  */ | 
 | 53 |   if (sigignore (SIGPIPE) != 0) | 
 | 54 |     { | 
 | 55 |       puts ("sigignore failed"); | 
 | 56 |       exit (1); | 
 | 57 |     } | 
 | 58 |  | 
 | 59 |   pid_t pid = fork (); | 
 | 60 |   if (pid == -1) | 
 | 61 |     { | 
 | 62 |       puts ("fork failed"); | 
 | 63 |       exit (1); | 
 | 64 |     } | 
 | 65 |  | 
 | 66 |   if (pid == 0) | 
 | 67 |     { | 
 | 68 |       /* Use the fd for stdout.  This is kind of ugly because it | 
 | 69 | 	 substitutes the fd of stdout but we know what we are doing | 
 | 70 | 	 here...  */ | 
 | 71 |       if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO) | 
 | 72 | 	{ | 
 | 73 | 	  puts ("dup2 failed"); | 
 | 74 | 	  exit (1); | 
 | 75 | 	} | 
 | 76 |  | 
 | 77 |       close (fd[0]); | 
 | 78 |  | 
 | 79 |       pthread_t th; | 
 | 80 |       if (pthread_create (&th, NULL, tf, NULL) != 0) | 
 | 81 | 	{ | 
 | 82 | 	  puts ("create failed"); | 
 | 83 | 	  exit (1); | 
 | 84 | 	} | 
 | 85 |  | 
 | 86 |       if (pthread_join (th, NULL) == 0) | 
 | 87 | 	{ | 
 | 88 | 	  puts ("join succeeded!?"); | 
 | 89 | 	  exit (1); | 
 | 90 | 	} | 
 | 91 |  | 
 | 92 |       puts ("join returned!?"); | 
 | 93 |       exit (1); | 
 | 94 |     } | 
 | 95 |  | 
 | 96 |   close (fd[1]); | 
 | 97 |  | 
 | 98 |   char buf[200]; | 
 | 99 |   ssize_t n; | 
 | 100 |   bool seen_pid = false; | 
 | 101 |   while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0) | 
 | 102 |     { | 
 | 103 |       /* We only expect to read the PID.  */ | 
 | 104 |       char *endp; | 
 | 105 |       long int rpid = strtol (buf, &endp, 10); | 
 | 106 |  | 
 | 107 |       if (*endp != '\n') | 
 | 108 | 	{ | 
 | 109 | 	  printf ("didn't parse whole line: \"%s\"\n", buf); | 
 | 110 | 	  exit (1); | 
 | 111 | 	} | 
 | 112 |       if (endp == buf) | 
 | 113 | 	{ | 
 | 114 | 	  puts ("read empty line"); | 
 | 115 | 	  exit (1); | 
 | 116 | 	} | 
 | 117 |  | 
 | 118 |       if (rpid != pid) | 
 | 119 | 	{ | 
 | 120 | 	  printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid); | 
 | 121 | 	  exit (1); | 
 | 122 | 	} | 
 | 123 |  | 
 | 124 |       if (seen_pid) | 
 | 125 | 	{ | 
 | 126 | 	  puts ("found more than one PID line"); | 
 | 127 | 	  exit (1); | 
 | 128 | 	} | 
 | 129 |       seen_pid = true; | 
 | 130 |     } | 
 | 131 |  | 
 | 132 |   close (fd[0]); | 
 | 133 |  | 
 | 134 |   int status; | 
 | 135 |   int err = waitpid (pid, &status, 0); | 
 | 136 |   if (err != pid) | 
 | 137 |     { | 
 | 138 |       puts ("waitpid failed"); | 
 | 139 |       exit (1); | 
 | 140 |     } | 
 | 141 |  | 
 | 142 |   if (!seen_pid) | 
 | 143 |     { | 
 | 144 |       puts ("didn't get PID"); | 
 | 145 |       exit (1); | 
 | 146 |     } | 
 | 147 |  | 
 | 148 |   return 0; | 
 | 149 | } | 
 | 150 |  | 
 | 151 | #define TEST_FUNCTION do_test () | 
 | 152 | #include "../test-skeleton.c" |