bpfLoader as a lib - make it actually build/work

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iff1c39de5ac8a07cdd16841c04db87ae874adc8f
diff --git a/loader/BpfLoader.cpp b/loader/BpfLoader.cpp
new file mode 100644
index 0000000..a44d354
--- /dev/null
+++ b/loader/BpfLoader.cpp
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+#include <libbpf_android.h>
+
+int main(int argc, char** argv, char * const envp[]) {
+  return android::bpf::legacyBpfLoader(argc, argv, envp);
+}
diff --git a/loader/Loader.cpp b/loader/Loader.cpp
index 919b4b3..4dd64b7 100644
--- a/loader/Loader.cpp
+++ b/loader/Loader.cpp
@@ -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 @@
     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 @@
         BPF_PROG_TYPE_SOCKET_FILTER,
 };
 
-const android::bpf::Location locations[] = {
+const Location locations[] = {
         // Core operating system
         {
                 .dir = "/system/etc/bpf/",
@@ -979,7 +928,7 @@
         },
 };
 
-int loadAllElfObjects(const android::bpf::Location& location) {
+int loadAllElfObjects(const Location& location) {
     int retVal = 0;
     DIR* dir;
     struct dirent* ent;
@@ -993,10 +942,10 @@
             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 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 @@
     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 @@
     ALOGE("FATAL: execve(): %d[%s]", errno, strerror(errno));
     return 121;
 }
+
+}  // namespace bpf
+}  // namespace android
diff --git a/loader/include/libbpf_android.h b/loader/include/libbpf_android.h
index 0a98a1d..df4e32c 100644
--- a/loader/include/libbpf_android.h
+++ b/loader/include/libbpf_android.h
@@ -37,5 +37,7 @@
 // Exposed for testing
 unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal);
 
+int legacyBpfLoader(int argc, char** argv, char * const envp[]);
+
 }  // namespace bpf
 }  // namespace android