diff --git a/src/app/setcap-helper.sh b/src/app/setcap-helper.sh new file mode 100644 index 0000000..c9a967d --- /dev/null +++ b/src/app/setcap-helper.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Simple post-install script to setcap the metrics helper + +setcap="${1?SETCAP_UNSET}" +bindir="${2?BINDIR_UNSET}" +exe="${3?EXE_UNSET}" +exe_fullpath="$MESON_INSTALL_DESTDIR_PREFIX/$bindir/$exe" + +euid=$(id -u) +if [ $euid != "0" ]; then + echo "Warning: installed by unprivileged user, could not setcap $exe_fullpath" >&2 + exit 0 +fi +if ! capsh --supports=CAP_PERFMON; then + echo "Warning: build environment does not support CAP_PERFMON, you must setuid root on this platform" >&2 + exit 0 +fi + +"$setcap" cap_perfmon=+ep "$exe_fullpath" diff --git a/src/meson.build b/src/meson.build index 3c8f6ab..2bd25cb 100644 --- a/src/meson.build +++ b/src/meson.build @@ -257,14 +257,24 @@ endif if get_option('mango_intel_stats').enabled() and is_unixy drm_dep = cc.find_library('libdrm') - warning('The mango_intel_stats binary must have CAP_PERFMON or setuid to be useful') - warning('You must do this manually post-install: setcap cap_perfmon=+ep /path/to/mango_intel_stats') + setcap = find_program('setcap', '/usr/sbin/setcap', '/sbin/setcap', required : false) mangointel = executable( 'mango_intel_stats', files('app/intel.cpp'), install : true, dependencies : [drm_dep, json_dep], ) + if setcap.found() + meson.add_install_script('app/setcap-helper.sh', + setcap.full_path(), + get_option('bindir'), + mangointel.name(), + ) + else + warning('The mango_intel_stats binary must have CAP_PERFMON or setuid to be useful') + warning('Install setcap to have CAP_PERFMON set automatically, or') + warning('You must do this manually post-install: setcap cap_perfmon=+ep /path/to/mango_intel_stats') + endif endif if get_option('mangoapp_layer')