|
|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
/*
|
|
|
|
|
htop - htop.c |
|
|
|
|
(C) 2004-2011 Hisham H. Muhammad |
|
|
|
|
(C) 2020-2021 htop dev team |
|
|
|
|
Released under the GNU GPLv2, see the COPYING file |
|
|
|
|
in the source distribution for its full text. |
|
|
|
|
*/ |
|
|
|
|
@ -8,7 +9,6 @@ in the source distribution for its full text. |
|
|
|
|
#include "config.h" // IWYU pragma: keep |
|
|
|
|
|
|
|
|
|
#include <assert.h> |
|
|
|
|
#include <errno.h> |
|
|
|
|
#include <getopt.h> |
|
|
|
|
#include <locale.h> |
|
|
|
|
#include <stdbool.h> |
|
|
|
|
@ -35,37 +35,19 @@ in the source distribution for its full text. |
|
|
|
|
#include "UsersTable.h" |
|
|
|
|
#include "XUtils.h" |
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
#include <sys/capability.h> |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
enum CapMode { |
|
|
|
|
CAP_MODE_NONE, |
|
|
|
|
CAP_MODE_BASIC, |
|
|
|
|
CAP_MODE_STRICT |
|
|
|
|
}; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
static void printVersionFlag(void) { |
|
|
|
|
fputs(PACKAGE " " VERSION "\n", stdout); |
|
|
|
|
static void printVersionFlag(const char* name) { |
|
|
|
|
printf("%s " VERSION "\n", name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void printHelpFlag(void) { |
|
|
|
|
fputs(PACKAGE " " VERSION "\n" |
|
|
|
|
static void printHelpFlag(const char* name) { |
|
|
|
|
printf("%s " VERSION "\n" |
|
|
|
|
COPYRIGHT "\n" |
|
|
|
|
"Released under the GNU GPLv2.\n\n" |
|
|
|
|
"-C --no-color Use a monochrome color scheme\n" |
|
|
|
|
"-d --delay=DELAY Set the delay between updates, in tenths of seconds\n" |
|
|
|
|
"-F --filter=FILTER Show only the commands matching the given filter\n" |
|
|
|
|
"-h --help Print this help screen\n" |
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
" --drop-capabilities[=none|basic|strict] Drop Linux capabilities when running as root\n" |
|
|
|
|
" none - do not drop any capabilities\n" |
|
|
|
|
" basic (default) - drop all capabilities not needed by htop\n" |
|
|
|
|
" strict - drop all capabilities except those needed for core functionality\n" |
|
|
|
|
#endif |
|
|
|
|
"-H --highlight-changes[=DELAY] Highlight new and old processes\n" |
|
|
|
|
"-M --no-mouse Disable the mouse\n" |
|
|
|
|
"-p --pid=PID[,PID,PID...] Show only the given PIDs\n" |
|
|
|
|
@ -73,12 +55,12 @@ static void printHelpFlag(void) { |
|
|
|
|
"-t --tree Show the tree view (can be combined with -s)\n" |
|
|
|
|
"-u --user[=USERNAME] Show only processes for a given user (or $USER)\n" |
|
|
|
|
"-U --no-unicode Do not use unicode but plain ASCII\n" |
|
|
|
|
"-V --version Print version info\n" |
|
|
|
|
"\n" |
|
|
|
|
"-V --version Print version info\n", name); |
|
|
|
|
Platform_longOptionsUsage(name); |
|
|
|
|
printf("\n" |
|
|
|
|
"Long options may be passed with a single dash.\n\n" |
|
|
|
|
"Press F1 inside " PACKAGE " for online help.\n" |
|
|
|
|
"See 'man " PACKAGE "' for more information.\n", |
|
|
|
|
stdout); |
|
|
|
|
"Press F1 inside %s for online help.\n" |
|
|
|
|
"See 'man %s' for more information.\n", name, name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
|
@ -95,12 +77,9 @@ typedef struct CommandLineSettings_ { |
|
|
|
|
bool allowUnicode; |
|
|
|
|
bool highlightChanges; |
|
|
|
|
int highlightDelaySecs; |
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
enum CapMode capabilitiesMode; |
|
|
|
|
#endif |
|
|
|
|
} CommandLineSettings; |
|
|
|
|
|
|
|
|
|
static CommandLineSettings parseArguments(int argc, char** argv) { |
|
|
|
|
static CommandLineSettings parseArguments(const char* program, int argc, char** argv) { |
|
|
|
|
|
|
|
|
|
CommandLineSettings flags = { |
|
|
|
|
.pidMatchList = NULL, |
|
|
|
|
@ -114,9 +93,6 @@ static CommandLineSettings parseArguments(int argc, char** argv) { |
|
|
|
|
.allowUnicode = true, |
|
|
|
|
.highlightChanges = false, |
|
|
|
|
.highlightDelaySecs = -1, |
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
.capabilitiesMode = CAP_MODE_BASIC, |
|
|
|
|
#endif |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const struct option long_opts[] = |
|
|
|
|
@ -134,9 +110,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) { |
|
|
|
|
{"pid", required_argument, 0, 'p'}, |
|
|
|
|
{"filter", required_argument, 0, 'F'}, |
|
|
|
|
{"highlight-changes", optional_argument, 0, 'H'}, |
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
{"drop-capabilities", optional_argument, 0, 128}, |
|
|
|
|
#endif |
|
|
|
|
PLATFORM_LONG_OPTIONS |
|
|
|
|
{0,0,0,0} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
@ -146,10 +120,10 @@ static CommandLineSettings parseArguments(int argc, char** argv) { |
|
|
|
|
if (opt == EOF) break; |
|
|
|
|
switch (opt) { |
|
|
|
|
case 'h': |
|
|
|
|
printHelpFlag(); |
|
|
|
|
printHelpFlag(program); |
|
|
|
|
exit(0); |
|
|
|
|
case 'V': |
|
|
|
|
printVersionFlag(); |
|
|
|
|
printVersionFlag(program); |
|
|
|
|
exit(0); |
|
|
|
|
case 's': |
|
|
|
|
assert(optarg); /* please clang analyzer, cause optarg can be NULL in the 'u' case */ |
|
|
|
|
@ -218,7 +192,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) { |
|
|
|
|
char* saveptr; |
|
|
|
|
const char* pid = strtok_r(argCopy, ",", &saveptr); |
|
|
|
|
|
|
|
|
|
if(!flags.pidMatchList) { |
|
|
|
|
if (!flags.pidMatchList) { |
|
|
|
|
flags.pidMatchList = Hashtable_new(8, false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -255,29 +229,11 @@ static CommandLineSettings parseArguments(int argc, char** argv) { |
|
|
|
|
flags.highlightChanges = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
case 128: { |
|
|
|
|
const char* mode = optarg; |
|
|
|
|
if (!mode && optind < argc && argv[optind] != NULL && |
|
|
|
|
(argv[optind][0] != '\0' && argv[optind][0] != '-')) { |
|
|
|
|
mode = argv[optind++]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!mode || String_eq(mode, "basic")) { |
|
|
|
|
flags.capabilitiesMode = CAP_MODE_BASIC; |
|
|
|
|
} else if (String_eq(mode, "none")) { |
|
|
|
|
flags.capabilitiesMode = CAP_MODE_NONE; |
|
|
|
|
} else if (String_eq(mode, "strict")) { |
|
|
|
|
flags.capabilitiesMode = CAP_MODE_STRICT; |
|
|
|
|
} else { |
|
|
|
|
fprintf(stderr, "Error: invalid capabilities mode \"%s\".\n", mode); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
default: |
|
|
|
|
exit(1); |
|
|
|
|
if (Platform_getLongOption(opt, argc, argv) == false) |
|
|
|
|
exit(1); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return flags; |
|
|
|
|
@ -304,92 +260,6 @@ static void setCommFilter(State* state, char** commFilter) { |
|
|
|
|
*commFilter = NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
static int dropCapabilities(enum CapMode mode) { |
|
|
|
|
|
|
|
|
|
if (mode == CAP_MODE_NONE) |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
/* capabilities we keep to operate */ |
|
|
|
|
const cap_value_t keepcapsStrict[] = { |
|
|
|
|
CAP_DAC_READ_SEARCH, |
|
|
|
|
CAP_SYS_PTRACE, |
|
|
|
|
}; |
|
|
|
|
const cap_value_t keepcapsBasic[] = { |
|
|
|
|
CAP_DAC_READ_SEARCH, /* read non world-readable process files of other users, like /proc/[pid]/io */ |
|
|
|
|
CAP_KILL, /* send signals to processes of other users */ |
|
|
|
|
CAP_SYS_NICE, /* lower process nice value / change nice value for arbitrary processes */ |
|
|
|
|
CAP_SYS_PTRACE, /* read /proc/[pid]/exe */ |
|
|
|
|
#ifdef HAVE_DELAYACCT |
|
|
|
|
CAP_NET_ADMIN, /* communicate over netlink socket for delay accounting */ |
|
|
|
|
#endif |
|
|
|
|
}; |
|
|
|
|
const cap_value_t* const keepcaps = (mode == CAP_MODE_BASIC) ? keepcapsBasic : keepcapsStrict; |
|
|
|
|
const size_t ncap = (mode == CAP_MODE_BASIC) ? ARRAYSIZE(keepcapsBasic) : ARRAYSIZE(keepcapsStrict); |
|
|
|
|
|
|
|
|
|
cap_t caps = cap_init(); |
|
|
|
|
if (caps == NULL) { |
|
|
|
|
fprintf(stderr, "Error: can not initialize capabilities: %s\n", strerror(errno)); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (cap_clear(caps) < 0) { |
|
|
|
|
fprintf(stderr, "Error: can not clear capabilities: %s\n", strerror(errno)); |
|
|
|
|
cap_free(caps); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cap_t currCaps = cap_get_proc(); |
|
|
|
|
if (currCaps == NULL) { |
|
|
|
|
fprintf(stderr, "Error: can not get current process capabilities: %s\n", strerror(errno)); |
|
|
|
|
cap_free(caps); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ncap; i++) { |
|
|
|
|
if (!CAP_IS_SUPPORTED(keepcaps[i])) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
cap_flag_value_t current; |
|
|
|
|
if (cap_get_flag(currCaps, keepcaps[i], CAP_PERMITTED, ¤t) < 0) { |
|
|
|
|
fprintf(stderr, "Error: can not get current value of capability %d: %s\n", keepcaps[i], strerror(errno)); |
|
|
|
|
cap_free(currCaps); |
|
|
|
|
cap_free(caps); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (current != CAP_SET) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
if (cap_set_flag(caps, CAP_PERMITTED, 1, &keepcaps[i], CAP_SET) < 0) { |
|
|
|
|
fprintf(stderr, "Error: can not set permitted capability %d: %s\n", keepcaps[i], strerror(errno)); |
|
|
|
|
cap_free(currCaps); |
|
|
|
|
cap_free(caps); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &keepcaps[i], CAP_SET) < 0) { |
|
|
|
|
fprintf(stderr, "Error: can not set effective capability %d: %s\n", keepcaps[i], strerror(errno)); |
|
|
|
|
cap_free(currCaps); |
|
|
|
|
cap_free(caps); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (cap_set_proc(caps) < 0) { |
|
|
|
|
fprintf(stderr, "Error: can not set process capabilities: %s\n", strerror(errno)); |
|
|
|
|
cap_free(currCaps); |
|
|
|
|
cap_free(caps); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cap_free(currCaps); |
|
|
|
|
cap_free(caps); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
|
|
|
|
|
/* initialize locale */ |
|
|
|
|
@ -399,12 +269,7 @@ int main(int argc, char** argv) { |
|
|
|
|
else |
|
|
|
|
setlocale(LC_CTYPE, ""); |
|
|
|
|
|
|
|
|
|
CommandLineSettings flags = parseArguments(argc, argv); |
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBCAP |
|
|
|
|
if (dropCapabilities(flags.capabilitiesMode) < 0) |
|
|
|
|
exit(1); |
|
|
|
|
#endif |
|
|
|
|
CommandLineSettings flags = parseArguments(PACKAGE, argc, argv); |
|
|
|
|
|
|
|
|
|
Platform_init(); |
|
|
|
|
|
|
|
|
|
|