Commit 2be91230 authored by 4ast's avatar 4ast

Merge pull request #314 from iovisor/map_unshare

Close fd and unshare when public map is destructed
parents 120bfad7 d83192d0
......@@ -49,6 +49,7 @@
#include "frontends/clang/b_frontend_action.h"
#include "bpf_module.h"
#include "kbuild_helper.h"
#include "shared_table.h"
#include "libbpf.h"
namespace ebpf {
......@@ -113,6 +114,10 @@ BPFModule::~BPFModule() {
engine_.reset();
rw_engine_.reset();
ctx_.reset();
for (auto table : *tables_) {
if (table.is_shared)
SharedTables::instance()->remove_fd(table.name);
}
}
static void debug_printf(Module *mod, IRBuilder<> &B, const string &fmt, vector<Value *> args) {
......
......@@ -599,6 +599,7 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
C.getDiagnostics().Report(Decl->getLocStart(), diag_id) << table.name << "already in use";
return false;
}
table_it->is_shared = true;
return true;
}
......
......@@ -14,6 +14,8 @@
* limitations under the License.
*/
#include <unistd.h>
#include "shared_table.h"
namespace ebpf {
......@@ -43,4 +45,13 @@ bool SharedTables::insert_fd(const string &name, int fd) {
return true;
}
bool SharedTables::remove_fd(const string &name) {
auto table = tables_.find(name);
if (table == tables_.end())
return false;
close(table->second);
tables_.erase(table);
return true;
}
}
......@@ -30,6 +30,8 @@ class SharedTables {
bool insert_fd(const std::string &name, int fd);
// lookup an fd in the shared table, or -1 if not found
int lookup_fd(const std::string &name) const;
// close and remove a shared fd. return true if the value was found
bool remove_fd(const std::string &name);
private:
static SharedTables *instance_;
std::map<std::string, int> tables_;
......
......@@ -38,6 +38,7 @@ struct TableDesc {
llvm::Function *leaf_sscanf;
llvm::Function *key_snprintf;
llvm::Function *leaf_snprintf;
bool is_shared;
};
} // namespace ebpf
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment