diff options
-rw-r--r-- | tools/perf/util/Build | 2 | ||||
-rw-r--r-- | tools/perf/util/cap.c | 29 | ||||
-rw-r--r-- | tools/perf/util/cap.h | 27 | ||||
-rw-r--r-- | tools/perf/util/event.h | 1 | ||||
-rw-r--r-- | tools/perf/util/python-ext-sources | 1 | ||||
-rw-r--r-- | tools/perf/util/setup.py | 2 | ||||
-rw-r--r-- | tools/perf/util/util.c | 9 |
7 files changed, 71 insertions, 0 deletions
diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 7abf05131889..7cda749059a9 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build | |||
@@ -148,6 +148,8 @@ perf-$(CONFIG_ZLIB) += zlib.o | |||
148 | perf-$(CONFIG_LZMA) += lzma.o | 148 | perf-$(CONFIG_LZMA) += lzma.o |
149 | perf-$(CONFIG_ZSTD) += zstd.o | 149 | perf-$(CONFIG_ZSTD) += zstd.o |
150 | 150 | ||
151 | perf-$(CONFIG_LIBCAP) += cap.o | ||
152 | |||
151 | perf-y += demangle-java.o | 153 | perf-y += demangle-java.o |
152 | perf-y += demangle-rust.o | 154 | perf-y += demangle-rust.o |
153 | 155 | ||
diff --git a/tools/perf/util/cap.c b/tools/perf/util/cap.c new file mode 100644 index 000000000000..c3ba841bbf37 --- /dev/null +++ b/tools/perf/util/cap.c | |||
@@ -0,0 +1,29 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * Capability utilities | ||
4 | */ | ||
5 | |||
6 | #ifdef HAVE_LIBCAP_SUPPORT | ||
7 | |||
8 | #include "cap.h" | ||
9 | #include <stdbool.h> | ||
10 | #include <sys/capability.h> | ||
11 | |||
12 | bool perf_cap__capable(cap_value_t cap) | ||
13 | { | ||
14 | cap_flag_value_t val; | ||
15 | cap_t caps = cap_get_proc(); | ||
16 | |||
17 | if (!caps) | ||
18 | return false; | ||
19 | |||
20 | if (cap_get_flag(caps, cap, CAP_EFFECTIVE, &val) != 0) | ||
21 | val = CAP_CLEAR; | ||
22 | |||
23 | if (cap_free(caps) != 0) | ||
24 | return false; | ||
25 | |||
26 | return val == CAP_SET; | ||
27 | } | ||
28 | |||
29 | #endif /* HAVE_LIBCAP_SUPPORT */ | ||
diff --git a/tools/perf/util/cap.h b/tools/perf/util/cap.h new file mode 100644 index 000000000000..10af94e473da --- /dev/null +++ b/tools/perf/util/cap.h | |||
@@ -0,0 +1,27 @@ | |||
1 | /* SPDX-License-Identifier: GPL-2.0 */ | ||
2 | #ifndef __PERF_CAP_H | ||
3 | #define __PERF_CAP_H | ||
4 | |||
5 | #include <stdbool.h> | ||
6 | #include <linux/capability.h> | ||
7 | #include <linux/compiler.h> | ||
8 | |||
9 | #ifdef HAVE_LIBCAP_SUPPORT | ||
10 | |||
11 | #include <sys/capability.h> | ||
12 | |||
13 | bool perf_cap__capable(cap_value_t cap); | ||
14 | |||
15 | #else | ||
16 | |||
17 | #include <unistd.h> | ||
18 | #include <sys/types.h> | ||
19 | |||
20 | static inline bool perf_cap__capable(int cap __maybe_unused) | ||
21 | { | ||
22 | return geteuid() == 0; | ||
23 | } | ||
24 | |||
25 | #endif /* HAVE_LIBCAP_SUPPORT */ | ||
26 | |||
27 | #endif /* __PERF_CAP_H */ | ||
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h index 70841d115349..0e164e8ae28d 100644 --- a/tools/perf/util/event.h +++ b/tools/perf/util/event.h | |||
@@ -851,6 +851,7 @@ void cpu_map_data__synthesize(struct cpu_map_data *data, struct perf_cpu_map *m | |||
851 | void event_attr_init(struct perf_event_attr *attr); | 851 | void event_attr_init(struct perf_event_attr *attr); |
852 | 852 | ||
853 | int perf_event_paranoid(void); | 853 | int perf_event_paranoid(void); |
854 | bool perf_event_paranoid_check(int max_level); | ||
854 | 855 | ||
855 | extern int sysctl_perf_event_max_stack; | 856 | extern int sysctl_perf_event_max_stack; |
856 | extern int sysctl_perf_event_max_contexts_per_stack; | 857 | extern int sysctl_perf_event_max_contexts_per_stack; |
diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources index 235bd9803390..c6dd478956f1 100644 --- a/tools/perf/util/python-ext-sources +++ b/tools/perf/util/python-ext-sources | |||
@@ -7,6 +7,7 @@ | |||
7 | 7 | ||
8 | util/python.c | 8 | util/python.c |
9 | ../lib/ctype.c | 9 | ../lib/ctype.c |
10 | util/cap.c | ||
10 | util/evlist.c | 11 | util/evlist.c |
11 | util/evsel.c | 12 | util/evsel.c |
12 | util/cpumap.c | 13 | util/cpumap.c |
diff --git a/tools/perf/util/setup.py b/tools/perf/util/setup.py index d48f9cd58964..aa344a163eaf 100644 --- a/tools/perf/util/setup.py +++ b/tools/perf/util/setup.py | |||
@@ -59,6 +59,8 @@ ext_sources = list(map(lambda x: '%s/%s' % (src_perf, x) , ext_sources)) | |||
59 | extra_libraries = [] | 59 | extra_libraries = [] |
60 | if '-DHAVE_LIBNUMA_SUPPORT' in cflags: | 60 | if '-DHAVE_LIBNUMA_SUPPORT' in cflags: |
61 | extra_libraries = [ 'numa' ] | 61 | extra_libraries = [ 'numa' ] |
62 | if '-DHAVE_LIBCAP_SUPPORT' in cflags: | ||
63 | extra_libraries += [ 'cap' ] | ||
62 | 64 | ||
63 | perf = Extension('perf', | 65 | perf = Extension('perf', |
64 | sources = ext_sources, | 66 | sources = ext_sources, |
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 9c3c97697387..6fd130a5d8f2 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c | |||
@@ -16,10 +16,12 @@ | |||
16 | #include <string.h> | 16 | #include <string.h> |
17 | #include <errno.h> | 17 | #include <errno.h> |
18 | #include <limits.h> | 18 | #include <limits.h> |
19 | #include <linux/capability.h> | ||
19 | #include <linux/kernel.h> | 20 | #include <linux/kernel.h> |
20 | #include <linux/log2.h> | 21 | #include <linux/log2.h> |
21 | #include <linux/time64.h> | 22 | #include <linux/time64.h> |
22 | #include <unistd.h> | 23 | #include <unistd.h> |
24 | #include "cap.h" | ||
23 | #include "strlist.h" | 25 | #include "strlist.h" |
24 | #include "string2.h" | 26 | #include "string2.h" |
25 | 27 | ||
@@ -403,6 +405,13 @@ int perf_event_paranoid(void) | |||
403 | 405 | ||
404 | return value; | 406 | return value; |
405 | } | 407 | } |
408 | |||
409 | bool perf_event_paranoid_check(int max_level) | ||
410 | { | ||
411 | return perf_cap__capable(CAP_SYS_ADMIN) || | ||
412 | perf_event_paranoid() <= max_level; | ||
413 | } | ||
414 | |||
406 | static int | 415 | static int |
407 | fetch_ubuntu_kernel_version(unsigned int *puint) | 416 | fetch_ubuntu_kernel_version(unsigned int *puint) |
408 | { | 417 | { |