diff --git a/netd/BpfHandler.cpp b/netd/BpfHandler.cpp index 2a9892f830a676616ce6abc7459369b5a6b89cf0..a00c3632543e55f93b3099c5ae7e705f4856efe6 100644 --- a/netd/BpfHandler.cpp +++ b/netd/BpfHandler.cpp @@ -53,14 +53,10 @@ static Status attachProgramToCgroup(const char* programPath, const unique_fd& cg bpf_attach_type type) { unique_fd cgroupProg(retrieveProgram(programPath)); if (!cgroupProg.ok()) { - const int err = errno; - ALOGE("Failed to get program from %s: %s", programPath, strerror(err)); - return statusFromErrno(err, "cgroup program get failed"); + return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath)); } if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) { - const int err = errno; - ALOGE("Program from %s attach failed: %s", programPath, strerror(err)); - return statusFromErrno(err, "program attach failed"); + return statusFromErrno(errno, fmt::format("Program {} attach failed", programPath)); } return netdutils::status::ok; } @@ -68,50 +64,38 @@ static Status attachProgramToCgroup(const char* programPath, const unique_fd& cg static Status checkProgramAccessible(const char* programPath) { unique_fd prog(retrieveProgram(programPath)); if (!prog.ok()) { - const int err = errno; - ALOGE("Failed to get program from %s: %s", programPath, strerror(err)); - return statusFromErrno(err, "program retrieve failed"); + return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath)); } return netdutils::status::ok; } static Status initPrograms(const char* cg2_path) { - if (!cg2_path) { - ALOGE("cg2_path is NULL"); - return statusFromErrno(EINVAL, "cg2_path is NULL"); - } + if (!cg2_path) return Status("cg2_path is NULL"); // This code was mainlined in T, so this should be trivially satisfied. - if (!modules::sdklevel::IsAtLeastT()) { - ALOGE("S- platform is unsupported"); - abort(); - } + if (!modules::sdklevel::IsAtLeastT()) return Status("S- platform is unsupported"); // S requires eBPF support which was only added in 4.9, so this should be satisfied. if (!bpf::isAtLeastKernelVersion(4, 9, 0)) { - ALOGE("kernel version < 4.9.0 is unsupported"); - abort(); + return Status("kernel version < 4.9.0 is unsupported"); } // U bumps the kernel requirement up to 4.14 if (modules::sdklevel::IsAtLeastU() && !bpf::isAtLeastKernelVersion(4, 14, 0)) { - ALOGE("U+ platform with kernel version < 4.14.0 is unsupported"); - abort(); + return Status("U+ platform with kernel version < 4.14.0 is unsupported"); } if (modules::sdklevel::IsAtLeastV()) { // V bumps the kernel requirement up to 4.19 // see also: //system/netd/tests/kernel_test.cpp TestKernel419 if (!bpf::isAtLeastKernelVersion(4, 19, 0)) { - ALOGE("V+ platform with kernel version < 4.19.0 is unsupported"); - abort(); + return Status("V+ platform with kernel version < 4.19.0 is unsupported"); } // Technically already required by U, but only enforce on V+ // see also: //system/netd/tests/kernel_test.cpp TestKernel64Bit if (bpf::isKernel32Bit() && bpf::isAtLeastKernelVersion(5, 16, 0)) { - ALOGE("V+ platform with 32 bit kernel, version >= 5.16.0 is unsupported"); - abort(); + return Status("V+ platform with 32 bit kernel, version >= 5.16.0 is unsupported"); } } @@ -123,14 +107,12 @@ static Status initPrograms(const char* cg2_path) { // problems are AFAIK limited to various CAP_NET_ADMIN protected interfaces. // see also: //system/bpf/bpfloader/BpfLoader.cpp main() if (bpf::isUserspace32bit() && bpf::isAtLeastKernelVersion(6, 2, 0)) { - ALOGE("32 bit userspace with Kernel version >= 6.2.0 is unsupported"); - abort(); + return Status("32 bit userspace with Kernel version >= 6.2.0 is unsupported"); } // U mandates this mount point (though it should also be the case on T) if (modules::sdklevel::IsAtLeastU() && !!strcmp(cg2_path, "/sys/fs/cgroup")) { - ALOGE("U+ platform with cg2_path != /sys/fs/cgroup is unsupported"); - abort(); + return Status("U+ platform with cg2_path != /sys/fs/cgroup is unsupported"); } unique_fd cg_fd(open(cg2_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC)); diff --git a/netd/NetdUpdatable.cpp b/netd/NetdUpdatable.cpp index 41b1fdbb12aa2a92e10661bed5d251b1b6eb9b99..8b9e5a759c589c74de9e3bd4cc6de40a0f7f9f53 100644 --- a/netd/NetdUpdatable.cpp +++ b/netd/NetdUpdatable.cpp @@ -31,7 +31,7 @@ int libnetd_updatable_init(const char* cg2_path) { android::netdutils::Status ret = sBpfHandler.init(cg2_path); if (!android::netdutils::isOk(ret)) { - LOG(ERROR) << __func__ << ": BPF handler init failed"; + LOG(ERROR) << __func__ << ": Failed. " << ret.code() << " " << ret.msg(); return -ret.code(); } return 0; diff --git a/staticlibs/netd/libnetdutils/include/netdutils/Status.h b/staticlibs/netd/libnetdutils/include/netdutils/Status.h index 7b0bd4729daeb6a02d5d84c4fa9e0f0df6a7cb9e..34f3bb24fc4f29624ac0770c29cbbce6792d430d 100644 --- a/staticlibs/netd/libnetdutils/include/netdutils/Status.h +++ b/staticlibs/netd/libnetdutils/include/netdutils/Status.h @@ -41,6 +41,9 @@ class [[nodiscard]] Status { // Constructs an error Status, |code| must be non-zero. Status(int code, std::string msg) : mCode(code), mMsg(std::move(msg)) { assert(!ok()); } + // Constructs an error Status with message. Error |code| is unspecified. + explicit Status(std::string msg) : Status(std::numeric_limits<int>::max(), std::move(msg)) {} + Status(android::base::Result<void> result) : mCode(result.ok() ? 0 : static_cast<int>(result.error().code())), mMsg(result.ok() ? "" : result.error().message()) {}