Skip to content

Commit 9c6c044

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpftool: Set errno on skeleton failures and propagate errors
Follow libbpf's error handling conventions and pass through errors and errno properly. Skeleton code always returned NULL on errors (not ERR_PTR(err)), so there are no backwards compatibility concerns. But now we also set errno properly, so it's possible to distinguish different reasons for failure, if necessary. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Toke Høiland-Jørgensen <toke@redhat.com> Link: https://lore.kernel.org/bpf/20210525035935.1461796-6-andrii@kernel.org
1 parent e9fc3ce commit 9c6c044

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ static int do_skeleton(int argc, char **argv)
713713
#ifndef %2$s \n\
714714
#define %2$s \n\
715715
\n\
716+
#include <errno.h> \n\
716717
#include <stdlib.h> \n\
717718
#include <bpf/libbpf.h> \n\
718719
\n\
@@ -793,18 +794,23 @@ static int do_skeleton(int argc, char **argv)
793794
%1$s__open_opts(const struct bpf_object_open_opts *opts) \n\
794795
{ \n\
795796
struct %1$s *obj; \n\
797+
int err; \n\
796798
\n\
797799
obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\
798-
if (!obj) \n\
800+
if (!obj) { \n\
801+
errno = ENOMEM; \n\
799802
return NULL; \n\
800-
if (%1$s__create_skeleton(obj)) \n\
801-
goto err; \n\
802-
if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
803-
goto err; \n\
803+
} \n\
804+
\n\
805+
err = %1$s__create_skeleton(obj); \n\
806+
err = err ?: bpf_object__open_skeleton(obj->skeleton, opts);\n\
807+
if (err) \n\
808+
goto err_out; \n\
804809
\n\
805810
return obj; \n\
806-
err: \n\
811+
err_out: \n\
807812
%1$s__destroy(obj); \n\
813+
errno = -err; \n\
808814
return NULL; \n\
809815
} \n\
810816
\n\
@@ -824,12 +830,15 @@ static int do_skeleton(int argc, char **argv)
824830
%1$s__open_and_load(void) \n\
825831
{ \n\
826832
struct %1$s *obj; \n\
833+
int err; \n\
827834
\n\
828835
obj = %1$s__open(); \n\
829836
if (!obj) \n\
830837
return NULL; \n\
831-
if (%1$s__load(obj)) { \n\
838+
err = %1$s__load(obj); \n\
839+
if (err) { \n\
832840
%1$s__destroy(obj); \n\
841+
errno = -err; \n\
833842
return NULL; \n\
834843
} \n\
835844
return obj; \n\
@@ -860,7 +869,7 @@ static int do_skeleton(int argc, char **argv)
860869
\n\
861870
s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
862871
if (!s) \n\
863-
return -1; \n\
872+
goto err; \n\
864873
obj->skeleton = s; \n\
865874
\n\
866875
s->sz = sizeof(*s); \n\
@@ -949,7 +958,7 @@ static int do_skeleton(int argc, char **argv)
949958
return 0; \n\
950959
err: \n\
951960
bpf_object__destroy_skeleton(s); \n\
952-
return -1; \n\
961+
return -ENOMEM; \n\
953962
} \n\
954963
\n\
955964
#endif /* %s */ \n\

0 commit comments

Comments
 (0)