#!/usr/bin/env python3 # libvirt 'run' programs locally script # Copyright (C) 2012-2021 Red Hat, Inc. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, see # . #---------------------------------------------------------------------- # # With this script you can run libvirt programs without needing to # install them first. You just have to do for example: # # ./run virsh [args ...] # # Note that this runs the locally compiled copy of virsh which # is usually want you want. # # You can also run the C programs under valgrind like this: # # ./run valgrind [valgrind opts...] ./program # # or under gdb: # # ./run gdb --args ./program # # This also works with sudo (eg. if you need root access for libvirt): # # sudo ./run virsh list --all # #---------------------------------------------------------------------- import os import os.path import random import sys # Function to intelligently prepend a path to an environment variable. # See https://stackoverflow.com/a/9631350 def prepend(env, varname, extradir): if varname in os.environ: env[varname] = extradir + ":" + env[varname] else: env[varname] = extradir here = "@abs_builddir@" if len(sys.argv) < 2: print("syntax: %s BINARY [ARGS...]" % sys.argv[0], file=sys.stderr) sys.exit(1) prog = sys.argv[1] args = sys.argv[1:] env = os.environ prepend(env, "LD_LIBRARY_PATH", os.path.join(here, "src")) prepend(env, "PKG_CONFIG_PATH", os.path.join(here, "src")) prepend(env, "PATH", os.path.join(here, "tools")) # Ensure that any 3rd party apps using libvirt.so from the build tree get # files resolved to the build/source tree too. Typically useful for language # bindings running tests against non-installed libvirt. env["LIBVIRT_DIR_OVERRIDE"] = "1" # This is a cheap way to find some use-after-free and uninitialized # read problems when using glibc. env["MALLOC_PERTURB_"] = "%d" % random.randint(1, 255) # Run the program. os.execvpe(prog, args, env)