filtering: terminate also the sender process when the user hits ^C

When the user interrupts an external command that hangs or takes too
long, nano should also kill the data-sending process (when present).

This fixes https://savannah.gnu.org/bugs/?63177.

Bug existed in this form since version 4.3, commit d946f38a,
but basically existed since version 3.0, commit ec339d3b.
This commit is contained in:
Benno Schulenberg 2022-10-07 16:04:06 +02:00
parent 19c8cea8e5
commit 858f411447

View File

@ -969,12 +969,19 @@ char *get_next_filename(const char *name, const char *suffix)
#ifndef NANO_TINY #ifndef NANO_TINY
static pid_t pid_of_command = -1; static pid_t pid_of_command = -1;
/* The PID of a forked process -- needed when wanting to abort it. */ /* The PID of a forked process -- needed when wanting to abort it. */
static pid_t pid_of_sender = -1;
/* The PID of the process that pipes data to the above process. */
static bool should_pipe = FALSE;
/* Whether we are piping data to the external command. */
/* Send an unconditional kill signal to the running external command. */ /* Send an unconditional kill signal to the running external command. */
void cancel_the_command(int signal) void cancel_the_command(int signal)
{ {
#ifdef SIGKILL #ifdef SIGKILL
kill(pid_of_command, SIGKILL); if (pid_of_command > 0)
kill(pid_of_command, SIGKILL);
if (should_pipe && pid_of_sender > 0)
kill(pid_of_sender, SIGKILL);
#endif #endif
} }
@ -1011,11 +1018,11 @@ void execute_command(const char *command)
struct sigaction oldaction, newaction = {{0}}; struct sigaction oldaction, newaction = {{0}};
/* Original and temporary handlers for SIGINT. */ /* Original and temporary handlers for SIGINT. */
ssize_t was_lineno = (openfile->mark ? 0 : openfile->current->lineno); ssize_t was_lineno = (openfile->mark ? 0 : openfile->current->lineno);
const bool should_pipe = (command[0] == '|');
int command_status, sender_status; int command_status, sender_status;
pid_t pid_of_sender;
FILE *stream; FILE *stream;
should_pipe = (command[0] == '|');
/* Create a pipe to read the command's output from, and, if needed, /* Create a pipe to read the command's output from, and, if needed,
* a pipe to feed the command's input through. */ * a pipe to feed the command's input through. */
if (pipe(from_fd) == -1 || (should_pipe && pipe(to_fd) == -1)) { if (pipe(from_fd) == -1 || (should_pipe && pipe(to_fd) == -1)) {