From 761e153f30a2e420301d88492779164313bb7c29 Mon Sep 17 00:00:00 2001 From: Mohamed Boussaffa Date: Thu, 10 Mar 2016 19:52:43 +0800 Subject: [PATCH] using process name as the key for add/remove events --- libnethogs.cpp | 17 +++++++++-------- libnethogs.h | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/libnethogs.cpp b/libnethogs.cpp index 97302e9..15e9833 100644 --- a/libnethogs.cpp +++ b/libnethogs.cpp @@ -8,6 +8,7 @@ extern "C" #include #include #include +#include #include #include @@ -23,7 +24,7 @@ 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; @@ -206,13 +207,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()->pid); + NethogsAppUpdateMap::iterator it = monitor_update_data.find(curproc->getVal()->name); if( it != monitor_update_data.end() ) { NethogsMonitorUpdate& data = it->second; data.action = NETHOGS_APP_ACTION_REMOVE; (*cb)(&data); - monitor_update_data.erase(curproc->getVal()->pid); + monitor_update_data.erase(curproc->getVal()->name); } ProcList * todelete = curproc; @@ -233,7 +234,7 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) } else { - const int pid = curproc->getVal()->pid; + const char* name = curproc->getVal()->name; const u_int32_t uid = curproc->getVal()->getUid(); u_int32_t sent_bytes; u_int32_t recv_bytes; @@ -243,16 +244,16 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) curproc->getVal()->gettotal (&recv_bytes, &sent_bytes); //notify update - bool const new_data = (monitor_update_data.find(pid) == monitor_update_data.end()); - NethogsMonitorUpdate &data = monitor_update_data[pid]; + bool const new_data = (monitor_update_data.find(name) == monitor_update_data.end()); + NethogsMonitorUpdate &data = monitor_update_data[name]; bool data_change = false; if( new_data ) { data_change = true; memset(&data, 0, sizeof(data)); - data.pid = pid; - data.app_name = curproc->getVal()->name; + data.name = curproc->getVal()->name; + data.pid = curproc->getVal()->pid; } data.device_name = curproc->getVal()->devicename; diff --git a/libnethogs.h b/libnethogs.h index 2479d8c..c5dce0e 100644 --- a/libnethogs.h +++ b/libnethogs.h @@ -20,10 +20,10 @@ extern "C" { typedef struct NethogsMonitorUpdate { - int action; + int action; // NETHOGS_APP_ACTION_SET or NETHOGS_APP_ACTION_REMOVE + const char* name; //the key of the set/remove int pid; uint32_t uid; - const char* app_name; const char* device_name; uint32_t sent_bytes; uint32_t recv_bytes;