Skip to contentSkip to conversation

osdep: Get environment pointer from global variable

PATCH In review Stefan Weil 2 participants 2 messages Dec 7, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 osdep: Get environment pointer from global variable

Linux provides a global variable __environ, so it is not necessary to use envp from main and propagate it via qemu_init_auxval.

---
 include/qemu/osdep.h |   12 ------------
 linux-user/main.c    |    3 +--
 util/getauxval.c     |    9 ++++++++-
 vl.c                 |    7 +++----
 4 files changed, 12 insertions(+), 19 deletions(-)
include/qemu/osdep.h +0 -12
index b3e2b6d..6191d5a 100644
@@ -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
linux-user/main.c +1 -2
index 54f71fe..7b8912d 100644
@@ -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) {
util/getauxval.c +8 -1
index 476c883..f1d84af 100644
@@ -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;
vl.c +3 -4
index e2c94bf..b17fa2e 100644
@@ -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);
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 2 lines · Stefan Weil

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