Disable capability check, handle failure better

Fixes #214
This commit is contained in:
Naïm Favier
2021-12-02 13:55:21 +01:00
parent 3e0bba6966
commit afb5759cc3

View File

@@ -206,27 +206,11 @@ int main(int argc, char **argv) {
init_ui(); init_ui();
} }
if (geteuid() != 0) { #ifndef __linux__
#ifdef __linux__ if (geteuid() != 0)
char exe_path[PATH_MAX];
ssize_t len;
unsigned int caps[5] = {0, 0, 0, 0, 0};
if ((len = readlink("/proc/self/exe", exe_path, PATH_MAX)) == -1)
forceExit(false, "Failed to locate nethogs binary.");
exe_path[len] = '\0';
getxattr(exe_path, "security.capability", (char *)caps, sizeof(caps));
if ((((caps[1] >> CAP_NET_ADMIN) & 1) != 1) ||
(((caps[1] >> CAP_NET_RAW) & 1) != 1))
forceExit(false, "To run nethogs without being root you need to enable "
"capabilities on the program (cap_net_admin, "
"cap_net_raw), see the documentation for details.");
#else
forceExit(false, "You need to be root to run NetHogs!"); forceExit(false, "You need to be root to run NetHogs!");
#endif #endif
} // on Linux, we can run as non-root given the cap_net_admin and cap_net_raw capabilities
// use the Self-Pipe trick to interrupt the select() in the main loop // use the Self-Pipe trick to interrupt the select() in the main loop
self_pipe = create_self_pipe(); self_pipe = create_self_pipe();
@@ -292,6 +276,10 @@ int main(int argc, char **argv) {
} }
if (nb_devices == nb_failed_devices) { if (nb_devices == nb_failed_devices) {
if (geteuid() != 0)
fprintf(stderr, "To run nethogs without being root, you need to enable "
"capabilities on the program (cap_net_admin, cap_new_raw). "
"See the documentation for details.\n");
forceExit(false, "Error opening pcap handlers for all devices.\n"); forceExit(false, "Error opening pcap handlers for all devices.\n");
} }