Mailing List Archive

[xen-unstable] libxl: Expose functions for helping with subprocesses.
# HG changeset patch
# User Keir Fraser <keir.fraser@citrix.com>
# Date 1271090465 -3600
# Node ID 86e82ab8d4de0f48248f5028c246d72b03526245
# Parent 5b8362505256165048210184d8183cf44035ad82
libxl: Expose functions for helping with subprocesses.

* Expose libxl_fork in libxl_utils.h
* Expose libxl_pipe in libxl_utils.h
* Make libxl_exec put SIGPIPE back (so that libxl callers may
have SIGPIPE ignored)

xl would like to use libxl_fork (which is like fork(2) except that it
logs errors) and also a similar function libxl_pipe. So put these in
libxl_utils.[ch] and use them in libxl.c as appropriate, to avoid
having to duplicate code between xl and libxl.

Also, make sure that subprocesses spawned by libxl have SIGPIPE set
back to SIG_DFL as they are entitled to expect. This means that a
libxl caller which sets SIGPIPE to SIG_IGN is no longer buggy. (This
is relevant for xl migration, because xl would like to be such a
caller.)

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
---
tools/libxl/libxl.c | 12 +++---------
tools/libxl/libxl_exec.c | 18 +++++-------------
tools/libxl/libxl_utils.c | 23 +++++++++++++++++++++++
tools/libxl/libxl_utils.h | 4 ++++
4 files changed, 35 insertions(+), 22 deletions(-)

diff -r 5b8362505256 -r 86e82ab8d4de tools/libxl/libxl.c
--- a/tools/libxl/libxl.c Mon Apr 12 17:40:34 2010 +0100
+++ b/tools/libxl/libxl.c Mon Apr 12 17:41:05 2010 +0100
@@ -1238,15 +1238,9 @@ int libxl_device_disk_add(struct libxl_c
char buf[1024], *dev;
dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
if (dev == NULL) {
- if (pipe(p) < 0) {
- XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
- return -1;
- }
- rc = fork();
- if (rc < 0) {
- XL_LOG(ctx, XL_LOG_ERROR, "Failed to fork a new process");
- return -1;
- } else if (!rc) { /* child */
+ rc= libxl_pipe(ctx, p); if (rc==-1) return -1;
+ rc= libxl_fork(ctx); if (rc==-1) return -1;
+ if (!rc) { /* child */
int null_r, null_w;
char *args[4];
args[0] = "tapdisk2";
diff -r 5b8362505256 -r 86e82ab8d4de tools/libxl/libxl_exec.c
--- a/tools/libxl/libxl_exec.c Mon Apr 12 17:40:34 2010 +0100
+++ b/tools/libxl/libxl_exec.c Mon Apr 12 17:41:05 2010 +0100
@@ -30,19 +30,6 @@
#include "libxl.h"
#include "libxl_internal.h"

-static pid_t libxl_fork(struct libxl_ctx *ctx)
-{
- pid_t pid;
-
- pid = fork();
- if (pid == -1) {
- XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "fork failed");
- return -1;
- }
-
- return pid;
-}
-
static int call_waitpid(pid_t (*waitpid_cb)(pid_t, int *, int), pid_t pid, int *status, int options)
{
return (waitpid_cb) ? waitpid_cb(pid, status, options) : waitpid(pid, status, options);
@@ -61,6 +48,11 @@ void libxl_exec(int stdinfd, int stdoutf
dup2(stderrfd, STDERR_FILENO);
for (i = 4; i < 256; i++)
close(i);
+
+ signal(SIGPIPE, SIG_DFL);
+ /* in case our caller set it to IGN. subprocesses are entitled
+ * to assume they got DFL. */
+
execv(arg0, args);
_exit(-1);
}
diff -r 5b8362505256 -r 86e82ab8d4de tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c Mon Apr 12 17:40:34 2010 +0100
+++ b/tools/libxl/libxl_utils.c Mon Apr 12 17:41:05 2010 +0100
@@ -280,3 +280,26 @@ int libxl_read_file_contents(struct libx

READ_WRITE_EXACTLY(read, 1, /* */)
READ_WRITE_EXACTLY(write, 0, const)
+
+
+pid_t libxl_fork(struct libxl_ctx *ctx)
+{
+ pid_t pid;
+
+ pid = fork();
+ if (pid == -1) {
+ XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "fork failed");
+ return -1;
+ }
+
+ return pid;
+}
+
+int libxl_pipe(struct libxl_ctx *ctx, int pipes[2])
+{
+ if (pipe(pipes) < 0) {
+ XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
+ return -1;
+ }
+ return 0;
+}
diff -r 5b8362505256 -r 86e82ab8d4de tools/libxl/libxl_utils.h
--- a/tools/libxl/libxl_utils.h Mon Apr 12 17:40:34 2010 +0100
+++ b/tools/libxl/libxl_utils.h Mon Apr 12 17:41:05 2010 +0100
@@ -44,5 +44,9 @@ int libxl_write_exactly(struct libxl_ctx
* logged using filename (which is only used for logging) and what
* (which may be 0). */

+pid_t libxl_fork(struct libxl_ctx *ctx);
+int libxl_pipe(struct libxl_ctx *ctx, int pipes[2]);
+ /* Just like fork(2), pipe(2), but log errors. */
+
#endif


_______________________________________________
Xen-changelog mailing list
Xen-changelog@lists.xensource.com
http://lists.xensource.com/xen-changelog