From 5db1bc427fc28af5b7e7e452096660e1953a2727 Mon Sep 17 00:00:00 2001 From: Kurt Kartaltepe Date: Sat, 22 Apr 2023 15:15:55 -0700 Subject: [PATCH] Add setcap helper This will setcap the perf stats helper appropriately when installed by root. It wont fail if you try an unprivileged install, but it means the perf stats helper also wont work. Also check that we have the CAP_PERFMON capability in the kernel as this isnt supported on ubuntu 20. On this platform packagers should setuid as root. --- src/app/setcap-helper.sh | 19 +++++++++++++++++++ src/meson.build | 14 ++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/app/setcap-helper.sh 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')