Patchwork osdep: Get environment pointer from global variable

login
register
mail settings
Submitter Stefan Weil
Date Dec. 7, 2013, 1:14 p.m.
Message ID <1386422052-9704-1-git-send-email-sw@weilnetz.de>
Download mbox | patch
Permalink /patch/298667/
State Changes Requested
Headers show

Comments

Stefan Weil - Dec. 7, 2013, 1:14 p.m.
Linux provides a global variable __environ, so it is not necessary to
use envp from main and propagate it via qemu_init_auxval.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
---
 include/qemu/osdep.h |   12 ------------
 linux-user/main.c    |    3 +--
 util/getauxval.c     |    9 ++++++++-
 vl.c                 |    7 +++----
 4 files changed, 12 insertions(+), 19 deletions(-)
Peter Maydell - Dec. 7, 2013, 6:03 p.m.
On 7 December 2013 13:14, Stefan Weil <sw@weilnetz.de> wrote:
> Linux provides a global variable __environ, so it is not necessary to
> use envp from main and propagate it via qemu_init_auxval.

I don't think this will work, because __environ changes whenever
the environment is modified (eg by setenv() calls). It's only the
initial environment pointer value that can be used to find the
aux vector by scanning upwards, so we need to capture it at the
start of main() anyhow and might as well just use the 3-arg main
form to do so.

thanks
-- PMM

Patch

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index b3e2b6d..6191d5a 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -228,16 +228,4 @@  unsigned long qemu_getauxval(unsigned long type);
 static inline unsigned long qemu_getauxval(unsigned long type) { return 0; }
 #endif
 
-/**
- * qemu_init_auxval:
- * @envp: the third argument to main
- *
- * If supported and required, locate the auxiliary vector at program startup.
- */
-#if defined(CONFIG_GETAUXVAL) || !defined(__linux__)
-static inline void qemu_init_auxval(char **envp) { }
-#else
-void qemu_init_auxval(char **envp);
-#endif
-
 #endif
diff --git a/linux-user/main.c b/linux-user/main.c
index 54f71fe..7b8912d 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -3663,7 +3663,7 @@  static int parse_args(int argc, char **argv)
     return optind;
 }
 
-int main(int argc, char **argv, char **envp)
+int main(int argc, char **argv)
 {
     struct target_pt_regs regs1, *regs = &regs1;
     struct image_info info1, *info = &info1;
@@ -3681,7 +3681,6 @@  int main(int argc, char **argv, char **envp)
 
     module_call_init(MODULE_INIT_QOM);
 
-    qemu_init_auxval(envp);
     qemu_cache_utils_init();
 
     if ((envlist = envlist_create()) == NULL) {
diff --git a/util/getauxval.c b/util/getauxval.c
index 476c883..f1d84af 100644
--- a/util/getauxval.c
+++ b/util/getauxval.c
@@ -48,8 +48,10 @@  typedef struct {
 
 static const ElfW_auxv_t *auxv;
 
-void qemu_init_auxval(char **envp)
+static void qemu_init_auxval(void)
 {
+    char **envp = __environ;
+
     /* The auxiliary vector is located just beyond the initial environment.  */
     while (*envp++ != NULL) {
         continue;
@@ -59,6 +61,11 @@  void qemu_init_auxval(char **envp)
 
 unsigned long qemu_getauxval(unsigned long type)
 {
+    if (!auxv) {
+        /* Try to find the auxiliary vector. */
+        qemu_init_auxval();
+    }
+
     /* If we were able to find the auxiliary vector, use it.  */
     if (auxv) {
         const ElfW_auxv_t *a;
diff --git a/vl.c b/vl.c
index e2c94bf..b17fa2e 100644
--- a/vl.c
+++ b/vl.c
@@ -96,10 +96,10 @@ 
 #ifdef CONFIG_SDL
 #if defined(__APPLE__) || defined(main)
 #include <SDL.h>
-int qemu_main(int argc, char **argv, char **envp);
+int qemu_main(int argc, char **argv);
 int main(int argc, char **argv)
 {
-    return qemu_main(argc, argv, NULL);
+    return qemu_main(argc, argv);
 }
 #undef main
 #define main qemu_main
@@ -2816,7 +2816,7 @@  static int object_create(QemuOpts *opts, void *opaque)
     return 0;
 }
 
-int main(int argc, char **argv, char **envp)
+int main(int argc, char **argv)
 {
     int i;
     int snapshot, linux_boot;
@@ -2894,7 +2894,6 @@  int main(int argc, char **argv, char **envp)
     init_clocks();
     rtc_clock = QEMU_CLOCK_HOST;
 
-    qemu_init_auxval(envp);
     qemu_cache_utils_init();
 
     QLIST_INIT (&vm_change_state_head);