xref: /linux/tools/bpf/bpftool/cgroup.c (revision c39bf6b6705f227ebdd191453122ac3db956db0e)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2017 Facebook
3 // Author: Roman Gushchin <guro@fb.com>
4 
5 #undef GCC_VERSION
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9 #define _XOPEN_SOURCE 500
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <ftw.h>
13 #include <mntent.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 
21 #include <bpf/bpf.h>
22 #include <bpf/btf.h>
23 
24 #include "main.h"
25 
26 static const int cgroup_attach_types[] = {
27 	BPF_CGROUP_INET_INGRESS,
28 	BPF_CGROUP_INET_EGRESS,
29 	BPF_CGROUP_INET_SOCK_CREATE,
30 	BPF_CGROUP_INET_SOCK_RELEASE,
31 	BPF_CGROUP_INET4_BIND,
32 	BPF_CGROUP_INET6_BIND,
33 	BPF_CGROUP_INET4_POST_BIND,
34 	BPF_CGROUP_INET6_POST_BIND,
35 	BPF_CGROUP_INET4_CONNECT,
36 	BPF_CGROUP_INET6_CONNECT,
37 	BPF_CGROUP_UNIX_CONNECT,
38 	BPF_CGROUP_INET4_GETPEERNAME,
39 	BPF_CGROUP_INET6_GETPEERNAME,
40 	BPF_CGROUP_UNIX_GETPEERNAME,
41 	BPF_CGROUP_INET4_GETSOCKNAME,
42 	BPF_CGROUP_INET6_GETSOCKNAME,
43 	BPF_CGROUP_UNIX_GETSOCKNAME,
44 	BPF_CGROUP_UDP4_SENDMSG,
45 	BPF_CGROUP_UDP6_SENDMSG,
46 	BPF_CGROUP_UNIX_SENDMSG,
47 	BPF_CGROUP_UDP4_RECVMSG,
48 	BPF_CGROUP_UDP6_RECVMSG,
49 	BPF_CGROUP_UNIX_RECVMSG,
50 	BPF_CGROUP_SOCK_OPS,
51 	BPF_CGROUP_DEVICE,
52 	BPF_CGROUP_SYSCTL,
53 	BPF_CGROUP_GETSOCKOPT,
54 	BPF_CGROUP_SETSOCKOPT,
55 	BPF_LSM_CGROUP
56 };
57 
58 #define HELP_SPEC_ATTACH_FLAGS						\
59 	"ATTACH_FLAGS := { [ multi | override ] [ preorder ] }"
60 
61 #define HELP_SPEC_ATTACH_TYPES						\
62 	"       ATTACH_TYPE := { cgroup_inet_ingress | cgroup_inet_egress |\n" \
63 	"                        cgroup_inet_sock_create | cgroup_sock_ops |\n" \
64 	"                        cgroup_device | cgroup_inet4_bind |\n" \
65 	"                        cgroup_inet6_bind | cgroup_inet4_post_bind |\n" \
66 	"                        cgroup_inet6_post_bind | cgroup_inet4_connect |\n" \
67 	"                        cgroup_inet6_connect | cgroup_unix_connect |\n" \
68 	"                        cgroup_inet4_getpeername | cgroup_inet6_getpeername |\n" \
69 	"                        cgroup_unix_getpeername | cgroup_inet4_getsockname |\n" \
70 	"                        cgroup_inet6_getsockname | cgroup_unix_getsockname |\n" \
71 	"                        cgroup_udp4_sendmsg | cgroup_udp6_sendmsg |\n" \
72 	"                        cgroup_unix_sendmsg | cgroup_udp4_recvmsg |\n" \
73 	"                        cgroup_udp6_recvmsg | cgroup_unix_recvmsg |\n" \
74 	"                        cgroup_sysctl | cgroup_getsockopt |\n" \
75 	"                        cgroup_setsockopt | cgroup_inet_sock_release }"
76 
77 static unsigned int query_flags;
78 static struct btf *btf_vmlinux;
79 static __u32 btf_vmlinux_id;
80 
81 static void free_btf_vmlinux(void)
82 {
83 	btf__free(btf_vmlinux);
84 	btf_vmlinux = NULL;
85 	btf_vmlinux_id = 0;
86 }
87 
88 static enum bpf_attach_type parse_attach_type(const char *str)
89 {
90 	const char *attach_type_str;
91 	enum bpf_attach_type type;
92 
93 	for (type = 0; ; type++) {
94 		attach_type_str = libbpf_bpf_attach_type_str(type);
95 		if (!attach_type_str)
96 			break;
97 		if (!strcmp(str, attach_type_str))
98 			return type;
99 	}
100 
101 	/* Also check traditionally used attach type strings. For these we keep
102 	 * allowing prefixed usage.
103 	 */
104 	for (type = 0; ; type++) {
105 		attach_type_str = bpf_attach_type_input_str(type);
106 		if (!attach_type_str)
107 			break;
108 		if (is_prefix(str, attach_type_str))
109 			return type;
110 	}
111 
112 	return __MAX_BPF_ATTACH_TYPE;
113 }
114 
115 static void guess_vmlinux_btf_id(__u32 attach_btf_obj_id)
116 {
117 	struct bpf_btf_info btf_info = {};
118 	__u32 btf_len = sizeof(btf_info);
119 	char name[16] = {};
120 	int err;
121 	int fd;
122 
123 	btf_info.name = ptr_to_u64(name);
124 	btf_info.name_len = sizeof(name);
125 
126 	fd = bpf_btf_get_fd_by_id(attach_btf_obj_id);
127 	if (fd < 0)
128 		return;
129 
130 	err = bpf_btf_get_info_by_fd(fd, &btf_info, &btf_len);
131 	if (err)
132 		goto out;
133 
134 	if (btf_info.kernel_btf && strncmp(name, "vmlinux", sizeof(name)) == 0)
135 		btf_vmlinux_id = btf_info.id;
136 
137 out:
138 	close(fd);
139 }
140 
141 static const struct {
142 	__u32 flag;
143 	const char *name;
144 } attach_flag_names[] = {
145 	{ BPF_F_ALLOW_MULTI, "multi" },
146 	{ BPF_F_ALLOW_OVERRIDE, "override" },
147 	{ BPF_F_PREORDER, "preorder" },
148 };
149 
150 static const char *format_attach_flags(__u32 flags, char *buf, size_t sz)
151 {
152 	size_t len = 0;
153 	size_t i;
154 	int n;
155 
156 	buf[0] = '\0';
157 	for (i = 0; i < ARRAY_SIZE(attach_flag_names); i++) {
158 		if (flags & attach_flag_names[i].flag) {
159 			n = snprintf(buf + len, sz - len, "%s%s",
160 				     len ? "," : "", attach_flag_names[i].name);
161 			if (n < 0 || (size_t)n >= sz - len)
162 				return buf;
163 			len += n;
164 			flags &= ~attach_flag_names[i].flag;
165 		}
166 	}
167 
168 	if (flags)
169 		snprintf(buf + len, sz - len, "%sunknown(%x)",
170 			 len ? "," : "", flags);
171 
172 	return buf;
173 }
174 
175 static void show_attach_flags_json(__u32 flags)
176 {
177 	char buf[32];
178 	size_t i;
179 
180 	jsonw_name(json_wtr, "attach_flags");
181 	jsonw_start_array(json_wtr);
182 
183 	for (i = 0; i < ARRAY_SIZE(attach_flag_names); i++) {
184 		if (!(flags & attach_flag_names[i].flag))
185 			continue;
186 
187 		jsonw_string(json_wtr, attach_flag_names[i].name);
188 		flags &= ~attach_flag_names[i].flag;
189 	}
190 
191 	if (flags) {
192 		snprintf(buf, sizeof(buf), "unknown(%x)", flags);
193 		jsonw_string(json_wtr, buf);
194 	}
195 
196 	jsonw_end_array(json_wtr);
197 }
198 
199 static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
200 			 __u32 attach_flags, int level)
201 {
202 	char prog_name[MAX_PROG_FULL_NAME];
203 	char attach_flags_str[64];
204 	const char *attach_btf_name = NULL;
205 	struct bpf_prog_info info = {};
206 	const char *attach_type_str;
207 	__u32 info_len = sizeof(info);
208 	int prog_fd;
209 
210 	prog_fd = bpf_prog_get_fd_by_id(id);
211 	if (prog_fd < 0)
212 		return -1;
213 
214 	if (bpf_prog_get_info_by_fd(prog_fd, &info, &info_len)) {
215 		close(prog_fd);
216 		return -1;
217 	}
218 
219 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
220 
221 	if (btf_vmlinux) {
222 		if (!btf_vmlinux_id)
223 			guess_vmlinux_btf_id(info.attach_btf_obj_id);
224 
225 		if (btf_vmlinux_id == info.attach_btf_obj_id &&
226 		    info.attach_btf_id < btf__type_cnt(btf_vmlinux)) {
227 			const struct btf_type *t =
228 				btf__type_by_id(btf_vmlinux, info.attach_btf_id);
229 			attach_btf_name =
230 				btf__name_by_offset(btf_vmlinux, t->name_off);
231 		}
232 	}
233 
234 	get_prog_full_name(&info, prog_fd, prog_name, sizeof(prog_name));
235 	if (json_output) {
236 		jsonw_start_object(json_wtr);
237 		jsonw_uint_field(json_wtr, "id", info.id);
238 		if (attach_type_str)
239 			jsonw_string_field(json_wtr, "attach_type", attach_type_str);
240 		else
241 			jsonw_uint_field(json_wtr, "attach_type", attach_type);
242 		if (!(query_flags & BPF_F_QUERY_EFFECTIVE))
243 			show_attach_flags_json(attach_flags);
244 		jsonw_string_field(json_wtr, "name", prog_name);
245 		if (attach_btf_name)
246 			jsonw_string_field(json_wtr, "attach_btf_name", attach_btf_name);
247 		jsonw_uint_field(json_wtr, "attach_btf_obj_id", info.attach_btf_obj_id);
248 		jsonw_uint_field(json_wtr, "attach_btf_id", info.attach_btf_id);
249 		jsonw_end_object(json_wtr);
250 	} else {
251 		printf("%s%-8u ", level ? "    " : "", info.id);
252 		if (attach_type_str)
253 			printf("%-15s", attach_type_str);
254 		else
255 			printf("type %-10u", attach_type);
256 		if (query_flags & BPF_F_QUERY_EFFECTIVE)
257 			printf(" %-15s", prog_name);
258 		else
259 			printf(" %-17s %-15s",
260 			       format_attach_flags(attach_flags, attach_flags_str,
261 						   sizeof(attach_flags_str)), prog_name);
262 		if (attach_btf_name)
263 			printf(" %-15s", attach_btf_name);
264 		else if (info.attach_btf_id)
265 			printf(" attach_btf_obj_id=%u attach_btf_id=%u",
266 			       info.attach_btf_obj_id, info.attach_btf_id);
267 		printf("\n");
268 	}
269 
270 	close(prog_fd);
271 	return 0;
272 }
273 
274 static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
275 {
276 	__u32 prog_cnt = 0;
277 	int ret;
278 
279 	ret = bpf_prog_query(cgroup_fd, type, query_flags, NULL,
280 			     NULL, &prog_cnt);
281 	if (ret)
282 		return -1;
283 
284 	return prog_cnt;
285 }
286 
287 static int cgroup_has_attached_progs(int cgroup_fd)
288 {
289 	unsigned int i = 0;
290 	bool no_prog = true;
291 
292 	for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++) {
293 		int count = count_attached_bpf_progs(cgroup_fd, cgroup_attach_types[i]);
294 
295 		if (count < 0 && errno != EINVAL)
296 			return -1;
297 
298 		if (count > 0) {
299 			no_prog = false;
300 			break;
301 		}
302 	}
303 
304 	return no_prog ? 0 : 1;
305 }
306 
307 static int show_effective_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
308 				    int level)
309 {
310 	LIBBPF_OPTS(bpf_prog_query_opts, p);
311 	__u32 prog_ids[1024] = {0};
312 	__u32 iter;
313 	int ret;
314 
315 	p.query_flags = query_flags;
316 	p.prog_cnt = ARRAY_SIZE(prog_ids);
317 	p.prog_ids = prog_ids;
318 
319 	ret = bpf_prog_query_opts(cgroup_fd, type, &p);
320 	if (ret)
321 		return ret;
322 
323 	if (p.prog_cnt == 0)
324 		return 0;
325 
326 	for (iter = 0; iter < p.prog_cnt; iter++)
327 		show_bpf_prog(prog_ids[iter], type, 0, level);
328 
329 	return 0;
330 }
331 
332 static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
333 				   int level)
334 {
335 	LIBBPF_OPTS(bpf_prog_query_opts, p);
336 	__u32 prog_attach_flags[1024] = {0};
337 	__u32 prog_ids[1024] = {0};
338 	__u32 iter;
339 	int ret;
340 
341 	p.query_flags = query_flags;
342 	p.prog_cnt = ARRAY_SIZE(prog_ids);
343 	p.prog_ids = prog_ids;
344 	p.prog_attach_flags = prog_attach_flags;
345 
346 	ret = bpf_prog_query_opts(cgroup_fd, type, &p);
347 	if (ret)
348 		return ret;
349 
350 	if (p.prog_cnt == 0)
351 		return 0;
352 
353 	for (iter = 0; iter < p.prog_cnt; iter++) {
354 		__u32 attach_flags;
355 
356 		attach_flags = prog_attach_flags[iter] ?: p.attach_flags;
357 		show_bpf_prog(prog_ids[iter], type, attach_flags, level);
358 	}
359 
360 	return 0;
361 }
362 
363 static int show_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
364 			  int level)
365 {
366 	return query_flags & BPF_F_QUERY_EFFECTIVE ?
367 	       show_effective_bpf_progs(cgroup_fd, type, level) :
368 	       show_attached_bpf_progs(cgroup_fd, type, level);
369 }
370 
371 static int do_show(int argc, char **argv)
372 {
373 	int has_attached_progs;
374 	const char *path;
375 	int cgroup_fd;
376 	int ret = -1;
377 	unsigned int i;
378 
379 	query_flags = 0;
380 
381 	if (!REQ_ARGS(1))
382 		return -1;
383 	path = GET_ARG();
384 
385 	while (argc) {
386 		if (is_prefix(*argv, "effective")) {
387 			if (query_flags & BPF_F_QUERY_EFFECTIVE) {
388 				p_err("duplicated argument: %s", *argv);
389 				return -1;
390 			}
391 			query_flags |= BPF_F_QUERY_EFFECTIVE;
392 			NEXT_ARG();
393 		} else {
394 			p_err("expected no more arguments, 'effective', got: '%s'?",
395 			      *argv);
396 			return -1;
397 		}
398 	}
399 
400 	cgroup_fd = open(path, O_RDONLY);
401 	if (cgroup_fd < 0) {
402 		p_err("can't open cgroup %s", path);
403 		goto exit;
404 	}
405 
406 	has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
407 	if (has_attached_progs < 0) {
408 		p_err("can't query bpf programs attached to %s: %s",
409 		      path, strerror(errno));
410 		goto exit_cgroup;
411 	} else if (!has_attached_progs) {
412 		ret = 0;
413 		goto exit_cgroup;
414 	}
415 
416 	if (json_output)
417 		jsonw_start_array(json_wtr);
418 	else if (query_flags & BPF_F_QUERY_EFFECTIVE)
419 		printf("%-8s %-15s %-15s\n", "ID", "AttachType", "Name");
420 	else
421 		printf("%-8s %-15s %-17s %-15s\n", "ID", "AttachType",
422 		       "AttachFlags", "Name");
423 
424 	btf_vmlinux = libbpf_find_kernel_btf();
425 	for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++) {
426 		/*
427 		 * Not all attach types may be supported, so it's expected,
428 		 * that some requests will fail.
429 		 * If we were able to get the show for at least one
430 		 * attach type, let's return 0.
431 		 */
432 		if (show_bpf_progs(cgroup_fd, cgroup_attach_types[i], 0) == 0)
433 			ret = 0;
434 	}
435 
436 	if (json_output)
437 		jsonw_end_array(json_wtr);
438 
439 	free_btf_vmlinux();
440 
441 exit_cgroup:
442 	close(cgroup_fd);
443 exit:
444 	return ret;
445 }
446 
447 /*
448  * To distinguish nftw() errors and do_show_tree_fn() errors
449  * and avoid duplicating error messages, let's return -2
450  * from do_show_tree_fn() in case of error.
451  */
452 #define NFTW_ERR		-1
453 #define SHOW_TREE_FN_ERR	-2
454 static int do_show_tree_fn(const char *fpath, const struct stat *sb,
455 			   int typeflag, struct FTW *ftw)
456 {
457 	int has_attached_progs;
458 	int cgroup_fd;
459 	unsigned int i;
460 
461 	if (typeflag != FTW_D)
462 		return 0;
463 
464 	cgroup_fd = open(fpath, O_RDONLY);
465 	if (cgroup_fd < 0) {
466 		p_err("can't open cgroup %s: %s", fpath, strerror(errno));
467 		return SHOW_TREE_FN_ERR;
468 	}
469 
470 	has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
471 	if (has_attached_progs < 0) {
472 		p_err("can't query bpf programs attached to %s: %s",
473 		      fpath, strerror(errno));
474 		close(cgroup_fd);
475 		return SHOW_TREE_FN_ERR;
476 	} else if (!has_attached_progs) {
477 		close(cgroup_fd);
478 		return 0;
479 	}
480 
481 	if (json_output) {
482 		jsonw_start_object(json_wtr);
483 		jsonw_string_field(json_wtr, "cgroup", fpath);
484 		jsonw_name(json_wtr, "programs");
485 		jsonw_start_array(json_wtr);
486 	} else {
487 		printf("%s\n", fpath);
488 	}
489 
490 	if (!btf_vmlinux)
491 		btf_vmlinux = libbpf_find_kernel_btf();
492 
493 	for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++)
494 		show_bpf_progs(cgroup_fd, cgroup_attach_types[i], ftw->level);
495 
496 	if (errno == EINVAL)
497 		/* Last attach type does not support query.
498 		 * Do not report an error for this, especially because batch
499 		 * mode would stop processing commands.
500 		 */
501 		errno = 0;
502 
503 	if (json_output) {
504 		jsonw_end_array(json_wtr);
505 		jsonw_end_object(json_wtr);
506 	}
507 
508 	close(cgroup_fd);
509 
510 	return 0;
511 }
512 
513 static char *find_cgroup_root(void)
514 {
515 	struct mntent *mnt;
516 	FILE *f;
517 
518 	f = fopen("/proc/mounts", "r");
519 	if (f == NULL)
520 		return NULL;
521 
522 	while ((mnt = getmntent(f))) {
523 		if (strcmp(mnt->mnt_type, "cgroup2") == 0) {
524 			fclose(f);
525 			return strdup(mnt->mnt_dir);
526 		}
527 	}
528 
529 	fclose(f);
530 	return NULL;
531 }
532 
533 static int do_show_tree(int argc, char **argv)
534 {
535 	char *cgroup_root, *cgroup_alloced = NULL;
536 	int ret;
537 
538 	query_flags = 0;
539 
540 	if (!argc) {
541 		cgroup_alloced = find_cgroup_root();
542 		if (!cgroup_alloced) {
543 			p_err("cgroup v2 isn't mounted");
544 			return -1;
545 		}
546 		cgroup_root = cgroup_alloced;
547 	} else {
548 		cgroup_root = GET_ARG();
549 
550 		while (argc) {
551 			if (is_prefix(*argv, "effective")) {
552 				if (query_flags & BPF_F_QUERY_EFFECTIVE) {
553 					p_err("duplicated argument: %s", *argv);
554 					return -1;
555 				}
556 				query_flags |= BPF_F_QUERY_EFFECTIVE;
557 				NEXT_ARG();
558 			} else {
559 				p_err("expected no more arguments, 'effective', got: '%s'?",
560 				      *argv);
561 				return -1;
562 			}
563 		}
564 	}
565 
566 	if (json_output)
567 		jsonw_start_array(json_wtr);
568 	else if (query_flags & BPF_F_QUERY_EFFECTIVE)
569 		printf("%s\n"
570 		       "%-8s %-15s %-15s\n",
571 		       "CgroupPath",
572 		       "ID", "AttachType", "Name");
573 	else
574 		printf("%s\n"
575 		       "%-8s %-15s %-17s %-15s\n",
576 		       "CgroupPath",
577 		       "ID", "AttachType", "AttachFlags", "Name");
578 
579 	switch (nftw(cgroup_root, do_show_tree_fn, 1024, FTW_MOUNT)) {
580 	case NFTW_ERR:
581 		p_err("can't iterate over %s: %s", cgroup_root,
582 		      strerror(errno));
583 		ret = -1;
584 		break;
585 	case SHOW_TREE_FN_ERR:
586 		ret = -1;
587 		break;
588 	default:
589 		ret = 0;
590 	}
591 
592 	if (json_output)
593 		jsonw_end_array(json_wtr);
594 
595 	free_btf_vmlinux();
596 	free(cgroup_alloced);
597 
598 	return ret;
599 }
600 
601 static int do_attach(int argc, char **argv)
602 {
603 	enum bpf_attach_type attach_type;
604 	int cgroup_fd, prog_fd;
605 	int attach_flags = 0;
606 	int ret = -1;
607 	int i;
608 
609 	if (argc < 4) {
610 		p_err("too few parameters for cgroup attach");
611 		goto exit;
612 	}
613 
614 	cgroup_fd = open(argv[0], O_RDONLY);
615 	if (cgroup_fd < 0) {
616 		p_err("can't open cgroup %s", argv[0]);
617 		goto exit;
618 	}
619 
620 	attach_type = parse_attach_type(argv[1]);
621 	if (attach_type == __MAX_BPF_ATTACH_TYPE) {
622 		p_err("invalid attach type");
623 		goto exit_cgroup;
624 	}
625 
626 	argc -= 2;
627 	argv = &argv[2];
628 	prog_fd = prog_parse_fd(&argc, &argv);
629 	if (prog_fd < 0)
630 		goto exit_cgroup;
631 
632 	for (i = 0; i < argc; i++) {
633 		if (is_prefix(argv[i], "multi")) {
634 			attach_flags |= BPF_F_ALLOW_MULTI;
635 		} else if (is_prefix(argv[i], "override")) {
636 			attach_flags |= BPF_F_ALLOW_OVERRIDE;
637 		} else if (is_prefix(argv[i], "preorder")) {
638 			attach_flags |= BPF_F_PREORDER;
639 		} else {
640 			p_err("unknown option: %s", argv[i]);
641 			goto exit_cgroup;
642 		}
643 	}
644 
645 	if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, attach_flags)) {
646 		p_err("failed to attach program");
647 		goto exit_prog;
648 	}
649 
650 	if (json_output)
651 		jsonw_null(json_wtr);
652 
653 	ret = 0;
654 
655 exit_prog:
656 	close(prog_fd);
657 exit_cgroup:
658 	close(cgroup_fd);
659 exit:
660 	return ret;
661 }
662 
663 static int do_detach(int argc, char **argv)
664 {
665 	enum bpf_attach_type attach_type;
666 	int prog_fd, cgroup_fd;
667 	int ret = -1;
668 
669 	if (argc < 4) {
670 		p_err("too few parameters for cgroup detach");
671 		goto exit;
672 	}
673 
674 	cgroup_fd = open(argv[0], O_RDONLY);
675 	if (cgroup_fd < 0) {
676 		p_err("can't open cgroup %s", argv[0]);
677 		goto exit;
678 	}
679 
680 	attach_type = parse_attach_type(argv[1]);
681 	if (attach_type == __MAX_BPF_ATTACH_TYPE) {
682 		p_err("invalid attach type");
683 		goto exit_cgroup;
684 	}
685 
686 	argc -= 2;
687 	argv = &argv[2];
688 	prog_fd = prog_parse_fd(&argc, &argv);
689 	if (prog_fd < 0)
690 		goto exit_cgroup;
691 
692 	if (bpf_prog_detach2(prog_fd, cgroup_fd, attach_type)) {
693 		p_err("failed to detach program");
694 		goto exit_prog;
695 	}
696 
697 	if (json_output)
698 		jsonw_null(json_wtr);
699 
700 	ret = 0;
701 
702 exit_prog:
703 	close(prog_fd);
704 exit_cgroup:
705 	close(cgroup_fd);
706 exit:
707 	return ret;
708 }
709 
710 static int do_help(int argc, char **argv)
711 {
712 	if (json_output) {
713 		jsonw_null(json_wtr);
714 		return 0;
715 	}
716 
717 	fprintf(stderr,
718 		"Usage: %1$s %2$s { show | list } CGROUP [**effective**]\n"
719 		"       %1$s %2$s tree [CGROUP_ROOT] [**effective**]\n"
720 		"       %1$s %2$s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
721 		"       %1$s %2$s detach CGROUP ATTACH_TYPE PROG\n"
722 		"       %1$s %2$s help\n"
723 		"\n"
724 		HELP_SPEC_ATTACH_TYPES "\n"
725 		"       " HELP_SPEC_ATTACH_FLAGS "\n"
726 		"       " HELP_SPEC_PROGRAM "\n"
727 		"       " HELP_SPEC_OPTIONS " |\n"
728 		"                    {-f|--bpffs} }\n"
729 		"",
730 		bin_name, argv[-2]);
731 
732 	return 0;
733 }
734 
735 static const struct cmd cmds[] = {
736 	{ "show",	do_show },
737 	{ "list",	do_show },
738 	{ "tree",       do_show_tree },
739 	{ "attach",	do_attach },
740 	{ "detach",	do_detach },
741 	{ "help",	do_help },
742 	{ 0 }
743 };
744 
745 int do_cgroup(int argc, char **argv)
746 {
747 	return cmd_select(cmds, argc, argv, do_help);
748 }
749