Tarantool development patches archive
 help / color / mirror / Atom feed
From: Alexander Turenko <alexander.turenko@tarantool.org>
To: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH 03/13] popen: add missed diag_set in popen_signal/delete
Date: Fri, 10 Apr 2020 05:50:41 +0300	[thread overview]
Message-ID: <f656bfd5cb6938ed226271967e412148a9afc739.1586486220.git.alexander.turenko@tarantool.org> (raw)
In-Reply-To: <cover.1586486219.git.alexander.turenko@tarantool.org>

Lua API will use content of the diagnostics area to report an error to a
caller, so it is critical to always have proper diagnostics at failure.

Part of #4031
---
 src/lib/core/popen.c | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/lib/core/popen.c b/src/lib/core/popen.c
index 3a0ac4b53..3fcbc325a 100644
--- a/src/lib/core/popen.c
+++ b/src/lib/core/popen.c
@@ -164,16 +164,19 @@ popen_may_io(struct popen_handle *handle, unsigned int idx,
 
 /**
  * Test if the handle still have a living child process.
+ *
+ * Return -1 and set errno to ESRCH when a process does not
+ * exist. Otherwise return 0.
  */
-static inline bool
+static inline int
 popen_may_pidop(struct popen_handle *handle)
 {
 	assert(handle != NULL);
 	if (handle->pid == -1) {
 		errno = ESRCH;
-		return false;
+		return -1;
 	}
-	return true;
+	return 0;
 }
 
 /**
@@ -423,6 +426,10 @@ popen_state_str(unsigned int state)
 
 /**
  * Send a signal to a child process.
+ *
+ * Return 0 at success or -1 at failure (and set a diag).
+ *
+ * Set errno to ESRCH when a process does not exist.
  */
 int
 popen_send_signal(struct popen_handle *handle, int signo)
@@ -434,16 +441,22 @@ popen_send_signal(struct popen_handle *handle, int signo)
 	/*
 	 * A child may be killed or exited already.
 	 */
-	if (!popen_may_pidop(handle))
+	ret = popen_may_pidop(handle);
+	if (ret == 0) {
+		say_debug("popen: kill %d signo %d", handle->pid, signo);
+		assert(handle->pid != -1);
+		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;
-
-	say_debug("popen: kill %d signo %d", handle->pid, signo);
-	ret = kill(handle->pid, signo);
-	if (ret < 0) {
+	} else if (ret < 0) {
 		diag_set(SystemError, "Unable to kill %d signo %d",
 			 handle->pid, signo);
+		return -1;
 	}
-	return ret;
+	return 0;
 }
 
 /**
@@ -460,7 +473,11 @@ popen_delete(struct popen_handle *handle)
 
 	assert(handle != NULL);
 
-	if (popen_send_signal(handle, SIGKILL) && errno != ESRCH)
+	/*
+	 * Unable to kill the process -- give an error.
+	 * The process is not exist already -- pass over.
+	 */
+	if (popen_send_signal(handle, SIGKILL) != 0 && errno != ESRCH)
 		return -1;
 
 	for (i = 0; i < lengthof(handle->ios); i++) {
-- 
2.25.0

  parent reply	other threads:[~2020-04-10  2:51 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-10  2:50 [Tarantool-patches] [PATCH 00/13] Popen Lua API: preliminary patches Alexander Turenko
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 01/13] popen: require popen handle to be non-NULL Alexander Turenko
2020-04-10  7:16   ` Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 02/13] popen: remove retval from popen_state() Alexander Turenko
2020-04-10  7:17   ` Cyrill Gorcunov
2020-04-10  2:50 ` Alexander Turenko [this message]
2020-04-10  7:23   ` [Tarantool-patches] [PATCH 03/13] popen: add missed diag_set in popen_signal/delete Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 04/13] popen: add logging of fds closed in a child Alexander Turenko
2020-04-10  7:46   ` Cyrill Gorcunov
2020-04-10 12:19     ` Alexander Turenko
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 05/13] say: allow to set a logger file descriptor Alexander Turenko
2020-04-10  8:33   ` Cyrill Gorcunov
2020-04-10 12:19     ` Alexander Turenko
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 06/13] popen: decouple logger fd from stderr Alexander Turenko
2020-04-10  9:18   ` Cyrill Gorcunov
2020-04-10 12:20     ` Alexander Turenko
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 07/13] popen: add const qualifier to popen_write_timeout Alexander Turenko
2020-04-10  8:04   ` Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 08/13] popen: unblock popen_read_timeout at a first byte Alexander Turenko
2020-04-10  8:10   ` Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 09/13] popen: remove redundant fd check before perform IO Alexander Turenko
2020-04-10  8:18   ` Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 10/13] popen: add missed diag_set() in popen IO functions Alexander Turenko
2020-04-10  8:28   ` Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 11/13] coio: fix obsoleted comment in coio_write_timeout Alexander Turenko
2020-04-10  8:28   ` Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 12/13] coio: add *_noxc read / write functions Alexander Turenko
2020-04-10  8:05   ` Konstantin Osipov
2020-04-10  8:17     ` Cyrill Gorcunov
2020-04-10 11:57     ` Alexander Turenko
2020-04-12 12:51     ` Alexander Turenko
2020-04-10  8:29   ` Cyrill Gorcunov
2020-04-10  2:50 ` [Tarantool-patches] [PATCH 13/13] popen: use of exception safe functions for IO Alexander Turenko
2020-04-10 11:50   ` Cyrill Gorcunov
2020-04-10 12:21     ` Alexander Turenko
2020-04-10 16:36 ` [Tarantool-patches] [PATCH 00/13] Popen Lua API: preliminary patches Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f656bfd5cb6938ed226271967e412148a9afc739.1586486220.git.alexander.turenko@tarantool.org \
    --to=alexander.turenko@tarantool.org \
    --cc=gorcunov@gmail.com \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 03/13] popen: add missed diag_set in popen_signal/delete' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox