xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Roger Pau Monne <roger.pau@citrix.com>
To: xen-devel@lists.xen.org
Cc: Ian Jackson <ian.jackson@eu.citrix.com>,
	Roger Pau Monne <roger.pau@citrix.com>
Subject: [PATCH 01/13] libxl: pass env vars to libxl__exec
Date: Wed, 16 May 2012 17:11:44 +0100	[thread overview]
Message-ID: <1337184716-49276-2-git-send-email-roger.pau@citrix.com> (raw)
In-Reply-To: <1337184716-49276-1-git-send-email-roger.pau@citrix.com>

Add another parameter to libxl__exec call that contains the
environment variables to use when performing the execvp call.

Changes since v4:

 * Unify error checking.

Changes since v3:

 * Use CTX instead of defining libxl_ctx *ctx.

 * Error checking on setenv.

 * Better comment on header for libxl__exec function.

 * Added some const-correctness.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c            |    2 +-
 tools/libxl/libxl_bootloader.c |    4 ++--
 tools/libxl/libxl_dm.c         |    2 +-
 tools/libxl/libxl_exec.c       |   14 ++++++++++++--
 tools/libxl/libxl_internal.h   |   20 ++++++++++++++++++--
 5 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 4d01cf8..2a09192 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -1261,7 +1261,7 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
         args[2] = "-autopass";
     }
 
-    libxl__exec(autopass_fd, -1, -1, args[0], args);
+    libxl__exec(gc, autopass_fd, -1, -1, args[0], args, NULL);
     abort();
 
  x_fail:
diff --git a/tools/libxl/libxl_bootloader.c b/tools/libxl/libxl_bootloader.c
index fb1302b..0021a7b 100644
--- a/tools/libxl/libxl_bootloader.c
+++ b/tools/libxl/libxl_bootloader.c
@@ -359,6 +359,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
     libxl__bootloader_state *bl = CONTAINER_OF(op, *bl, openpty);
     STATE_AO_GC(bl->ao);
     int rc, r;
+    char *const env[] = { "TERM", "vt100", NULL };
 
     if (bl->openpty.rc) {
         rc = bl->openpty.rc;
@@ -452,8 +453,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
         /* child */
         r = login_tty(libxl__carefd_fd(bl->ptys[0].slave));
         if (r) { LOGE(ERROR, "login_tty failed"); exit(-1); }
-        setenv("TERM", "vt100", 1);
-        libxl__exec(-1, -1, -1, bl->args[0], (char**)bl->args);
+        libxl__exec(gc, -1, -1, -1, bl->args[0], (char **) bl->args, env);
         exit(-1);
     }
 
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index 4ad7d02..95fd0ac 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -1015,7 +1015,7 @@ retry_transaction:
         goto out_close;
     if (!rc) { /* inner child */
         setsid();
-        libxl__exec(null, logfile_w, logfile_w, dm, args);
+        libxl__exec(gc, null, logfile_w, logfile_w, dm, args, NULL);
     }
 
     rc = 0;
diff --git a/tools/libxl/libxl_exec.c b/tools/libxl/libxl_exec.c
index b46b893..082bf44 100644
--- a/tools/libxl/libxl_exec.c
+++ b/tools/libxl/libxl_exec.c
@@ -66,8 +66,8 @@ static void check_open_fds(const char *what)
     if (badness) abort();
 }
 
-void libxl__exec(int stdinfd, int stdoutfd, int stderrfd, const char *arg0,
-                char **args)
+void libxl__exec(libxl__gc *gc, int stdinfd, int stdoutfd, int stderrfd,
+                 const char *arg0, char *const args[], char *const env[])
      /* call this in the child */
 {
     if (stdinfd != -1)
@@ -90,8 +90,18 @@ void libxl__exec(int stdinfd, int stdoutfd, int stderrfd, const char *arg0,
     /* in case our caller set it to IGN.  subprocesses are entitled
      * to assume they got DFL. */
 
+    if (env != NULL) {
+        for (int i = 0; env[i] != NULL && env[i+1] != NULL; i += 2) {
+            if (setenv(env[i], env[i+1], 1) < 0) {
+                LOGEV(ERROR, errno, "setting env vars (%s = %s)",
+                                    env[i], env[i+1]);
+                goto out;
+            }
+        }
+    }
     execvp(arg0, args);
 
+out:
     fprintf(stderr, "libxl: cannot execute %s: %s\n", arg0, strerror(errno));
     _exit(-1);
 }
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index f71e76a..aaaf644 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1138,8 +1138,24 @@ _hidden int libxl__wait_for_offspring(libxl__gc *gc,
 
  /* low-level stuff, for synchronous subprocesses etc. */
 
-_hidden void libxl__exec(int stdinfd, int stdoutfd, int stderrfd,
-               const char *arg0, char **args); // logs errors, never returns
+/*
+ * env should be passed using the following format,
+ *
+ * env[0]: name of env variable
+ * env[1]: value of env variable
+ * env[n]: ...
+ *
+ * So it efectively becomes something like:
+ * export env[n]=env[n+1]
+ * (where n%2 = 0)
+ *
+ * The last entry of the array always has to be NULL.
+ *
+ * Logs errors, never returns.
+ */
+_hidden  void libxl__exec(libxl__gc *gc, int stdinfd, int stdoutfd,
+                          int stderrfd, const char *arg0, char *const args[],
+                          char *const env[]);
 
 /* from xl_create */
 
-- 
1.7.7.5 (Apple Git-26)

  reply	other threads:[~2012-05-16 16:11 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-16 16:11 [PATCH 0/13] execute hotplug scripts from libxl Roger Pau Monne
2012-05-16 16:11 ` Roger Pau Monne [this message]
2012-05-18 16:02   ` [PATCH 01/13] libxl: pass env vars to libxl__exec Ian Jackson
2012-05-16 16:11 ` [PATCH 02/13] libxl: fix libxl__xs_directory usage of transaction Roger Pau Monne
2012-05-18 16:03   ` Ian Jackson
2012-05-16 16:11 ` [PATCH 03/13] libxl: add libxl__xs_path_cleanup Roger Pau Monne
2012-05-18 16:06   ` Ian Jackson
2012-05-16 16:11 ` [PATCH 04/13] libxl: move libxl_device_disk_add to libxl_device Roger Pau Monne
2012-05-16 16:11 ` [PATCH 05/13] libxl: move libxl_device_nic_add " Roger Pau Monne
2012-05-16 16:11 ` [PATCH 06/13] libxl: cleanup libxl__device_{disk, nic}_add Roger Pau Monne
2012-05-18 16:07   ` Ian Jackson
2012-05-16 16:11 ` [PATCH 07/13] libxl: convert libxl_domain_destroy to an AO op Roger Pau Monne
2012-05-18 16:19   ` Ian Jackson
2012-05-18 16:24   ` Ian Jackson
2012-05-16 16:11 ` [PATCH 08/13] libxl: convert libxl_device_disk_add to an async operation Roger Pau Monne
2012-05-18 16:33   ` Ian Jackson
2012-05-16 16:11 ` [PATCH 09/13] libxl: convert libxl_device_nic_add " Roger Pau Monne
2012-05-18 16:38   ` Ian Jackson
2012-05-22 13:49     ` Roger Pau Monne
2012-05-22 14:04       ` Ian Jackson
2012-05-16 16:11 ` [PATCH 10/13] libxl: add option to choose who executes hotplug scripts Roger Pau Monne
2012-05-18 16:40   ` Ian Jackson
2012-05-16 16:11 ` [PATCH 11/13] libxl: set nic type to VIF by default Roger Pau Monne
2012-05-18 16:41   ` Ian Jackson
2012-05-21 16:29     ` Roger Pau Monne
2012-05-29 14:40       ` Ian Jackson
2012-05-29 14:46         ` Ian Campbell
2012-05-29 15:02           ` Ian Jackson
2012-05-29 15:06             ` Ian Campbell
2012-05-30 12:03           ` Roger Pau Monne
2012-06-07 14:30             ` Ian Jackson
2012-06-11 14:05               ` Roger Pau Monne
2012-05-16 16:11 ` [PATCH 12/13] libxl: call hotplug scripts for disk devices from libxl Roger Pau Monne
2012-05-18 16:51   ` Ian Jackson
2012-05-16 16:11 ` [PATCH 13/13] libxl: call hotplug scripts for nic " Roger Pau Monne

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=1337184716-49276-2-git-send-email-roger.pau@citrix.com \
    --to=roger.pau@citrix.com \
    --cc=ian.jackson@eu.citrix.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).