When parsing an essential pid entry file like 'status' fails, we treat
the process as a short-lived one and skip adding it into the process
table.
This should be done before the process is added, as the goto label used
for error handling can free the process structure, thus causing an
use-after-free scenario.
Fixes: 22d25db46 ("Linux: detect container process by different PID namespace")
Closes: #1455
Since \n is used internally by htop to split command lines, replace \n
with \r in command lines to not display them as space.
Merging the command string will not work, but newlines in commands
should be rather the exception.
Deprecate the use of 'st' and other names. The 'sb' name is often seen
in example codes in Linux man pages. (The 'statbuf' name is sometimes
also used but I choose 'sb' name because it's shorter.)
No code changes.
It is inappropriate to use the 'fd' name for 'FILE*' variables.
POSIX file descriptiors are of type 'int' and are distinguished from
ISO C stream pointers (type 'FILE*').
Rename these variables to 'fp', which is a preferred naming in POSIX.
(Note that ISO C preferred the 'stream' name for the variables.)
No code changes.
Container engines like docker and podman rely on Linux namespaces. If
available check if the target process is inside a different PID
namespace than htop. This check will mark sandbox'ed applications, e.g.
under bubblewrap, but not light-wight containers, like distrobox.
#1211 showed reading /proc/<pid>/status might have a significant
performance impact. Do not read by default only if actually needed,
since the permitted capabilities are no longer gather from it.
Improves: 8ea144df ("Linux: Refactor /proc/<pid>/status parsing")
Improves: #1211
Document for each block gathering information about a task whether the
information is shared for the whole process or specific to a single
userland thread.
Also avoid system calls for process-shared information and reuse the
information from the main task.
#1211 has shown reading /proc/<pid>/status might have a significant
performance impact. It was started to be read by default to gather the
permitted capabilities of the process.
Gather permitted capabilities via the syscall capget(2) instead.
cap_get_proc(3) is not used to avoid linking with -lcap.
Compare the return value of sscanf(3) and fscanf(3) explicitly against
the expected number of parsed items and avoid implicit boolean
conversion. Such an implicit conversion would treat EOF (-1) the same
as at least one item parsed successfully.
Reported by CodeQL.
Make it more clear the variable stores the previous tty number (similar
to lasttimes), while the current one gets set in the intermediate
LinuxProcessTable_readStatFile().
Following up with some discusson from a few months back,
where it was proposed that ProcessTable is a better name.
This data structure is definitely not a list ... if it
was one-dimensional it'd be a set, but in practice it has
much more in common with a two-dimensional table.
The Process table is a familiar operating system concept
for many people too so it resonates a little in that way
as well.
This commit refactors the Process and ProcessList structures such
they each have a new parent - Row and Table, respectively. These
new classes handle screen updates relating to anything that could
be represented in tabular format, e.g. cgroups, filesystems, etc,
without us having to reimplement the display logic repeatedly for
each new entity.
* Use saturatingSub() when subtracting CPU time and I/O read/write
bytes of processes so that the values would never go negative (causing unsigned integer wraparound).
* The CPU percentages of processes are now NaN if the time interval
between measures (the "delta time") is zero. Make the conditional
explicit and avoid division by zero. Previously the percentage values
would be 0.0.
Note: The saturatingSub() function is not used in cpu_delay_percent,
blkio_delay_percent and swapin_delay_percent with a comment added that
explains the reason.
Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
The recent split of machine-wide metric gathering from process metrics
gathering introduced a regression in the Linux running task accounting.
The way we extract this value from /proc/stat (which typically is used
for machine-wide metrics) conspired with a now-incorrectly placed init-
to-zero at the start of the per-process sampling, caused this value to
always be zero by the time the Tasks Meter used it.
Fix this by making it much more clear that Linux has this special case
handling of runningTasks.
Resolves#1256
First stage in sanitizing the process list structure so that htop
can support other types of lists too (cgroups, filesystems, ...),
in the not-too-distant future.
This introduces struct Machine for system-wide information while
keeping process-list information in ProcessList (now much less).
Next step is to propogate this separation into each platform, to
match these core changes.
This removes the duplication of dynamic meter/column hashtable
pointers that has come in between the Settings and ProcessList
structures - only one copy of these is needed. With the future
planned dynamic screens feature adding another pointer, let us
first clean this up before any further duplication happens.
Below is a small example of two threads program, whose main thread
statistics is incorrect in htop. Despite main thread is permanently
sleeping, its CPU load is 100% (must be 0%). CPU load of secondary
thread is correct (100%).
void *thread_func(void *data)
{
while(1) {
}
return NULL;
}
int main()
{
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
pthread_join(thread, NULL);
}
The reason is in there is a difference in behavior of some files
in /proc/pid and /proc/pid/task/pid directories. For example,
/proc/pid/stat shows agregated user and sys times for all threads
of a process. For the details please see do_task_stat() implementation
and this function behavior difference in dependence of last argument:
https://elixir.bootlin.com/linux/v6.2.8/source/fs/proc/array.c#L652
So, the problem occurs because of user and sys times of main thread are
polluted by secondary thread statistics. This patch fixes the problem
by reading correct stat from /proc/pid/task/pid/stat for main thread.
Looking at other files from /proc/pid directory I found /proc/pid/io
file with the same problem:
https://elixir.bootlin.com/linux/v6.2.8/source/fs/proc/base.c#L3029
This problem is also fixed in this patch by reading correct data
from /proc/pid/task/pid/io file for main thread.
/proc/pid directory files are declared in tgid_base_stuff, while
/proc/pid/task/tid directory files are declared in tid_base_stuff
in kernel's fs/proc/base.c file:
https://elixir.bootlin.com/linux/v6.2.8/source/fs/proc/base.c#L3238
I checked the difference between rest of declarations and it looks like
there is no more problems of such type affecting htop.
LXC shows the real host CPU ids but can be limited in configuration
as to which cores are used. Still the sysfs files are visible and the
CPUs (stay) marked online. We will need to parse
/sys/devices/system/cpu/online to follow LXC's logic.
Revert for now until we can come up with a better handling of the LXC hacks.
Cf. issue #1195
Essentially reverting 33973f7e and 0d53245c (#993, #995)
Highlight processes started from binaries with file capabilities set,
like kwin_wayland, or retaining Linux capabilities, via the ambient set,
after switching from the root user, e.g. rtkit.
Merge parsing /proc/<pid>/status for context switches, vserver ID and
container detection into one function.
(OpenVZ still does a separate cycle but is left as an exercise for
someone actually using it.)
Add a process column for scheduling policy to show the current
scheduling policy of the process.
Add a the ability to change the scheduling policy of a process via the
key 'Y'.
Currently implemented on Linux and FreeBSD only but should be portable,
since sched_getscheduler(2) is part of POSIX.1-2001.
Closes: #1161
This includes:
- Wrap function implementations
- Pointer alignment for function signatures
- Pointer alignment for variable declarations
- Whitespace after keywords
- Whitespace after comma
- Whitespace around initializers
- Whitespace around operators
- Code indentation
- Line break for single line statements
- Misleading alignment
Add actionToggle and fix LinuxProcessList_checkPidNamespace
Read cgroup file irrespective of flags
Improve logic to check if running in container
Add isContainerOrVMSlice()
Also change "(Process *)lp" to "proc"
Remove check for root slice
Remove Process_isRunningInContainer
Co-authored-by: BenBE <BenBE@geshi.org>