目录
函数原型与解释
LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
参数说明:
- prog:一个指向 bpf_program 结构的指针,表示要解除固定的 eBPF 程序。
- path:一个字符串,表示要从中解除固定 eBPF 程序的文件系统路径。
返回值:一个整数。正常情况下返回 0;如果发生错误,返回负数。
代码demo
#include <linux/bpf.h> #include <bpf/bpf_helpers.h> SEC("kprobe/__x64_sys_getpid") int bpf_prog1(struct pt_regs *ctx) { bpf_printk("sys_getpid called\n"); return 0; } char _license[] SEC("license") = "GPL";
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <errno.h> #include <fcntl.h> #include <linux/bpf.h> #include <bpf/bpf.h> #include <bpf/libbpf.h> int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: %s <bpf_program.o> <pin_path>\n", argv[0]); return 1; } const char *bpf_program_path = argv[1]; const char *pin_path = argv[2]; struct bpf_object *obj = NULL; struct bpf_program *prog = NULL; if (bpf_prog_load(bpf_program_path, BPF_PROG_TYPE_KPROBE, &obj, &prog)) { fprintf(stderr, "Error loading BPF program: %s\n", strerror(errno)); return 1; } if (bpf_program__pin(prog, pin_path)) { fprintf(stderr, "Error pinning BPF program: %s\n", strerror(errno)); return 1; } printf("BPF program pinned at: %s\n", pin_path); // Wait for user input to unpin printf("Press Enter to unpin the BPF program...\n"); getchar(); if (bpf_program__unpin(prog, pin_path)) { fprintf(stderr, "Error unpinning BPF program: %s\n", strerror(errno)); return 1; } printf("BPF program unpinned from: %s\n", pin_path); return 0; }
makefile
CC=gcc CFLAGS=-I/usr/include/bpf -I./include LDFLAGS=-lbpf all: test test: test.c $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) clean: rm -f test
cmake
cmake_minimum_required(VERSION 2.8) project(test) set(CMAKE_C_FLAGS "-I/usr/include/bpf -I./include") add_executable(test test.c) target_link_libraries(test bpf)
期望输出
make
./demo
BPF program pinned at: /sys/fs/bpf/my_bpf_prog Press Enter to unpin the BPF program... BPF program unpinned from: /sys/fs/bpf/my_bpf_prog