From a5fbdc1910cd2393f0c500867dc1f58fab789c9a Mon Sep 17 00:00:00 2001 From: rain1 Date: Thu, 14 Apr 2016 12:15:48 +0000 Subject: [PATCH] * src/main.cpp: Make the capability imports conditional. * src/nethogs.cpp: Make the capability check conditional. Bug fix in the != test. --- src/main.cpp | 23 +++++++++++------------ src/nethogs.cpp | 3 +++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8d77726..b4bb3e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -173,20 +173,19 @@ int main(int argc, char **argv) { init_ui(); } - if (NEEDROOT) { - if (geteuid() != 0) - forceExit(false, "You need to be root to run NetHogs!"); - } - else { - unsigned int caps[5] = {0}; +#if NEEDROOT == 1 + if (geteuid() != 0) + forceExit(false, "You need to be root to run NetHogs!"); +#else + unsigned int caps[5] = {0}; - getxattr(argv[0], "security.capability", (char *)caps, sizeof(caps)); + getxattr(argv[0], "security.capability", (char *)caps, sizeof(caps)); - if ((caps[1] >> CAP_NET_ADMIN) & 1 != 1) - forceExit(false, "You need to enable cap_net_admin (and cap_net_raw) to run NetHogs!"); - if ((caps[1] >> CAP_NET_RAW) & 1 != 1) - forceExit(false, "You need to enable cap_net_raw to run NetHogs!"); - } + if (((caps[1] >> CAP_NET_ADMIN) & 1) != 1) + forceExit(false, "You need to enable cap_net_admin (and cap_net_raw) to run NetHogs!"); + if (((caps[1] >> CAP_NET_RAW) & 1) != 1) + forceExit(false, "You need to enable cap_net_raw to run NetHogs!"); +#endif // use the Self-Pipe trick to interrupt the select() in the main loop self_pipe = create_self_pipe(); diff --git a/src/nethogs.cpp b/src/nethogs.cpp index f2fba8a..855261c 100644 --- a/src/nethogs.cpp +++ b/src/nethogs.cpp @@ -37,9 +37,12 @@ #include #include #include + +#if NEEDROOT == 0 #include #include #include +#endif #include "cui.h"