From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp3.mail.ru (smtp3.mail.ru [94.100.179.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4FF9B4696C4 for ; Tue, 14 Apr 2020 14:38:39 +0300 (MSK) From: Alexander Turenko Date: Tue, 14 Apr 2020 14:38:10 +0300 Message-Id: <08c4560c568a09b681424006441bc3536a5cd30b.1586862436.git.alexander.turenko@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 01/12] popen: allow to kill process group List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Cyrill Gorcunov Cc: tarantool-patches@dev.tarantool.org From: Cyrill Gorcunov As Alexander pointed out this might be useful for running a pipe of programs inside shell (i.e. popen.shell('foo | bar | baz', 'r')). Part of #4031 Reported-by: Alexander Turenko Acked-by: Alexander Turenko Signed-off-by: Cyrill Gorcunov --- src/lib/core/popen.c | 28 ++++++++++++++++++++++++---- src/lib/core/popen.h | 6 ++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/lib/core/popen.c b/src/lib/core/popen.c index df7f797b9..a5a305013 100644 --- a/src/lib/core/popen.c +++ b/src/lib/core/popen.c @@ -138,6 +138,19 @@ handle_new(struct popen_opts *opts) assert(opts->argv != NULL && opts->nr_argv > 0); + /* + * Killing group of signals allowed for a new + * session only where it makes sense, otherwise + * child gonna inherit group and we will be killing + * ourself. + */ + if (opts->flags & POPEN_FLAG_GROUP_SIGNAL && + (opts->flags & POPEN_FLAG_SETSID) == 0) { + diag_set(IllegalParams, + "popen: group signal without setting sid"); + return NULL; + } + for (i = 0; i < opts->nr_argv; i++) { if (opts->argv[i] == NULL) continue; @@ -526,6 +539,9 @@ popen_state_str(unsigned int state) int popen_send_signal(struct popen_handle *handle, int signo) { + static const char *killops[] = { "kill", "killpg" }; + const char *killop = handle->flags & POPEN_FLAG_GROUP_SIGNAL ? + killops[1] : killops[0]; int ret; assert(handle != NULL); @@ -535,17 +551,21 @@ popen_send_signal(struct popen_handle *handle, int signo) */ ret = popen_may_pidop(handle); if (ret == 0) { - say_debug("popen: kill %d signo %d", handle->pid, signo); + say_debug("popen: %s %d signo %d", killop, + handle->pid, signo); assert(handle->pid != -1); - ret = kill(handle->pid, signo); + if (handle->flags & POPEN_FLAG_GROUP_SIGNAL) + ret = killpg(handle->pid, signo); + else + ret = kill(handle->pid, signo); } if (ret < 0 && errno == ESRCH) { diag_set(SystemError, "Attempt to send a signal %d to a " "process that does not exist anymore", signo); return -1; } else if (ret < 0) { - diag_set(SystemError, "Unable to kill %d signo %d", - handle->pid, signo); + diag_set(SystemError, "Unable to %s %d signo %d", + killop, handle->pid, signo); return -1; } return 0; diff --git a/src/lib/core/popen.h b/src/lib/core/popen.h index 97c581c13..623d826b9 100644 --- a/src/lib/core/popen.h +++ b/src/lib/core/popen.h @@ -96,6 +96,12 @@ enum popen_flag_bits { */ POPEN_FLAG_RESTORE_SIGNALS_BIT = 15, POPEN_FLAG_RESTORE_SIGNALS = (1 << POPEN_FLAG_RESTORE_SIGNALS_BIT), + + /* + * Send signal to a process group. + */ + POPEN_FLAG_GROUP_SIGNAL_BIT = 16, + POPEN_FLAG_GROUP_SIGNAL = (1 << POPEN_FLAG_GROUP_SIGNAL_BIT), }; /** -- 2.25.0