diff options
author | Ross Burton <ross.burton@intel.com> | 2018-09-21 16:11:32 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2018-12-15 11:55:38 +0000 |
commit | a785fdbe62595af092e050c19e291596e84fb930 (patch) | |
tree | 7cd47422956493da0a76e1c1fc34501c1b8c2aa8 | |
parent | 1e534573c73104819f455b6816b5271a24bee09a (diff) | |
download | poky-a785fdbe62595af092e050c19e291596e84fb930.tar.gz |
runqemu: clean up subprocess usage
Where possible pass lists instead of strings, don't use a subshell, and call
check*() instead of using Popen directly.
(From OE-Core rev: d2374623444752af1ad748ed36b68ea58f629bf6)
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rwxr-xr-x | scripts/runqemu | 63 |
1 files changed, 29 insertions, 34 deletions
diff --git a/scripts/runqemu b/scripts/runqemu index f661892aec..c4a0ca811d 100755 --- a/scripts/runqemu +++ b/scripts/runqemu | |||
@@ -116,9 +116,9 @@ def check_tun(): | |||
116 | raise RunQemuError("TUN control device %s is not writable, please fix (e.g. sudo chmod 666 %s)" % (dev_tun, dev_tun)) | 116 | raise RunQemuError("TUN control device %s is not writable, please fix (e.g. sudo chmod 666 %s)" % (dev_tun, dev_tun)) |
117 | 117 | ||
118 | def check_libgl(qemu_bin): | 118 | def check_libgl(qemu_bin): |
119 | cmd = 'ldd %s' % qemu_bin | 119 | cmd = ('ldd', qemu_bin) |
120 | logger.debug('Running %s...' % cmd) | 120 | logger.debug('Running %s...' % str(cmd)) |
121 | need_gl = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') | 121 | need_gl = subprocess.check_output(cmd).decode('utf-8') |
122 | if re.search('libGLU', need_gl): | 122 | if re.search('libGLU', need_gl): |
123 | # We can't run without a libGL.so | 123 | # We can't run without a libGL.so |
124 | libgl = False | 124 | libgl = False |
@@ -400,7 +400,7 @@ class BaseConfig(object): | |||
400 | 400 | ||
401 | cmd = 'MACHINE=%s bitbake -e' % arg | 401 | cmd = 'MACHINE=%s bitbake -e' % arg |
402 | logger.info('Running %s...' % cmd) | 402 | logger.info('Running %s...' % cmd) |
403 | self.bitbake_e = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') | 403 | self.bitbake_e = subprocess.check_output(cmd, shell=True).decode('utf-8') |
404 | # bitbake -e doesn't report invalid MACHINE as an error, so | 404 | # bitbake -e doesn't report invalid MACHINE as an error, so |
405 | # let's check DEPLOY_DIR_IMAGE to make sure that it is a valid | 405 | # let's check DEPLOY_DIR_IMAGE to make sure that it is a valid |
406 | # MACHINE. | 406 | # MACHINE. |
@@ -843,10 +843,7 @@ class BaseConfig(object): | |||
843 | self.nfs_server = '192.168.7.1' | 843 | self.nfs_server = '192.168.7.1' |
844 | 844 | ||
845 | # Figure out a new nfs_instance to allow multiple qemus running. | 845 | # Figure out a new nfs_instance to allow multiple qemus running. |
846 | # CentOS 7.1's ps doesn't print full command line without "ww" | 846 | ps = subprocess.check_output(("ps", "auxww")).decode('utf-8') |
847 | # when invoke by subprocess.Popen(). | ||
848 | cmd = "ps auxww" | ||
849 | ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') | ||
850 | pattern = '/bin/unfsd .* -i .*\.pid -e .*/exports([0-9]+) ' | 847 | pattern = '/bin/unfsd .* -i .*\.pid -e .*/exports([0-9]+) ' |
851 | all_instances = re.findall(pattern, ps, re.M) | 848 | all_instances = re.findall(pattern, ps, re.M) |
852 | if all_instances: | 849 | if all_instances: |
@@ -886,17 +883,17 @@ class BaseConfig(object): | |||
886 | if not src: | 883 | if not src: |
887 | raise RunQemuError("No NFS_DIR is set, and can't find %s or %s to extract" % (src1, src2)) | 884 | raise RunQemuError("No NFS_DIR is set, and can't find %s or %s to extract" % (src1, src2)) |
888 | logger.info('NFS_DIR not found, extracting %s to %s' % (src, dest)) | 885 | logger.info('NFS_DIR not found, extracting %s to %s' % (src, dest)) |
889 | cmd = 'runqemu-extract-sdk %s %s' % (src, dest) | 886 | cmd = ('runqemu-extract-sdk', src, dest) |
890 | logger.info('Running %s...' % cmd) | 887 | logger.info('Running %s...' % str(cmd)) |
891 | if subprocess.call(cmd, shell=True) != 0: | 888 | if subprocess.call(cmd) != 0: |
892 | raise RunQemuError('Failed to run %s' % cmd) | 889 | raise RunQemuError('Failed to run %s' % cmd) |
893 | self.clean_nfs_dir = True | 890 | self.clean_nfs_dir = True |
894 | self.rootfs = dest | 891 | self.rootfs = dest |
895 | 892 | ||
896 | # Start the userspace NFS server | 893 | # Start the userspace NFS server |
897 | cmd = 'runqemu-export-rootfs start %s' % self.rootfs | 894 | cmd = ('runqemu-export-rootfs', 'start', self.rootfs) |
898 | logger.info('Running %s...' % cmd) | 895 | logger.info('Running %s...' % str(cmd)) |
899 | if subprocess.call(cmd, shell=True) != 0: | 896 | if subprocess.call(cmd) != 0: |
900 | raise RunQemuError('Failed to run %s' % cmd) | 897 | raise RunQemuError('Failed to run %s' % cmd) |
901 | 898 | ||
902 | self.nfs_running = True | 899 | self.nfs_running = True |
@@ -962,9 +959,9 @@ class BaseConfig(object): | |||
962 | except FileExistsError: | 959 | except FileExistsError: |
963 | pass | 960 | pass |
964 | 961 | ||
965 | cmd = '%s link' % ip | 962 | cmd = (ip, 'link') |
966 | logger.debug('Running %s...' % cmd) | 963 | logger.debug('Running %s...' % str(cmd)) |
967 | ip_link = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') | 964 | ip_link = subprocess.check_output(cmd).decode('utf-8') |
968 | # Matches line like: 6: tap0: <foo> | 965 | # Matches line like: 6: tap0: <foo> |
969 | possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M) | 966 | possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M) |
970 | tap = "" | 967 | tap = "" |
@@ -989,8 +986,8 @@ class BaseConfig(object): | |||
989 | gid = os.getgid() | 986 | gid = os.getgid() |
990 | uid = os.getuid() | 987 | uid = os.getuid() |
991 | logger.info("Setting up tap interface under sudo") | 988 | logger.info("Setting up tap interface under sudo") |
992 | cmd = 'sudo %s %s %s %s' % (self.qemuifup, uid, gid, self.bindir_native) | 989 | cmd = ('sudo', self.qemuifup, str(uid), str(gid), self.bindir_native) |
993 | tap = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8').rstrip('\n') | 990 | tap = subprocess.check_output(cmd).decode('utf-8').strip() |
994 | lockfile = os.path.join(lockdir, tap) | 991 | lockfile = os.path.join(lockdir, tap) |
995 | self.lock = lockfile + '.lock' | 992 | self.lock = lockfile + '.lock' |
996 | self.acquire_lock() | 993 | self.acquire_lock() |
@@ -1025,7 +1022,7 @@ class BaseConfig(object): | |||
1025 | if self.get('QB_NET') == 'none': | 1022 | if self.get('QB_NET') == 'none': |
1026 | return | 1023 | return |
1027 | if sys.stdin.isatty(): | 1024 | if sys.stdin.isatty(): |
1028 | self.saved_stty = subprocess.check_output("stty -g", shell=True).decode('utf-8') | 1025 | self.saved_stty = subprocess.check_output(("stty", "-g")).decode('utf-8').strip() |
1029 | self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device | 1026 | self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device |
1030 | if self.slirp_enabled: | 1027 | if self.slirp_enabled: |
1031 | self.setup_slirp() | 1028 | self.setup_slirp() |
@@ -1139,7 +1136,7 @@ class BaseConfig(object): | |||
1139 | if not qemu_system: | 1136 | if not qemu_system: |
1140 | raise RunQemuError("Failed to boot, QB_SYSTEM_NAME is NULL!") | 1137 | raise RunQemuError("Failed to boot, QB_SYSTEM_NAME is NULL!") |
1141 | 1138 | ||
1142 | qemu_bin = '%s/%s' % (self.bindir_native, qemu_system) | 1139 | qemu_bin = os.path.join(self.bindir_native, qemu_system) |
1143 | 1140 | ||
1144 | # It is possible to have qemu-native in ASSUME_PROVIDED, and it won't | 1141 | # It is possible to have qemu-native in ASSUME_PROVIDED, and it won't |
1145 | # find QEMU in sysroot, it needs to use host's qemu. | 1142 | # find QEMU in sysroot, it needs to use host's qemu. |
@@ -1177,7 +1174,7 @@ class BaseConfig(object): | |||
1177 | 1174 | ||
1178 | if self.serialconsole: | 1175 | if self.serialconsole: |
1179 | if sys.stdin.isatty(): | 1176 | if sys.stdin.isatty(): |
1180 | subprocess.check_call("stty intr ^]", shell=True) | 1177 | subprocess.check_call(("stty", "intr", "^]")) |
1181 | logger.info("Interrupt character is '^]'") | 1178 | logger.info("Interrupt character is '^]'") |
1182 | 1179 | ||
1183 | first_serial = "" | 1180 | first_serial = "" |
@@ -1241,20 +1238,19 @@ class BaseConfig(object): | |||
1241 | 1238 | ||
1242 | logger.info("Cleaning up") | 1239 | logger.info("Cleaning up") |
1243 | if self.cleantap: | 1240 | if self.cleantap: |
1244 | cmd = 'sudo %s %s %s' % (self.qemuifdown, self.tap, self.bindir_native) | 1241 | cmd = ('sudo', self.qemuifdown, self.tap, self.bindir_native) |
1245 | logger.debug('Running %s' % cmd) | 1242 | logger.debug('Running %s' % str(cmd)) |
1246 | subprocess.check_call(cmd, shell=True) | 1243 | subprocess.check_call(cmd) |
1247 | self.release_lock() | 1244 | self.release_lock() |
1248 | 1245 | ||
1249 | if self.nfs_running: | 1246 | if self.nfs_running: |
1250 | logger.info("Shutting down the userspace NFS server...") | 1247 | logger.info("Shutting down the userspace NFS server...") |
1251 | cmd = "runqemu-export-rootfs stop %s" % self.rootfs | 1248 | cmd = ("runqemu-export-rootfs", "stop", self.rootfs) |
1252 | logger.debug('Running %s' % cmd) | 1249 | logger.debug('Running %s' % str(cmd)) |
1253 | subprocess.check_call(cmd, shell=True) | 1250 | subprocess.check_call(cmd) |
1254 | 1251 | ||
1255 | if self.saved_stty: | 1252 | if self.saved_stty: |
1256 | cmd = "stty %s" % self.saved_stty | 1253 | subprocess.check_call(("stty", self.saved_stty)) |
1257 | subprocess.check_call(cmd, shell=True) | ||
1258 | 1254 | ||
1259 | if self.clean_nfs_dir: | 1255 | if self.clean_nfs_dir: |
1260 | logger.info('Removing %s' % self.rootfs) | 1256 | logger.info('Removing %s' % self.rootfs) |
@@ -1296,10 +1292,9 @@ class BaseConfig(object): | |||
1296 | if result and os.path.exists(result): | 1292 | if result and os.path.exists(result): |
1297 | return result | 1293 | return result |
1298 | 1294 | ||
1299 | cmd = 'bitbake qemu-helper-native -e' | 1295 | cmd = ('bitbake', 'qemu-helper-native', '-e') |
1300 | logger.info('Running %s...' % cmd) | 1296 | logger.info('Running %s...' % str(cmd)) |
1301 | out = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) | 1297 | out = subprocess.check_output(cmd).decode('utf-8') |
1302 | out = out.stdout.read().decode('utf-8') | ||
1303 | 1298 | ||
1304 | match = re.search('^STAGING_BINDIR_NATIVE="(.*)"', out, re.M) | 1299 | match = re.search('^STAGING_BINDIR_NATIVE="(.*)"', out, re.M) |
1305 | if match: | 1300 | if match: |