summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-06-13 12:42:43 -0400
committerScott Moser <smoser@brickies.net>2018-06-13 12:42:43 -0400
commit171378613ef4de3c1bb4170a10ec1748ac62f39f (patch)
tree5bbf7543e2ee27ef0dd851fc3e3eb7ef8d8db022 /cloudinit
parent27283c31f4bf85f40588cfa3b31389d70ec00243 (diff)
downloadvyos-cloud-init-171378613ef4de3c1bb4170a10ec1748ac62f39f.tar.gz
vyos-cloud-init-171378613ef4de3c1bb4170a10ec1748ac62f39f.zip
Fix get_proc_env for pids that have non-utf8 content in environment.
There is no requirement that the environment of a process contains only utf-8 data. This modifies get_proc_env to support it reading data as binary and decoding if provided with an encoding. The default case is now that we now do: contents.decode('utf-8', 'replace') rather than contents.decode('utf-8', 'strict') LP: #1775371
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/util.py35
1 files changed, 22 insertions, 13 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 0017de72..26a41122 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -2131,24 +2131,33 @@ def is_container():
return False
-def get_proc_env(pid):
+def get_proc_env(pid, encoding='utf-8', errors='replace'):
"""
Return the environment in a dict that a given process id was started with.
- """
- env = {}
- fn = os.path.join("/proc/", str(pid), "environ")
+ @param encoding: if true, then decoding will be done with
+ .decode(encoding, errors) and text will be returned.
+ if false then binary will be returned.
+ @param errors: only used if encoding is true."""
+ fn = os.path.join("/proc", str(pid), "environ")
+
try:
- contents = load_file(fn)
- toks = contents.split("\x00")
- for tok in toks:
- if tok == "":
- continue
- (name, val) = tok.split("=", 1)
- if name:
- env[name] = val
+ contents = load_file(fn, decode=False)
except (IOError, OSError):
- pass
+ return {}
+
+ env = {}
+ null, equal = (b"\x00", b"=")
+ if encoding:
+ null, equal = ("\x00", "=")
+ contents = contents.decode(encoding, errors)
+
+ for tok in contents.split(null):
+ if not tok:
+ continue
+ (name, val) = tok.split(equal, 1)
+ if name:
+ env[name] = val
return env