1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <unistd.h>
3 #include <string.h>
4 #include <stdbool.h>
5
6 #include "bpf_util.h"
7 #include "bpftool_helpers.h"
8
9 #define BPFTOOL_PATH_MAX_LEN 64
10 #define BPFTOOL_FULL_CMD_MAX_LEN 512
11
12 #define BPFTOOL_DEFAULT_PATH "tools/sbin/bpftool"
13
detect_bpftool_path(char * buffer,size_t size)14 static int detect_bpftool_path(char *buffer, size_t size)
15 {
16 char tmp[BPFTOOL_PATH_MAX_LEN];
17 const char *env_path;
18
19 /* First, check if BPFTOOL environment variable is set */
20 env_path = getenv("BPFTOOL");
21 if (env_path && access(env_path, X_OK) == 0) {
22 strscpy(buffer, env_path, size);
23 return 0;
24 } else if (env_path) {
25 fprintf(stderr, "bpftool '%s' doesn't exist or is not executable\n", env_path);
26 return 1;
27 }
28
29 /* Check default bpftool location (will work if we are running the
30 * default flavor of test_progs)
31 */
32 snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH);
33 if (access(tmp, X_OK) == 0) {
34 strscpy(buffer, tmp, size);
35 return 0;
36 }
37
38 /* Check alternate bpftool location (will work if we are running a
39 * specific flavor of test_progs, e.g. cpuv4 or no_alu32)
40 */
41 snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
42 if (access(tmp, X_OK) == 0) {
43 strscpy(buffer, tmp, size);
44 return 0;
45 }
46
47 fprintf(stderr, "Failed to detect bpftool path, use BPFTOOL env var to override\n");
48 return 1;
49 }
50
run_command(char * args,char * output_buf,size_t output_max_len)51 static int run_command(char *args, char *output_buf, size_t output_max_len)
52 {
53 static char bpftool_path[BPFTOOL_PATH_MAX_LEN] = {0};
54 bool suppress_output = !(output_buf && output_max_len);
55 char command[BPFTOOL_FULL_CMD_MAX_LEN];
56 FILE *f;
57 int ret;
58
59 /* Detect and cache bpftool binary location */
60 if (bpftool_path[0] == 0 && detect_bpftool_path(bpftool_path, sizeof(bpftool_path)))
61 return 1;
62
63 ret = snprintf(command, BPFTOOL_FULL_CMD_MAX_LEN, "%s %s%s",
64 bpftool_path, args,
65 suppress_output ? " > /dev/null 2>&1" : "");
66
67 f = popen(command, "r");
68 if (!f)
69 return 1;
70
71 if (!suppress_output)
72 fread(output_buf, 1, output_max_len, f);
73 ret = pclose(f);
74
75 return ret;
76 }
77
run_bpftool_command(char * args)78 int run_bpftool_command(char *args)
79 {
80 return run_command(args, NULL, 0);
81 }
82
get_bpftool_command_output(char * args,char * output_buf,size_t output_max_len)83 int get_bpftool_command_output(char *args, char *output_buf, size_t output_max_len)
84 {
85 return run_command(args, output_buf, output_max_len);
86 }
87
88