From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: George Dunlap <george.dunlap@eu.citrix.com>,
Jim Fehlig <jfehlig@suse.com>,
Ian Jackson <Ian.Jackson@eu.citrix.com>,
Ian Campbell <Ian.Campbell@citrix.com>
Subject: [PATCH 06/18] libxl: fork: Provide libxl_childproc_sigchld_occurred
Date: Mon, 3 Feb 2014 16:14:39 +0000 [thread overview]
Message-ID: <1391444091-22796-7-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1391444091-22796-1-git-send-email-ian.jackson@eu.citrix.com>
Applications exist which don't keep track of all their child processes
in a manner suitable for coherent dispatch of their termination. In
such a situation, nothing in the whole process may call wait, or
waitpid(-1,,). Doing so reaps processes belonging to other parts of
the application and there is then no way to deliver the exit status to
the right place.
To facilitate this, provide a facility for such an application to ask
libxl to call waitpid on each of its children individually.
Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Cc: Jim Fehlig <jfehlig@suse.com>
Cc: Ian Campbell <Ian.Campbell@citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
---
tools/libxl/libxl_event.h | 29 +++++++++++++++++++++++++----
tools/libxl/libxl_fork.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 4 deletions(-)
diff --git a/tools/libxl/libxl_event.h b/tools/libxl/libxl_event.h
index 4f72c4b..3c93955 100644
--- a/tools/libxl/libxl_event.h
+++ b/tools/libxl/libxl_event.h
@@ -482,9 +482,10 @@ typedef enum {
* all children, including those not spawned by libxl. */
libxl_sigchld_owner_libxl,
- /* Application promises to call libxl_childproc_exited but NOT
- * from within a signal handler. libxl will not itself arrange to
- * (un)block or catch SIGCHLD. */
+ /* Application promises to discover when SIGCHLD occurs and call
+ * libxl_childproc_exited or libxl_childproc_sigchld_occurred (but
+ * NOT from within a signal handler). libxl will not itself
+ * arrange to (un)block or catch SIGCHLD. */
libxl_sigchld_owner_mainloop,
/* libxl owns SIGCHLD all the time, and the application is
@@ -542,7 +543,8 @@ void libxl_childproc_setmode(libxl_ctx *ctx, const libxl_childproc_hooks *hooks,
/*
* This function is for an application which owns SIGCHLD and which
- * therefore reaps all of the process's children.
+ * reaps all of the process's children, and dispatches the exit status
+ * to the correct place inside the application.
*
* May be called only by an application which has called setmode with
* chldowner == libxl_sigchld_owner_mainloop. If pid was a process started
@@ -558,6 +560,25 @@ void libxl_childproc_setmode(libxl_ctx *ctx, const libxl_childproc_hooks *hooks,
int libxl_childproc_reaped(libxl_ctx *ctx, pid_t, int status)
LIBXL_EXTERNAL_CALLERS_ONLY;
+/*
+ * This function is for an application which owns SIGCHLD but which
+ * doesn't keep track of all of its own children in a manner suitable
+ * for reaping all of them and then dispatching them.
+ *
+ * Such an the application must notify libxl, by calling this
+ * function, that a SIGCHLD occurred. libxl will then check all its
+ * children, reap any that are ready, and take any action necessary -
+ * but it will not reap anything else.
+ *
+ * May be called only by an application which has called setmode with
+ * chldowner == libxl_sigchld_owner_mainloop.
+ *
+ * May NOT be called from within a signal handler which might
+ * interrupt any libxl operation (just like libxl_childproc_reaped).
+ */
+void libxl_childproc_sigchld_occurred(libxl_ctx *ctx)
+ LIBXL_EXTERNAL_CALLERS_ONLY;
+
/*
* An application which initialises a libxl_ctx in a parent process
diff --git a/tools/libxl/libxl_fork.c b/tools/libxl/libxl_fork.c
index 85db2fb..b2325e0 100644
--- a/tools/libxl/libxl_fork.c
+++ b/tools/libxl/libxl_fork.c
@@ -330,6 +330,51 @@ int libxl_childproc_reaped(libxl_ctx *ctx, pid_t pid, int status)
return rc;
}
+static void childproc_checkall(libxl__egc *egc)
+{
+ EGC_GC;
+ libxl__ev_child *ch;
+
+ for (;;) {
+ int status;
+ pid_t got;
+
+ LIBXL_LIST_FOREACH(ch, &CTX->children, entry) {
+ got = checked_waitpid(egc, ch->pid, &status);
+ if (got)
+ goto found;
+ }
+ /* not found */
+ return;
+
+ found:
+ if (got == -1) {
+ LIBXL__EVENT_DISASTER
+ (egc, "waitpid() gave ECHILD but we have a child",
+ ECHILD, 0);
+ /* it must have finished but we don't know its status */
+ status = 255<<8; /* no wait.h macro for this! */
+ assert(WIFEXITED(status));
+ assert(WEXITSTATUS(status)==255);
+ assert(!WIFSIGNALED(status));
+ assert(!WIFSTOPPED(status));
+ }
+ childproc_reaped_ours(egc, ch, status);
+ /* we need to restart the loop, as children may have been edited */
+ }
+}
+
+void libxl_childproc_sigchld_occurred(libxl_ctx *ctx)
+{
+ EGC_INIT(ctx);
+ CTX_LOCK;
+ assert(CTX->childproc_hooks->chldowner
+ == libxl_sigchld_owner_mainloop);
+ childproc_checkall(egc);
+ CTX_UNLOCK;
+ EGC_FREE;
+}
+
static void sigchld_selfpipe_handler(libxl__egc *egc, libxl__ev_fd *ev,
int fd, short events, short revents)
{
--
1.7.10.4
next prev parent reply other threads:[~2014-02-03 16:14 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-03 16:14 [PATCH 00/18 v3] libxl: fork and event fixes for libvirt and 4.4 Ian Jackson
2014-02-03 16:14 ` [PATCH 01/18] libxl: fork: Break out checked_waitpid Ian Jackson
2014-02-03 16:14 ` [PATCH 02/18] libxl: fork: Break out childproc_reaped_ours Ian Jackson
2014-02-03 16:14 ` [PATCH 03/18] libxl: fork: Clarify docs for libxl_sigchld_owner Ian Jackson
2014-02-03 16:14 ` [PATCH 04/18] libxl: fork: Document libxl_sigchld_owner_libxl better Ian Jackson
2014-02-03 16:14 ` [PATCH 05/18] libxl: fork: assert that chldmode is right Ian Jackson
2014-02-03 16:14 ` Ian Jackson [this message]
2014-02-03 16:14 ` [PATCH 07/18] libxl: fork: Provide ..._always_selective_reap Ian Jackson
2014-02-03 16:14 ` [PATCH 08/18] libxl: fork: Provide LIBXL_HAVE_SIGCHLD_SELECTIVE_REAP Ian Jackson
2014-02-03 16:14 ` [PATCH 09/18] libxl: fork: Rename sigchld handler functions Ian Jackson
2014-02-03 16:14 ` [PATCH 10/18] libxl: fork: Break out sigchld_installhandler_core Ian Jackson
2014-02-03 16:14 ` [PATCH 11/18] libxl: fork: Break out sigchld_sethandler_raw Ian Jackson
2014-02-06 13:53 ` Ian Campbell
2014-02-03 16:14 ` [PATCH 12/18] libxl: fork: Share SIGCHLD handler amongst ctxs Ian Jackson
2014-02-03 16:14 ` [PATCH 13/18] libxl: events: Break out libxl__pipe_nonblock, _close Ian Jackson
2014-02-03 16:14 ` [PATCH 14/18] libxl: fork: Make SIGCHLD self-pipe nonblocking Ian Jackson
2014-02-03 16:14 ` [PATCH 15/18] libxl: events: Makefile builds internal unit tests Ian Jackson
2014-02-06 14:00 ` Ian Campbell
2014-02-03 16:14 ` [PATCH 16/18] libxl: events: timedereg internal unit test Ian Jackson
2014-02-06 14:01 ` Ian Campbell
2014-02-03 16:14 ` [PATCH 17/18] libxl: timeouts: Break out time_occurs Ian Jackson
2014-02-06 14:02 ` Ian Campbell
2014-02-03 16:14 ` [PATCH 18/18] libxl: timeouts: Record deregistration when one occurs Ian Jackson
2014-02-06 14:04 ` Ian Campbell
2014-02-06 14:24 ` Ian Jackson
2014-02-06 14:27 ` Ian Campbell
2014-02-03 16:16 ` [PATCH 00/18 v3] libxl: fork and event fixes for libvirt and 4.4 Ian Jackson
2014-02-05 5:46 ` Jim Fehlig
2014-02-05 11:21 ` Ian Jackson
2014-02-05 14:10 ` George Dunlap
2014-02-05 15:03 ` Ian Jackson
2014-02-06 10:52 ` George Dunlap
2014-02-06 12:35 ` Ian Jackson
2014-02-06 14:07 ` Ian Campbell
2014-02-06 14:33 ` Ian Jackson
2014-02-07 4:17 ` Jim Fehlig
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=1391444091-22796-7-git-send-email-ian.jackson@eu.citrix.com \
--to=ian.jackson@eu.citrix.com \
--cc=Ian.Campbell@citrix.com \
--cc=george.dunlap@eu.citrix.com \
--cc=jfehlig@suse.com \
--cc=xen-devel@lists.xensource.com \
/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).