Skip to content

Commit 1162f84

Browse files
Hechao Liborkmann
authored andcommitted
bpf: Print error message for bpftool cgroup show
Currently, when bpftool cgroup show <path> has an error, no error message is printed. This is confusing because the user may think the result is empty. Before the change: $ bpftool cgroup show /sys/fs/cgroup ID AttachType AttachFlags Name $ echo $? 255 After the change: $ ./bpftool cgroup show /sys/fs/cgroup Error: can't query bpf programs attached to /sys/fs/cgroup: Operation not permitted v2: Rename check_query_cgroup_progs to cgroup_has_attached_progs Signed-off-by: Hechao Li <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8ab9da5 commit 1162f84

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

tools/bpf/bpftool/cgroup.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
117117
return prog_cnt;
118118
}
119119

120+
static int cgroup_has_attached_progs(int cgroup_fd)
121+
{
122+
enum bpf_attach_type type;
123+
bool no_prog = true;
124+
125+
for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
126+
int count = count_attached_bpf_progs(cgroup_fd, type);
127+
128+
if (count < 0 && errno != EINVAL)
129+
return -1;
130+
131+
if (count > 0) {
132+
no_prog = false;
133+
break;
134+
}
135+
}
136+
137+
return no_prog ? 0 : 1;
138+
}
120139
static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
121140
int level)
122141
{
@@ -161,6 +180,7 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
161180
static int do_show(int argc, char **argv)
162181
{
163182
enum bpf_attach_type type;
183+
int has_attached_progs;
164184
const char *path;
165185
int cgroup_fd;
166186
int ret = -1;
@@ -192,6 +212,16 @@ static int do_show(int argc, char **argv)
192212
goto exit;
193213
}
194214

215+
has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
216+
if (has_attached_progs < 0) {
217+
p_err("can't query bpf programs attached to %s: %s",
218+
path, strerror(errno));
219+
goto exit_cgroup;
220+
} else if (!has_attached_progs) {
221+
ret = 0;
222+
goto exit_cgroup;
223+
}
224+
195225
if (json_output)
196226
jsonw_start_array(json_wtr);
197227
else
@@ -212,6 +242,7 @@ static int do_show(int argc, char **argv)
212242
if (json_output)
213243
jsonw_end_array(json_wtr);
214244

245+
exit_cgroup:
215246
close(cgroup_fd);
216247
exit:
217248
return ret;
@@ -228,7 +259,7 @@ static int do_show_tree_fn(const char *fpath, const struct stat *sb,
228259
int typeflag, struct FTW *ftw)
229260
{
230261
enum bpf_attach_type type;
231-
bool skip = true;
262+
int has_attached_progs;
232263
int cgroup_fd;
233264

234265
if (typeflag != FTW_D)
@@ -240,22 +271,13 @@ static int do_show_tree_fn(const char *fpath, const struct stat *sb,
240271
return SHOW_TREE_FN_ERR;
241272
}
242273

243-
for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
244-
int count = count_attached_bpf_progs(cgroup_fd, type);
245-
246-
if (count < 0 && errno != EINVAL) {
247-
p_err("can't query bpf programs attached to %s: %s",
248-
fpath, strerror(errno));
249-
close(cgroup_fd);
250-
return SHOW_TREE_FN_ERR;
251-
}
252-
if (count > 0) {
253-
skip = false;
254-
break;
255-
}
256-
}
257-
258-
if (skip) {
274+
has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
275+
if (has_attached_progs < 0) {
276+
p_err("can't query bpf programs attached to %s: %s",
277+
fpath, strerror(errno));
278+
close(cgroup_fd);
279+
return SHOW_TREE_FN_ERR;
280+
} else if (!has_attached_progs) {
259281
close(cgroup_fd);
260282
return 0;
261283
}

0 commit comments

Comments
 (0)