OLD | NEW |
(Empty) | |
| 1 /* Subprocesses with pipes. |
| 2 |
| 3 Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. |
| 4 |
| 5 This program is free software: you can redistribute it and/or modify |
| 6 it under the terms of the GNU General Public License as published by |
| 7 the Free Software Foundation, either version 3 of the License, or |
| 8 (at your option) any later version. |
| 9 |
| 10 This program is distributed in the hope that it will be useful, |
| 11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 GNU General Public License for more details. |
| 14 |
| 15 You should have received a copy of the GNU General Public License |
| 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 |
| 18 /* Written by Paul Eggert <eggert@twinsun.com> |
| 19 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */ |
| 20 |
| 21 #include <config.h> |
| 22 |
| 23 #include "subpipe.h" |
| 24 |
| 25 #include <errno.h> |
| 26 |
| 27 #include <signal.h> |
| 28 #if ! defined SIGCHLD && defined SIGCLD |
| 29 # define SIGCHLD SIGCLD |
| 30 #endif |
| 31 |
| 32 #include <stdlib.h> |
| 33 |
| 34 #include <unistd.h> |
| 35 #ifndef STDIN_FILENO |
| 36 # define STDIN_FILENO 0 |
| 37 #endif |
| 38 #ifndef STDOUT_FILENO |
| 39 # define STDOUT_FILENO 1 |
| 40 #endif |
| 41 #if ! HAVE_DUP2 && ! defined dup2 |
| 42 # include <fcntl.h> |
| 43 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t)) |
| 44 #endif |
| 45 |
| 46 #if HAVE_SYS_WAIT_H |
| 47 # include <sys/wait.h> |
| 48 #endif |
| 49 #ifndef WEXITSTATUS |
| 50 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8) |
| 51 #endif |
| 52 #ifndef WIFEXITED |
| 53 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
| 54 #endif |
| 55 |
| 56 #if HAVE_VFORK_H |
| 57 # include <vfork.h> |
| 58 #endif |
| 59 #if ! HAVE_WORKING_VFORK |
| 60 # define vfork fork |
| 61 #endif |
| 62 |
| 63 #include "error.h" |
| 64 #include "unistd-safer.h" |
| 65 |
| 66 #include "gettext.h" |
| 67 #define _(Msgid) gettext (Msgid) |
| 68 |
| 69 #ifndef __attribute__ |
| 70 /* This feature is available in gcc versions 2.5 and later. */ |
| 71 # if ! defined __GNUC__ || __GNUC__ < 2 || \ |
| 72 (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ |
| 73 # define __attribute__(Spec) /* empty */ |
| 74 # endif |
| 75 #endif |
| 76 |
| 77 #ifndef ATTRIBUTE_UNUSED |
| 78 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) |
| 79 #endif |
| 80 |
| 81 /* Initialize this module. */ |
| 82 |
| 83 void |
| 84 init_subpipe (void) |
| 85 { |
| 86 #ifdef SIGCHLD |
| 87 /* System V fork+wait does not work if SIGCHLD is ignored. */ |
| 88 signal (SIGCHLD, SIG_DFL); |
| 89 #endif |
| 90 } |
| 91 |
| 92 |
| 93 /* Create a subprocess that is run as a filter. ARGV is the |
| 94 NULL-terminated argument vector for the subprocess. Store read and |
| 95 write file descriptors for communication with the subprocess into |
| 96 FD[0] and FD[1]: input meant for the process can be written into |
| 97 FD[0], and output from the process can be read from FD[1]. Return |
| 98 the subprocess id. |
| 99 |
| 100 To avoid deadlock, the invoker must not let incoming data pile up |
| 101 in FD[1] while writing data to FD[0]. */ |
| 102 |
| 103 pid_t |
| 104 create_subpipe (char const * const *argv, int fd[2]) |
| 105 { |
| 106 int pipe_fd[2]; |
| 107 int child_fd[2]; |
| 108 pid_t pid; |
| 109 |
| 110 if (pipe_safer (child_fd) != 0 || pipe_safer (pipe_fd) != 0) |
| 111 error (EXIT_FAILURE, errno, "pipe"); |
| 112 fd[0] = child_fd[1]; |
| 113 fd[1] = pipe_fd[0]; |
| 114 child_fd[1] = pipe_fd[1]; |
| 115 |
| 116 /* WIN32 patch taken from the Mingw port: |
| 117 http://prdownloads.sourceforge.net/mingw/bison-1.875-2003.02.10-1-src.tar.bz2
?download |
| 118 */ |
| 119 #ifdef _WIN32 |
| 120 /* In windows we must control what the child inherits with the parent process |
| 121 * and then use a spawn function instead of a fork/exec pair. |
| 122 * |
| 123 * So, in assigning the pipe pair we must dup stdio to save the originals, |
| 124 * dup2 the pipe handles to the stdio handles, spawn and then dup2 the saved |
| 125 * handles back to stdio and close the dupped ones. |
| 126 */ |
| 127 int osi, oso; |
| 128 |
| 129 if ((osi = dup (STDIN_FILENO)) < 0) |
| 130 error (EXIT_FAILURE, errno, "pipe"); |
| 131 setmode(osi, O_NOINHERIT); |
| 132 if ((oso = dup (STDOUT_FILENO)) < 0) |
| 133 error (EXIT_FAILURE, errno, "pipe"); |
| 134 setmode(oso, O_NOINHERIT); |
| 135 |
| 136 if (dup2(child_fd[0], STDIN_FILENO) < 0) |
| 137 error (EXIT_FAILURE, errno, "pipe"); |
| 138 if (dup2(child_fd[1], STDOUT_FILENO) < 0) |
| 139 error (EXIT_FAILURE, errno, "pipe"); |
| 140 |
| 141 #include <conio.h> |
| 142 pid = spawnvp(P_NOWAIT, argv[0], argv); |
| 143 if (pid < 0) { |
| 144 printf ("spawnvp: %s\n", argv[0]); |
| 145 error (EXIT_FAILURE, errno, argv[0]); |
| 146 } |
| 147 if (dup2 (osi, STDIN_FILENO)) |
| 148 error (EXIT_FAILURE, errno, "dup2"); |
| 149 close (osi); |
| 150 if (dup2 (oso, STDOUT_FILENO)) |
| 151 error (EXIT_FAILURE, errno, "dup2"); |
| 152 close (oso); |
| 153 |
| 154 #else |
| 155 pid = vfork (); |
| 156 if (pid < 0) |
| 157 error (EXIT_FAILURE, errno, "fork"); |
| 158 |
| 159 if (! pid) |
| 160 { |
| 161 /* Child. */ |
| 162 close (fd[0]); |
| 163 close (fd[1]); |
| 164 dup2 (child_fd[0], STDIN_FILENO); |
| 165 close (child_fd[0]); |
| 166 dup2 (child_fd[1], STDOUT_FILENO); |
| 167 close (child_fd[1]); |
| 168 |
| 169 /* The cast to (char **) rather than (char * const *) is needed |
| 170 for portability to older hosts with a nonstandard prototype |
| 171 for execvp. */ |
| 172 execvp (argv[0], (char **) argv); |
| 173 |
| 174 _exit (errno == ENOENT ? 127 : 126); |
| 175 } |
| 176 #endif /* _WIN32 */ |
| 177 /* Parent. */ |
| 178 close (child_fd[0]); |
| 179 close (child_fd[1]); |
| 180 return pid; |
| 181 } |
| 182 |
| 183 |
| 184 /* Wait for the subprocess to exit. */ |
| 185 |
| 186 void |
| 187 reap_subpipe (pid_t pid, char const *program) |
| 188 { |
| 189 #if HAVE_WAITPID || defined waitpid |
| 190 int wstatus; |
| 191 if (waitpid (pid, &wstatus, 0) < 0) |
| 192 error (EXIT_FAILURE, errno, "waitpid"); |
| 193 else |
| 194 { |
| 195 int status = WIFEXITED (wstatus) ? WEXITSTATUS (wstatus) : -1; |
| 196 if (status) |
| 197 error (EXIT_FAILURE, 0, |
| 198 _(status == 126 |
| 199 ? "subsidiary program `%s' could not be invoked" |
| 200 : status == 127 |
| 201 ? "subsidiary program `%s' not found" |
| 202 : status < 0 |
| 203 ? "subsidiary program `%s' failed" |
| 204 : "subsidiary program `%s' failed (exit status %d)"), |
| 205 program, status); |
| 206 } |
| 207 #endif |
| 208 } |
| 209 |
| 210 void |
| 211 end_of_output_subpipe (pid_t pid ATTRIBUTE_UNUSED, |
| 212 int fd[2] ATTRIBUTE_UNUSED) |
| 213 { |
| 214 } |
OLD | NEW |