diff options
author | Daniel Lange <DLange@git.local> | 2023-02-05 04:25:56 +0100 |
---|---|---|
committer | Daniel Lange <DLange@git.local> | 2023-02-05 04:25:56 +0100 |
commit | f288666edc9180a2e81e6655951878124f321df6 (patch) | |
tree | da70bf44b2423f6f8e9a070c063fed79d190b489 /linux/LinuxProcessList.c | |
parent | 937052b231259a47d881d539ad5748245ef55b99 (diff) | |
download | debian_htop-f288666edc9180a2e81e6655951878124f321df6.tar.gz debian_htop-f288666edc9180a2e81e6655951878124f321df6.tar.bz2 debian_htop-f288666edc9180a2e81e6655951878124f321df6.zip |
New upstream version 3.2.2upstream/3.2.2
Diffstat (limited to 'linux/LinuxProcessList.c')
-rw-r--r-- | linux/LinuxProcessList.c | 95 |
1 files changed, 74 insertions, 21 deletions
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 45b045c..eca9459 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -10,6 +10,7 @@ in the source distribution for its full text. #include "linux/LinuxProcessList.h" #include <assert.h> +#include <ctype.h> #include <dirent.h> #include <errno.h> #include <fcntl.h> @@ -181,7 +182,7 @@ static unsigned int scanAvailableCPUsFromCPUinfo(LinuxProcessList* this) { if (String_startsWith(buffer, "processor")) availableCPUs++; - } + } fclose(file); @@ -219,7 +220,7 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { if (!String_startsWith(entry->d_name, "cpu")) continue; - char *endp; + char* endp; unsigned long int id = strtoul(entry->d_name + 3, &endp, 10); if (id == ULONG_MAX || endp == entry->d_name + 3 || *endp != '\0') continue; @@ -263,9 +264,9 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { return; if (Running_containerized) { - /* LXC munges /proc/cpuinfo but not the /sys/devices/system/cpu/ files, - * so limit the visible CPUs to what the guest has been configured to see: */ - currExisting = active = scanAvailableCPUsFromCPUinfo(this); + /* LXC munges /proc/cpuinfo but not the /sys/devices/system/cpu/ files, + * so limit the visible CPUs to what the guest has been configured to see: */ + currExisting = active = scanAvailableCPUsFromCPUinfo(this); } #ifdef HAVE_SENSORS_SENSORS_H @@ -671,7 +672,7 @@ static void LinuxProcessList_readMaps(LinuxProcess* process, openat_arg_t procFd if (' ' != *readptr++) continue; - while(*readptr > ' ') + while (*readptr > ' ') readptr++; // Skip parsing this hex value if (' ' != *readptr++) continue; @@ -763,6 +764,43 @@ static bool LinuxProcessList_readStatmFile(LinuxProcess* process, openat_arg_t p return r == 7; } +static bool LinuxProcessList_checkPidNamespace(Process* process, openat_arg_t procFd) { + FILE* statusfile = fopenat(procFd, "status", "r"); + if (!statusfile) + return false; + + while (true) { + char buffer[PROC_LINE_LENGTH + 1]; + if (fgets(buffer, sizeof(buffer), statusfile) == NULL) + break; + + if (!String_startsWith(buffer, "NSpid:")) + continue; + + char* ptr = buffer; + int pid_ns_count = 0; + while (*ptr && *ptr != '\n' && !isdigit(*ptr)) + ++ptr; + + while (*ptr && *ptr != '\n') { + if (isdigit(*ptr)) + pid_ns_count++; + while (isdigit(*ptr)) + ++ptr; + while (*ptr && *ptr != '\n' && !isdigit(*ptr)) + ++ptr; + } + + if (pid_ns_count > 1) + process->isRunningInContainer = true; + + break; + } + + fclose(statusfile); + return true; +} + static bool LinuxProcessList_readSmapsFile(LinuxProcess* process, openat_arg_t procFd, bool haveSmapsRollup) { //http://elixir.free-electrons.com/linux/v4.10/source/fs/proc/task_mmu.c#L719 //kernel will return data in chunks of size PAGE_SIZE or less. @@ -851,7 +889,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t char* value_end = name_value_sep; - while(*value_end > 32) { + while (*value_end > 32) { value_end++; } @@ -861,7 +899,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t *value_end = '\0'; - switch(field) { + switch (field) { case 1: foundEnvID = true; if (!String_eq(name_value_sep, process->ctid ? process->ctid : "")) @@ -891,6 +929,13 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t #endif +static bool isContainerOrVMSlice(char* cgroup) { + if (String_startsWith(cgroup, "/user") || String_startsWith(cgroup, "/system")) + return false; + + return true; +} + static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t procFd) { FILE* file = fopenat(procFd, "cgroup", "r"); if (!file) { @@ -941,7 +986,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t free_and_xStrdup(&process->cgroup, output); if (!changed) { - if(process->cgroup_short) { + if (process->cgroup_short) { Process_updateFieldWidth(CCGROUP, strlen(process->cgroup_short)); } else { //CCGROUP is alias to normal CGROUP if shortening fails @@ -1090,9 +1135,7 @@ static void LinuxProcessList_readCwd(LinuxProcess* process, openat_arg_t procFd) #if defined(HAVE_READLINKAT) && defined(HAVE_OPENAT) ssize_t r = readlinkat(procFd, "cwd", pathBuffer, sizeof(pathBuffer) - 1); #else - char filename[MAX_NAME + 1]; - xSnprintf(filename, sizeof(filename), "%s/cwd", procFd); - ssize_t r = readlink(filename, pathBuffer, sizeof(pathBuffer) - 1); + ssize_t r = Compat_readlink(procFd, "cwd", pathBuffer, sizeof(pathBuffer) - 1); #endif if (r < 0) { @@ -1329,9 +1372,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc #if defined(HAVE_READLINKAT) && defined(HAVE_OPENAT) amtRead = readlinkat(procFd, "exe", filename, sizeof(filename) - 1); #else - char path[4096]; - xSnprintf(path, sizeof(path), "%s/exe", procFd); - amtRead = readlink(path, filename, sizeof(filename) - 1); + amtRead = Compat_readlink(procFd, "exe", filename, sizeof(filename) - 1); #endif if (amtRead > 0) { filename[amtRead] = 0; @@ -1423,7 +1464,7 @@ static bool isOlderThan(const ProcessList* pl, const Process* proc, unsigned int /* Starttime might not yet be parsed */ if (proc->starttime_ctime <= 0) - return false; + return false; uint64_t realtime = pl->realtimeMs / 1000; @@ -1457,6 +1498,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ const unsigned int activeCPUs = pl->activeCPUs; const bool hideKernelThreads = settings->hideKernelThreads; const bool hideUserlandThreads = settings->hideUserlandThreads; + const bool hideRunningInContainer = settings->hideRunningInContainer; while ((entry = readdir(dir)) != NULL) { const char* name = entry->d_name; @@ -1508,6 +1550,15 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ LinuxProcessList_recurseProcTree(this, procFd, "task", proc, period); + if (ss->flags & PROCESS_FLAG_LINUX_CGROUP || hideRunningInContainer) { + LinuxProcessList_readCGroupFile(lp, procFd); + if (hideRunningInContainer && lp->cgroup && isContainerOrVMSlice(lp->cgroup)) { + if (!LinuxProcessList_checkPidNamespace(proc, procFd)) { + goto errorReadingProcess; + } + } + } + /* * These conditions will not trigger on first occurrence, cause we need to * add the process to the ProcessList and do all one time scans @@ -1530,6 +1581,12 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ Compat_openatArgClose(procFd); continue; } + if (preExisting && hideRunningInContainer && proc->isRunningInContainer) { + proc->updated = true; + proc->show = false; + Compat_openatArgClose(procFd); + continue; + } if (ss->flags & PROCESS_FLAG_IO) LinuxProcessList_readIoFile(lp, procFd, pl->realtimeMs); @@ -1580,7 +1637,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ char statCommand[MAX_NAME + 1]; unsigned long long int lasttimes = (lp->utime + lp->stime); unsigned long int tty_nr = proc->tty_nr; - if (! LinuxProcessList_readStatFile(proc, procFd, statCommand, sizeof(statCommand))) + if (!LinuxProcessList_readStatFile(proc, procFd, statCommand, sizeof(statCommand))) goto errorReadingProcess; if (lp->flags & PF_KTHREAD) { @@ -1644,10 +1701,6 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ } #endif - if (ss->flags & PROCESS_FLAG_LINUX_CGROUP) { - LinuxProcessList_readCGroupFile(lp, procFd); - } - if (ss->flags & PROCESS_FLAG_LINUX_OOM) { LinuxProcessList_readOomData(lp, procFd); } |