From 9015e6c27fe3d5aa214d8af3e07ccaaa177b4de1 Mon Sep 17 00:00:00 2001 From: Mohamed Boussaffa Date: Mon, 7 Mar 2016 06:13:04 +0800 Subject: [PATCH] No more need for the refresh signal because select() timeout replaces it --- Makefile | 4 +--- main.cpp | 31 +++++++++++++++---------------- nethogs.cpp | 2 -- refresh.cpp | 39 --------------------------------------- refresh.h | 23 ----------------------- 5 files changed, 16 insertions(+), 83 deletions(-) delete mode 100644 refresh.cpp delete mode 100644 refresh.h diff --git a/Makefile b/Makefile index 12fee9e..35638fe 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ CFLAGS?=-Wall -Wextra CXXFLAGS?=-Wall -Wextra endif -OBJS=packet.o connection.o process.o refresh.o decpcap.o cui.o inode2prog.o conninode.o devices.o +OBJS=packet.o connection.o process.o decpcap.o cui.o inode2prog.o conninode.o devices.o NCURSES_LIBS?=-lncurses @@ -64,8 +64,6 @@ decpcap_test: decpcap_test.cpp decpcap.o #-lefence -refresh.o: refresh.cpp refresh.h nethogs.h - $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c refresh.cpp process.o: process.cpp process.h nethogs.h $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c process.cpp packet.o: packet.cpp packet.h nethogs.h diff --git a/main.cpp b/main.cpp index 70d6bde..70bbfcc 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include std::pair self_pipe = std::make_pair(-1, -1); +time_t last_refresh_time = 0; static void versiondisplay(void) { @@ -130,18 +131,18 @@ int main (int argc, char** argv) return 0; } } - - if ((!tracemode) && (!DEBUG)){ - init_ui(); - } - std::pair self_pipe = createSelfPipe(); - if( self_pipe.first == -1|| self_pipe.second == -1 ) + self_pipe = createSelfPipe(); + if( self_pipe.first == -1 || self_pipe.second == -1 ) { perror("Error creating pipe file descriptors\n"); return 0; } + if ((!tracemode) && (!DEBUG)){ + init_ui(); + } + if (NEEDROOT && (geteuid() != 0)) forceExit(false, "You need to be root to run NetHogs!"); @@ -209,9 +210,7 @@ int main (int argc, char** argv) pc_loop_fd_list.push_back(self_pipe.first); } - signal (SIGALRM, &alarm_cb); signal (SIGINT, &quit_cb); - alarm (refreshdelay); fprintf(stderr, "Waiting for first packet to arrive (see sourceforge.net bug 1019381)\n"); struct dpargs * userdata = (dpargs *) malloc (sizeof (struct dpargs)); @@ -241,18 +240,18 @@ int main (int argc, char** argv) current_handle = current_handle->next; } - - if (needrefresh) - { - if ((!DEBUG)&&(!tracemode)) + time_t const now = ::time(NULL); + if( last_refresh_time + refreshdelay <= now ) + { + last_refresh_time = now; + if ((!DEBUG)&&(!tracemode)) { // handle user input ui_tick(); } do_refresh(); - needrefresh = false; } - + //if not packets, do a select() until next packet if (!packets_read) { @@ -270,8 +269,8 @@ int main (int argc, char** argv) timeval timeout = {refreshdelay, 0}; if( select(nfds, &pc_loop_fd_set, 0, 0, &timeout) == -1 ) { - perror("error in select()\n"); - break; + //this happens on system signal + continue; } if( FD_ISSET(self_pipe.first, &pc_loop_fd_set) ) { diff --git a/nethogs.cpp b/nethogs.cpp index 0860742..fdc5914 100644 --- a/nethogs.cpp +++ b/nethogs.cpp @@ -46,7 +46,6 @@ extern "C" { #include "packet.h" #include "connection.h" #include "process.h" -#include "refresh.h" #include "devices.h" extern Process * unknownudp; @@ -57,7 +56,6 @@ unsigned refreshcount = 0; unsigned processlimit = 0; bool tracemode = false; bool bughuntmode = false; -bool needrefresh = false; // sort on sent or received? bool sortRecv = true; // viewMode: kb/s or total diff --git a/refresh.cpp b/refresh.cpp deleted file mode 100644 index 5195983..0000000 --- a/refresh.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * refresh.cpp - * - * Copyright (c) 2004 Arnout Engelen - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - - -#include -#include -#include -#include "nethogs.h" - -extern bool needrefresh; -extern unsigned refreshdelay; - -void alarm_cb (int /*i*/) -{ - needrefresh = true; - //cout << "Setting needrefresh\n"; - - signal (SIGALRM, &alarm_cb); - alarm(refreshdelay); -} - diff --git a/refresh.h b/refresh.h deleted file mode 100644 index 152907f..0000000 --- a/refresh.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * refresh.h - * - * Copyright (c) 2004 Arnout Engelen - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - - -void alarm_cb (int i);