Skip to content

Commit

Permalink
Merge pull request #314 from iovisor/map_unshare
Browse files Browse the repository at this point in the history
Close fd and unshare when public map is destructed
  • Loading branch information
4ast committed Jan 18, 2016
2 parents 120bfad + d83192d commit 2be9123
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cc/bpf_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/cc/frontends/clang/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions src/cc/shared_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#include <unistd.h>

#include "shared_table.h"

namespace ebpf {
Expand Down Expand Up @@ -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;
}

}
2 changes: 2 additions & 0 deletions src/cc/shared_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
1 change: 1 addition & 0 deletions src/cc/table_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct TableDesc {
llvm::Function *leaf_sscanf;
llvm::Function *key_snprintf;
llvm::Function *leaf_snprintf;
bool is_shared;
};

} // namespace ebpf

0 comments on commit 2be9123

Please sign in to comment.