Skip to contentSkip to conversation

tools/libxl: Initialise both parts of ctx->sigchld_selfpipe[] to -1

PATCH In review Andrew Cooper 3 participants 6 messages Aug 18, 2014 – Aug 27, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libxl: Initialise both parts of ctx->sigchld_selfpipe[] to -1

Otherwise, if it is not used, libxl_ctx_free() will close fd 0.

---
 tools/libxl/libxl.c |    1 +
 1 file changed, 1 insertion(+)
tools/libxl/libxl.c +1 -0
index 3526539..a1e0b5e 100644
@@ -71,6 +71,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     ctx->childproc_user = 0;
         
     ctx->sigchld_selfpipe[0] = -1;
+    ctx->sigchld_selfpipe[1] = -1;
     libxl__ev_fd_init(&ctx->sigchld_selfpipe_efd);
 
     /* The mutex is special because we can't idempotently destroy it */
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 1 line · Andrew Cooper

Otherwise, if it is not used, libxl_ctx_free() will close fd 0.

Is there some way to work around this on the caller side without a library change? Perhaps something that forces sigchld_selfpipe to be opened?

Alex

Quoted 21 lines
---
 tools/libxl/libxl.c |    1 +
 1 file changed, 1 insertion(+)
tools/libxl/libxl.c +1 -0
index 3526539..a1e0b5e 100644
@@ -71,6 +71,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     ctx->childproc_user = 0;
 
     ctx->sigchld_selfpipe[0] = -1;
+    ctx->sigchld_selfpipe[1] = -1;
     libxl__ev_fd_init(&ctx->sigchld_selfpipe_efd);
 
     /* The mutex is special because we can't idempotently destroy it */
Signature · 1 line
Alex Bligh
#2 lore raw
Quoted 7 lines · Alex Bligh
Quoted 1 line · Andrew Cooper

Otherwise, if it is not used, libxl_ctx_free() will close fd 0.

Is there some way to work around this on the caller side without a library change? Perhaps something that forces sigchld_selfpipe to be opened?

Alex

It looks like a call to libxl_childproc_setmode() will properly set up both halves of ctx->sigchld_selfpipe[]

The call is to do with setting up the ownership of SIGCHILD in the process, and a call to libxl_childproc_setmode(ctx, NULL, 0) should be fine if you dont plan to fork in your own code.

~Andrew

Quoted 24 lines
Quoted 23 lines
---
 tools/libxl/libxl.c |    1 +
 1 file changed, 1 insertion(+)
tools/libxl/libxl.c +1 -0
index 3526539..a1e0b5e 100644
@@ -71,6 +71,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     ctx->childproc_user = 0;
 
     ctx->sigchld_selfpipe[0] = -1;
+    ctx->sigchld_selfpipe[1] = -1;
     libxl__ev_fd_init(&ctx->sigchld_selfpipe_efd);
 
     /* The mutex is special because we can't idempotently destroy it */
#3 lore raw
Quoted 6 lines · Andrew Cooper

It looks like a call to libxl_childproc_setmode() will properly set up both halves of ctx->sigchld_selfpipe[]

The call is to do with setting up the ownership of SIGCHILD in the process, and a call to libxl_childproc_setmode(ctx, NULL, 0) should be fine if you dont plan to fork in your own code.

That does not appear to make any difference.

As far as I can see I need to ensure libxl__sigchld_needed gets called, which means I need to ensure perhaps_sigchld_needed gets called and chldmode_ours returns true. As 'creating' is false, in libxl_sigchld_owner_libxl mode, I appear to need a child for this to work. I can set it to libxl_sigchld_owner_libxl_always or libxl_sigchld_owner_libxl_always_selective_reap and then set it back. The code below is the minimum I can find that gets it to work:

#include <stdio.h>
#include <stdlib.h>
#include <libxl.h>

#define LIBXL_API_VERSION 0x040200

/* compile with:
* gcc -g -O0 xentest.c -o xentest -lxenlight
*/

void
fixctx (libxl_ctx *ctx)
{
const libxl_childproc_hooks libxl_child_hooks = {
.chldowner = libxl_sigchld_owner_libxl_always
};
libxl_childproc_setmode (ctx, &libxl_child_hooks, 0); libxl_childproc_setmode (ctx, NULL, 0);
}

void
test ()
{
libxl_ctx *ctx = NULL;
if (libxl_ctx_alloc (&ctx, LIBXL_VERSION, XTL_NONE, NULL))

    {
      fprintf (stderr, "libxl_ctx_alloc failed");
      exit (1);
    }

fixctx (ctx);
libxl_ctx_free (ctx);
}

int
main (int argc, char **argv)
{
printf ("Before:\n");
system ("ls -la /proc/self/fd");
test ();
printf ("\nAfter:\n");
system ("ls -la /proc/self/fd");
exit (0);
}

Signature · 1 line
Alex Bligh
#4 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

Otherwise, if it is not used, libxl_ctx_free() will close fd 0.

---

Ping? This is a rather nasty bug which needs fixing in 4.4 as well.

Quoted 15 lines
 tools/libxl/libxl.c |    1 +
 1 file changed, 1 insertion(+)
tools/libxl/libxl.c +1 -0
index 3526539..a1e0b5e 100644
@@ -71,6 +71,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     ctx->childproc_user = 0;
 
     ctx->sigchld_selfpipe[0] = -1;
+    ctx->sigchld_selfpipe[1] = -1;
     libxl__ev_fd_init(&ctx->sigchld_selfpipe_efd);
 
     /* The mutex is special because we can't idempotently destroy it */