bpfloader: use unique_fd

Using unique_fd to ensure we release bpf prog fds when they are no
longer needed.

Change-Id: I9d5c2673c7e1bdf05052771e06ac78cec21957ec
Merged-In: I9d5c2673c7e1bdf05052771e06ac78cec21957ec
Signed-off-by: Connor O'Brien <connoro@google.com>
(cherry picked from commit 206dd3381e20b663648620db051c170afccccfea)
diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp
index 16b4a2a..3e8a1f7 100644
--- a/libbpf_android/Loader.cpp
+++ b/libbpf_android/Loader.cpp
@@ -37,6 +37,7 @@
 #include <vector>
 
 #include <android-base/strings.h>
+#include <android-base/unique_fd.h>
 
 #define BPF_FS_PATH "/sys/fs/bpf/"
 
@@ -44,6 +45,7 @@
 #define BPF_LOAD_LOG_SZ 0x1ffff
 
 using android::base::StartsWith;
+using android::base::unique_fd;
 using std::ifstream;
 using std::ios;
 using std::string;
@@ -82,7 +84,7 @@
     vector<char> data;
     vector<char> rel_data;
 
-    int prog_fd; /* fd after loading */
+    unique_fd prog_fd; /* fd after loading */
 } codeSection;
 
 /* Common with the eBPF C program */
@@ -319,7 +321,7 @@
         }
 
         if (cs_temp.data.size() > 0) {
-            cs.push_back(cs_temp);
+            cs.push_back(std::move(cs_temp));
             ALOGD("Adding section %d to cs list\n", i);
         }
     }
@@ -380,8 +382,8 @@
     return 0;
 }
 
-static int createMaps(const char* elfPath, ifstream& elfFile, vector<int>& mapFds) {
-    int ret, fd;
+static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds) {
+    int ret;
     vector<char> mdData;
     vector<struct bpf_map_def> md;
     vector<string> mapNames;
@@ -395,22 +397,21 @@
     ret = getMapNames(elfFile, mapNames);
     if (ret) return ret;
 
-    mapFds.resize(mapNames.size());
-
     for (int i = 0; i < (int)mapNames.size(); i++) {
+        unique_fd fd;
         // Format of pin location is /sys/fs/bpf/map_<filename>_<mapname>
         string mapPinLoc;
         bool reuse = false;
 
         mapPinLoc = string(BPF_FS_PATH) + "map_" + fname + "_" + string(mapNames[i]);
         if (access(mapPinLoc.c_str(), F_OK) == 0) {
-            fd = bpf_obj_get(mapPinLoc.c_str());
-            ALOGD("bpf_create_map reusing map %s, ret: %d\n", mapNames[i].c_str(), fd);
+            fd.reset(bpf_obj_get(mapPinLoc.c_str()));
+            ALOGD("bpf_create_map reusing map %s, ret: %d\n", mapNames[i].c_str(), fd.get());
             reuse = true;
         } else {
-            fd = bpf_create_map(md[i].type, mapNames[i].c_str(), md[i].key_size, md[i].value_size,
-                                md[i].max_entries, md[i].map_flags);
-            ALOGD("bpf_create_map name %s, ret: %d\n", mapNames[i].c_str(), fd);
+            fd.reset(bpf_create_map(md[i].type, mapNames[i].c_str(), md[i].key_size, md[i].value_size,
+                                    md[i].max_entries, md[i].map_flags));
+            ALOGD("bpf_create_map name %s, ret: %d\n", mapNames[i].c_str(), fd.get());
         }
 
         if (fd < 0) return fd;
@@ -421,7 +422,7 @@
             if (ret < 0) return ret;
         }
 
-        mapFds[i] = fd;
+        mapFds.push_back(std::move(fd));
     }
 
     return ret;
@@ -472,7 +473,7 @@
     insn->src_reg = BPF_PSEUDO_MAP_FD;
 }
 
-static void applyMapRelo(ifstream& elfFile, vector<int> mapFds, vector<codeSection>& cs) {
+static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) {
     vector<string> mapNames;
 
     int ret = getMapNames(elfFile, mapNames);
@@ -539,7 +540,7 @@
             if (ret < 0) return ret;
         }
 
-        cs[i].prog_fd = fd;
+        cs[i].prog_fd.reset(fd);
     }
 
     return 0;
@@ -548,7 +549,7 @@
 int loadProg(const char* elfPath) {
     vector<char> license;
     vector<codeSection> cs;
-    vector<int> mapFds;
+    vector<unique_fd> mapFds;
     int ret;
 
     ifstream elfFile(elfPath, ios::in | ios::binary);
@@ -578,7 +579,7 @@
     }
 
     for (int i = 0; i < (int)mapFds.size(); i++)
-        ALOGD("map_fd found at %d is %d in %s\n", i, mapFds[i], elfPath);
+        ALOGD("map_fd found at %d is %d in %s\n", i, mapFds[i].get(), elfPath);
 
     applyMapRelo(elfFile, mapFds, cs);