Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/pipe-filter-gi.c @ 17476:6057744acd2c default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 16 Aug 2013 06:32:22 -0700 (2013-08-16) |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
11760 | 1 /* Filtering of data through a subprocess. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
17101
diff
changeset
|
2 Copyright (C) 2001-2003, 2008-2013 Free Software Foundation, Inc. |
11760 | 3 Written by Paolo Bonzini <bonzini@gnu.org>, 2009, |
4 and Bruno Haible <bruno@clisp.org>, 2009. | |
5 | |
6 This program is free software: you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 3 of the License, or | |
9 (at your option) any later version. | |
10 | |
11 This program 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 | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include "pipe-filter.h" | |
22 | |
23 #include <errno.h> | |
24 #include <fcntl.h> | |
25 #include <stdbool.h> | |
26 #include <stdint.h> | |
27 #include <stdlib.h> | |
28 #include <unistd.h> | |
29 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
30 # include <windows.h> | |
31 #else | |
32 # include <signal.h> | |
33 # include <sys/select.h> | |
34 #endif | |
35 | |
36 #include "error.h" | |
13924
5be0c314f2f8
Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
37 #include "spawn-pipe.h" |
11760 | 38 #include "wait-process.h" |
39 #include "xalloc.h" | |
40 #include "gettext.h" | |
41 | |
42 #define _(str) gettext (str) | |
43 | |
44 #include "pipe-filter-aux.h" | |
45 | |
46 struct pipe_filter_gi | |
47 { | |
48 /* Arguments passed to pipe_filter_gi_create. */ | |
49 const char *progname; | |
50 bool null_stderr; | |
51 bool exit_on_error; | |
52 prepare_read_fn prepare_read; | |
53 done_read_fn done_read; | |
54 void *private_data; | |
55 | |
56 /* Management of the subprocess. */ | |
57 pid_t child; | |
58 int fd[2]; | |
59 bool exited; | |
60 int exitstatus; | |
61 | |
62 /* Status of the writer part. */ | |
63 volatile bool writer_terminated; | |
64 int writer_errno; | |
65 /* Status of the reader part. */ | |
66 volatile bool reader_terminated; | |
67 volatile int reader_errno; | |
68 | |
69 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
70 CRITICAL_SECTION lock; /* protects the volatile fields */ | |
71 HANDLE reader_thread_handle; | |
72 #else | |
73 struct sigaction orig_sigpipe_action; | |
74 fd_set readfds; /* All bits except fd[0] are always cleared. */ | |
75 fd_set writefds; /* All bits except fd[1] are always cleared. */ | |
76 #endif | |
77 }; | |
78 | |
79 | |
80 /* Platform dependent functions. */ | |
81 | |
82 /* Perform additional initializations. | |
83 Return 0 if successful, -1 upon failure. */ | |
17101
99fa3b05f1c8
pipe-filter-gi, pipe-filter-ii: better use of 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16242
diff
changeset
|
84 static int filter_init (struct pipe_filter_gi *filter); |
11760 | 85 |
86 /* Write count bytes starting at buf, while at the same time invoking the | |
87 read iterator (the functions prepare_read/done_read) when needed. */ | |
88 static void filter_loop (struct pipe_filter_gi *filter, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
89 const char *wbuf, size_t count); |
11760 | 90 |
91 /* Perform cleanup actions at the end. | |
92 finish_reading is true if there was no error, or false if some error | |
93 occurred already. */ | |
17101
99fa3b05f1c8
pipe-filter-gi, pipe-filter-ii: better use of 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16242
diff
changeset
|
94 static void filter_cleanup (struct pipe_filter_gi *filter, |
99fa3b05f1c8
pipe-filter-gi, pipe-filter-ii: better use of 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16242
diff
changeset
|
95 bool finish_reading); |
11760 | 96 |
97 | |
98 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
16242
59c686e5b2df
Talk about "native Windows API", not "Woe32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
99 /* Native Windows API. */ |
11760 | 100 |
101 static unsigned int WINAPI | |
102 reader_thread_func (void *thread_arg) | |
103 { | |
104 struct pipe_filter_gi *filter = (struct pipe_filter_gi *) thread_arg; | |
105 | |
106 for (;;) | |
107 { | |
108 size_t bufsize; | |
109 void *buf = filter->prepare_read (&bufsize, filter->private_data); | |
110 if (!(buf != NULL && bufsize > 0)) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
111 /* prepare_read returned wrong values. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
112 abort (); |
11760 | 113 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
114 ssize_t nread = |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
115 read (filter->fd[0], buf, bufsize > SSIZE_MAX ? SSIZE_MAX : bufsize); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
116 EnterCriticalSection (&filter->lock); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
117 /* If the writer already encountered an error, terminate. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
118 if (filter->writer_terminated) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
119 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
120 if (nread < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
121 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
122 filter->reader_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
123 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
124 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
125 else if (nread > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
126 filter->done_read (buf, nread, filter->private_data); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
127 else /* nread == 0 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
128 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
129 LeaveCriticalSection (&filter->lock); |
11760 | 130 } |
131 } | |
132 | |
133 filter->reader_terminated = true; | |
134 LeaveCriticalSection (&filter->lock); | |
135 _endthreadex (0); /* calls ExitThread (0) */ | |
136 abort (); | |
137 } | |
138 | |
17101
99fa3b05f1c8
pipe-filter-gi, pipe-filter-ii: better use of 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16242
diff
changeset
|
139 static int |
11760 | 140 filter_init (struct pipe_filter_gi *filter) |
141 { | |
142 InitializeCriticalSection (&filter->lock); | |
143 EnterCriticalSection (&filter->lock); | |
144 | |
145 filter->reader_thread_handle = | |
146 (HANDLE) _beginthreadex (NULL, 100000, reader_thread_func, filter, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
147 0, NULL); |
11760 | 148 |
149 if (filter->reader_thread_handle == NULL) | |
150 { | |
151 if (filter->exit_on_error) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
152 error (EXIT_FAILURE, 0, _("creation of reading thread failed")); |
11760 | 153 return -1; |
154 } | |
155 else | |
156 return 0; | |
157 } | |
158 | |
159 static void | |
160 filter_loop (struct pipe_filter_gi *filter, const char *wbuf, size_t count) | |
161 { | |
162 if (!filter->writer_terminated) | |
163 { | |
164 for (;;) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
165 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
166 ssize_t nwritten; |
11760 | 167 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
168 /* Allow the reader thread to continue. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
169 LeaveCriticalSection (&filter->lock); |
11760 | 170 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
171 nwritten = |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
172 write (filter->fd[1], wbuf, count > SSIZE_MAX ? SSIZE_MAX : count); |
11760 | 173 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
174 /* Get the lock back from the reader thread. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
175 EnterCriticalSection (&filter->lock); |
11760 | 176 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
177 if (nwritten < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
178 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
179 /* Don't assume that the gnulib modules 'write' and 'sigpipe' are |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
180 used. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
181 if (GetLastError () == ERROR_NO_DATA) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
182 errno = EPIPE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
183 filter->writer_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
184 filter->writer_terminated = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
185 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
186 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
187 else if (nwritten > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
188 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
189 count -= nwritten; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
190 if (count == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
191 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
192 wbuf += nwritten; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
193 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
194 else /* nwritten == 0 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
195 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
196 filter->writer_terminated = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
197 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
198 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
199 } |
11760 | 200 } |
201 } | |
202 | |
17101
99fa3b05f1c8
pipe-filter-gi, pipe-filter-ii: better use of 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16242
diff
changeset
|
203 static void |
11760 | 204 filter_cleanup (struct pipe_filter_gi *filter, bool finish_reading) |
205 { | |
206 if (finish_reading) | |
207 { | |
208 LeaveCriticalSection (&filter->lock); | |
209 WaitForSingleObject (filter->reader_thread_handle, INFINITE); | |
210 } | |
211 else | |
212 TerminateThread (filter->reader_thread_handle, 1); | |
213 | |
214 CloseHandle (filter->reader_thread_handle); | |
215 DeleteCriticalSection (&filter->lock); | |
216 } | |
217 | |
218 #else | |
219 /* Unix API. */ | |
220 | |
17101
99fa3b05f1c8
pipe-filter-gi, pipe-filter-ii: better use of 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16242
diff
changeset
|
221 static int |
11760 | 222 filter_init (struct pipe_filter_gi *filter) |
223 { | |
224 #if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__) | |
225 /* When we write to the child process and it has just terminated, | |
226 we don't want to die from a SIGPIPE signal. So set the SIGPIPE | |
227 handler to SIG_IGN, and handle EPIPE error codes in write(). */ | |
228 { | |
229 struct sigaction sigpipe_action; | |
230 | |
231 sigpipe_action.sa_handler = SIG_IGN; | |
232 sigpipe_action.sa_flags = 0; | |
233 sigemptyset (&sigpipe_action.sa_mask); | |
234 if (sigaction (SIGPIPE, &sigpipe_action, &filter->orig_sigpipe_action) < 0) | |
235 abort (); | |
236 } | |
237 #endif | |
238 | |
239 /* Enable non-blocking I/O. This permits the read() and write() calls | |
240 to return -1/EAGAIN without blocking; this is important for polling | |
241 if HAVE_SELECT is not defined. It also permits the read() and write() | |
242 calls to return after partial reads/writes; this is important if | |
243 HAVE_SELECT is defined, because select() only says that some data | |
244 can be read or written, not how many. Without non-blocking I/O, | |
245 Linux 2.2.17 and BSD systems prefer to block instead of returning | |
246 with partial results. */ | |
247 { | |
248 int fcntl_flags; | |
249 | |
250 if ((fcntl_flags = fcntl (filter->fd[1], F_GETFL, 0)) < 0 | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
251 || fcntl (filter->fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1 |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
252 || (fcntl_flags = fcntl (filter->fd[0], F_GETFL, 0)) < 0 |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
253 || fcntl (filter->fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1) |
11760 | 254 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
255 if (filter->exit_on_error) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
256 error (EXIT_FAILURE, errno, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
257 _("cannot set up nonblocking I/O to %s subprocess"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
258 filter->progname); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
259 return -1; |
11760 | 260 } |
261 } | |
262 | |
263 FD_ZERO (&filter->readfds); | |
264 FD_ZERO (&filter->writefds); | |
265 | |
266 return 0; | |
267 } | |
268 | |
269 static void | |
270 filter_loop (struct pipe_filter_gi *filter, const char *wbuf, size_t count) | |
271 { | |
272 /* This function is used in two situations: | |
273 - in order to write some data to the subprocess | |
274 [done_writing = false], | |
275 - in order to read the remaining data after everything was written | |
276 [done_writing = true]. In this case buf is NULL and count is | |
277 ignored. */ | |
278 bool done_writing = (wbuf == NULL); | |
279 | |
280 if (!done_writing) | |
281 { | |
282 if (filter->writer_terminated || filter->reader_terminated) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
283 /* pipe_filter_gi_write was called when it should not be. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
284 abort (); |
11760 | 285 } |
286 else | |
287 { | |
288 if (filter->reader_terminated) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
289 return; |
11760 | 290 } |
291 | |
292 /* Loop, trying to write the given buffer or reading, whichever is | |
293 possible. */ | |
294 for (;;) | |
295 { | |
296 /* Here filter->writer_terminated is false. When it becomes true, this | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
297 loop is terminated. */ |
11760 | 298 /* Whereas filter->reader_terminated is initially false but may become |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
299 true during this loop. */ |
11760 | 300 /* Here, if !done_writing, count > 0. When count becomes 0, this loop |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
301 is terminated. */ |
11760 | 302 /* Here, if done_writing, filter->reader_terminated is false. When |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
303 filter->reader_terminated becomes true, this loop is terminated. */ |
11760 | 304 # if HAVE_SELECT |
305 int n; | |
306 | |
307 /* See whether reading or writing is possible. */ | |
308 n = 1; | |
309 if (!filter->reader_terminated) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
310 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
311 FD_SET (filter->fd[0], &filter->readfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
312 n = filter->fd[0] + 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
313 } |
11760 | 314 if (!done_writing) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
315 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
316 FD_SET (filter->fd[1], &filter->writefds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
317 if (n <= filter->fd[1]) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
318 n = filter->fd[1] + 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
319 } |
11760 | 320 n = select (n, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
321 (!filter->reader_terminated ? &filter->readfds : NULL), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
322 (!done_writing ? &filter->writefds : NULL), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
323 NULL, NULL); |
11760 | 324 |
325 if (n < 0) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
326 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
327 if (filter->exit_on_error) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
328 error (EXIT_FAILURE, errno, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
329 _("communication with %s subprocess failed"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
330 filter->progname); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
331 filter->writer_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
332 filter->writer_terminated = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
333 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
334 } |
11760 | 335 |
336 if (!done_writing && FD_ISSET (filter->fd[1], &filter->writefds)) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
337 goto try_write; |
11760 | 338 if (!filter->reader_terminated |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
339 && FD_ISSET (filter->fd[0], &filter->readfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
340 goto try_read; |
11760 | 341 /* How could select() return if none of the two descriptors is ready? */ |
342 abort (); | |
343 # endif | |
344 | |
345 /* Attempt to write. */ | |
346 # if HAVE_SELECT | |
347 try_write: | |
348 # endif | |
349 if (!done_writing) | |
350 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
351 ssize_t nwritten = |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
352 write (filter->fd[1], wbuf, count > SSIZE_MAX ? SSIZE_MAX : count); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
353 if (nwritten < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
354 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
355 if (!IS_EAGAIN (errno)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
356 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
357 if (filter->exit_on_error) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
358 error (EXIT_FAILURE, errno, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
359 _("write to %s subprocess failed"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
360 filter->progname); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
361 filter->writer_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
362 filter->writer_terminated = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
363 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
364 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
365 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
366 else if (nwritten > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
367 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
368 count -= nwritten; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
369 if (count == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
370 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
371 wbuf += nwritten; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
372 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
373 } |
11760 | 374 # if HAVE_SELECT |
375 continue; | |
376 # endif | |
377 | |
378 /* Attempt to read. */ | |
379 # if HAVE_SELECT | |
380 try_read: | |
381 # endif | |
382 if (!filter->reader_terminated) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
383 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
384 size_t bufsize; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
385 void *buf = filter->prepare_read (&bufsize, filter->private_data); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
386 if (!(buf != NULL && bufsize > 0)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
387 /* prepare_read returned wrong values. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
388 abort (); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
389 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
390 ssize_t nread = |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
391 read (filter->fd[0], buf, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
392 bufsize > SSIZE_MAX ? SSIZE_MAX : bufsize); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
393 if (nread < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
394 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
395 if (!IS_EAGAIN (errno)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
396 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
397 if (filter->exit_on_error) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
398 error (EXIT_FAILURE, errno, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
399 _("read from %s subprocess failed"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
400 filter->progname); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
401 filter->reader_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
402 filter->reader_terminated = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
403 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
404 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
405 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
406 else if (nread > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
407 filter->done_read (buf, nread, filter->private_data); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
408 else /* nread == 0 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
409 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
410 filter->reader_terminated = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
411 if (done_writing) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
412 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
413 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
414 } |
11760 | 415 } |
416 # if HAVE_SELECT | |
417 continue; | |
418 # endif | |
419 } | |
420 } | |
421 | |
422 static void | |
423 filter_cleanup (struct pipe_filter_gi *filter, bool finish_reading) | |
424 { | |
425 if (finish_reading) | |
426 /* A select loop, with done_writing = true. */ | |
427 filter_loop (filter, NULL, 0); | |
428 | |
429 if (sigaction (SIGPIPE, &filter->orig_sigpipe_action, NULL) < 0) | |
430 abort (); | |
431 } | |
432 | |
433 #endif | |
434 | |
435 | |
436 /* Terminate the child process. Do nothing if it already exited. */ | |
437 static void | |
438 filter_terminate (struct pipe_filter_gi *filter) | |
439 { | |
440 if (!filter->exited) | |
441 { | |
442 /* Tell the child there is nothing more the parent will send. */ | |
443 close (filter->fd[1]); | |
444 filter_cleanup (filter, !filter->reader_terminated); | |
445 close (filter->fd[0]); | |
446 filter->exitstatus = | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
447 wait_subprocess (filter->child, filter->progname, true, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
448 filter->null_stderr, true, filter->exit_on_error, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
449 NULL); |
11760 | 450 if (filter->exitstatus != 0 && filter->exit_on_error) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
451 error (EXIT_FAILURE, 0, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
452 _("subprocess %s terminated with exit code %d"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
453 filter->progname, filter->exitstatus); |
11760 | 454 filter->exited = true; |
455 } | |
456 } | |
457 | |
458 /* After filter_terminate: | |
459 Return 0 upon success, or (only if exit_on_error is false): | |
460 - -1 with errno set upon failure, | |
461 - the positive exit code of the subprocess if that failed. */ | |
17101
99fa3b05f1c8
pipe-filter-gi, pipe-filter-ii: better use of 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16242
diff
changeset
|
462 static int |
11760 | 463 filter_retcode (struct pipe_filter_gi *filter) |
464 { | |
465 if (filter->writer_errno != 0) | |
466 { | |
467 errno = filter->writer_errno; | |
468 return -1; | |
469 } | |
470 else if (filter->reader_errno != 0) | |
471 { | |
472 errno = filter->reader_errno; | |
473 return -1; | |
474 } | |
475 else | |
476 return filter->exitstatus; | |
477 } | |
478 | |
479 struct pipe_filter_gi * | |
480 pipe_filter_gi_create (const char *progname, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
481 const char *prog_path, const char **prog_argv, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
482 bool null_stderr, bool exit_on_error, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
483 prepare_read_fn prepare_read, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
484 done_read_fn done_read, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
485 void *private_data) |
11760 | 486 { |
487 struct pipe_filter_gi *filter; | |
488 | |
489 filter = | |
490 (struct pipe_filter_gi *) xmalloc (sizeof (struct pipe_filter_gi)); | |
491 | |
492 /* Open a bidirectional pipe to a subprocess. */ | |
493 filter->child = create_pipe_bidi (progname, prog_path, (char **) prog_argv, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
494 null_stderr, true, exit_on_error, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
495 filter->fd); |
11760 | 496 filter->progname = progname; |
497 filter->null_stderr = null_stderr; | |
498 filter->exit_on_error = exit_on_error; | |
499 filter->prepare_read = prepare_read; | |
500 filter->done_read = done_read; | |
501 filter->private_data = private_data; | |
502 filter->exited = false; | |
503 filter->exitstatus = 0; | |
504 filter->writer_terminated = false; | |
505 filter->writer_errno = 0; | |
506 filter->reader_terminated = false; | |
507 filter->reader_errno = 0; | |
508 | |
509 if (filter->child == -1) | |
510 { | |
511 /* Child process could not be created. | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
512 Arrange for filter_retcode (filter) to be the current errno. */ |
11760 | 513 filter->writer_errno = errno; |
514 filter->writer_terminated = true; | |
515 filter->exited = true; | |
516 } | |
517 else if (filter_init (filter) < 0) | |
518 filter_terminate (filter); | |
519 | |
520 return filter; | |
521 } | |
522 | |
523 int | |
524 pipe_filter_gi_write (struct pipe_filter_gi *filter, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
525 const void *buf, size_t size) |
11760 | 526 { |
527 if (buf == NULL) | |
528 /* Invalid argument. */ | |
529 abort (); | |
530 | |
531 if (filter->exited) | |
532 return filter_retcode (filter); | |
533 | |
534 if (size > 0) | |
535 { | |
536 filter_loop (filter, buf, size); | |
537 if (filter->writer_terminated || filter->reader_terminated) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
538 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
539 filter_terminate (filter); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
540 return filter_retcode (filter); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11873
diff
changeset
|
541 } |
11760 | 542 } |
543 return 0; | |
544 } | |
545 | |
546 int | |
547 pipe_filter_gi_close (struct pipe_filter_gi *filter) | |
548 { | |
549 int ret; | |
550 int saved_errno; | |
551 | |
552 filter_terminate (filter); | |
553 ret = filter_retcode (filter); | |
554 saved_errno = errno; | |
555 free (filter); | |
556 errno = saved_errno; | |
557 return ret; | |
558 } |