debuggers.hg

changeset 21185:86e82ab8d4de

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>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 12 17:41:05 2010 +0100 (2010-04-12)
parents 5b8362505256
children d005aa895b5a
files tools/libxl/libxl.c tools/libxl/libxl_exec.c tools/libxl/libxl_utils.c tools/libxl/libxl_utils.h
line diff
     1.1 --- a/tools/libxl/libxl.c	Mon Apr 12 17:40:34 2010 +0100
     1.2 +++ b/tools/libxl/libxl.c	Mon Apr 12 17:41:05 2010 +0100
     1.3 @@ -1238,15 +1238,9 @@ int libxl_device_disk_add(struct libxl_c
     1.4                  char buf[1024], *dev;
     1.5                  dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
     1.6                  if (dev == NULL) {
     1.7 -                    if (pipe(p) < 0) {
     1.8 -                        XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
     1.9 -                        return -1;
    1.10 -                    }
    1.11 -                    rc = fork();
    1.12 -                    if (rc < 0) {
    1.13 -                        XL_LOG(ctx, XL_LOG_ERROR, "Failed to fork a new process");
    1.14 -                        return -1;
    1.15 -                    } else if (!rc) { /* child */
    1.16 +                    rc= libxl_pipe(ctx, p);  if (rc==-1) return -1;
    1.17 +                    rc= libxl_fork(ctx);  if (rc==-1) return -1;
    1.18 +                    if (!rc) { /* child */
    1.19                          int null_r, null_w;
    1.20                          char *args[4];
    1.21                          args[0] = "tapdisk2";
     2.1 --- a/tools/libxl/libxl_exec.c	Mon Apr 12 17:40:34 2010 +0100
     2.2 +++ b/tools/libxl/libxl_exec.c	Mon Apr 12 17:41:05 2010 +0100
     2.3 @@ -30,19 +30,6 @@
     2.4  #include "libxl.h"
     2.5  #include "libxl_internal.h"
     2.6  
     2.7 -static pid_t libxl_fork(struct libxl_ctx *ctx)
     2.8 -{
     2.9 -    pid_t pid;
    2.10 -
    2.11 -    pid = fork();
    2.12 -    if (pid == -1) {
    2.13 -        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "fork failed");
    2.14 -        return -1;
    2.15 -    }
    2.16 -
    2.17 -    return pid;
    2.18 -}
    2.19 -
    2.20  static int call_waitpid(pid_t (*waitpid_cb)(pid_t, int *, int), pid_t pid, int *status, int options)
    2.21  {
    2.22      return (waitpid_cb) ? waitpid_cb(pid, status, options) : waitpid(pid, status, options);
    2.23 @@ -61,6 +48,11 @@ void libxl_exec(int stdinfd, int stdoutf
    2.24          dup2(stderrfd, STDERR_FILENO);
    2.25      for (i = 4; i < 256; i++)
    2.26          close(i);
    2.27 +
    2.28 +    signal(SIGPIPE, SIG_DFL);
    2.29 +    /* in case our caller set it to IGN.  subprocesses are entitled
    2.30 +     * to assume they got DFL. */
    2.31 +
    2.32      execv(arg0, args);
    2.33      _exit(-1);
    2.34  }
     3.1 --- a/tools/libxl/libxl_utils.c	Mon Apr 12 17:40:34 2010 +0100
     3.2 +++ b/tools/libxl/libxl_utils.c	Mon Apr 12 17:41:05 2010 +0100
     3.3 @@ -280,3 +280,26 @@ int libxl_read_file_contents(struct libx
     3.4  
     3.5  READ_WRITE_EXACTLY(read, 1, /* */)
     3.6  READ_WRITE_EXACTLY(write, 0, const)
     3.7 +
     3.8 +
     3.9 +pid_t libxl_fork(struct libxl_ctx *ctx)
    3.10 +{
    3.11 +    pid_t pid;
    3.12 +
    3.13 +    pid = fork();
    3.14 +    if (pid == -1) {
    3.15 +        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "fork failed");
    3.16 +        return -1;
    3.17 +    }
    3.18 +
    3.19 +    return pid;
    3.20 +}
    3.21 +
    3.22 +int libxl_pipe(struct libxl_ctx *ctx, int pipes[2])
    3.23 +{
    3.24 +    if (pipe(pipes) < 0) {
    3.25 +        XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
    3.26 +        return -1;
    3.27 +    }
    3.28 +    return 0;
    3.29 +}
     4.1 --- a/tools/libxl/libxl_utils.h	Mon Apr 12 17:40:34 2010 +0100
     4.2 +++ b/tools/libxl/libxl_utils.h	Mon Apr 12 17:41:05 2010 +0100
     4.3 @@ -44,5 +44,9 @@ int libxl_write_exactly(struct libxl_ctx
     4.4     * logged using filename (which is only used for logging) and what
     4.5     * (which may be 0). */
     4.6      
     4.7 +pid_t libxl_fork(struct libxl_ctx *ctx);
     4.8 +int libxl_pipe(struct libxl_ctx *ctx, int pipes[2]);
     4.9 +  /* Just like fork(2), pipe(2), but log errors. */
    4.10 +
    4.11  #endif
    4.12