bpfLoader as a lib - make it actually build/work

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iff1c39de5ac8a07cdd16841c04db87ae874adc8f
This commit is contained in:
Maciej Żenczykowski 2024-08-06 15:32:00 -07:00
commit 6d4d9d89c2

View file

@ -46,6 +46,7 @@
#include <android-base/cmsg.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
@ -57,11 +58,13 @@
// Unspecified attach type is 0 which is BPF_CGROUP_INET_INGRESS.
#define BPF_ATTACH_TYPE_UNSPEC BPF_CGROUP_INET_INGRESS
using android::base::EndsWith;
using android::base::StartsWith;
using android::base::unique_fd;
using std::ifstream;
using std::ios;
using std::optional;
using std::strerror;
using std::string;
using std::vector;
@ -877,60 +880,6 @@ int loadProg(const char* elfPath, bool* isCritical, const Location& location) {
return ret;
}
} // namespace bpf
} // namespace android
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "BpfLoader"
#include <arpa/inet.h>
#include <dirent.h>
#include <elf.h>
#include <error.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/bpf.h>
#include <linux/unistd.h>
#include <net/if.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <libbpf_android.h>
#include <log/log.h>
#include "BpfSyscallWrappers.h"
#include "bpf/BpfUtils.h"
using android::base::EndsWith;
using std::string;
// Networking-related program types are limited to the Tethering Apex
// to prevent things from breaking due to conflicts on mainline updates
// (exception made for socket filters, ie. xt_bpf for potential use in iptables,
@ -955,7 +904,7 @@ constexpr bpf_prog_type kVendorAllowedProgTypes[] = {
BPF_PROG_TYPE_SOCKET_FILTER,
};
const android::bpf::Location locations[] = {
const Location locations[] = {
// Core operating system
{
.dir = "/system/etc/bpf/",
@ -979,7 +928,7 @@ const android::bpf::Location locations[] = {
},
};
int loadAllElfObjects(const android::bpf::Location& location) {
int loadAllElfObjects(const Location& location) {
int retVal = 0;
DIR* dir;
struct dirent* ent;
@ -993,10 +942,10 @@ int loadAllElfObjects(const android::bpf::Location& location) {
progPath += s;
bool critical;
int ret = android::bpf::loadProg(progPath.c_str(), &critical, location);
int ret = loadProg(progPath.c_str(), &critical, location);
if (ret) {
if (critical) retVal = ret;
ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), strerror(-ret));
} else {
ALOGV("Loaded object: %s", progPath.c_str());
}
@ -1017,7 +966,7 @@ int createSysFsBpfSubDir(const char* const prefix) {
int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
if (ret && errno != EEXIST) {
const int err = errno;
ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err));
ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), strerror(err));
return -err;
}
@ -1026,8 +975,8 @@ int createSysFsBpfSubDir(const char* const prefix) {
return 0;
}
int main(int __unused argc, char** argv, char * const envp[]) {
android::base::InitLogging(argv, &android::base::KernelLogger);
int legacyBpfLoader(int __unused argc, char** argv, char * const envp[]) {
base::InitLogging(argv, &base::KernelLogger);
// Load all ELF objects, create programs and maps, and pin them
for (const auto& location : locations) {
@ -1047,3 +996,6 @@ int main(int __unused argc, char** argv, char * const envp[]) {
ALOGE("FATAL: execve(): %d[%s]", errno, strerror(errno));
return 121;
}
} // namespace bpf
} // namespace android