2021-03-18 20:51:49 +03:00
#!/usr/bin/env python3
2012-09-14 13:08:54 +04:00
# libvirt 'run' programs locally script
2021-03-18 20:51:49 +03:00
# Copyright (C) 2012-2021 Red Hat, Inc.
2012-09-14 13:08:54 +04:00
#
2013-02-23 04:10:48 +04:00
# 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.
2012-09-14 13:08:54 +04:00
#
2013-02-23 04:10:48 +04:00
# This library is distributed in the hope that it will be useful,
2012-09-14 13:08:54 +04:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2013-02-23 04:10:48 +04:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
2012-09-14 13:08:54 +04:00
#
2013-02-23 04:10:48 +04:00
# You should have received a copy of the GNU Lesser General Public
# License along with this library; If not, see
# <http://www.gnu.org/licenses/>.
2012-09-14 13:08:54 +04:00
#----------------------------------------------------------------------
#
# With this script you can run libvirt programs without needing to
# install them first. You just have to do for example:
#
2020-01-16 20:15:42 +03:00
# ./run virsh [args ...]
2012-09-14 13:08:54 +04:00
#
2020-01-16 20:15:42 +03:00
# Note that this runs the locally compiled copy of virsh which
# is usually want you want.
2012-09-14 13:08:54 +04:00
#
# 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):
#
2020-01-16 20:15:42 +03:00
# sudo ./run virsh list --all
2012-09-14 13:08:54 +04:00
#
#----------------------------------------------------------------------
2021-03-18 20:51:49 +03:00
import os
import os.path
import random
import sys
2020-01-16 20:15:41 +03:00
# Function to intelligently prepend a path to an environment variable.
2020-08-26 01:44:00 +03:00
# See https://stackoverflow.com/a/9631350
2021-03-18 20:51:49 +03:00
def prepend(env, varname, extradir):
if varname in os.environ:
env[varname] = extradir + ":" + env[varname]
else:
env[varname] = extradir
here = "@abs_builddir@"
2020-01-16 20:15:41 +03:00
2021-03-18 20:51:49 +03:00
if len(sys.argv) < 2:
print("syntax: %s BINARY [ARGS...]" % sys.argv[0], file=sys.stderr)
sys.exit(1)
2012-09-14 13:08:54 +04:00
2021-03-18 20:51:49 +03:00
prog = sys.argv[1]
args = sys.argv[1:]
env = os.environ
2012-09-14 13:08:54 +04:00
2014-06-26 14:53:20 +04:00
2021-03-18 20:51:49 +03:00
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"))
2020-01-16 20:15:42 +03:00
2019-08-29 13:52:08 +03:00
# 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.
2021-03-18 20:51:49 +03:00
env["LIBVIRT_DIR_OVERRIDE"] = "1"
2019-08-29 13:52:08 +03:00
2012-09-14 13:08:54 +04:00
# This is a cheap way to find some use-after-free and uninitialized
# read problems when using glibc.
2021-03-18 20:51:49 +03:00
env["MALLOC_PERTURB_"] = "%d" % random.randint(1, 255)
2012-09-14 13:08:54 +04:00
# Run the program.
2021-03-18 20:51:49 +03:00
os.execvpe(prog, args, env)