util: Add change_elf_rpath API

Linux dynamic loader avoids loading shared libraries from env vars such
as LD_LIBRALY_PATH for ELF binaries which have capabilitiy flags enabled
for security reasons. For these binaries, we modify the RPATH of the ELF
header to tell the loader where to find the libraries.

Process class from process module is imported inside the method after
module initialization in order to avoid circular dependency of relative
imports, which is only available since python 3.5.

Change-Id: Ibc356957fb3dbcf9947bfe96b671ec8c7ede33ff
diff --git a/src/osmo_gsm_tester/util.py b/src/osmo_gsm_tester/util.py
index 1ecbf41..8fb6e19 100644
--- a/src/osmo_gsm_tester/util.py
+++ b/src/osmo_gsm_tester/util.py
@@ -38,6 +38,19 @@
         return path
     return path + ':' + lp
 
+def change_elf_rpath(binary, paths, run_dir):
+    '''
+    Change RPATH field in ELF executable binary.
+    This feature can be used to tell the loaded to load the trial libraries, as
+    LD_LIBRARY_PATH is disabled for paths with modified capabilities.
+    '''
+    from .process import Process
+    proc = Process('patchelf', run_dir, ['patchelf', '--set-rpath', paths, binary])
+    proc.launch()
+    proc.wait()
+    if proc.result != 0:
+        raise RuntimeError('patchelf finished with err code %d' % proc.result)
+
 def ip_to_iface(ip):
     try:
         for iface in os.listdir('/sys/class/net'):