1
0
mirror of https://git.yoctoproject.org/poky synced 2026-05-09 05:29:32 +00:00

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>
This commit is contained in:
Ross Burton
2018-09-21 16:11:32 +01:00
committed by Richard Purdie
parent 1e534573c7
commit a785fdbe62
+29 -34
View File
@@ -116,9 +116,9 @@ def check_tun():
raise RunQemuError("TUN control device %s is not writable, please fix (e.g. sudo chmod 666 %s)" % (dev_tun, dev_tun)) raise RunQemuError("TUN control device %s is not writable, please fix (e.g. sudo chmod 666 %s)" % (dev_tun, dev_tun))
def check_libgl(qemu_bin): def check_libgl(qemu_bin):
cmd = 'ldd %s' % qemu_bin cmd = ('ldd', qemu_bin)
logger.debug('Running %s...' % cmd) logger.debug('Running %s...' % str(cmd))
need_gl = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') need_gl = subprocess.check_output(cmd).decode('utf-8')
if re.search('libGLU', need_gl): if re.search('libGLU', need_gl):
# We can't run without a libGL.so # We can't run without a libGL.so
libgl = False libgl = False
@@ -400,7 +400,7 @@ class BaseConfig(object):
cmd = 'MACHINE=%s bitbake -e' % arg cmd = 'MACHINE=%s bitbake -e' % arg
logger.info('Running %s...' % cmd) logger.info('Running %s...' % cmd)
self.bitbake_e = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') self.bitbake_e = subprocess.check_output(cmd, shell=True).decode('utf-8')
# bitbake -e doesn't report invalid MACHINE as an error, so # bitbake -e doesn't report invalid MACHINE as an error, so
# let's check DEPLOY_DIR_IMAGE to make sure that it is a valid # let's check DEPLOY_DIR_IMAGE to make sure that it is a valid
# MACHINE. # MACHINE.
@@ -843,10 +843,7 @@ class BaseConfig(object):
self.nfs_server = '192.168.7.1' self.nfs_server = '192.168.7.1'
# Figure out a new nfs_instance to allow multiple qemus running. # Figure out a new nfs_instance to allow multiple qemus running.
# CentOS 7.1's ps doesn't print full command line without "ww" ps = subprocess.check_output(("ps", "auxww")).decode('utf-8')
# when invoke by subprocess.Popen().
cmd = "ps auxww"
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
pattern = '/bin/unfsd .* -i .*\.pid -e .*/exports([0-9]+) ' pattern = '/bin/unfsd .* -i .*\.pid -e .*/exports([0-9]+) '
all_instances = re.findall(pattern, ps, re.M) all_instances = re.findall(pattern, ps, re.M)
if all_instances: if all_instances:
@@ -886,17 +883,17 @@ class BaseConfig(object):
if not src: if not src:
raise RunQemuError("No NFS_DIR is set, and can't find %s or %s to extract" % (src1, src2)) raise RunQemuError("No NFS_DIR is set, and can't find %s or %s to extract" % (src1, src2))
logger.info('NFS_DIR not found, extracting %s to %s' % (src, dest)) logger.info('NFS_DIR not found, extracting %s to %s' % (src, dest))
cmd = 'runqemu-extract-sdk %s %s' % (src, dest) cmd = ('runqemu-extract-sdk', src, dest)
logger.info('Running %s...' % cmd) logger.info('Running %s...' % str(cmd))
if subprocess.call(cmd, shell=True) != 0: if subprocess.call(cmd) != 0:
raise RunQemuError('Failed to run %s' % cmd) raise RunQemuError('Failed to run %s' % cmd)
self.clean_nfs_dir = True self.clean_nfs_dir = True
self.rootfs = dest self.rootfs = dest
# Start the userspace NFS server # Start the userspace NFS server
cmd = 'runqemu-export-rootfs start %s' % self.rootfs cmd = ('runqemu-export-rootfs', 'start', self.rootfs)
logger.info('Running %s...' % cmd) logger.info('Running %s...' % str(cmd))
if subprocess.call(cmd, shell=True) != 0: if subprocess.call(cmd) != 0:
raise RunQemuError('Failed to run %s' % cmd) raise RunQemuError('Failed to run %s' % cmd)
self.nfs_running = True self.nfs_running = True
@@ -962,9 +959,9 @@ class BaseConfig(object):
except FileExistsError: except FileExistsError:
pass pass
cmd = '%s link' % ip cmd = (ip, 'link')
logger.debug('Running %s...' % cmd) logger.debug('Running %s...' % str(cmd))
ip_link = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') ip_link = subprocess.check_output(cmd).decode('utf-8')
# Matches line like: 6: tap0: <foo> # Matches line like: 6: tap0: <foo>
possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M) possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M)
tap = "" tap = ""
@@ -989,8 +986,8 @@ class BaseConfig(object):
gid = os.getgid() gid = os.getgid()
uid = os.getuid() uid = os.getuid()
logger.info("Setting up tap interface under sudo") logger.info("Setting up tap interface under sudo")
cmd = 'sudo %s %s %s %s' % (self.qemuifup, uid, gid, self.bindir_native) cmd = ('sudo', self.qemuifup, str(uid), str(gid), self.bindir_native)
tap = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8').rstrip('\n') tap = subprocess.check_output(cmd).decode('utf-8').strip()
lockfile = os.path.join(lockdir, tap) lockfile = os.path.join(lockdir, tap)
self.lock = lockfile + '.lock' self.lock = lockfile + '.lock'
self.acquire_lock() self.acquire_lock()
@@ -1025,7 +1022,7 @@ class BaseConfig(object):
if self.get('QB_NET') == 'none': if self.get('QB_NET') == 'none':
return return
if sys.stdin.isatty(): if sys.stdin.isatty():
self.saved_stty = subprocess.check_output("stty -g", shell=True).decode('utf-8') self.saved_stty = subprocess.check_output(("stty", "-g")).decode('utf-8').strip()
self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device
if self.slirp_enabled: if self.slirp_enabled:
self.setup_slirp() self.setup_slirp()
@@ -1139,7 +1136,7 @@ class BaseConfig(object):
if not qemu_system: if not qemu_system:
raise RunQemuError("Failed to boot, QB_SYSTEM_NAME is NULL!") raise RunQemuError("Failed to boot, QB_SYSTEM_NAME is NULL!")
qemu_bin = '%s/%s' % (self.bindir_native, qemu_system) qemu_bin = os.path.join(self.bindir_native, qemu_system)
# It is possible to have qemu-native in ASSUME_PROVIDED, and it won't # It is possible to have qemu-native in ASSUME_PROVIDED, and it won't
# find QEMU in sysroot, it needs to use host's qemu. # find QEMU in sysroot, it needs to use host's qemu.
@@ -1177,7 +1174,7 @@ class BaseConfig(object):
if self.serialconsole: if self.serialconsole:
if sys.stdin.isatty(): if sys.stdin.isatty():
subprocess.check_call("stty intr ^]", shell=True) subprocess.check_call(("stty", "intr", "^]"))
logger.info("Interrupt character is '^]'") logger.info("Interrupt character is '^]'")
first_serial = "" first_serial = ""
@@ -1241,20 +1238,19 @@ class BaseConfig(object):
logger.info("Cleaning up") logger.info("Cleaning up")
if self.cleantap: if self.cleantap:
cmd = 'sudo %s %s %s' % (self.qemuifdown, self.tap, self.bindir_native) cmd = ('sudo', self.qemuifdown, self.tap, self.bindir_native)
logger.debug('Running %s' % cmd) logger.debug('Running %s' % str(cmd))
subprocess.check_call(cmd, shell=True) subprocess.check_call(cmd)
self.release_lock() self.release_lock()
if self.nfs_running: if self.nfs_running:
logger.info("Shutting down the userspace NFS server...") logger.info("Shutting down the userspace NFS server...")
cmd = "runqemu-export-rootfs stop %s" % self.rootfs cmd = ("runqemu-export-rootfs", "stop", self.rootfs)
logger.debug('Running %s' % cmd) logger.debug('Running %s' % str(cmd))
subprocess.check_call(cmd, shell=True) subprocess.check_call(cmd)
if self.saved_stty: if self.saved_stty:
cmd = "stty %s" % self.saved_stty subprocess.check_call(("stty", self.saved_stty))
subprocess.check_call(cmd, shell=True)
if self.clean_nfs_dir: if self.clean_nfs_dir:
logger.info('Removing %s' % self.rootfs) logger.info('Removing %s' % self.rootfs)
@@ -1296,10 +1292,9 @@ class BaseConfig(object):
if result and os.path.exists(result): if result and os.path.exists(result):
return result return result
cmd = 'bitbake qemu-helper-native -e' cmd = ('bitbake', 'qemu-helper-native', '-e')
logger.info('Running %s...' % cmd) logger.info('Running %s...' % str(cmd))
out = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) out = subprocess.check_output(cmd).decode('utf-8')
out = out.stdout.read().decode('utf-8')
match = re.search('^STAGING_BINDIR_NATIVE="(.*)"', out, re.M) match = re.search('^STAGING_BINDIR_NATIVE="(.*)"', out, re.M)
if match: if match: