[PATCH 13/22] Add a method to set environment variables for child processes

David Shea dshea at redhat.com
Wed Jun 3 16:03:37 UTC 2015


iutil.setenv will manipulate a dictionary that is added to a copy of
os.environ that is passed to Popen. This way environment variables can
be made available to child processes without modifying the environment
of the anaconda process, which is not thread safe.

Use iutil.setenv in place of os.environment for a variety of environment
variables not needed by the anaconda process.

(cherry picked from commit 93b942e50e63873d2355f1f99d2801853abc978d)

Also applied the change from e4392530941b7eab7ca6f3d3305e9a0729ba180b to
fix the bug introduced by this 93b942e50e63873d2355f1f99d2801853abc978d

Related: rhbz#1188287
---
 anaconda                      | 12 ++++++------
 pyanaconda/iutil.py           | 16 ++++++++++++++++
 pyanaconda/rescue.py          |  6 +++---
 pyanaconda/ui/gui/__init__.py |  2 +-
 4 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/anaconda b/anaconda
index 00f4c5b..e8f1776 100755
--- a/anaconda
+++ b/anaconda
@@ -1042,14 +1042,14 @@ if __name__ == "__main__":
             log.info("Failed to parse proxy \"%s\": %s", anaconda.proxy, e)
         else:
             # Set environmental variables to be used by pre/post scripts
-            os.environ["PROXY"] = proxy.noauth_url
-            os.environ["PROXY_USER"] = proxy.username or ""
-            os.environ["PROXY_PASSWORD"] = proxy.password or ""
+            iutil.setenv("PROXY", proxy.noauth_url)
+            iutil.setenv("PROXY_USER", proxy.username or "")
+            iutil.setenv("PROXY_PASSWORD", proxy.password or "")
 
             # Variables used by curl, libreport, etc.
-            os.environ["http_proxy"] = proxy.url
-            os.environ["ftp_proxy"] = proxy.url
-            os.environ["HTTPS_PROXY"] = proxy.url
+            iutil.setenv("http_proxy", proxy.url)
+            iutil.setenv("ftp_proxy", proxy.url)
+            iutil.setenv("HTTPS_PROXY", proxy.url)
 
     if flags.noverifyssl:
         ksdata.method.noverifyssl = flags.noverifyssl
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index d384696..6432ecf 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -46,11 +46,27 @@ program_log = logging.getLogger("program")
 
 from pyanaconda.anaconda_log import program_log_lock
 
+_child_env = {}
+
+def setenv(name, value):
+    """ Set an environment variable to be used by child processes.
+
+        This method does not modify os.environ for the running process, which
+        is not thread-safe. If setenv has already been called for a particular
+        variable name, the old value is overwritten.
+
+        :param str name: The name of the environment variable
+        :param str value: The value of the environment variable
+    """
+
+    _child_env[name] = value
+
 def augmentEnv():
     env = os.environ.copy()
     env.update({"LC_ALL": "C",
                 "ANA_INSTALL_PATH": getSysroot()
                })
+    env.update(_child_env)
     return env
 
 _root_path = "/mnt/sysimage"
diff --git a/pyanaconda/rescue.py b/pyanaconda/rescue.py
index a07a4ab..a597c8b 100644
--- a/pyanaconda/rescue.py
+++ b/pyanaconda/rescue.py
@@ -396,7 +396,7 @@ def doRescue(intf, rescue_mount, ksdata):
                 # set a library path to use mounted fs
                 libdirs = os.environ.get("LD_LIBRARY_PATH", "").split(":")
                 mounted = map(lambda dir: "/mnt/sysimage%s" % dir, libdirs)
-                os.environ["LD_LIBRARY_PATH"] = ":".join(libdirs + mounted)
+                iutil.setenv("LD_LIBRARY_PATH", ":".join(libdirs + mounted))
 
                 # find groff data dir
                 gversion = None
@@ -414,8 +414,8 @@ def doRescue(intf, rescue_mount, ksdata):
 
                 if gversion is not None:
                     gpath = "/mnt/sysimage/usr/share/groff/"+gversion
-                    os.environ["GROFF_FONT_PATH"] = gpath + '/font'
-                    os.environ["GROFF_TMAC_PATH"] = "%s:/mnt/sysimage/usr/share/groff/site-tmac" % (gpath + '/tmac',)
+                    iutil.setenv("GROFF_FONT_PATH", gpath + '/font')
+                    iutil.setenv("GROFF_TMAC_PATH", "%s:/mnt/sysimage/usr/share/groff/site-tmac" % (gpath + '/tmac',))
 
                 # do we have bash?
                 try:
diff --git a/pyanaconda/ui/gui/__init__.py b/pyanaconda/ui/gui/__init__.py
index 979a1cf..5a5f2a4 100644
--- a/pyanaconda/ui/gui/__init__.py
+++ b/pyanaconda/ui/gui/__init__.py
@@ -577,7 +577,7 @@ class GraphicalUserInterface(UserInterface):
         if monitor_height_px >= 1200 and monitor_dpi_x > 192 and monitor_dpi_y > 192:
             display.set_window_scale(2)
             # Export the scale so that Gtk programs launched by anaconda are also scaled
-            os.environ["GDK_SCALE"] = "2"
+            iutil.setenv("GDK_SCALE", "2")
 
     @property
     def tty_num(self):
-- 
2.1.0



More information about the anaconda-patches mailing list