gen_manifest 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python2
  2. import os
  3. import sys
  4. import subprocess
  5. import re
  6. runtime_libs = ['libc',
  7. 'libdl',
  8. 'libm',
  9. 'libpthread',
  10. 'libutil',
  11. 'libnss_dns',
  12. 'libresolv',
  13. 'librt']
  14. def parse_libs(bin_path):
  15. ldd_out = subprocess.check_output(['ldd', bin_path])
  16. lib_list = []
  17. for line in ldd_out.splitlines():
  18. match = re.match(r'\t(.*) => (.*) \(0x', line)
  19. if match and match.group(1) and match.group(2):
  20. name_match = re.match(r'([\w\d]*)(-*)([\w\d]*)(\.*)(.*)', match.group(1))
  21. if name_match:
  22. lib_name = name_match.group(1)
  23. # library can be formatted as libxxx-xxx.so which is invalid format in
  24. # the manifest, so reformat to libxxx_xxx as the option key
  25. if name_match.group(2) == '-' and name_match.group(3):
  26. lib_name += '_' + name_match.group(3)
  27. if lib_name not in runtime_libs:
  28. lib_list.append((lib_name, match.group(2)))
  29. return lib_list
  30. def make_executable(path):
  31. mode = os.stat(path).st_mode
  32. mode |= (mode & 0o444) >> 2 # copy R bits to X
  33. os.chmod(path, mode)
  34. def gen_manifest(app_name, bin_name, g_path):
  35. m_path = g_path + '/LibOS/shim/test/apps/' + app_name + '/' + app_name + '.' + 'manifest'
  36. print('generating manifest: ' + m_path)
  37. mf = open(m_path, 'w')
  38. make_executable(m_path)
  39. mf.write('#!' + g_path + '/Runtime/pal_loader\n')
  40. mf.write('loader.preload = file:../../../../../Runtime/libsysdb.so\n')
  41. # Get path of binary
  42. bin_path = subprocess.check_output(['which', bin_name]).strip()
  43. mf.write('loader.exec = file:' + bin_path + '\n')
  44. mf.write('loader.execname = ' + bin_name + '\n')
  45. mf.write('loader.env.LD_LIBRARY_PATH = /graphene:/graphene/resolv:/host:/usr/local/lib:/usr/lib:/usr/lib/x86_64-linux-gnu')
  46. if 'LD_LIBRARY_PATH' in os.environ and os.environ['LD_LIBRARY_PATH']:
  47. mf.write(':' + os.environ['LD_LIBRARY_PATH'])
  48. mf.write('\n')
  49. mf.write('loader.env.PATH = /usr/local/bin:/usr/bin:/bin\n' +
  50. 'loader.env.USERNAME =\n' +
  51. 'loader.env.PWD =\n' +
  52. 'loader.debug_type = none\n')
  53. mf.write('\n')
  54. # File system setting
  55. mf.write('fs.mount.lib1.type = chroot\n' +
  56. 'fs.mount.lib1.path = /graphene\n' +
  57. 'fs.mount.lib1.uri = file:../../../../../Runtime\n\n')
  58. mf.write('fs.mount.lib2.type = chroot\n' +
  59. 'fs.mount.lib2.path = /host\n' +
  60. 'fs.mount.lib2.uri = file:/lib/x86_64-linux-gnu\n\n')
  61. mf.write('fs.mount.bin.type = chroot\n' +
  62. 'fs.mount.bin.path = /bin\n' +
  63. 'fs.mount.bin.uri = file:/bin\n\n')
  64. mf.write('fs.mount.usr.type = chroot\n' +
  65. 'fs.mount.usr.path = /usr\n' +
  66. 'fs.mount.usr.uri = file:/usr\n\n')
  67. mf.write('fs.mount.etc.type = chroot\n' +
  68. 'fs.mount.etc.path = /etc\n' +
  69. 'fs.mount.etc.uri = file:\n\n')
  70. mf.write('fs.mount.opt.type = chroot\n' +
  71. 'fs.mount.opt.path = /opt\n' +
  72. 'fs.mount.opt.uri = file:\n\n')
  73. # Set Dependent Libraries
  74. mf.write('sgx.trusted_files.ld = file:../../../../../Runtime/ld-linux-x86-64.so.2\n' +
  75. 'sgx.trusted_files.libc = file:../../../../../Runtime/libc.so.6\n' +
  76. 'sgx.trusted_files.libdl = file:../../../../../Runtime/libdl.so.2\n' +
  77. 'sgx.trusted_files.libm = file:../../../../../Runtime/libm.so.6\n' +
  78. 'sgx.trusted_files.libpthread = file:../../../../../Runtime/libpthread.so.0\n' +
  79. 'sgx.trusted_files.libutil = file:../../../../../Runtime/libutil.so.1\n' +
  80. 'sgx.trusted_files.libnss_dns = file:../../../../../Runtime/libnss_dns.so.2\n' +
  81. 'sgx.trusted_files.libresolv = file:../../../../../Runtime/libresolv.so.2\n' +
  82. 'sgx.trusted_files.librt = file:../../../../../Runtime/librt.so.1\n')
  83. lib_list = parse_libs(bin_path)
  84. for lib_name, lib_path in lib_list:
  85. print('lib name: ' + lib_name)
  86. print('lib path: ' + lib_path)
  87. mf.write('sgx.trusted_files.' + lib_name + ' = file:' + lib_path + '\n')
  88. mf.write('\n')
  89. mf.close()
  90. if __name__ == '__main__':
  91. if len(sys.argv) != 4:
  92. print('Usage: gen_manifest [application name] [actual binary name] [path to graphene]')
  93. print(' e.g. gen_manifest apache httpd /home/me/graphene')
  94. exit(1)
  95. app_name = sys.argv[1]
  96. bin_name = sys.argv[2]
  97. g_path = sys.argv[3]
  98. gen_manifest(app_name, bin_name, g_path)