| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me> |
|---|
| 2 | | - * |
|---|
| 3 | | - * This program is free software; you can redistribute it and/or |
|---|
| 4 | | - * modify it under the terms of version 2 of the GNU General Public |
|---|
| 5 | | - * License as published by the Free Software Foundation. |
|---|
| 6 | 3 | */ |
|---|
| 7 | 4 | |
|---|
| 8 | 5 | #define _GNU_SOURCE |
|---|
| 9 | 6 | #include <stdio.h> |
|---|
| 10 | | -#include <linux/bpf.h> |
|---|
| 11 | 7 | #include <unistd.h> |
|---|
| 12 | 8 | #include <bpf/bpf.h> |
|---|
| 13 | | -#include "bpf_load.h" |
|---|
| 14 | | -#include <linux/bpf.h> |
|---|
| 9 | +#include <bpf/libbpf.h> |
|---|
| 15 | 10 | #include "cgroup_helpers.h" |
|---|
| 16 | 11 | |
|---|
| 17 | 12 | #define CGROUP_PATH "/my-cgroup" |
|---|
| .. | .. |
|---|
| 19 | 14 | int main(int argc, char **argv) |
|---|
| 20 | 15 | { |
|---|
| 21 | 16 | pid_t remote_pid, local_pid = getpid(); |
|---|
| 22 | | - int cg2, idx = 0, rc = 0; |
|---|
| 17 | + struct bpf_link *link = NULL; |
|---|
| 18 | + struct bpf_program *prog; |
|---|
| 19 | + int cg2, idx = 0, rc = 1; |
|---|
| 20 | + struct bpf_object *obj; |
|---|
| 23 | 21 | char filename[256]; |
|---|
| 22 | + int map_fd[2]; |
|---|
| 24 | 23 | |
|---|
| 25 | 24 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 26 | | - if (load_bpf_file(filename)) { |
|---|
| 27 | | - printf("%s", bpf_log_buf); |
|---|
| 28 | | - return 1; |
|---|
| 25 | + obj = bpf_object__open_file(filename, NULL); |
|---|
| 26 | + if (libbpf_get_error(obj)) { |
|---|
| 27 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
|---|
| 28 | + return 0; |
|---|
| 29 | + } |
|---|
| 30 | + |
|---|
| 31 | + prog = bpf_object__find_program_by_name(obj, "bpf_prog1"); |
|---|
| 32 | + if (!prog) { |
|---|
| 33 | + printf("finding a prog in obj file failed\n"); |
|---|
| 34 | + goto cleanup; |
|---|
| 35 | + } |
|---|
| 36 | + |
|---|
| 37 | + /* load BPF program */ |
|---|
| 38 | + if (bpf_object__load(obj)) { |
|---|
| 39 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
|---|
| 40 | + goto cleanup; |
|---|
| 41 | + } |
|---|
| 42 | + |
|---|
| 43 | + map_fd[0] = bpf_object__find_map_fd_by_name(obj, "cgroup_map"); |
|---|
| 44 | + map_fd[1] = bpf_object__find_map_fd_by_name(obj, "perf_map"); |
|---|
| 45 | + if (map_fd[0] < 0 || map_fd[1] < 0) { |
|---|
| 46 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
|---|
| 47 | + goto cleanup; |
|---|
| 48 | + } |
|---|
| 49 | + |
|---|
| 50 | + link = bpf_program__attach(prog); |
|---|
| 51 | + if (libbpf_get_error(link)) { |
|---|
| 52 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
|---|
| 53 | + link = NULL; |
|---|
| 54 | + goto cleanup; |
|---|
| 29 | 55 | } |
|---|
| 30 | 56 | |
|---|
| 31 | 57 | if (setup_cgroup_environment()) |
|---|
| .. | .. |
|---|
| 33 | 59 | |
|---|
| 34 | 60 | cg2 = create_and_get_cgroup(CGROUP_PATH); |
|---|
| 35 | 61 | |
|---|
| 36 | | - if (!cg2) |
|---|
| 62 | + if (cg2 < 0) |
|---|
| 37 | 63 | goto err; |
|---|
| 38 | 64 | |
|---|
| 39 | 65 | if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) { |
|---|
| .. | .. |
|---|
| 74 | 100 | goto err; |
|---|
| 75 | 101 | } |
|---|
| 76 | 102 | |
|---|
| 77 | | - goto out; |
|---|
| 78 | | -err: |
|---|
| 79 | | - rc = 1; |
|---|
| 103 | + rc = 0; |
|---|
| 80 | 104 | |
|---|
| 81 | | -out: |
|---|
| 105 | +err: |
|---|
| 82 | 106 | close(cg2); |
|---|
| 83 | 107 | cleanup_cgroup_environment(); |
|---|
| 108 | + |
|---|
| 109 | +cleanup: |
|---|
| 110 | + bpf_link__destroy(link); |
|---|
| 111 | + bpf_object__close(obj); |
|---|
| 84 | 112 | return rc; |
|---|
| 85 | 113 | } |
|---|