From fcb318b9c5158ab66ca1d5797b197237eae7d5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Wed, 23 Jul 2025 18:18:52 +0200 Subject: [PATCH] pwtest: is_debugger_attached(): rework test Check if the "TracerPid" field in `/proc/self/status` is non-zero. This does not need libcap, and does not depend on capabilities, and it also works under qemu user emulation. --- meson.build | 3 --- test/meson.build | 1 - test/pwtest.c | 43 +++++++++++-------------------------------- 3 files changed, 11 insertions(+), 36 deletions(-) diff --git a/meson.build b/meson.build index 307f1155c..0a01a8f37 100644 --- a/meson.build +++ b/meson.build @@ -381,9 +381,6 @@ libusb_dep = dependency('libusb-1.0', required : get_option('libusb')) summary({'libusb (Bluetooth quirks)': libusb_dep.found()}, bool_yn: true, section: 'Backend') cdata.set('HAVE_LIBUSB', libusb_dep.found()) -cap_lib = dependency('libcap', required : false) -cdata.set('HAVE_LIBCAP', cap_lib.found()) - glib2_dep = dependency('glib-2.0', required : get_option('flatpak')) summary({'GLib-2.0 (Flatpak support)': glib2_dep.found()}, bool_yn: true, section: 'Misc dependencies') flatpak_support = glib2_dep.found() diff --git a/test/meson.build b/test/meson.build index b4f24cf1b..c5671149d 100644 --- a/test/meson.build +++ b/test/meson.build @@ -9,7 +9,6 @@ pwtest_deps = [ pipewire_dep, mathlib, dl_lib, - cap_lib, epoll_shim_dep ] diff --git a/test/pwtest.c b/test/pwtest.c index 7094a59e1..89a385f53 100644 --- a/test/pwtest.c +++ b/test/pwtest.c @@ -19,9 +19,6 @@ #ifdef HAVE_PIDFD_OPEN #include #endif -#ifdef HAVE_LIBCAP -#include -#endif #include #include #include @@ -33,6 +30,7 @@ #include #include "spa/utils/ansi.h" +#include "spa/utils/cleanup.h" #include "spa/utils/string.h" #include "spa/utils/defs.h" #include "spa/utils/list.h" @@ -1298,39 +1296,20 @@ static void list_tests(struct pwtest_context *ctx) static bool is_debugger_attached(void) { - bool rc = false; -#ifdef HAVE_LIBCAP - int status; - int pid = fork(); + spa_autofree char *line = NULL; + size_t length = 0; - if (pid == -1) - return 0; + spa_autoptr(FILE) f = fopen("/proc/self/status", "re"); + if (!f) + return false; - if (pid == 0) { - int ppid = getppid(); - cap_t caps = cap_get_pid(ppid); - cap_flag_value_t cap_val; - - if (cap_get_flag(caps, CAP_SYS_PTRACE, CAP_EFFECTIVE, &cap_val) == -1 || - cap_val != CAP_SET) - _exit(false); - - if (ptrace(PTRACE_ATTACH, ppid, NULL, 0) == 0) { - waitpid(ppid, NULL, 0); - ptrace(PTRACE_CONT, ppid, NULL, 0); - ptrace(PTRACE_DETACH, ppid, NULL, 0); - rc = false; - } else { - rc = true; - } - _exit(rc); - } else { - waitpid(pid, &status, 0); - rc = WEXITSTATUS(status); + while (getline(&line, &length, f) >= 0) { + unsigned int tracer_pid; + if (sscanf(line, "TracerPid: %u", &tracer_pid) == 1) + return tracer_pid > 0; } -#endif - return !!rc; + return false; } static void usage(FILE *fp, const char *progname)