xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xen.org
Cc: Jim Fehlig <jfehlig@suse.com>,
	Ian Jackson <Ian.Jackson@eu.citrix.com>,
	Ian Campbell <ian.campbell@citrix.com>,
	Bamvor Jian Zhang <bjzhang@suse.com>
Subject: [PATCH 2/3] libxl: event system: Make libxl_sigchld_owner_libxl_always work
Date: Tue, 5 Nov 2013 19:19:12 +0000	[thread overview]
Message-ID: <1383679153-3137-3-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1383679153-3137-1-git-send-email-ian.jackson@eu.citrix.com>

Previously, libxl_sigchld_owner_libxl_always was mishandled.  It would
result in libxl paying no attention to the sigchld self pipe.

Fix this by fixing chldmode_ours so that it returns true iff we are
supposed to be handling SIGCHLD.

Additionally, we arrange to use chldmode_ours everywhere where we are
installing/removing signal handlers and/or deciding whether to check
the self pipe, etc.  This means it needs a new "creating" flag
argument for the benefit of libxl__ev_child_fork, which needs to
install the signal handler in libxl_sigchld_owner_libxl even if there
are not currently any children.

ctx->childproc_hooks->chldowner is now interpreted only by
chldmode_ours.

Reported-by: Bamvor Jian Zhang <bjzhang@suse.com>
Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Cc: Bamvor Jian Zhang <bjzhang@suse.com>
Cc: Ian Campbell <ian.campbell@citrix.com>
Cc: Jim Fehlig <jfehlig@suse.com>
---
 tools/libxl/libxl_fork.c |   51 ++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/tools/libxl/libxl_fork.c b/tools/libxl/libxl_fork.c
index f392d67..335ca40 100644
--- a/tools/libxl/libxl_fork.c
+++ b/tools/libxl/libxl_fork.c
@@ -222,18 +222,23 @@ int libxl__sigchld_installhandler(libxl__gc *gc) /* non-reentrant */
     return rc;
 }
 
-static int chldmode_ours(libxl_ctx *ctx)
+static bool chldmode_ours(libxl_ctx *ctx, bool creating)
 {
-    return ctx->childproc_hooks->chldowner == libxl_sigchld_owner_libxl;
+    switch (ctx->childproc_hooks->chldowner) {
+    case libxl_sigchld_owner_libxl:
+        return creating || !LIBXL_LIST_EMPTY(&ctx->children);
+    case libxl_sigchld_owner_mainloop:
+        return 0;
+    case libxl_sigchld_owner_libxl_always:
+        return 1;
+    }
+    abort();
 }
 
 int libxl__fork_selfpipe_active(libxl_ctx *ctx)
 {
     /* Returns the fd to read, or -1 */
-    if (!chldmode_ours(ctx))
-        return -1;
-
-    if (LIBXL_LIST_EMPTY(&ctx->children))
+    if (!chldmode_ours(ctx, 0))
         return -1;
 
     return ctx->sigchld_selfpipe[0];
@@ -241,11 +246,21 @@ int libxl__fork_selfpipe_active(libxl_ctx *ctx)
 
 static void perhaps_removehandler(libxl__gc *gc)
 {
-    if (LIBXL_LIST_EMPTY(&CTX->children) &&
-        CTX->childproc_hooks->chldowner != libxl_sigchld_owner_libxl_always)
+    if (chldmode_ours(CTX, 0))
         libxl__sigchld_removehandler(gc);
 }
 
+static int perhaps_installhandler(libxl__gc *gc, bool creating)
+{
+    int rc;
+
+    if (chldmode_ours(CTX, creating)) {
+        rc = libxl__sigchld_installhandler(gc);
+        if (rc) return rc;
+    }
+    return 0;
+}
+
 static int childproc_reaped(libxl__egc *egc, pid_t pid, int status)
 {
     EGC_GC;
@@ -284,7 +299,7 @@ void libxl__fork_selfpipe_woken(libxl__egc *egc)
      * ctx must be locked EXACTLY ONCE */
     EGC_GC;
 
-    while (chldmode_ours(CTX) /* in case the app changes the mode */) {
+    while (chldmode_ours(CTX, 0) /* in case the app changes the mode */) {
         int status;
         pid_t pid = waitpid(-1, &status, WNOHANG);
 
@@ -330,10 +345,7 @@ pid_t libxl__ev_child_fork(libxl__gc *gc, libxl__ev_child *ch,
     CTX_LOCK;
     int rc;
 
-    if (chldmode_ours(CTX)) {
-        rc = libxl__sigchld_installhandler(gc);
-        if (rc) goto out;
-    }
+    perhaps_installhandler(gc, 1);
 
     pid_t pid =
         CTX->childproc_hooks->fork_replacement
@@ -380,17 +392,8 @@ void libxl_childproc_setmode(libxl_ctx *ctx, const libxl_childproc_hooks *hooks,
     ctx->childproc_hooks = hooks;
     ctx->childproc_user = user;
 
-    switch (ctx->childproc_hooks->chldowner) {
-    case libxl_sigchld_owner_mainloop:
-    case libxl_sigchld_owner_libxl:
-        libxl__sigchld_removehandler(gc);
-        break;
-    case libxl_sigchld_owner_libxl_always:
-        libxl__sigchld_installhandler(gc);
-        break;
-    default:
-        abort();
-    }
+    perhaps_removehandler(gc);
+    perhaps_installhandler(gc, 0); /* idempotent, ok to ignore errors for now */
 
     CTX_UNLOCK;
     GC_FREE;
-- 
1.7.10.4

  parent reply	other threads:[~2013-11-05 19:19 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-05  9:22 [PATCH 0/2] question on libxl ao Bamvor Jian Zhang
2013-11-05  9:22 ` [PATCH 1/2] expose child fd in order to handle child exit in libvirt Bamvor Jian Zhang
2013-11-05 16:05   ` [PATCH 0/2] question on libxl ao [and 1 more messages] Ian Jackson
2013-11-05 19:19     ` [PATCH 0/3] libxl: event system: SIGCHLD related fixes Ian Jackson
2013-11-05 19:19       ` [PATCH 1/3] libxl: event system: pass gc, not just ctx, to internal sigchld manipulators Ian Jackson
2013-11-06 10:30         ` Ian Campbell
2013-11-05 19:19       ` Ian Jackson [this message]
2013-11-06 10:34         ` [PATCH 2/3] libxl: event system: Make libxl_sigchld_owner_libxl_always work Ian Campbell
2013-11-05 19:19       ` [PATCH 3/3] libxl: event system: properly register the SIGCHLD self-pipe Ian Jackson
2013-11-06 10:36         ` Ian Campbell
2013-11-06 10:38       ` [PATCH 0/3] libxl: event system: SIGCHLD related fixes Ian Campbell
2013-11-06 12:08         ` Ian Jackson
2013-11-12 18:27           ` [PATCH v2 " Ian Jackson
2013-11-12 18:27             ` [PATCH 1/3] libxl: event system: pass gc, not just ctx, to internal sigchld manipulators Ian Jackson
2013-11-12 18:27             ` [PATCH 2/3] libxl: event system: Make libxl_sigchld_owner_libxl_always work Ian Jackson
2013-11-12 18:27             ` [PATCH 3/3] libxl: event system: properly register the SIGCHLD self-pipe Ian Jackson
2013-11-13 10:02             ` [PATCH v2 0/3] libxl: event system: SIGCHLD related fixes Ian Campbell
2013-11-21 18:49               ` Ian Jackson
2013-11-20  4:28             ` Jim Fehlig
2013-11-20 15:22               ` 答复: " Bamvor Jian Zhang
2013-11-20 16:40                 ` 答复: Re: [PAT CH " Ian Jackson
2013-11-07  3:00     ` [PATCH 0/2] question on libxl ao [and 1 more messages] Bamvor Jian Zhang
2013-11-05 19:23   ` [PATCH 0/2] question on libxl ao [and 1 more messages] " Ian Jackson
2013-11-05  9:22 ` [PATCH 2/2] call ao_how callback explicitly Bamvor Jian Zhang
2013-11-05 15:51   ` Ian Jackson
2013-11-07 14:51     ` Bamvor Jian Zhang
2013-11-07 17:03       ` Ian Jackson
2013-11-08 13:17         ` Bamvor Jian Zhang
2013-11-08 16:07           ` Ian Jackson

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=1383679153-3137-3-git-send-email-ian.jackson@eu.citrix.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=bjzhang@suse.com \
    --cc=ian.campbell@citrix.com \
    --cc=jfehlig@suse.com \
    --cc=xen-devel@lists.xen.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).