From 91ae697d41185b06e8d3926e100cf10c8685b94c Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 7 Oct 2016 17:24:31 +0100 Subject: [PATCH] Clean up how we arrange for environ to be declared Annoyingly, the POSIX way to declare environ (as "extern char **environ") is a redundant declaration in glibc with _GNU_SOURCE; work around that. We also have a workaround for _NSGetEnviron() needing to be used instead of direct access to environ in at least some circumstances on Mac OS. Attempt to sync that up between all the files that use environ, consistently sorting the most special special-cases first (Windows for files that are compiled there, then Mac, then GNU, with lowest-common-denominator POSIX last). The affected files are already OS-specific, so I'm not bothering to introduce a nicer or higher-level API for this. Based on the best bits of an earlier patch from me, and an earlier patch from Thomas Zimmermann. Signed-off-by: Simon McVittie Reviewed-by: Thomas Zimmermann Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357 --- configure.ac | 4 ++++ dbus/dbus-spawn.c | 5 +++++ dbus/dbus-sysdeps-util.c | 2 ++ dbus/dbus-sysdeps.c | 2 ++ tools/dbus-update-activation-environment.c | 7 +++++-- 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 7b20e11..20d41a7 100644 --- a/configure.ac +++ b/configure.ac @@ -491,6 +491,10 @@ case $host_os in ;; esac +# As a GNU extension, glibc declares environ in unistd.h, which is one of +# the AC_INCLUDES_DEFAULT. +AC_CHECK_DECLS([environ]) + dnl ********************************** dnl *** va_copy checks (from GLib) *** dnl ********************************** diff --git a/dbus/dbus-spawn.c b/dbus/dbus-spawn.c index df6eafd..0be8800 100644 --- a/dbus/dbus-spawn.c +++ b/dbus/dbus-spawn.c @@ -45,7 +45,12 @@ #include #endif +#if defined(__APPLE__) +# include +# define environ (*_NSGetEnviron ()) +#elif !HAVE_DECL_ENVIRON extern char **environ; +#endif /** * @addtogroup DBusInternalsUtils diff --git a/dbus/dbus-sysdeps-util.c b/dbus/dbus-sysdeps-util.c index f35c966..44cc6a2 100644 --- a/dbus/dbus-sysdeps-util.c +++ b/dbus/dbus-sysdeps-util.c @@ -35,6 +35,8 @@ #elif (defined __APPLE__) # include # define environ (*_NSGetEnviron()) +#elif HAVE_DECL_ENVIRON && defined(HAVE_UNISTD_H) +# include #else extern char **environ; #endif diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index c58377b..d1a96d1 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -52,6 +52,8 @@ #elif (defined __APPLE__) # include # define environ (*_NSGetEnviron()) +#elif HAVE_DECL_ENVIRON && defined(HAVE_UNISTD_H) +# include #else extern char **environ; #endif diff --git a/tools/dbus-update-activation-environment.c b/tools/dbus-update-activation-environment.c index 160ac5d..452e505 100644 --- a/tools/dbus-update-activation-environment.c +++ b/tools/dbus-update-activation-environment.c @@ -62,9 +62,12 @@ #ifdef DBUS_WIN /* The Windows C runtime uses a different name */ #define environ _environ +#elif defined(__APPLE__) +# include +# define environ (*_NSGetEnviron ()) +#elif HAVE_DECL_ENVIRON && defined(HAVE_UNISTD_H) +# include #else -/* apparently this is the portable way to get the entire environment... - * GNU platforms also put it in unistd.h but that's not portable */ extern char **environ; #endif -- 2.7.4