cloexec.c (a5b0153c880c2775bf5bdd78306f0a47e860ea04) | cloexec.c (f6edb53c4993ffe92ce521fb449d1c146cea6ec2) |
---|---|
1#include <sched.h> |
|
1#include "util.h" 2#include "../perf.h" 3#include "cloexec.h" 4#include "asm/bug.h" 5 6static unsigned long flag = PERF_FLAG_FD_CLOEXEC; 7 8static int perf_flag_probe(void) 9{ 10 /* use 'safest' configuration as used in perf_evsel__fallback() */ 11 struct perf_event_attr attr = { 12 .type = PERF_TYPE_SOFTWARE, 13 .config = PERF_COUNT_SW_CPU_CLOCK, 14 .exclude_kernel = 1, 15 }; 16 int fd; 17 int err; | 2#include "util.h" 3#include "../perf.h" 4#include "cloexec.h" 5#include "asm/bug.h" 6 7static unsigned long flag = PERF_FLAG_FD_CLOEXEC; 8 9static int perf_flag_probe(void) 10{ 11 /* use 'safest' configuration as used in perf_evsel__fallback() */ 12 struct perf_event_attr attr = { 13 .type = PERF_TYPE_SOFTWARE, 14 .config = PERF_COUNT_SW_CPU_CLOCK, 15 .exclude_kernel = 1, 16 }; 17 int fd; 18 int err; |
19 int cpu; 20 pid_t pid = -1; |
|
18 | 21 |
19 /* check cloexec flag */ 20 fd = sys_perf_event_open(&attr, 0, -1, -1, 21 PERF_FLAG_FD_CLOEXEC); | 22 cpu = sched_getcpu(); 23 if (cpu < 0) 24 cpu = 0; 25 26 while (1) { 27 /* check cloexec flag */ 28 fd = sys_perf_event_open(&attr, pid, cpu, -1, 29 PERF_FLAG_FD_CLOEXEC); 30 if (fd < 0 && pid == -1 && errno == EACCES) { 31 pid = 0; 32 continue; 33 } 34 break; 35 } |
22 err = errno; 23 24 if (fd >= 0) { 25 close(fd); 26 return 1; 27 } 28 29 WARN_ONCE(err != EINVAL && err != EBUSY, 30 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n", 31 err, strerror(err)); 32 33 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */ | 36 err = errno; 37 38 if (fd >= 0) { 39 close(fd); 40 return 1; 41 } 42 43 WARN_ONCE(err != EINVAL && err != EBUSY, 44 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n", 45 err, strerror(err)); 46 47 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */ |
34 fd = sys_perf_event_open(&attr, 0, -1, -1, 0); | 48 fd = sys_perf_event_open(&attr, pid, cpu, -1, 0); |
35 err = errno; 36 37 if (WARN_ONCE(fd < 0 && err != EBUSY, 38 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n", 39 err, strerror(err))) 40 return -1; 41 42 close(fd); --- 16 unchanged lines hidden --- | 49 err = errno; 50 51 if (WARN_ONCE(fd < 0 && err != EBUSY, 52 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n", 53 err, strerror(err))) 54 return -1; 55 56 close(fd); --- 16 unchanged lines hidden --- |