From d38f62f802b8b62ef9335fce75c1b4040aed3913 Mon Sep 17 00:00:00 2001 From: Mohamed Boussaffa Date: Thu, 10 Mar 2016 20:10:03 +0800 Subject: [PATCH] using a more unique key for add/remove action --- libnethogs.cpp | 17 ++++++++++------- libnethogs.h | 3 ++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/libnethogs.cpp b/libnethogs.cpp index 15e9833..2752253 100644 --- a/libnethogs.cpp +++ b/libnethogs.cpp @@ -23,8 +23,7 @@ extern Process * unknownip; static std::pair self_pipe = std::make_pair(-1, -1); static bool monitor_run_flag = false; -static NethogsMonitorCallback monitor_udpate_callback; -typedef std::map NethogsAppUpdateMap; +typedef std::map NethogsAppUpdateMap; static NethogsAppUpdateMap monitor_update_data; static int monitor_refresh_delay = 1; @@ -52,6 +51,11 @@ static std::pair create_self_pipe() return std::make_pair(pfd[0], pfd[1]); } +static uint64_t getProcAsKey(ProcList * curproc) +{ + return reinterpret_cast(curproc); +} + static bool wait_for_next_trigger() { if( pc_loop_use_select ) @@ -207,13 +211,13 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) if (DEBUG) std::cout << "PROC: Deleting process\n"; - NethogsAppUpdateMap::iterator it = monitor_update_data.find(curproc->getVal()->name); + NethogsAppUpdateMap::iterator it = monitor_update_data.find(getProcAsKey(curproc)); if( it != monitor_update_data.end() ) { NethogsMonitorUpdate& data = it->second; data.action = NETHOGS_APP_ACTION_REMOVE; (*cb)(&data); - monitor_update_data.erase(curproc->getVal()->name); + monitor_update_data.erase(getProcAsKey(curproc)); } ProcList * todelete = curproc; @@ -234,7 +238,6 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) } else { - const char* name = curproc->getVal()->name; const u_int32_t uid = curproc->getVal()->getUid(); u_int32_t sent_bytes; u_int32_t recv_bytes; @@ -244,8 +247,8 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) curproc->getVal()->gettotal (&recv_bytes, &sent_bytes); //notify update - bool const new_data = (monitor_update_data.find(name) == monitor_update_data.end()); - NethogsMonitorUpdate &data = monitor_update_data[name]; + bool const new_data = (monitor_update_data.find(getProcAsKey(curproc)) == monitor_update_data.end()); + NethogsMonitorUpdate &data = monitor_update_data[getProcAsKey(curproc)]; bool data_change = false; if( new_data ) diff --git a/libnethogs.h b/libnethogs.h index c5dce0e..2eca2f1 100644 --- a/libnethogs.h +++ b/libnethogs.h @@ -21,7 +21,8 @@ extern "C" { typedef struct NethogsMonitorUpdate { int action; // NETHOGS_APP_ACTION_SET or NETHOGS_APP_ACTION_REMOVE - const char* name; //the key of the set/remove + uint64_t key; //A unique key for the record, used with set/remove actions + const char* name; int pid; uint32_t uid; const char* device_name;