libbpf: Add error returns to two API functions
authorGrant Seltzer <grantseltzer@gmail.com>
Wed, 20 Apr 2022 16:12:24 +0000 (12:12 -0400)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 21 Apr 2022 14:28:11 +0000 (16:28 +0200)
This adds an error return to the following API functions:

- bpf_program__set_expected_attach_type()
- bpf_program__set_type()

In both cases, the error occurs when the BPF object has
already been loaded when the function is called. In this
case -EBUSY is returned.

Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220420161226.86803-1-grantseltzer@gmail.com
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h

index 8375021800f335fd97708e8b95a402c341dcc40d..342340aee948b27cb70ce67bdf48b312e0ef9409 100644 (file)
@@ -8562,9 +8562,13 @@ enum bpf_prog_type bpf_program__type(const struct bpf_program *prog)
        return prog->type;
 }
 
-void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
+int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
 {
+       if (prog->obj->loaded)
+               return libbpf_err(-EBUSY);
+
        prog->type = type;
+       return 0;
 }
 
 static bool bpf_program__is_type(const struct bpf_program *prog,
@@ -8609,10 +8613,14 @@ enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program
        return prog->expected_attach_type;
 }
 
-void bpf_program__set_expected_attach_type(struct bpf_program *prog,
+int bpf_program__set_expected_attach_type(struct bpf_program *prog,
                                           enum bpf_attach_type type)
 {
+       if (prog->obj->loaded)
+               return libbpf_err(-EBUSY);
+
        prog->expected_attach_type = type;
+       return 0;
 }
 
 __u32 bpf_program__flags(const struct bpf_program *prog)
index 63d66f1adf1ad132dc3ee0e59062b6328d5dfaa7..66735623ca63288a62138ccc92a8b4edc0fdced5 100644 (file)
@@ -686,12 +686,12 @@ LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
 
 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
-LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
-                                     enum bpf_prog_type type);
+LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
+                                    enum bpf_prog_type type);
 
 LIBBPF_API enum bpf_attach_type
 bpf_program__expected_attach_type(const struct bpf_program *prog);
-LIBBPF_API void
+LIBBPF_API int
 bpf_program__set_expected_attach_type(struct bpf_program *prog,
                                      enum bpf_attach_type type);