Skip to contentSkip to conversation

Creating a pristine environment?

PATCH Holger Freyther 8 participants 16 messages May 8, 2008 – May 21, 2008

lore
Share & tools
lore
#0 lore raw

Hey,

I have seen raster's environment breaking various buildsystems and we might want to really start having a clean environment before starting to build.

the patch below should implement it. Should we do this in OE? I'm aware of the unexport inside OE but we really should do this the otherway around.

comments?

	z.
bitbake +52 -0
Index: bin/bitbake
===================================================================
@@ -42,6 +42,56 @@
 
 
 #============================================================================#
+# clean_environment 
+#============================================================================#
+def clean_environment():
+    """
+    Create a pristine environment for bitbake. This will remove variables that
+    are not known and may influence the build in a negative way.
+    """
+    good_keys = [
+        "BBPATH",
+        "COLORTERM",
+        "DBUS_SESSION_BUS_ADDRESS",
+        "DESKTOP_SESSION",
+        "DESKTOP_STARTUP_ID",
+        "DISPLAY",
+        "GNOME_KEYRING_PID",
+        "GNOME_KEYRING_SOCKET",
+        "GPG_AGENT_INFO",
+        "GTK_RC_FILES",
+        "HOME",
+        "LANG",
+        "LOGNAME",
+        "PATH",
+        "PWD",
+        "SESSION_MANAGER",
+        "SHELL",
+        "SSH_AUTH_SOCK",
+        "TERM",
+        "USER",
+        "USERNAME",
+        "_",
+        "XAUTHORITY",
+        "XDG_DATA_DIRS",
+        "XDG_SESSION_COOKIE",
+    ]
+
+
+    removed_vars = []
+    for key in os.environ.keys():
+        if key in good_keys:
+            continue
+        
+        removed_vars.append(key)
+        os.unsetenv(key)
+        del os.environ[key]
+
+    if len(removed_vars):
+        bb.note("Removed the following variables from the environment:", ",".join(removed_vars))
+
+
+#============================================================================#
 # main
 #============================================================================#
 
@@ -111,6 +161,8 @@
     configuration.pkgs_to_build = []
     configuration.pkgs_to_build.extend(args[1:])
 
+    clean_environment()
+
     cooker = bb.cooker.BBCooker(configuration)
 
     if configuration.profile:
#1 lore raw
Quoted 7 lines · Holger Freyther

Hey,

I have seen raster's environment breaking various buildsystems and we might want to really start having a clean environment before starting to build.

the patch below should implement it. Should we do this in OE? I'm aware of the unexport inside OE but we really should do this the otherway around.

While this is a good idea, your current implementation breaks:

MACHINE=foo ANGSTROM_MODE=bar bitbake foo-bar-image

Quoted 13 lines

comments?

	z.

------------------------------------------------------------------------

_______________________________________________
Openembedded-devel mailing list
[email protected]
http://lists.linuxtogo.org/cgi-bin/mailman/listinfo/openembedded-devel

#2 lore raw
Quoted 7 lines · On Thursday 08 May 2008 12:05:38 Koen Kooi wrote:
Quoted 6 lines · Holger Freyther

Hey,

I have seen raster's environment breaking various buildsystems and we might want to really start having a clean environment before starting to build.

Quoted 7 lines
Quoted 3 lines

the patch below should implement it. Should we do this in OE? I'm aware of the unexport inside OE but we really should do this the otherway around.

While this is a good idea, your current implementation breaks:

MACHINE=foo ANGSTROM_MODE=bar bitbake foo-bar-image

Well, that would make a case for keeping the unexport feature in OE. Or we could move the unsetting to after having parsed the configuration. I will give the 2nd idea a go...

z.

#3 lore raw
Quoted 12 lines · Holger Freyther
Quoted 11 lines · On Thursday 08 May 2008 12:05:38 Koen Kooi wrote:
Quoted 9 lines · Holger Freyther

Hey,

I have seen raster's environment breaking various buildsystems and we might want to really start having a clean environment before starting to build.

the patch below should implement it. Should we do this in OE? I'm aware of the unexport inside OE but we really should do this the otherway around.

Quoted 7 lines
Quoted 3 lines

While this is a good idea, your current implementation breaks:

MACHINE=foo ANGSTROM_MODE=bar bitbake foo-bar-image

Well, that would make a case for keeping the unexport feature in OE. Or we could move the unsetting to after having parsed the configuration. I will give the 2nd idea a go...

I agree we should do something, I'm just not sure what. Personally I do set things in the environment quite heavily and I'd prefer not to lose that capability. Perhaps we clean by default but allow a user to override and claim their starting environment is clean at their own risk?

Cheers,

Richard

#11 lore raw
In reply to Holger Freyther
Quoted 7 lines · Holger Freyther

Hey,

I have seen raster's environment breaking various buildsystems and we might want to really start having a clean environment before starting to build.

the patch below should implement it. Should we do this in OE? I'm aware of the unexport inside OE but we really should do this the otherway around.

I currently use a few other env variables in my builds to make things a little more convenient:
export OEDIR="/build/myproject/oe"
export TOPDIR="${OEDIR}/build/angstrom-2007.1"
export BBPATH="${OEDIR}/oe/custom:${TOPDIR}:${OEDIR}/oe/org.openembedded.dev" export PATH="${OEDIR}/bitbake/bin:${PATH}"

and then in site.conf:
DL_DIR = "${OEDIR}/downloads"

BBFILES := "${OEDIR}/oe/org.openembedded.dev/packages/*/*.bb ${OEDIR}/oe/custom/packages/*/*.bb"
BBFILE_COLLECTIONS = "upstream custom" BBFILE_PATTERN_upstream = "^${OEDIR}/oe/org.openembedded.dev/packages/" BBFILE_PATTERN_custom = "^${OEDIR}/oe/custom/packages" BBFILE_PRIORITY_upstream = "5"
BBFILE_PRIORITY_custom = "10"

So, I would like OEDIR and TOPDIR included in the "good" list :-)

Thanks,
Cliff

Signature · 3 lines
=======================
Cliff Brake
http://bec-systems.com
#12 lore raw

Hello Cliff,

Quoted 1 line · Cliff Brake

export TOPDIR="${OEDIR}/build/angstrom-2007.1"

A warning here, I have seen breakage with some environment variables.

I think TOPDIR is used by u-boot or some such.

Think of a better (more uniquer) namespace, such as OE_TOPDIR.

Regards,

Signature · 1 line
Leon
#4 lore raw
In reply to Richard Purdie
Quoted 3 lines · Richard Purdie

that capability. Perhaps we clean by default but allow a user to override and claim their starting environment is clean at their own risk?

That's roughly how SCons handles a similar feature - it defaults to cleaning the environment but makes the original environment availiable to the build system so it can either be referred to or imported wholesale as desired.

Signature · 1 line
"You grabbed my hand and we fell into it, like a daydream - or a fever."
#13 lore raw
In reply to Leon Woestenberg
Quoted 8 lines · Leon Woestenberg
Quoted 1 line · Cliff Brake

export TOPDIR="${OEDIR}/build/angstrom-2007.1"

A warning here, I have seen breakage with some environment variables.

I think TOPDIR is used by u-boot or some such.

Think of a better (more uniquer) namespace, such as OE_TOPDIR.

Good point. TOPDIR is currently coded in bitbake:

./lib/bb/parse/parse_py/ConfHandler.py: if not
bb.data.getVar('TOPDIR', data):
./lib/bb/parse/parse_py/ConfHandler.py:
bb.data.setVar('TOPDIR', os.getcwd(), data)
./lib/bb/build.py: prevdir = data.expand('${TOPDIR}', d)

so, it won't be trivial to change without breaking existing build environments.

Thanks,
Cliff

Signature · 3 lines
=======================
Cliff Brake
http://bec-systems.com
#14 lore raw
In reply to Holger Freyther
Quoted 5 lines · Holger Freyther

I have seen raster's environment breaking various buildsystems and we might want to really start having a clean environment before starting to build.

the patch below should implement it. Should we do this in OE? I'm aware of the unexport inside OE but we really should do this the otherway around.

Using some variables from the users environment is good, as others have mentioned. But some (I'm looking at you, CPATH) really need to be unset. My Gentoo system automatically sets CPATH, and having that set results in "CROSS COMPILE Badness: /usr/include in INCLUDEPATH: /usr/include/libffi".

---
Geoffrey

#15 lore raw
In reply to Holger Freyther

Hello,

Quoted 2 lines · Holger Freyther

comments?

How about in the insanity checker?

We would like the user to make sure his environment is sane for OpenEmbedded, and not try to fix it up ourselves.

Regards,

Signature · 1 line
Leon
#5 lore raw
In reply to Richard Purdie
Quoted 21 lines · On Thursday 08 May 2008 13:29:30 Richard Purdie wrote:
Quoted 12 lines · Holger Freyther
Quoted 11 lines · On Thursday 08 May 2008 12:05:38 Koen Kooi wrote:
Quoted 9 lines · Holger Freyther

Hey,

I have seen raster's environment breaking various buildsystems and we might want to really start having a clean environment before starting to build.

the patch below should implement it. Should we do this in OE? I'm aware of the unexport inside OE but we really should do this the otherway around.

Quoted 7 lines
Quoted 3 lines

While this is a good idea, your current implementation breaks:

MACHINE=foo ANGSTROM_MODE=bar bitbake foo-bar-image

Well, that would make a case for keeping the unexport feature in OE. Or we could move the unsetting to after having parsed the configuration. I will give the 2nd idea a go...

Quoted 5 lines

I agree we should do something, I'm just not sure what. Personally I do set things in the environment quite heavily and I'd prefer not to lose that capability. Perhaps we clean by default but allow a user to override and claim their starting environment is clean at their own risk?

Okay. I added an option, I clean the environment after having parsed the configuration. This means everything used from conf/local.conf (TOPDIR, BBPATH, BBFILES, MACHINE...) will be inside the configuration data of bitbake but not be exported. I also updated the documentation (man page and usermanual).

comments?

	z.
bitbake.1 +11 -6
Index: doc/bitbake.1
===================================================================
@@ -97,13 +97,18 @@
 .B \-IIGNORED\_DOT\_DEPS, \-\-ignore-deps=IGNORED_DOT_DEPS
 Stop processing at the given list of dependencies when generating dependency
 graphs. This can help to make the graph more appealing
-.\"
-.\" Next option is only in BitBake 1.7.x (trunk)
-.\"
-.\".TP
-.\".B \-lDEBUG_DOMAINS, \-\-log-domains=DEBUG_DOMAINS
-.\"Show debug logging for the specified logging domains
+.TP
+.B \-lDEBUG_DOMAINS, \-\-log-domains=DEBUG_DOMAINS
+Show debug logging for the specified logging domains
+.TP
+.B \-P, \-\-profile
+profile the command and print a report
+.TP
+.B \-S, \-\-sane-environment
+Do not clean the environment variables. Use this on your own risk.
+.TP
 
+
 .SH AUTHORS
 BitBake was written by 
 Phil Blundell,
manual/usermanual.xml +4 -0
Index: doc/manual/usermanual.xml
===================================================================
@@ -415,6 +415,10 @@
                         the graph more appealing
   -l DEBUG_DOMAINS, --log-domains=DEBUG_DOMAINS
                         Show debug logging for the specified logging domains
+  -P, --profile         profile the command and print a report
+  -S, --sane-environment
+                        Do not clean the environment variables. Use this on
+                        your own risk.
 
 </screen>
                 </para>
bitbake +55 -0

73 lines not on this page · raw message

#6 lore raw
Quoted 4 lines · Holger Freyther

Okay. I added an option, I clean the environment after having parsed the configuration. This means everything used from conf/local.conf (TOPDIR, BBPATH, BBFILES, MACHINE...) will be inside the configuration data of bitbake but not be exported.

Implementation question: who will fix all the shell based functions in OE that rely on things like MACHINE being exported?

regards,

Koen

#7 lore raw
Quoted 8 lines · On Saturday 10 May 2008 14:28:39 Koen Kooi wrote:
Quoted 4 lines · Holger Freyther

Okay. I added an option, I clean the environment after having parsed the configuration. This means everything used from conf/local.conf (TOPDIR, BBPATH, BBFILES, MACHINE...) will be inside the configuration data of bitbake but not be exported.

Implementation question: who will fix all the shell based functions in OE that rely on things like MACHINE being exported?

To my understanding there is nothing to fix. Bitbake will unexport the environment after having parsed the configuration. If you have export FOO inside a Bitbake file. data.py:emit_var will put an "export FOO=.." into the shell script.

comments.

	z.
#8 lore raw
Quoted 2 lines · Holger Freyther

To my understanding there is nothing to fix. Bitbake will unexport the environment after having parsed the configuration.

MACHINE=spitz bitbake opie-image

will still work?

#9 lore raw
In reply to Holger Freyther

Hi,

Quoted 5 lines · Holger Freyther

Okay. I added an option, I clean the environment after having parsed the configuration. This means everything used from conf/local.conf (TOPDIR, BBPATH, BBFILES, MACHINE...) will be inside the configuration data of bitbake but not be exported. I also updated the documentation (man page and usermanual).

I merged the manual bits of this so they no longer complicate the equation.

I've given this patch some thought and I'd like to propose the following rehashed version of it. It has bitbake look at three different environmental variables which whilst complicated, does keep everyone happy:

BB_PRESERVE_ENV - if set don't touch the environment BB_ENV_WHITELIST - a list of variables to whitelist overriding the default BB_ENV_EXTRAWHITE - extra variables to whitelist (not touch)

So in Poky I can add MACHINE and POKYMODE to the whitelist by default in the environment setup script and in my personal case, I can preserve my environment since I trust myself to do this :).

Any thoughts on this?

Cheers,

Richard

bb/utils.py +53 -0
Index: lib/bb/utils.py
===================================================================
@@ -268,3 +268,56 @@
     for line in open(filename):
         s.update(line)
     return s.hexdigest()
+
+def preserved_envvars_list():
+    return [
+        "BBPATH",
+        "COLORTERM",
+        "DBUS_SESSION_BUS_ADDRESS",
+        "DESKTOP_SESSION",
+        "DESKTOP_STARTUP_ID",
+        "DISPLAY",
+        "GNOME_KEYRING_PID",
+        "GNOME_KEYRING_SOCKET",
+        "GPG_AGENT_INFO",
+        "GTK_RC_FILES",
+        "HOME",
+        "LANG",
+        "LOGNAME",
+        "PATH",
+        "PWD",
+        "SESSION_MANAGER",
+        "SHELL",
+        "SSH_AUTH_SOCK",
+        "TERM",
+        "USER",
+        "USERNAME",
+        "_",
+        "XAUTHORITY",
+        "XDG_DATA_DIRS",
+        "XDG_SESSION_COOKIE",
+    ]
+
+def filter_environment(good_vars):
+    """
+    Create a pristine environment for bitbake. This will remove variables that
+    are not known and may influence the build in a negative way.
+    """
+
+    import bb
+
+    removed_vars = []
+    for key in os.environ.keys():
+        if key in good_vars:
+            continue
+        
+        removed_vars.append(key)
+        os.unsetenv(key)
+        del os.environ[key]
+
+    #if len(removed_vars):
+    #    bb.debug(1, "Removed the following variables from the environment:", ",".join(removed_vars))
+
+    return removed_vars
+
+
bb/cooker.py +2 -0
Index: lib/bb/cooker.py
===================================================================
@@ -59,6 +59,8 @@
 
         self.configuration.data = bb.data.init()
 
+    def parseConfiguration(self):
+
         for f in self.configuration.file:
             self.parseConfigurationFile( f )
 
bitbake +13 -0
Index: bin/bitbake
===================================================================
@@ -113,6 +113,19 @@
 
     cooker = bb.cooker.BBCooker(configuration)
 
+    # Optionally clean up the environment
+    if 'BB_PRESERVE_ENV' not in os.environ:
+        if 'BB_ENV_WHITELIST' in os.environ:
+            good_vars = os.environ['BB_ENV_WHITELIST'].split()
+        else:
+            good_vars = bb.utils.preserved_envvars_list()
+        if 'BB_ENV_EXTRAWHITE' in os.environ:
+            good_vars.extend(os.environ['BB_ENV_EXTRAWHITE'].split())
+            print "here %s" % good_vars
+        bb.utils.filter_environment(good_vars)
+
+    cooker.parseConfiguration()
+
     if configuration.profile:
         try:
             import cProfile as profile
#10 lore raw

Hi,

I talked to Holger about the previous patch and he rightly IMO didn't like some aspects of it. In the discussion we concluded (I think, correct me if I'm wrong!) the best idea was to:

Take the original environment and process it as I proposed cleaning completely by default apart from the standard whitelist and allowing this behaviour to be overridden with:

BB_PRESERVE_ENV - if set don't touch the environment BB_ENV_WHITELIST - a list of variables to whitelist overriding the default BB_ENV_EXTRAWHITE - extra variables to whitelist (not touch)

Any variables still in the environment after this process are added into the data dictionary. The difference is that whilst we used to export everything in the original environment when executing shell tasks we now will only export anything explicitly marked as an export.

I ran some tests and various things did break, specifically, PATH went missing, devshell was missing some variables and bbimage broke badly. I did fix up bbimage but then I decided to remove it entirely, something long planned we've just never got around to it. I've added fixes for the PATH and devshell issues although devshell could perhaps use some further variables.

One side effect of this is we can throw the 'matchesenv' flag away which might actually give a small parsing speedup.

So, any further comments?

I should probably s/BB_PRESERVE_ENV/BB_ENV_PRESERVE/ for consistency.

Cheers,

Richard

bb/utils.py +56 -0
Index: lib/bb/utils.py
===================================================================
@@ -268,3 +268,59 @@
     for line in open(filename):
         s.update(line)
     return s.hexdigest()
+
+def preserved_envvars_list():
+    return [
+        'BBPATH',
+        'BB_PRESERVE_ENV',
+        'BB_ENV_WHITELIST',
+        'BB_ENV_EXTRAWHITE',
+        'COLORTERM',
+        'DBUS_SESSION_BUS_ADDRESS',
+        'DESKTOP_SESSION',
+        'DESKTOP_STARTUP_ID',
+        'DISPLAY',
+        'GNOME_KEYRING_PID',
+        'GNOME_KEYRING_SOCKET',
+        'GPG_AGENT_INFO',
+        'GTK_RC_FILES',
+        'HOME',
+        'LANG',
+        'LOGNAME',
+        'PATH',
+        'PWD',
+        'SESSION_MANAGER',
+        'SHELL',
+        'SSH_AUTH_SOCK',
+        'TERM',
+        'USER',
+        'USERNAME',
+        '_',
+        'XAUTHORITY',
+        'XDG_DATA_DIRS',
+        'XDG_SESSION_COOKIE',
+    ]
+
+def filter_environment(good_vars):
+    """
+    Create a pristine environment for bitbake. This will remove variables that
+    are not known and may influence the build in a negative way.
+    """
+
+    import bb
+
+    removed_vars = []
+    for key in os.environ.keys():
+        if key in good_vars:
+            continue
+        
+        removed_vars.append(key)
+        os.unsetenv(key)
+        del os.environ[key]
+
+    if len(removed_vars):
+        bb.debug(1, "Removed the following variables from the environment:", ",".join(removed_vars))
+
+    return removed_vars
+
+
bb/data.py +6 -15
Index: lib/bb/data.py
===================================================================
@@ -324,22 +324,16 @@
         if val != expanded:
             setVar(key, expanded, alterdata)
 
-import os
-
 def inheritFromOS(d):
     """Inherit variables from the environment."""
-#   fakeroot needs to be able to set these
-    non_inherit_vars = [ "LD_LIBRARY_PATH", "LD_PRELOAD" ]
     for s in os.environ.keys():
-        if not s in non_inherit_vars:
-            try:
-                setVar(s, os.environ[s], d)
-                setVarFlag(s, 'matchesenv', '1', d)
-            except TypeError:
-                pass
+        try:
+            setVar(s, os.environ[s], d)
+        except TypeError:
+            pass
+        os.unsetenv(s)
+        del os.environ[s]
 
-import sys
-
 def emit_var(var, o=sys.__stdout__, d = init(), all=False):
     """Emit a variable to be sourced by a shell."""
     if getVarFlag(var, "python", d):
@@ -379,9 +373,6 @@
         o.write('unset %s\n' % varExpanded)
         return 1
 
-    if getVarFlag(var, 'matchesenv', d):
-        return 0
-
     val.rstrip()
     if not val:
         return 0
bb/data_smart.py +0 -3
Index: lib/bb/data_smart.py
===================================================================
@@ -149,9 +149,6 @@
 
         if not var in self.dict:
             self._makeShadowCopy(var)
-        if self.getVarFlag(var, 'matchesenv'):
-            self.delVarFlag(var, 'matchesenv')
-            self.setVarFlag(var, 'export', 1)
 
         # more cookies for the cookie monster
         if '_' in var:
bb/cooker.py +4 -0
Index: lib/bb/cooker.py
===================================================================
@@ -59,6 +59,10 @@
 
         self.configuration.data = bb.data.init()
 
+    def parseConfiguration(self):
+
+        bb.data.inheritFromOS(self.configuration.data)
+
         for f in self.configuration.file:
             self.parseConfigurationFile( f )
 
bb/parse/parse_py/ConfHandler.py +0 -1
Index: lib/bb/parse/parse_py/ConfHandler.py
===================================================================
@@ -118,7 +118,6 @@
     init(data)
 
     if include == 0:
-        bb.data.inheritFromOS(data)
         oldfile = None
     else:
         oldfile = bb.data.getVar('FILE', data)
bitbake +12 -0
Index: bin/bitbake
===================================================================
@@ -113,6 +113,18 @@
 
     cooker = bb.cooker.BBCooker(configuration)
 
+    # Optionally clean up the environment
+    if 'BB_PRESERVE_ENV' not in os.environ:
+        if 'BB_ENV_WHITELIST' in os.environ:
+            good_vars = os.environ['BB_ENV_WHITELIST'].split()
+        else:
+            good_vars = bb.utils.preserved_envvars_list()
+        if 'BB_ENV_EXTRAWHITE' in os.environ:
+            good_vars.extend(os.environ['BB_ENV_EXTRAWHITE'].split())
+        bb.utils.filter_environment(good_vars)
+
+    cooker.parseConfiguration()
+
     if configuration.profile:
         try:
             import cProfile as profile