2 * @brief Run an external filter and capture its output in a std::string.
4 /* Copyright (C) 2003,2006,2007,2009,2010,2011,2013,2015,2017,2018 Olly Betts
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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "runfilter.h"
29 #include <sys/types.h>
30 #include "safefcntl.h"
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
37 #ifdef HAVE_SYS_RESOURCE_H
38 # include <sys/resource.h>
40 #include "safesysselect.h"
41 #ifdef HAVE_SYS_SOCKET_H
42 # include <sys/socket.h>
44 #include "safesyswait.h"
45 #include "safeunistd.h"
47 #if defined HAVE_FORK && defined HAVE_SOCKETPAIR
53 #include "stringutils.h"
57 # define pclose _pclose
62 static int devnull
= -1;
64 #if defined HAVE_FORK && defined HAVE_SOCKETPAIR
66 command_needs_shell(const char * p
)
69 // Probably overly conservative, but suitable for
71 if (strchr("!\"#$&()*;<>?[\\]^`{|}~", *p
) != NULL
) {
79 unquote(string
& s
, size_t & j
)
87 j
= s
.find('\'', j
+ 1);
89 // Unmatched ' in command string.
90 // dash exits 2 in this case, bash exits 1.
93 // Replace four character sequence '\'' with ' - this is
94 // how a single quote inside single quotes gets escaped.
95 if (s
[j
+ 1] != '\\' ||
102 if (j
+ 1 != s
.size()) {
104 if (ch
!= ' ' && ch
!= '\t' && ch
!= '\n') {
105 // Handle the expansion of e.g.: --input=%f,html
112 j
= s
.find_first_of(" \t\n'", j
+ 1);
113 // Handle the expansion of e.g.: --input=%f
114 if (j
!= s
.npos
&& s
[j
] == '\'') goto single_quoted
;
122 static pid_t pid_to_kill_on_signal
;
124 #ifdef HAVE_SIGACTION
125 static struct sigaction old_hup_handler
;
126 static struct sigaction old_int_handler
;
127 static struct sigaction old_quit_handler
;
128 static struct sigaction old_term_handler
;
133 handle_signal(int signum
)
135 if (pid_to_kill_on_signal
) {
136 kill(pid_to_kill_on_signal
, SIGKILL
);
137 pid_to_kill_on_signal
= 0;
141 sigaction(signum
, &old_hup_handler
, NULL
);
144 sigaction(signum
, &old_int_handler
, NULL
);
147 sigaction(signum
, &old_quit_handler
, NULL
);
150 sigaction(signum
, &old_term_handler
, NULL
);
161 runfilter_init_signal_handlers_()
164 sa
.sa_handler
= handle_signal
;
165 sigemptyset(&sa
.sa_mask
);
168 sigaction(SIGHUP
, &sa
, &old_hup_handler
);
169 sigaction(SIGINT
, &sa
, &old_int_handler
);
170 sigaction(SIGQUIT
, &sa
, &old_quit_handler
);
171 sigaction(SIGTERM
, &sa
, &old_term_handler
);
174 static sighandler_t old_hup_handler
;
175 static sighandler_t old_int_handler
;
176 static sighandler_t old_quit_handler
;
177 static sighandler_t old_term_handler
;
182 handle_signal(int signum
)
184 if (pid_to_kill_on_signal
) {
185 kill(pid_to_kill_on_signal
, SIGKILL
);
186 pid_to_kill_on_signal
= 0;
190 signal(signum
, old_hup_handler
);
193 signal(signum
, old_int_handler
);
196 signal(signum
, old_quit_handler
);
199 signal(signum
, old_term_handler
);
210 runfilter_init_signal_handlers_()
212 old_hup_handler
= signal(SIGHUP
, handle_signal
);
213 old_int_handler
= signal(SIGINT
, handle_signal
);
214 old_quit_handler
= signal(SIGQUIT
, handle_signal
);
215 old_term_handler
= signal(SIGTERM
, handle_signal
);
220 command_needs_shell(const char *)
222 // We don't try to avoid the shell on this platform, so don't waste time
223 // analysing commands to see if they could.
228 runfilter_init_signal_handlers_()
236 runfilter_init_signal_handlers_();
237 devnull
= open("/dev/null", O_WRONLY
);
239 cerr
<< "Failed to open /dev/null: " << strerror(errno
) << endl
;
242 // Ensure that devnull isn't fd 0, 1 or 2 (stdin, stdout or stderr) and
243 // that we have open fds for stdin, stdout and stderr. This simplifies the
244 // code after fork() because it doesn't need to worry about such corner
246 while (devnull
<= 2) {
247 devnull
= dup(devnull
);
252 run_filter(int fd_in
, const string
& cmd
, bool use_shell
, string
* out
,
255 #if defined HAVE_FORK && defined HAVE_SOCKETPAIR
256 // We want to be able to get the exit status of the child process.
257 signal(SIGCHLD
, SIG_DFL
);
260 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, fds
) < 0)
261 throw ReadError("socketpair failed");
263 pid_t child
= fork();
265 // We're the child process.
268 // Put the child process into its own process group, so that we can
269 // easily kill it and any children it in turn forks if we need to.
273 // Close the parent's side of the socket pair.
277 // Connect piped input to stdin.
282 // Connect stdout to our side of the socket pair.
285 #ifdef HAVE_SETRLIMIT
286 // Impose some pretty generous resource limits to prevent run-away
287 // filter programs from causing problems.
289 // Limit CPU time to 300 seconds (5 minutes).
290 struct rlimit cpu_limit
= { 300, RLIM_INFINITY
};
291 setrlimit(RLIMIT_CPU
, &cpu_limit
);
293 #if defined RLIMIT_AS || defined RLIMIT_VMEM || defined RLIMIT_DATA
294 // Limit process data to free physical memory.
295 long mem
= get_free_physical_memory();
297 struct rlimit ram_limit
= {
298 static_cast<rlim_t
>(mem
),
302 setrlimit(RLIMIT_AS
, &ram_limit
);
303 #elif defined RLIMIT_VMEM
304 setrlimit(RLIMIT_VMEM
, &ram_limit
);
306 // Only limits the data segment rather than the total address
307 // space, but that's better than nothing.
308 setrlimit(RLIMIT_DATA
, &ram_limit
);
315 execl("/bin/sh", "/bin/sh", "-c", cmd
.c_str(), (void*)NULL
);
320 // Handle any environment variable assignments.
321 // Name must start with alpha or '_', contain only alphanumerics and
322 // '_', and there must be no quoting of either the name or the '='.
325 j
= s
.find_first_not_of(" \t\n", j
);
326 if (!(C_isalnum(s
[j
]) || s
[j
] == '_')) break;
328 do ++j
; while (C_isalnum(s
[j
]) || s
[j
] == '_');
337 setenv(&s
[i
], &s
[eq
+ 1], 1);
338 j
= s
.find_first_not_of(" \t\n", j
);
341 vector
<const char *> argv
;
343 size_t i
= s
.find_first_not_of(" \t\n", j
);
344 if (i
== string::npos
) break;
345 bool quoted
= unquote(s
, j
);
346 const char * word
= s
.c_str() + i
;
348 // Handle simple cases of redirection.
349 if (strcmp(word
, ">/dev/null") == 0) {
353 if (strcmp(word
, "2>/dev/null") == 0) {
357 if (strcmp(word
, "2>&1") == 0) {
361 if (strcmp(word
, "1>&2") == 0) {
366 argv
.push_back(word
);
368 if (argv
.empty()) _exit(0);
369 argv
.push_back(NULL
);
371 execvp(argv
[0], const_cast<char **>(&argv
[0]));
372 // Emulate shell behaviour and exit with status 127 if the command
373 // isn't found, and status 126 for other problems. In particular, we
374 // rely on 127 below to throw NoSuchFilter.
375 _exit(errno
== ENOENT
? 127 : 126);
378 // We're the parent process.
380 pid_to_kill_on_signal
= -child
;
382 pid_to_kill_on_signal
= child
;
385 // Close the child's side of the socket pair.
390 throw ReadError("fork failed");
398 // If we wait 300 seconds (5 minutes) without getting data from the
399 // filter, then give up to avoid waiting forever for a filter which
400 // has ended up blocked waiting for something which will never happen.
404 FD_SET(fd
, &readfds
);
405 int r
= select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
408 if (errno
== EINTR
|| errno
== EAGAIN
) {
409 // select() interrupted by a signal, so retry.
412 cerr
<< "Reading from filter failed (" << strerror(errno
) << ")"
415 cerr
<< "Filter inactive for too long" << endl
;
418 kill(-child
, SIGKILL
);
420 kill(child
, SIGKILL
);
424 while (waitpid(child
, &status
, 0) < 0 && errno
== EINTR
) { }
425 pid_to_kill_on_signal
= 0;
426 throw ReadError(status
);
430 ssize_t res
= read(fd
, buf
, sizeof(buf
));
433 if (errno
== EINTR
) {
434 // read() interrupted by a signal, so retry.
439 kill(-child
, SIGKILL
);
442 while (waitpid(child
, &status
, 0) < 0 && errno
== EINTR
) { }
443 pid_to_kill_on_signal
= 0;
444 throw ReadError(status
);
446 if (out
) out
->append(buf
, res
);
451 kill(-child
, SIGKILL
);
454 while (waitpid(child
, &status
, 0) < 0) {
456 throw ReadError("wait pid failed");
458 pid_to_kill_on_signal
= 0;
461 FILE * fh
= popen(cmd
.c_str(), "r");
462 if (fh
== NULL
) throw ReadError("popen failed");
465 size_t len
= fread(buf
, 1, 4096, fh
);
468 throw ReadError("fread failed");
470 if (out
) out
->append(buf
, len
);
472 int status
= pclose(fh
);
475 if (WIFEXITED(status
)) {
476 int exit_status
= WEXITSTATUS(status
);
477 if (exit_status
== 0 || exit_status
== alt_status
)
479 if (exit_status
== 127)
480 throw NoSuchFilter();
483 if (WIFSIGNALED(status
) && WTERMSIG(status
) == SIGXCPU
) {
484 cerr
<< "Filter process consumed too much CPU time" << endl
;
487 throw ReadError(status
);