Commit 9c6c0449 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

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: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Acked-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210525035935.1461796-6-andrii@kernel.org
parent e9fc3ce9
...@@ -713,6 +713,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -713,6 +713,7 @@ static int do_skeleton(int argc, char **argv)
#ifndef %2$s \n\ #ifndef %2$s \n\
#define %2$s \n\ #define %2$s \n\
\n\ \n\
#include <errno.h> \n\
#include <stdlib.h> \n\ #include <stdlib.h> \n\
#include <bpf/libbpf.h> \n\ #include <bpf/libbpf.h> \n\
\n\ \n\
...@@ -793,18 +794,23 @@ static int do_skeleton(int argc, char **argv) ...@@ -793,18 +794,23 @@ static int do_skeleton(int argc, char **argv)
%1$s__open_opts(const struct bpf_object_open_opts *opts) \n\ %1$s__open_opts(const struct bpf_object_open_opts *opts) \n\
{ \n\ { \n\
struct %1$s *obj; \n\ struct %1$s *obj; \n\
int err; \n\
\n\ \n\
obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\ obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\
if (!obj) \n\ if (!obj) { \n\
errno = ENOMEM; \n\
return NULL; \n\ return NULL; \n\
if (%1$s__create_skeleton(obj)) \n\ } \n\
goto err; \n\ \n\
if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\ err = %1$s__create_skeleton(obj); \n\
goto err; \n\ err = err ?: bpf_object__open_skeleton(obj->skeleton, opts);\n\
if (err) \n\
goto err_out; \n\
\n\ \n\
return obj; \n\ return obj; \n\
err: \n\ err_out: \n\
%1$s__destroy(obj); \n\ %1$s__destroy(obj); \n\
errno = -err; \n\
return NULL; \n\ return NULL; \n\
} \n\ } \n\
\n\ \n\
...@@ -824,12 +830,15 @@ static int do_skeleton(int argc, char **argv) ...@@ -824,12 +830,15 @@ static int do_skeleton(int argc, char **argv)
%1$s__open_and_load(void) \n\ %1$s__open_and_load(void) \n\
{ \n\ { \n\
struct %1$s *obj; \n\ struct %1$s *obj; \n\
int err; \n\
\n\ \n\
obj = %1$s__open(); \n\ obj = %1$s__open(); \n\
if (!obj) \n\ if (!obj) \n\
return NULL; \n\ return NULL; \n\
if (%1$s__load(obj)) { \n\ err = %1$s__load(obj); \n\
if (err) { \n\
%1$s__destroy(obj); \n\ %1$s__destroy(obj); \n\
errno = -err; \n\
return NULL; \n\ return NULL; \n\
} \n\ } \n\
return obj; \n\ return obj; \n\
...@@ -860,7 +869,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -860,7 +869,7 @@ static int do_skeleton(int argc, char **argv)
\n\ \n\
s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\ s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
if (!s) \n\ if (!s) \n\
return -1; \n\ goto err; \n\
obj->skeleton = s; \n\ obj->skeleton = s; \n\
\n\ \n\
s->sz = sizeof(*s); \n\ s->sz = sizeof(*s); \n\
...@@ -949,7 +958,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -949,7 +958,7 @@ static int do_skeleton(int argc, char **argv)
return 0; \n\ return 0; \n\
err: \n\ err: \n\
bpf_object__destroy_skeleton(s); \n\ bpf_object__destroy_skeleton(s); \n\
return -1; \n\ return -ENOMEM; \n\
} \n\ } \n\
\n\ \n\
#endif /* %s */ \n\ #endif /* %s */ \n\
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment