using a more unique key for add/remove action

This commit is contained in:
Mohamed Boussaffa
2016-03-10 20:10:03 +08:00
parent 761e153f30
commit d38f62f802
2 changed files with 12 additions and 8 deletions

View File

@@ -23,8 +23,7 @@ extern Process * unknownip;
static std::pair<int,int> self_pipe = std::make_pair(-1, -1); static std::pair<int,int> self_pipe = std::make_pair(-1, -1);
static bool monitor_run_flag = false; static bool monitor_run_flag = false;
static NethogsMonitorCallback monitor_udpate_callback; typedef std::map<uint64_t, NethogsMonitorUpdate> NethogsAppUpdateMap;
typedef std::map<const char*, NethogsMonitorUpdate> NethogsAppUpdateMap;
static NethogsAppUpdateMap monitor_update_data; static NethogsAppUpdateMap monitor_update_data;
static int monitor_refresh_delay = 1; static int monitor_refresh_delay = 1;
@@ -52,6 +51,11 @@ static std::pair<int, int> create_self_pipe()
return std::make_pair(pfd[0], pfd[1]); return std::make_pair(pfd[0], pfd[1]);
} }
static uint64_t getProcAsKey(ProcList * curproc)
{
return reinterpret_cast<uint64_t>(curproc);
}
static bool wait_for_next_trigger() static bool wait_for_next_trigger()
{ {
if( pc_loop_use_select ) if( pc_loop_use_select )
@@ -207,13 +211,13 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb)
if (DEBUG) if (DEBUG)
std::cout << "PROC: Deleting process\n"; 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() ) if( it != monitor_update_data.end() )
{ {
NethogsMonitorUpdate& data = it->second; NethogsMonitorUpdate& data = it->second;
data.action = NETHOGS_APP_ACTION_REMOVE; data.action = NETHOGS_APP_ACTION_REMOVE;
(*cb)(&data); (*cb)(&data);
monitor_update_data.erase(curproc->getVal()->name); monitor_update_data.erase(getProcAsKey(curproc));
} }
ProcList * todelete = curproc; ProcList * todelete = curproc;
@@ -234,7 +238,6 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb)
} }
else else
{ {
const char* name = curproc->getVal()->name;
const u_int32_t uid = curproc->getVal()->getUid(); const u_int32_t uid = curproc->getVal()->getUid();
u_int32_t sent_bytes; u_int32_t sent_bytes;
u_int32_t recv_bytes; u_int32_t recv_bytes;
@@ -244,8 +247,8 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb)
curproc->getVal()->gettotal (&recv_bytes, &sent_bytes); curproc->getVal()->gettotal (&recv_bytes, &sent_bytes);
//notify update //notify update
bool const new_data = (monitor_update_data.find(name) == monitor_update_data.end()); bool const new_data = (monitor_update_data.find(getProcAsKey(curproc)) == monitor_update_data.end());
NethogsMonitorUpdate &data = monitor_update_data[name]; NethogsMonitorUpdate &data = monitor_update_data[getProcAsKey(curproc)];
bool data_change = false; bool data_change = false;
if( new_data ) if( new_data )

View File

@@ -21,7 +21,8 @@ extern "C" {
typedef struct NethogsMonitorUpdate typedef struct NethogsMonitorUpdate
{ {
int action; // NETHOGS_APP_ACTION_SET or NETHOGS_APP_ACTION_REMOVE 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; int pid;
uint32_t uid; uint32_t uid;
const char* device_name; const char* device_name;