From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-f66.google.com (mail-lf1-f66.google.com [209.85.167.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 9FB1B4696C3 for ; Fri, 10 Apr 2020 17:40:37 +0300 (MSK) Received: by mail-lf1-f66.google.com with SMTP id u10so347735lfo.8 for ; Fri, 10 Apr 2020 07:40:37 -0700 (PDT) From: Cyrill Gorcunov Date: Fri, 10 Apr 2020 17:40:20 +0300 Message-Id: <20200410144021.5704-2-gorcunov@gmail.com> In-Reply-To: <20200410144021.5704-1-gorcunov@gmail.com> References: <20200410144021.5704-1-gorcunov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 1/2] popen: Allow to kill process group List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tml As Alexander pointed out this might be useful for running a pipe of programs inside shell (i.e. popen.shell('foo | bar | baz', 'r')). Reported-by: Alexander Turenko Signed-off-by: Cyrill Gorcunov --- src/lib/core/popen.c | 26 +++++++++++++++++++++++--- src/lib/core/popen.h | 6 ++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/lib/core/popen.c b/src/lib/core/popen.c index 6b6062215..200b31b21 100644 --- a/src/lib/core/popen.c +++ b/src/lib/core/popen.c @@ -86,6 +86,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; @@ -443,10 +456,17 @@ popen_send_signal(struct popen_handle *handle, int signo) if (!popen_may_pidop(handle)) return -1; - say_debug("popen: kill %d signo %d", handle->pid, signo); - ret = kill(handle->pid, signo); + say_debug("popen: %s %d signo %d", + handle->flags & POPEN_FLAG_GROUP_SIGNAL ? + "killpg" : "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) { - diag_set(SystemError, "Unable to kill %d signo %d", + diag_set(SystemError, "Unable to %s %d signo %d", + handle->flags & POPEN_FLAG_GROUP_SIGNAL ? + "killpg" : "kill", handle->pid, signo); } return ret; diff --git a/src/lib/core/popen.h b/src/lib/core/popen.h index 9cbfed60c..f9dd0ff45 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.20.1