do not close extern table when a module destructs
authorHuapeng Zhou <hzhou@fb.com>
Tue, 20 Dec 2016 21:10:12 +0000 (13:10 -0800)
committerHuapeng Zhou <hzhou@fb.com>
Tue, 20 Dec 2016 21:10:12 +0000 (13:10 -0800)
src/cc/bpf_module.cc
src/cc/frontends/clang/b_frontend_action.cc
src/cc/table_desc.h

index be0a524..ee39c00 100644 (file)
@@ -118,10 +118,11 @@ BPFModule::~BPFModule() {
   ctx_.reset();
   if (tables_) {
     for (auto table : *tables_) {
-      if (table.is_shared)
+      if (table.is_shared) {
         SharedTables::instance()->remove_fd(table.name);
-      else
+      } else if (!table.is_extern) {
         close(table.fd);
+      }
     }
   }
 }
index 4e71873..397ecc6 100644 (file)
@@ -631,7 +631,6 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
       ++i;
     }
 
-    bool is_extern = false;
     bpf_map_type map_type = BPF_MAP_TYPE_UNSPEC;
     if (A->getName() == "maps/hash") {
       map_type = BPF_MAP_TYPE_HASH;
@@ -666,7 +665,7 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
     } else if (A->getName() == "maps/stacktrace") {
       map_type = BPF_MAP_TYPE_STACK_TRACE;
     } else if (A->getName() == "maps/extern") {
-      is_extern = true;
+      table.is_extern = true;
       table.fd = SharedTables::instance()->lookup_fd(table.name);
       table.type = SharedTables::instance()->lookup_type(table.name);
     } else if (A->getName() == "maps/export") {
@@ -687,7 +686,7 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
       return true;
     }
 
-    if (!is_extern) {
+    if (!table.is_extern) {
       if (map_type == BPF_MAP_TYPE_UNSPEC) {
         error(Decl->getLocStart(), "unsupported map type: %0") << A->getName();
         return false;
index a5196e2..d299f5d 100644 (file)
@@ -40,6 +40,7 @@ struct TableDesc {
   llvm::Function *key_snprintf;
   llvm::Function *leaf_snprintf;
   bool is_shared;
+  bool is_extern;
 };
 
 }  // namespace ebpf