blob: f56f8fd6a9207864777a64b3264ddcab9881802b [file] [log] [blame]
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00001/*
2htop - Process.c
Hisham Muhammadb1b3f572015-03-21 16:52:54 -03003(C) 2004-2015 Hisham H. Muhammad
Nathan Scott500fb282020-08-20 09:35:24 +10004(C) 2020 Red Hat, Inc. All Rights Reserved.
Daniel Lange079c2ab2020-10-05 09:51:32 +02005Released under the GNU GPLv2, see the COPYING file
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00006in the source distribution for its full text.
7*/
8
Benny Baumann0f526292020-09-19 13:55:23 +02009#include "config.h" // IWYU pragma: keep
Hisham Muhammad272e2d92015-03-16 23:01:48 -030010
Benny Baumann0f526292020-09-19 13:55:23 +020011#include "Process.h"
12
13#include <assert.h>
14#include <limits.h>
15#include <math.h>
16#include <signal.h>
17#include <stdbool.h>
18#include <stdio.h>
19#include <stdlib.h>
mayurdahibhate1b74dfe2021-04-29 20:42:43 +053020#include <string.h>
Benny Baumann0f526292020-09-19 13:55:23 +020021#include <time.h>
22#include <unistd.h>
23#include <sys/resource.h>
Explorer0935129712018-12-30 12:18:27 +080024
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000025#include "CRT.h"
Christian Göttsche7cf52772020-11-18 14:26:30 +010026#include "Macros.h"
Hisham Muhammadb4f6b112014-11-27 20:10:23 -020027#include "Platform.h"
Christian Göttsche7cf52772020-11-18 14:26:30 +010028#include "ProcessList.h"
Benny Baumann0f526292020-09-19 13:55:23 +020029#include "RichString.h"
30#include "Settings.h"
Benny Baumann872e5422020-10-14 20:21:09 +020031#include "XUtils.h"
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000032
Benny Baumann0f526292020-09-19 13:55:23 +020033#if defined(MAJOR_IN_MKDEV)
Kang-Che Sung (宋岡哲)c01f40e2018-02-26 21:15:05 +080034#include <sys/mkdev.h>
Kang-Che Sung (宋岡哲)c01f40e2018-02-26 21:15:05 +080035#endif
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000036
Benny Baumann0f526292020-09-19 13:55:23 +020037
Benny Baumannaa8552b2021-04-18 19:25:56 +020038/* Used to identify kernel threads in Comm and Exe columns */
39static const char *const kthreadID = "KTHREAD";
40
Christian Göttsche42073ba2020-11-04 17:46:04 +010041static uid_t Process_getuid = (uid_t)-1;
Hisham Muhammadeb2803c2006-07-12 01:35:59 +000042
Christian Göttsche9f68c8d2020-12-15 19:44:52 +010043int Process_pidDigits = 7;
Hisham Muhammad94280102015-08-20 00:32:47 -030044
45void Process_setupColumnWidths() {
46 int maxPid = Platform_getMaxPid();
Benny Baumann45869512020-11-01 01:09:51 +010047 if (maxPid == -1)
48 return;
49
Christian Göttsche9f68c8d2020-12-15 19:44:52 +010050 Process_pidDigits = ceil(log10(maxPid));
51 assert(Process_pidDigits <= PROCESS_MAX_PID_DIGITS);
Hisham Muhammad94280102015-08-20 00:32:47 -030052}
53
Christian Göttscheb41e4d92021-04-14 20:16:16 +020054void Process_printBytes(RichString* str, unsigned long long number, bool coloring) {
55 char buffer[16];
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000056 int len;
Daniel Flanagandd334442019-10-31 11:39:12 -050057
Christian Göttschefdaa15b2021-01-27 15:11:48 +010058 int largeNumberColor = coloring ? CRT_colors[LARGE_NUMBER] : CRT_colors[PROCESS];
59 int processMegabytesColor = coloring ? CRT_colors[PROCESS_MEGABYTES] : CRT_colors[PROCESS];
60 int processGigabytesColor = coloring ? CRT_colors[PROCESS_GIGABYTES] : CRT_colors[PROCESS];
61 int shadowColor = coloring ? CRT_colors[PROCESS_SHADOW] : CRT_colors[PROCESS];
Hisham Muhammada939cdf2014-04-24 15:00:09 -030062 int processColor = CRT_colors[PROCESS];
Daniel Flanagandd334442019-10-31 11:39:12 -050063
Christian Göttschefdaa15b2021-01-27 15:11:48 +010064 if (number == ULLONG_MAX) {
65 //Invalid number
66 RichString_appendAscii(str, shadowColor, " N/A ");
Christian Göttscheb41e4d92021-04-14 20:16:16 +020067 return;
68 }
69
70 number /= ONE_K;
71
72 if (number < 1000) {
Benny Baumann40441dc2020-09-13 23:50:24 +020073 //Plain number, no markings
Daniel Lange7899ae22020-11-28 17:57:51 +010074 len = xSnprintf(buffer, sizeof(buffer), "%5llu ", number);
Christian Göttsche157086e2020-12-04 14:44:57 +010075 RichString_appendnAscii(str, processColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +020076 } else if (number < 100000) {
77 //2 digit MB, 3 digit KB
Daniel Lange7899ae22020-11-28 17:57:51 +010078 len = xSnprintf(buffer, sizeof(buffer), "%2llu", number/1000);
Christian Göttsche157086e2020-12-04 14:44:57 +010079 RichString_appendnAscii(str, processMegabytesColor, buffer, len);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000080 number %= 1000;
Daniel Lange7899ae22020-11-28 17:57:51 +010081 len = xSnprintf(buffer, sizeof(buffer), "%03llu ", number);
Christian Göttsche157086e2020-12-04 14:44:57 +010082 RichString_appendnAscii(str, processColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +020083 } else if (number < 1000 * ONE_K) {
84 //3 digit MB
85 number /= ONE_K;
Daniel Lange7899ae22020-11-28 17:57:51 +010086 len = xSnprintf(buffer, sizeof(buffer), "%4lluM ", number);
Christian Göttsche157086e2020-12-04 14:44:57 +010087 RichString_appendnAscii(str, processMegabytesColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +020088 } else if (number < 10000 * ONE_K) {
89 //1 digit GB, 3 digit MB
90 number /= ONE_K;
Daniel Lange7899ae22020-11-28 17:57:51 +010091 len = xSnprintf(buffer, sizeof(buffer), "%1llu", number/1000);
Christian Göttsche157086e2020-12-04 14:44:57 +010092 RichString_appendnAscii(str, processGigabytesColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +020093 number %= 1000;
Daniel Lange7899ae22020-11-28 17:57:51 +010094 len = xSnprintf(buffer, sizeof(buffer), "%03lluM ", number);
Christian Göttsche157086e2020-12-04 14:44:57 +010095 RichString_appendnAscii(str, processMegabytesColor, buffer, len);
Benny Baumanne0e59972020-09-20 19:54:53 +020096 } else if (number < 100000 * ONE_K) {
Benny Baumann40441dc2020-09-13 23:50:24 +020097 //2 digit GB, 1 digit MB
98 number /= 100 * ONE_K;
Daniel Lange7899ae22020-11-28 17:57:51 +010099 len = xSnprintf(buffer, sizeof(buffer), "%2llu", number/10);
Christian Göttsche157086e2020-12-04 14:44:57 +0100100 RichString_appendnAscii(str, processGigabytesColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +0200101 number %= 10;
Christian Göttschecd305b42020-11-29 14:14:46 +0100102 len = xSnprintf(buffer, sizeof(buffer), ".%1llu", number);
Christian Göttsche157086e2020-12-04 14:44:57 +0100103 RichString_appendnAscii(str, processMegabytesColor, buffer, len);
104 RichString_appendAscii(str, processGigabytesColor, "G ");
Benny Baumann40441dc2020-09-13 23:50:24 +0200105 } else if (number < 1000 * ONE_M) {
106 //3 digit GB
107 number /= ONE_M;
Daniel Lange7899ae22020-11-28 17:57:51 +0100108 len = xSnprintf(buffer, sizeof(buffer), "%4lluG ", number);
Christian Göttsche157086e2020-12-04 14:44:57 +0100109 RichString_appendnAscii(str, processGigabytesColor, buffer, len);
Michael Wittenab3171d2020-09-29 14:04:22 +0000110 } else if (number < 10000ULL * ONE_M) {
Benny Baumann40441dc2020-09-13 23:50:24 +0200111 //1 digit TB, 3 digit GB
112 number /= ONE_M;
Daniel Lange7899ae22020-11-28 17:57:51 +0100113 len = xSnprintf(buffer, sizeof(buffer), "%1llu", number/1000);
Christian Göttsche157086e2020-12-04 14:44:57 +0100114 RichString_appendnAscii(str, largeNumberColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +0200115 number %= 1000;
Daniel Lange7899ae22020-11-28 17:57:51 +0100116 len = xSnprintf(buffer, sizeof(buffer), "%03lluG ", number);
Christian Göttsche157086e2020-12-04 14:44:57 +0100117 RichString_appendnAscii(str, processGigabytesColor, buffer, len);
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200118 } else if (number < 100000 * ONE_M) {
119 //2 digit TB, 1 digit GB
120 number /= 100 * ONE_M;
121 len = xSnprintf(buffer, sizeof(buffer), "%2llu", number/10);
122 RichString_appendnAscii(str, largeNumberColor, buffer, len);
123 number %= 10;
124 len = xSnprintf(buffer, sizeof(buffer), ".%1llu", number);
125 RichString_appendnAscii(str, processGigabytesColor, buffer, len);
126 RichString_appendAscii(str, largeNumberColor, "T ");
127 } else if (number < 10000ULL * ONE_G) {
128 //3 digit TB or 1 digit PB, 3 digit TB
129 number /= ONE_G;
130 len = xSnprintf(buffer, sizeof(buffer), "%4lluT ", number);
131 RichString_appendnAscii(str, largeNumberColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +0200132 } else {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200133 //2 digit PB and above
134 len = xSnprintf(buffer, sizeof(buffer), "%4.1lfP ", (double)number/ONE_T);
Christian Göttsche157086e2020-12-04 14:44:57 +0100135 RichString_appendnAscii(str, largeNumberColor, buffer, len);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000136 }
137}
138
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200139void Process_printKBytes(RichString* str, unsigned long long number, bool coloring) {
140 if (number == ULLONG_MAX)
141 Process_printBytes(str, ULLONG_MAX, coloring);
142 else
143 Process_printBytes(str, number * ONE_K, coloring);
144}
145
146void Process_printCount(RichString* str, unsigned long long number, bool coloring) {
Daniel Lange7899ae22020-11-28 17:57:51 +0100147 char buffer[13];
Hisham Muhammada939cdf2014-04-24 15:00:09 -0300148
Christian Göttschefee744a2021-01-27 15:11:46 +0100149 int largeNumberColor = coloring ? CRT_colors[LARGE_NUMBER] : CRT_colors[PROCESS];
150 int processMegabytesColor = coloring ? CRT_colors[PROCESS_MEGABYTES] : CRT_colors[PROCESS];
Hisham Muhammada939cdf2014-04-24 15:00:09 -0300151 int processColor = CRT_colors[PROCESS];
Christian Göttschefee744a2021-01-27 15:11:46 +0100152 int processShadowColor = coloring ? CRT_colors[PROCESS_SHADOW] : CRT_colors[PROCESS];
Hisham Muhammada939cdf2014-04-24 15:00:09 -0300153
Michael Wittenab3171d2020-09-29 14:04:22 +0000154 if (number == ULLONG_MAX) {
Christian Göttsche157086e2020-12-04 14:44:57 +0100155 RichString_appendAscii(str, CRT_colors[PROCESS_SHADOW], " N/A ");
adrien1018536941f2018-12-30 20:18:35 +0800156 } else if (number >= 100000LL * ONE_DECIMAL_T) {
Benny Baumann0b29e502020-11-28 17:42:02 +0100157 xSnprintf(buffer, sizeof(buffer), "%11llu ", number / ONE_DECIMAL_G);
Christian Göttsche157086e2020-12-04 14:44:57 +0100158 RichString_appendnAscii(str, largeNumberColor, buffer, 12);
adrien1018536941f2018-12-30 20:18:35 +0800159 } else if (number >= 100LL * ONE_DECIMAL_T) {
Benny Baumann0b29e502020-11-28 17:42:02 +0100160 xSnprintf(buffer, sizeof(buffer), "%11llu ", number / ONE_DECIMAL_M);
Christian Göttsche157086e2020-12-04 14:44:57 +0100161 RichString_appendnAscii(str, largeNumberColor, buffer, 8);
162 RichString_appendnAscii(str, processMegabytesColor, buffer+8, 4);
adrien1018536941f2018-12-30 20:18:35 +0800163 } else if (number >= 10LL * ONE_DECIMAL_G) {
Benny Baumann0b29e502020-11-28 17:42:02 +0100164 xSnprintf(buffer, sizeof(buffer), "%11llu ", number / ONE_DECIMAL_K);
Christian Göttsche157086e2020-12-04 14:44:57 +0100165 RichString_appendnAscii(str, largeNumberColor, buffer, 5);
166 RichString_appendnAscii(str, processMegabytesColor, buffer+5, 3);
167 RichString_appendnAscii(str, processColor, buffer+8, 4);
Hisham Muhammad9b351402011-05-26 16:31:18 +0000168 } else {
Benny Baumann0b29e502020-11-28 17:42:02 +0100169 xSnprintf(buffer, sizeof(buffer), "%11llu ", number);
Christian Göttsche157086e2020-12-04 14:44:57 +0100170 RichString_appendnAscii(str, largeNumberColor, buffer, 2);
171 RichString_appendnAscii(str, processMegabytesColor, buffer+2, 3);
172 RichString_appendnAscii(str, processColor, buffer+5, 3);
173 RichString_appendnAscii(str, processShadowColor, buffer+8, 4);
Hisham Muhammad9b351402011-05-26 16:31:18 +0000174 }
175}
176
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200177void Process_printTime(RichString* str, unsigned long long totalHundredths, bool coloring) {
178 char buffer[10];
179 int len;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000180
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200181 unsigned long long totalSeconds = totalHundredths / 100;
Hisham Muhammad272e2d92015-03-16 23:01:48 -0300182 unsigned long long hours = totalSeconds / 3600;
David Zarzyckif3d9eca2021-04-10 08:02:59 -0400183 unsigned long long days = totalSeconds / 86400;
Hisham Muhammad272e2d92015-03-16 23:01:48 -0300184 int minutes = (totalSeconds / 60) % 60;
185 int seconds = totalSeconds % 60;
186 int hundredths = totalHundredths - (totalSeconds * 100);
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200187
188 int yearColor = coloring ? CRT_colors[LARGE_NUMBER] : CRT_colors[PROCESS];
189 int dayColor = coloring ? CRT_colors[PROCESS_GIGABYTES] : CRT_colors[PROCESS];
190 int hourColor = coloring ? CRT_colors[PROCESS_MEGABYTES] : CRT_colors[PROCESS];
191 int defColor = CRT_colors[PROCESS];
192
193 if (days >= /* Ignore leapyears */365) {
194 int years = days / 365;
195 int daysLeft = days - 365 * years;
196
197 if (daysLeft >= 100) {
198 len = xSnprintf(buffer, sizeof(buffer), "%3dy", years);
199 RichString_appendnAscii(str, yearColor, buffer, len);
200 len = xSnprintf(buffer, sizeof(buffer), "%3dd ", daysLeft);
201 RichString_appendnAscii(str, dayColor, buffer, len);
202 } else if (daysLeft >= 10) {
203 len = xSnprintf(buffer, sizeof(buffer), "%4dy", years);
204 RichString_appendnAscii(str, yearColor, buffer, len);
205 len = xSnprintf(buffer, sizeof(buffer), "%2dd ", daysLeft);
206 RichString_appendnAscii(str, dayColor, buffer, len);
Hisham Muhammad9c44f582011-12-14 23:29:07 +0000207 } else {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200208 len = xSnprintf(buffer, sizeof(buffer), "%5dy", years);
209 RichString_appendnAscii(str, yearColor, buffer, len);
210 len = xSnprintf(buffer, sizeof(buffer), "%1dd ", daysLeft);
211 RichString_appendnAscii(str, dayColor, buffer, len);
Hisham Muhammad9c44f582011-12-14 23:29:07 +0000212 }
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200213 } else if (days >= 100) {
214 int hoursLeft = hours - days * 24;
215
216 if (hoursLeft >= 10) {
217 len = xSnprintf(buffer, sizeof(buffer), "%4llud", days);
218 RichString_appendnAscii(str, dayColor, buffer, len);
219 len = xSnprintf(buffer, sizeof(buffer), "%2dh ", hoursLeft);
220 RichString_appendnAscii(str, hourColor, buffer, len);
221 } else {
222 len = xSnprintf(buffer, sizeof(buffer), "%5llud", days);
223 RichString_appendnAscii(str, dayColor, buffer, len);
224 len = xSnprintf(buffer, sizeof(buffer), "%1dh ", hoursLeft);
225 RichString_appendnAscii(str, hourColor, buffer, len);
226 }
227 } else if (hours >= 100) {
228 int minutesLeft = totalSeconds / 60 - hours * 60;
229
230 if (minutesLeft >= 10) {
231 len = xSnprintf(buffer, sizeof(buffer), "%4lluh", hours);
232 RichString_appendnAscii(str, hourColor, buffer, len);
233 len = xSnprintf(buffer, sizeof(buffer), "%2dm ", minutesLeft);
234 RichString_appendnAscii(str, defColor, buffer, len);
235 } else {
236 len = xSnprintf(buffer, sizeof(buffer), "%5lluh", hours);
237 RichString_appendnAscii(str, hourColor, buffer, len);
238 len = xSnprintf(buffer, sizeof(buffer), "%1dm ", minutesLeft);
239 RichString_appendnAscii(str, defColor, buffer, len);
240 }
241 } else if (hours > 0) {
242 len = xSnprintf(buffer, sizeof(buffer), "%2lluh", hours);
243 RichString_appendnAscii(str, hourColor, buffer, len);
244 len = xSnprintf(buffer, sizeof(buffer), "%02d:%02d ", minutes, seconds);
245 RichString_appendnAscii(str, defColor, buffer, len);
246 } else {
247 len = xSnprintf(buffer, sizeof(buffer), "%2d:%02d.%02d ", minutes, seconds, hundredths);
248 RichString_appendnAscii(str, defColor, buffer, len);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000249 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000250}
251
Christian Göttschea63cfc82020-10-13 14:26:40 +0200252void Process_fillStarttimeBuffer(Process* this) {
253 struct tm date;
254 (void) localtime_r(&this->starttime_ctime, &date);
255 strftime(this->starttime_show, sizeof(this->starttime_show) - 1, (this->starttime_ctime > (time(NULL) - 86400)) ? "%R " : "%b%d ", &date);
256}
257
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200258/*
259 * TASK_COMM_LEN is defined to be 16 for /proc/[pid]/comm in man proc(5), but it is
260 * not available in an userspace header - so define it.
261 *
262 * Note: This is taken from LINUX headers, but implicitly taken for other platforms
263 * for sake of brevity.
264 *
265 * Note: when colorizing a basename with the comm prefix, the entire basename
266 * (not just the comm prefix) is colorized for better readability, and it is
267 * implicit that only upto (TASK_COMM_LEN - 1) could be comm.
268 */
269#define TASK_COMM_LEN 16
Tobias Geerinckx-Rice293eec42015-07-29 21:14:29 +0200270
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200271static bool findCommInCmdline(const char *comm, const char *cmdline, int cmdlineBasenameStart, int *pCommStart, int *pCommEnd) {
272 /* Try to find procComm in tokenized cmdline - this might in rare cases
273 * mis-identify a string or fail, if comm or cmdline had been unsuitably
274 * modified by the process */
275 const char *tokenBase;
276 size_t tokenLen;
277 const size_t commLen = strlen(comm);
278
279 if (cmdlineBasenameStart < 0)
280 return false;
281
282 for (const char *token = cmdline + cmdlineBasenameStart; *token;) {
283 for (tokenBase = token; *token && *token != '\n'; ++token) {
284 if (*token == '/') {
285 tokenBase = token + 1;
Hisham Muhammadf2a190b2014-02-27 17:11:23 -0300286 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000287 }
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200288 tokenLen = token - tokenBase;
289
290 if ((tokenLen == commLen || (tokenLen > commLen && commLen == (TASK_COMM_LEN - 1))) &&
291 strncmp(tokenBase, comm, commLen) == 0) {
292 *pCommStart = tokenBase - cmdline;
293 *pCommEnd = token - cmdline;
294 return true;
295 }
296
297 if (*token) {
298 do {
299 ++token;
300 } while (*token && '\n' == *token);
Tobias Geerinckx-Rice293eec42015-07-29 21:14:29 +0200301 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000302 }
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200303 return false;
304}
Tobias Geerinckx-Rice293eec42015-07-29 21:14:29 +0200305
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200306static int matchCmdlinePrefixWithExeSuffix(const char *cmdline, int cmdlineBaseOffset, const char *exe, int exeBaseOffset, int exeBaseLen) {
307 int matchLen; /* matching length to be returned */
308 char delim; /* delimiter following basename */
Tobias Geerinckx-Rice293eec42015-07-29 21:14:29 +0200309
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200310 /* cmdline prefix is an absolute path: it must match whole exe. */
311 if (cmdline[0] == '/') {
312 matchLen = exeBaseLen + exeBaseOffset;
313 if (strncmp(cmdline, exe, matchLen) == 0) {
314 delim = cmdline[matchLen];
315 if (delim == 0 || delim == '\n' || delim == ' ') {
316 return matchLen;
317 }
318 }
319 return 0;
320 }
321
322 /* cmdline prefix is a relative path: We need to first match the basename at
323 * cmdlineBaseOffset and then reverse match the cmdline prefix with the exe
324 * suffix. But there is a catch: Some processes modify their cmdline in ways
325 * that make htop's identification of the basename in cmdline unreliable.
326 * For e.g. /usr/libexec/gdm-session-worker modifies its cmdline to
327 * "gdm-session-worker [pam/gdm-autologin]" and htop ends up with
328 * proccmdlineBasenameEnd at "gdm-autologin]". This issue could arise with
329 * chrome as well as it stores in cmdline its concatenated argument vector,
330 * without NUL delimiter between the arguments (which may contain a '/')
331 *
332 * So if needed, we adjust cmdlineBaseOffset to the previous (if any)
333 * component of the cmdline relative path, and retry the procedure. */
334 bool delimFound; /* if valid basename delimiter found */
335 do {
336 /* match basename */
337 matchLen = exeBaseLen + cmdlineBaseOffset;
338 if (cmdlineBaseOffset < exeBaseOffset &&
339 strncmp(cmdline + cmdlineBaseOffset, exe + exeBaseOffset, exeBaseLen) == 0) {
340 delim = cmdline[matchLen];
341 if (delim == 0 || delim == '\n' || delim == ' ') {
342 int i, j;
343 /* reverse match the cmdline prefix and exe suffix */
344 for (i = cmdlineBaseOffset - 1, j = exeBaseOffset - 1;
345 i >= 0 && j >= 0 && cmdline[i] == exe[j]; --i, --j)
346 ;
347
348 /* full match, with exe suffix being a valid relative path */
349 if (i < 0 && j >= 0 && exe[j] == '/')
350 return matchLen;
351 }
352 }
353
354 /* Try to find the previous potential cmdlineBaseOffset - it would be
355 * preceded by '/' or nothing, and delimited by ' ' or '\n' */
356 for (delimFound = false, cmdlineBaseOffset -= 2; cmdlineBaseOffset > 0; --cmdlineBaseOffset) {
357 if (delimFound) {
358 if (cmdline[cmdlineBaseOffset - 1] == '/') {
359 break;
360 }
361 } else if (cmdline[cmdlineBaseOffset] == ' ' || cmdline[cmdlineBaseOffset] == '\n') {
362 delimFound = true;
363 }
364 }
365 } while (delimFound);
366
367 return 0;
368}
369
370/* stpcpy, but also converts newlines to spaces */
371static inline char *stpcpyWithNewlineConversion(char *dstStr, const char *srcStr) {
372 for (; *srcStr; ++srcStr) {
373 *dstStr++ = (*srcStr == '\n') ? ' ' : *srcStr;
374 }
375 *dstStr = 0;
376 return dstStr;
377}
378
379/*
380 * This function makes the merged Command string. It also stores the offsets of the
381 * basename, comm w.r.t the merged Command string - these offsets will be used by
382 * Process_writeCommand() for coloring. The merged Command string is also
383 * returned by Process_getCommandStr() for searching, sorting and filtering.
384 */
385void Process_makeCommandStr(Process *this) {
386 ProcessMergedCommand *mc = &this->mergedCommand;
387 const Settings *settings = this->settings;
388
389 bool showMergedCommand = settings->showMergedCommand;
390 bool showProgramPath = settings->showProgramPath;
391 bool searchCommInCmdline = settings->findCommInCmdline;
392 bool stripExeFromCmdline = settings->stripExeFromCmdline;
393
Benny Baumanna61a2e62021-04-18 18:10:04 +0200394 /* Nothing to do to (Re)Generate the Command string, if the process is:
395 * - a kernel thread, or
396 * - a zombie from before being under htop's watch, or
397 * - a user thread and showThreadNames is not set */
398 if (Process_isKernelThread(this))
399 return;
400 if (this->state == 'Z' && !this->mergedCommand.str)
401 return;
402 if (Process_isUserlandThread(this) && settings->showThreadNames)
403 return;
404
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200405 /* this->mergedCommand.str needs updating only if its state or contents changed.
406 * Its content is based on the fields cmdline, comm, and exe. */
407 if (
408 mc->prevMergeSet == showMergedCommand &&
409 mc->prevPathSet == showProgramPath &&
410 mc->prevCommSet == searchCommInCmdline &&
411 mc->prevCmdlineSet == stripExeFromCmdline &&
412 !mc->cmdlineChanged &&
413 !mc->commChanged &&
414 !mc->exeChanged
415 ) {
416 return;
417 }
418
419 /* The field separtor "│" has been chosen such that it will not match any
420 * valid string used for searching or filtering */
421 const char *SEPARATOR = CRT_treeStr[TREE_STR_VERT];
422 const int SEPARATOR_LEN = strlen(SEPARATOR);
423
424 /* Check for any changed fields since we last built this string */
425 if (mc->cmdlineChanged || mc->commChanged || mc->exeChanged) {
426 free(mc->str);
427 /* Accommodate the column text, two field separators and terminating NUL */
428 mc->str = xCalloc(1, mc->maxLen + 2 * SEPARATOR_LEN + 1);
429 }
430
431 /* Preserve the settings used in this run */
432 mc->prevMergeSet = showMergedCommand;
433 mc->prevPathSet = showProgramPath;
434 mc->prevCommSet = searchCommInCmdline;
435 mc->prevCmdlineSet = stripExeFromCmdline;
436
437 /* Mark everything as unchanged */
438 mc->cmdlineChanged = false;
439 mc->commChanged = false;
440 mc->exeChanged = false;
441
442 /* Reset all locations that need extra handling when actually displaying */
443 mc->highlightCount = 0;
444 memset(mc->highlights, 0, sizeof(mc->highlights));
445
446 size_t mbMismatch = 0;
Benny Baumann9a781552021-05-15 21:54:46 +0200447 #define WRITE_HIGHLIGHT(_offset, _length, _attr, _flags) \
448 do { \
449 /* Check if we still have capacity */ \
450 assert(mc->highlightCount < ARRAYSIZE(mc->highlights)); \
451 if (mc->highlightCount >= ARRAYSIZE(mc->highlights)) \
452 continue; \
453 \
454 mc->highlights[mc->highlightCount].offset = str - strStart + (_offset) - mbMismatch; \
455 mc->highlights[mc->highlightCount].length = _length; \
456 mc->highlights[mc->highlightCount].attr = _attr; \
457 mc->highlights[mc->highlightCount].flags = _flags; \
458 mc->highlightCount++; \
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200459 } while (0)
460
Benny Baumann9a781552021-05-15 21:54:46 +0200461 #define WRITE_SEPARATOR \
462 do { \
463 WRITE_HIGHLIGHT(0, 1, CRT_colors[FAILED_READ], CMDLINE_HIGHLIGHT_FLAG_SEPARATOR); \
464 mbMismatch += SEPARATOR_LEN - 1; \
465 str = stpcpy(str, SEPARATOR); \
Benny Baumannbcb18ef2021-04-10 13:31:39 +0200466 } while (0)
467
468 const int baseAttr = Process_isThread(this) ? CRT_colors[PROCESS_THREAD_BASENAME] : CRT_colors[PROCESS_BASENAME];
469 const int commAttr = Process_isThread(this) ? CRT_colors[PROCESS_THREAD_COMM] : CRT_colors[PROCESS_COMM];
470 const int delAttr = CRT_colors[FAILED_READ];
471
472 /* Establish some shortcuts to data we need */
473 const char *cmdline = this->cmdline;
474 const char *procComm = this->procComm;
475 const char *procExe = this->procExe;
476
477 char *strStart = mc->str;
478 char *str = strStart;
479
480 int cmdlineBasenameStart = this->cmdlineBasenameStart;
481 int cmdlineBasenameEnd = this->cmdlineBasenameEnd;
482
483 if (!cmdline) {
484 cmdlineBasenameStart = 0;
485 cmdlineBasenameEnd = 0;
486 cmdline = "(zombie)";
487 }
488
489 assert(cmdlineBasenameStart >= 0);
490 assert(cmdlineBasenameStart <= (int)strlen(cmdline));
491
492 if (!showMergedCommand || !procExe || !procComm) { /* fall back to cmdline */
493 if (showMergedCommand && !procExe && procComm && strlen(procComm)) { /* Prefix column with comm */
494 if (strncmp(cmdline + cmdlineBasenameStart, procComm, MINIMUM(TASK_COMM_LEN - 1, strlen(procComm))) != 0) {
495 WRITE_HIGHLIGHT(0, strlen(procComm), commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
496 str = stpcpy(str, procComm);
497
498 WRITE_SEPARATOR;
499 }
500 }
501
502 if (showProgramPath) {
503 WRITE_HIGHLIGHT(cmdlineBasenameStart, cmdlineBasenameEnd - cmdlineBasenameStart, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
504 (void)stpcpyWithNewlineConversion(str, cmdline);
505 } else {
506 WRITE_HIGHLIGHT(0, cmdlineBasenameEnd - cmdlineBasenameStart, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
507 (void)stpcpyWithNewlineConversion(str, cmdline + cmdlineBasenameStart);
508 }
509
510 return;
511 }
512
513 int exeLen = strlen(this->procExe);
514 int exeBasenameOffset = this->procExeBasenameOffset;
515 int exeBasenameLen = exeLen - exeBasenameOffset;
516
517 assert(exeBasenameOffset >= 0);
518 assert(exeBasenameOffset <= (int)strlen(procExe));
519
520 bool haveCommInExe = false;
521 if (procExe && procComm) {
522 haveCommInExe = strncmp(procExe + exeBasenameOffset, procComm, TASK_COMM_LEN - 1) == 0;
523 }
524
525 /* Start with copying exe */
526 if (showProgramPath) {
527 if (haveCommInExe)
528 WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
529 WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
530 if (this->procExeDeleted)
531 WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, delAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED);
532 str = stpcpy(str, procExe);
533 } else {
534 if (haveCommInExe)
535 WRITE_HIGHLIGHT(0, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
536 WRITE_HIGHLIGHT(0, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
537 if (this->procExeDeleted)
538 WRITE_HIGHLIGHT(0, exeBasenameLen, delAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED);
539 str = stpcpy(str, procExe + exeBasenameOffset);
540 }
541
542 bool haveCommInCmdline = false;
543 int commStart = 0;
544 int commEnd = 0;
545
546 /* Try to match procComm with procExe's basename: This is reliable (predictable) */
547 if (searchCommInCmdline) {
548 /* commStart/commEnd will be adjusted later along with cmdline */
549 haveCommInCmdline = findCommInCmdline(procComm, cmdline, cmdlineBasenameStart, &commStart, &commEnd);
550 }
551
552 int matchLen = matchCmdlinePrefixWithExeSuffix(cmdline, cmdlineBasenameStart, procExe, exeBasenameOffset, exeBasenameLen);
553
554 bool haveCommField = false;
555
556 if (!haveCommInExe && !haveCommInCmdline && procComm) {
557 WRITE_SEPARATOR;
558 WRITE_HIGHLIGHT(0, strlen(procComm), commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
559 str = stpcpy(str, procComm);
560 haveCommField = true;
561 }
562
563 if (matchLen) {
564 /* strip the matched exe prefix */
565 cmdline += matchLen;
566
567 commStart -= matchLen;
568 commEnd -= matchLen;
569 }
570
571 if (!matchLen || (haveCommField && *cmdline)) {
572 /* cmdline will be a separate field */
573 WRITE_SEPARATOR;
574 }
575
576 if (!haveCommInExe && haveCommInCmdline && !haveCommField)
577 WRITE_HIGHLIGHT(commStart, commEnd - commStart, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
578
579 /* Display cmdline if it hasn't been consumed by procExe */
580 if (*cmdline)
581 (void)stpcpyWithNewlineConversion(str, cmdline);
582
583 #undef WRITE_SEPARATOR
584 #undef WRITE_HIGHLIGHT
585}
586
587void Process_writeCommand(const Process* this, int attr, int baseAttr, RichString* str) {
588 (void)baseAttr;
589
590 const ProcessMergedCommand *mc = &this->mergedCommand;
591
592 int strStart = RichString_size(str);
593
594 const bool highlightBaseName = this->settings->highlightBaseName;
595 const bool highlightSeparator = true;
596 const bool highlightDeleted = true;
597
598 if (!this->mergedCommand.str) {
599 int len = 0;
600 const char* cmdline = this->cmdline;
601
602 if (highlightBaseName || !this->settings->showProgramPath) {
603 int basename = 0;
604 for (int i = 0; i < this->cmdlineBasenameEnd; i++) {
605 if (cmdline[i] == '/') {
606 basename = i + 1;
607 } else if (cmdline[i] == ':') {
608 len = i + 1;
609 break;
610 }
611 }
612 if (len == 0) {
613 if (this->settings->showProgramPath) {
614 strStart += basename;
615 } else {
616 cmdline += basename;
617 }
618 len = this->cmdlineBasenameEnd - basename;
619 }
620 }
621
622 RichString_appendWide(str, attr, cmdline);
623
624 if (this->settings->highlightBaseName) {
625 RichString_setAttrn(str, baseAttr, strStart, len);
626 }
627
628 return;
629 }
630
631 RichString_appendWide(str, attr, this->mergedCommand.str);
632
633 for (size_t i = 0, hlCount = CLAMP(mc->highlightCount, 0, ARRAYSIZE(mc->highlights)); i < hlCount; i++) {
634 const ProcessCmdlineHighlight *hl = &mc->highlights[i];
635
636 if (!hl->length)
637 continue;
638
639 if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_SEPARATOR)
640 if (!highlightSeparator)
641 continue;
642
643 if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_BASENAME)
644 if (!highlightBaseName)
645 continue;
646
647 if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_DELETED)
648 if (!highlightDeleted)
649 continue;
650
651 RichString_setAttrn(str, hl->attr, strStart + hl->offset, hl->length);
Benny Baumann45869512020-11-01 01:09:51 +0100652 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000653}
654
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200655void Process_printRate(RichString* str, double rate, bool coloring) {
656 char buffer[16];
657
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200658 int largeNumberColor = CRT_colors[LARGE_NUMBER];
659 int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
660 int processColor = CRT_colors[PROCESS];
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200661 int shadowColor = CRT_colors[PROCESS_SHADOW];
Benny Baumann0b29e502020-11-28 17:42:02 +0100662
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200663 if (!coloring) {
664 largeNumberColor = CRT_colors[PROCESS];
665 processMegabytesColor = CRT_colors[PROCESS];
Hisham Muhammad2338ad52008-03-14 18:50:49 +0000666 }
Benny Baumann0b29e502020-11-28 17:42:02 +0100667
Benny Baumann29ec1152020-09-07 11:53:58 +0200668 if (isnan(rate)) {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200669 RichString_appendAscii(str, shadowColor, " N/A ");
670 } else if (rate < 0.005) {
671 int len = snprintf(buffer, sizeof(buffer), "%7.2f B/s ", rate);
672 RichString_appendnAscii(str, shadowColor, buffer, len);
Hisham797bcd02016-02-20 02:22:57 -0200673 } else if (rate < ONE_K) {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200674 int len = snprintf(buffer, sizeof(buffer), "%7.2f B/s ", rate);
Christian Göttsche157086e2020-12-04 14:44:57 +0100675 RichString_appendnAscii(str, processColor, buffer, len);
adrien1018536941f2018-12-30 20:18:35 +0800676 } else if (rate < ONE_M) {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200677 int len = snprintf(buffer, sizeof(buffer), "%7.2f K/s ", rate / ONE_K);
Christian Göttsche157086e2020-12-04 14:44:57 +0100678 RichString_appendnAscii(str, processColor, buffer, len);
adrien1018536941f2018-12-30 20:18:35 +0800679 } else if (rate < ONE_G) {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200680 int len = snprintf(buffer, sizeof(buffer), "%7.2f M/s ", rate / ONE_M);
Christian Göttsche157086e2020-12-04 14:44:57 +0100681 RichString_appendnAscii(str, processMegabytesColor, buffer, len);
adrien1018536941f2018-12-30 20:18:35 +0800682 } else if (rate < ONE_T) {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200683 int len = snprintf(buffer, sizeof(buffer), "%7.2f G/s ", rate / ONE_G);
684 RichString_appendnAscii(str, largeNumberColor, buffer, len);
685 } else if (rate < ONE_P) {
686 int len = snprintf(buffer, sizeof(buffer), "%7.2f T/s ", rate / ONE_T);
Christian Göttsche157086e2020-12-04 14:44:57 +0100687 RichString_appendnAscii(str, largeNumberColor, buffer, len);
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200688 } else {
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200689 int len = snprintf(buffer, sizeof(buffer), "%7.2f P/s ", rate / ONE_P);
Christian Göttsche157086e2020-12-04 14:44:57 +0100690 RichString_appendnAscii(str, largeNumberColor, buffer, len);
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200691 }
Hisham Muhammad2338ad52008-03-14 18:50:49 +0000692}
693
Christian Göttschea5db1392021-01-10 15:57:46 +0100694void Process_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width) {
Christian Göttsche08ac22d2021-01-14 09:59:11 +0100695 int columns = width;
696 RichString_appendnWideColumns(str, attr, content, strlen(content), &columns);
697 RichString_appendChr(str, attr, ' ', width + 1 - columns);
Christian Göttschea5db1392021-01-10 15:57:46 +0100698}
699
Christian Göttsche79ad39c2020-10-06 12:28:11 +0200700void Process_writeField(const Process* this, RichString* str, ProcessField field) {
Christian Göttschefee744a2021-01-27 15:11:46 +0100701 char buffer[256];
702 size_t n = sizeof(buffer);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000703 int attr = CRT_colors[DEFAULT_COLOR];
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200704 bool coloring = this->settings->highlightMegabytes;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000705
706 switch (field) {
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000707 case COMM: {
Christian Göttschefee744a2021-01-27 15:11:46 +0100708 int baseattr = CRT_colors[PROCESS_BASENAME];
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200709 if (this->settings->highlightThreads && Process_isThread(this)) {
Hisham Muhammad93f091c2008-03-08 23:39:48 +0000710 attr = CRT_colors[PROCESS_THREAD];
711 baseattr = CRT_colors[PROCESS_THREAD_BASENAME];
712 }
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200713 if (!this->settings->treeView || this->indent == 0) {
Hisham Muhammad93f091c2008-03-08 23:39:48 +0000714 Process_writeCommand(this, attr, baseattr, str);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000715 return;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000716 }
Christian Göttschefee744a2021-01-27 15:11:46 +0100717
718 char* buf = buffer;
719 int maxIndent = 0;
720 bool lastItem = (this->indent < 0);
721 int indent = (this->indent < 0 ? -this->indent : this->indent);
722
723 for (int i = 0; i < 32; i++) {
724 if (indent & (1U << i)) {
725 maxIndent = i+1;
726 }
727 }
728
729 for (int i = 0; i < maxIndent - 1; i++) {
730 int written, ret;
731 if (indent & (1 << i)) {
732 ret = xSnprintf(buf, n, "%s ", CRT_treeStr[TREE_STR_VERT]);
733 } else {
734 ret = xSnprintf(buf, n, " ");
735 }
736 if (ret < 0 || (size_t)ret >= n) {
737 written = n;
738 } else {
739 written = ret;
740 }
741 buf += written;
742 n -= written;
743 }
744
745 const char* draw = CRT_treeStr[lastItem ? TREE_STR_BEND : TREE_STR_RTEE];
746 xSnprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] );
747 RichString_appendWide(str, CRT_colors[PROCESS_TREE], buffer);
748 Process_writeCommand(this, attr, baseattr, str);
749 return;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000750 }
Benny Baumannaa8552b2021-04-18 19:25:56 +0200751 case PROC_COMM: {
752 const char* procComm;
753 if (this->procComm) {
754 attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM];
755 procComm = this->procComm;
756 } else {
757 attr = CRT_colors[PROCESS_SHADOW];
758 procComm = Process_isKernelThread(this) ? kthreadID : "N/A";
759 }
760
761 Process_printLeftAlignedField(str, attr, procComm, TASK_COMM_LEN - 1);
762 return;
763 }
764 case PROC_EXE: {
765 const char* procExe;
766 if (this->procExe) {
767 attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_BASENAME : PROCESS_BASENAME];
768 if (this->procExeDeleted)
769 attr = CRT_colors[FAILED_READ];
770 procExe = this->procExe + this->procExeBasenameOffset;
771 } else {
772 attr = CRT_colors[PROCESS_SHADOW];
773 procExe = Process_isKernelThread(this) ? kthreadID : "N/A";
774 }
775
776 Process_printLeftAlignedField(str, attr, procExe, TASK_COMM_LEN - 1);
777 return;
778 }
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200779 case MAJFLT: Process_printCount(str, this->majflt, coloring); return;
780 case MINFLT: Process_printCount(str, this->minflt, coloring); return;
781 case M_RESIDENT: Process_printKBytes(str, this->m_resident, coloring); return;
782 case M_VIRT: Process_printKBytes(str, this->m_virt, coloring); return;
Christian Göttschefee744a2021-01-27 15:11:46 +0100783 case NICE:
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300784 xSnprintf(buffer, n, "%3ld ", this->nice);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300785 attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
786 : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
Christian Göttschefee744a2021-01-27 15:11:46 +0100787 : CRT_colors[PROCESS_SHADOW];
788 break;
789 case NLWP:
790 if (this->nlwp == 1)
791 attr = CRT_colors[PROCESS_SHADOW];
792
793 xSnprintf(buffer, n, "%4ld ", this->nlwp);
794 break;
795 case PERCENT_CPU:
796 case PERCENT_NORM_CPU: {
797 float cpuPercentage = this->percent_cpu;
798 if (field == PERCENT_NORM_CPU) {
799 cpuPercentage /= this->processList->cpuCount;
800 }
Christian Göttsche979aca92021-03-02 21:59:56 +0100801 if (cpuPercentage > 999.9F) {
Christian Göttschefee744a2021-01-27 15:11:46 +0100802 xSnprintf(buffer, n, "%4u ", (unsigned int)cpuPercentage);
Christian Göttsche979aca92021-03-02 21:59:56 +0100803 } else if (cpuPercentage > 99.9F) {
Christian Göttschefee744a2021-01-27 15:11:46 +0100804 xSnprintf(buffer, n, "%3u. ", (unsigned int)cpuPercentage);
805 } else {
Christian Göttsche979aca92021-03-02 21:59:56 +0100806 if (cpuPercentage < 0.05F)
Christian Göttschefee744a2021-01-27 15:11:46 +0100807 attr = CRT_colors[PROCESS_SHADOW];
808
809 xSnprintf(buffer, n, "%4.1f ", cpuPercentage);
810 }
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300811 break;
812 }
Christian Göttschefee744a2021-01-27 15:11:46 +0100813 case PERCENT_MEM:
Christian Göttsche979aca92021-03-02 21:59:56 +0100814 if (this->percent_mem > 99.9F) {
Christian Göttschefee744a2021-01-27 15:11:46 +0100815 xSnprintf(buffer, n, "100. ");
816 } else {
Christian Göttsche979aca92021-03-02 21:59:56 +0100817 if (this->percent_mem < 0.05F)
Christian Göttschefee744a2021-01-27 15:11:46 +0100818 attr = CRT_colors[PROCESS_SHADOW];
819
820 xSnprintf(buffer, n, "%4.1f ", this->percent_mem);
821 }
822 break;
Christian Göttsche9f68c8d2020-12-15 19:44:52 +0100823 case PGRP: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pgrp); break;
824 case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pid); break;
825 case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->ppid); break;
Christian Göttschefee744a2021-01-27 15:11:46 +0100826 case PRIORITY:
827 if (this->priority <= -100)
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300828 xSnprintf(buffer, n, " RT ");
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300829 else
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300830 xSnprintf(buffer, n, "%3ld ", this->priority);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300831 break;
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300832 case PROCESSOR: xSnprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break;
Christian Göttsche9f68c8d2020-12-15 19:44:52 +0100833 case SESSION: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->session); break;
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300834 case STARTTIME: xSnprintf(buffer, n, "%s", this->starttime_show); break;
Christian Göttschefee744a2021-01-27 15:11:46 +0100835 case STATE:
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300836 xSnprintf(buffer, n, "%c ", this->state);
Christian Göttschefee744a2021-01-27 15:11:46 +0100837 switch (this->state) {
838 case 'R':
839 attr = CRT_colors[PROCESS_R_STATE];
840 break;
841 case 'D':
842 attr = CRT_colors[PROCESS_D_STATE];
843 break;
844 case 'I':
845 case 'S':
846 attr = CRT_colors[PROCESS_SHADOW];
847 break;
Valmiky Arquissandas64e0d942014-10-14 02:30:17 +0100848 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000849 break;
Daniel Langec34be412018-10-07 11:16:12 +0200850 case ST_UID: xSnprintf(buffer, n, "%5d ", this->st_uid); break;
Christian Göttscheb41e4d92021-04-14 20:16:16 +0200851 case TIME: Process_printTime(str, this->time, coloring); return;
Christian Göttschefee744a2021-01-27 15:11:46 +0100852 case TGID:
853 if (this->tgid == this->pid)
854 attr = CRT_colors[PROCESS_SHADOW];
855
856 xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->tgid);
857 break;
Christian Göttsche9f68c8d2020-12-15 19:44:52 +0100858 case TPGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->tpgid); break;
Christian Göttsche9a822152021-03-21 19:40:56 +0100859 case TTY:
860 if (!this->tty_name) {
Christian Göttschea3c82852021-01-27 15:11:42 +0100861 attr = CRT_colors[PROCESS_SHADOW];
Christian Göttsche9a822152021-03-21 19:40:56 +0100862 xSnprintf(buffer, n, "(no tty) ");
Christian Göttschea3c82852021-01-27 15:11:42 +0100863 } else {
Christian Göttsche9a822152021-03-21 19:40:56 +0100864 const char* name = String_startsWith(this->tty_name, "/dev/") ? (this->tty_name + strlen("/dev/")) : this->tty_name;
865 xSnprintf(buffer, n, "%-8s ", name);
Christian Göttschea3c82852021-01-27 15:11:42 +0100866 }
867 break;
Christian Göttschefee744a2021-01-27 15:11:46 +0100868 case USER:
Christian Göttsche42073ba2020-11-04 17:46:04 +0100869 if (Process_getuid != this->st_uid)
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000870 attr = CRT_colors[PROCESS_SHADOW];
Christian Göttsche5359eae2020-12-23 12:21:29 +0100871
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000872 if (this->user) {
Christian Göttschea5db1392021-01-10 15:57:46 +0100873 Process_printLeftAlignedField(str, attr, this->user, 9);
Christian Göttsche5359eae2020-12-23 12:21:29 +0100874 return;
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000875 }
Christian Göttsche5359eae2020-12-23 12:21:29 +0100876
877 xSnprintf(buffer, n, "%-9d ", this->st_uid);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000878 break;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000879 default:
Christian Göttsche615fc932021-04-18 15:52:28 +0200880 assert(0 && "Process_writeField: default key reached"); /* should never be reached */
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300881 xSnprintf(buffer, n, "- ");
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000882 }
Christian Göttschea2be57d2021-04-14 20:54:38 +0200883 RichString_appendAscii(str, attr, buffer);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000884}
885
Christian Göttsche79ad39c2020-10-06 12:28:11 +0200886void Process_display(const Object* cast, RichString* out) {
887 const Process* this = (const Process*) cast;
888 const ProcessField* fields = this->settings->fields;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000889 for (int i = 0; fields[i]; i++)
Hisham Muhammad4c24a9b2015-03-31 23:23:10 -0300890 As_Process(this)->writeField(this, out, fields[i]);
Benny Baumann45869512020-11-01 01:09:51 +0100891
Christian Göttsche42073ba2020-11-04 17:46:04 +0100892 if (this->settings->shadowOtherUsers && this->st_uid != Process_getuid) {
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000893 RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
Benny Baumann45869512020-11-01 01:09:51 +0100894 }
895
896 if (this->tag == true) {
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000897 RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
Benny Baumann45869512020-11-01 01:09:51 +0100898 }
899
Daniel Lange09510902020-11-16 12:13:47 +0100900 if (this->settings->highlightChanges) {
901 if (Process_isTomb(this)) {
902 out->highlightAttr = CRT_colors[PROCESS_TOMB];
903 } else if (Process_isNew(this)) {
904 out->highlightAttr = CRT_colors[PROCESS_NEW];
905 }
906 }
907
Christian Göttsche3f99c2d2021-03-12 16:46:04 +0100908 assert(RichString_size(out) > 0);
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000909}
910
Hisham Muhammad6f868b02015-02-20 14:52:10 -0200911void Process_done(Process* this) {
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000912 assert (this != NULL);
Benny Baumann02431c42020-12-19 16:21:08 +0100913 free(this->cmdline);
Benny Baumannd74e8b72021-01-30 15:31:59 +0100914 free(this->procComm);
915 free(this->procExe);
Benny Baumanncdb660a2021-04-10 11:10:50 +0200916 free(this->mergedCommand.str);
Christian Göttsche9a822152021-03-21 19:40:56 +0100917 free(this->tty_name);
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000918}
919
Benny Baumannc0d02022021-04-24 12:06:49 +0200920/* This function returns the string displayed in Command column, so that sorting
921 * happens on what is displayed - whether comm, full path, basename, etc.. So
922 * this follows Process_writeField(COMM) and Process_writeCommand */
923const char *Process_getCommandStr(const Process *this) {
924 if ((Process_isUserlandThread(this) && this->settings->showThreadNames) || !this->mergedCommand.str) {
925 return this->cmdline;
926 }
927
928 return this->mergedCommand.str;
Narendran Gopalakrishnan09fe94d2020-10-17 16:24:45 +0530929}
930
Christian Göttscheba282cf2020-10-05 13:19:50 +0200931const ProcessClass Process_class = {
Hisham Muhammad4c24a9b2015-03-31 23:23:10 -0300932 .super = {
933 .extends = Class(Object),
934 .display = Process_display,
935 .delete = Process_delete,
936 .compare = Process_compare
937 },
938 .writeField = Process_writeField,
Narendran Gopalakrishnan09fe94d2020-10-17 16:24:45 +0530939 .getCommandStr = Process_getCommandStr,
Hisham Muhammad00b324b2012-12-05 15:12:20 +0000940};
941
Christian Göttsche3035e292021-01-26 18:41:04 +0100942void Process_init(Process* this, const Settings* settings) {
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200943 this->settings = settings;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000944 this->tag = false;
Hisham Muhammad9eb91212010-06-17 19:02:03 +0000945 this->showChildren = true;
Hisham Muhammadd8e14802010-11-22 12:40:20 +0000946 this->show = true;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000947 this->updated = false;
Benny Baumann94a52cb2021-04-10 11:46:57 +0200948 this->cmdlineBasenameEnd = -1;
Benny Baumann45869512020-11-01 01:09:51 +0100949
Christian Göttsche42073ba2020-11-04 17:46:04 +0100950 if (Process_getuid == (uid_t)-1) {
Benny Baumann45869512020-11-01 01:09:51 +0100951 Process_getuid = getuid();
952 }
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000953}
954
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000955void Process_toggleTag(Process* this) {
Christian Göttsche2d231d72020-12-08 22:37:15 +0100956 this->tag = !this->tag;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000957}
958
Adam Saponaradde71c62020-10-30 21:56:16 -0400959bool Process_isNew(const Process* this) {
Adam Saponaraa83f5152020-10-31 20:36:53 -0400960 assert(this->processList);
Nathan Scott356488a2021-03-30 15:55:48 +1100961 if (this->processList->monotonicMs >= this->seenStampMs) {
962 return this->processList->monotonicMs - this->seenStampMs <= 1000 * (uint64_t)this->processList->settings->highlightDelaySecs;
Daniel Lange09510902020-11-16 12:13:47 +0100963 }
Adam Saponaradde71c62020-10-30 21:56:16 -0400964 return false;
965}
966
967bool Process_isTomb(const Process* this) {
Nathan Scott356488a2021-03-30 15:55:48 +1100968 return this->tombStampMs > 0;
Adam Saponaradde71c62020-10-30 21:56:16 -0400969}
970
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000971bool Process_setPriority(Process* this, int priority) {
Christian Göttsche36880cd2021-01-21 20:27:37 +0100972 if (Settings_isReadonly())
973 return false;
974
Michael Kleinab3a7c22015-12-07 20:10:09 +0100975 int old_prio = getpriority(PRIO_PROCESS, this->pid);
976 int err = setpriority(PRIO_PROCESS, this->pid, priority);
Benny Baumann82157f52021-02-16 19:44:59 +0100977
Michael Kleinab3a7c22015-12-07 20:10:09 +0100978 if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
979 this->nice = priority;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000980 }
Michael Kleinab3a7c22015-12-07 20:10:09 +0100981 return (err == 0);
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000982}
983
Nathan Scott500fb282020-08-20 09:35:24 +1000984bool Process_changePriorityBy(Process* this, Arg delta) {
985 return Process_setPriority(this, this->nice + delta.i);
Hisham Muhammad47e881f2012-10-04 23:59:45 +0000986}
987
Nathan Scott500fb282020-08-20 09:35:24 +1000988bool Process_sendSignal(Process* this, Arg sgn) {
Benny Baumann82157f52021-02-16 19:44:59 +0100989 return kill(this->pid, sgn.i) == 0;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000990}
991
Christian Göttsche90ea3ac2020-12-23 13:02:32 +0100992int Process_pidCompare(const void* v1, const void* v2) {
Christian Göttsche18b1e9f2020-09-23 14:15:51 +0200993 const Process* p1 = (const Process*)v1;
994 const Process* p2 = (const Process*)v2;
Christian Göttsche2d231d72020-12-08 22:37:15 +0100995
996 return SPACESHIP_NUMBER(p1->pid, p2->pid);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000997}
998
Christian Göttsche90ea3ac2020-12-23 13:02:32 +0100999int Process_compare(const void* v1, const void* v2) {
Daniel Lange074703b2021-01-21 20:57:34 +01001000 const Process *p1 = (const Process*)v1;
1001 const Process *p2 = (const Process*)v2;
Christian Göttsche397b5c42020-11-04 17:46:24 +01001002
Daniel Lange074703b2021-01-21 20:57:34 +01001003 const Settings *settings = p1->settings;
Christian Göttsche397b5c42020-11-04 17:46:24 +01001004
Hisham Muhammad23272602020-12-18 11:03:31 -03001005 ProcessField key = Settings_getActiveSortKey(settings);
Hisham Muhammade8c69942020-12-17 19:08:56 -03001006
Christian Göttsche90ea3ac2020-12-23 13:02:32 +01001007 int result = Process_compareByKey(p1, p2, key);
Benny Baumann77db2402020-12-18 22:12:26 +01001008
1009 // Implement tie-breaker (needed to make tree mode more stable)
1010 if (!result)
Daniel Lange074703b2021-01-21 20:57:34 +01001011 return SPACESHIP_NUMBER(p1->pid, p2->pid);
Benny Baumann77db2402020-12-18 22:12:26 +01001012
Christian Göttschebea7f8e2021-03-12 16:44:46 +01001013 return (Settings_getActiveDirection(settings) == 1) ? result : -result;
Benny Baumann77db2402020-12-18 22:12:26 +01001014}
1015
Christian Göttsched5de1bc2021-01-27 15:11:44 +01001016static uint8_t stateCompareValue(char state) {
1017 switch (state) {
1018
1019 case 'S':
1020 return 10;
1021
1022 case 'I':
1023 return 9;
1024
1025 case 'X':
1026 return 8;
1027
1028 case 'Z':
1029 return 7;
1030
1031 case 't':
1032 return 6;
1033
1034 case 'T':
1035 return 5;
1036
1037 case 'L':
1038 return 4;
1039
1040 case 'D':
1041 return 3;
1042
1043 case 'R':
1044 return 2;
1045
1046 case '?':
1047 return 1;
1048
1049 default:
1050 return 0;
1051 }
1052}
1053
Christian Göttsche90ea3ac2020-12-23 13:02:32 +01001054int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key) {
Benny Baumann77db2402020-12-18 22:12:26 +01001055 int r;
1056
Christian Göttsche89473cc2020-12-15 19:44:48 +01001057 switch (key) {
Hisham Muhammad5d48ab82006-07-11 06:13:32 +00001058 case PERCENT_CPU:
Christian Göttsche15eab202020-10-30 17:02:20 +01001059 case PERCENT_NORM_CPU:
Daniel Lange4531b312021-01-21 14:27:23 +01001060 return SPACESHIP_NUMBER(p1->percent_cpu, p2->percent_cpu);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +00001061 case PERCENT_MEM:
Daniel Lange4531b312021-01-21 14:27:23 +01001062 return SPACESHIP_NUMBER(p1->m_resident, p2->m_resident);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +00001063 case COMM:
Narendran Gopalakrishnan09fe94d2020-10-17 16:24:45 +05301064 return SPACESHIP_NULLSTR(Process_getCommand(p1), Process_getCommand(p2));
Benny Baumannaa8552b2021-04-18 19:25:56 +02001065 case PROC_COMM: {
1066 const char *comm1 = p1->procComm ? p1->procComm : (Process_isKernelThread(p1) ? kthreadID : "");
1067 const char *comm2 = p2->procComm ? p2->procComm : (Process_isKernelThread(p2) ? kthreadID : "");
1068 return SPACESHIP_NULLSTR(comm1, comm2);
1069 }
1070 case PROC_EXE: {
1071 const char *exe1 = p1->procExe ? (p1->procExe + p1->procExeBasenameOffset) : (Process_isKernelThread(p1) ? kthreadID : "");
1072 const char *exe2 = p2->procExe ? (p2->procExe + p2->procExeBasenameOffset) : (Process_isKernelThread(p2) ? kthreadID : "");
1073 return SPACESHIP_NULLSTR(exe1, exe2);
1074 }
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001075 case MAJFLT:
Daniel Lange4531b312021-01-21 14:27:23 +01001076 return SPACESHIP_NUMBER(p1->majflt, p2->majflt);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001077 case MINFLT:
Daniel Lange4531b312021-01-21 14:27:23 +01001078 return SPACESHIP_NUMBER(p1->minflt, p2->minflt);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001079 case M_RESIDENT:
Daniel Lange4531b312021-01-21 14:27:23 +01001080 return SPACESHIP_NUMBER(p1->m_resident, p2->m_resident);
Christian Göttschefa002c02020-11-20 17:09:34 +01001081 case M_VIRT:
Daniel Lange4531b312021-01-21 14:27:23 +01001082 return SPACESHIP_NUMBER(p1->m_virt, p2->m_virt);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001083 case NICE:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001084 return SPACESHIP_NUMBER(p1->nice, p2->nice);
Hisham Muhammadd357c672007-05-21 19:10:53 +00001085 case NLWP:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001086 return SPACESHIP_NUMBER(p1->nlwp, p2->nlwp);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001087 case PGRP:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001088 return SPACESHIP_NUMBER(p1->pgrp, p2->pgrp);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001089 case PID:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001090 return SPACESHIP_NUMBER(p1->pid, p2->pid);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001091 case PPID:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001092 return SPACESHIP_NUMBER(p1->ppid, p2->ppid);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001093 case PRIORITY:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001094 return SPACESHIP_NUMBER(p1->priority, p2->priority);
Hisham Muhammad272e2d92015-03-16 23:01:48 -03001095 case PROCESSOR:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001096 return SPACESHIP_NUMBER(p1->processor, p2->processor);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001097 case SESSION:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001098 return SPACESHIP_NUMBER(p1->session, p2->session);
1099 case STARTTIME:
1100 r = SPACESHIP_NUMBER(p1->starttime_ctime, p2->starttime_ctime);
1101 return r != 0 ? r : SPACESHIP_NUMBER(p1->pid, p2->pid);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001102 case STATE:
Christian Göttsched5de1bc2021-01-27 15:11:44 +01001103 return SPACESHIP_NUMBER(stateCompareValue(p1->state), stateCompareValue(p2->state));
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001104 case ST_UID:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001105 return SPACESHIP_NUMBER(p1->st_uid, p2->st_uid);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001106 case TIME:
Daniel Lange4531b312021-01-21 14:27:23 +01001107 return SPACESHIP_NUMBER(p1->time, p2->time);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001108 case TGID:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001109 return SPACESHIP_NUMBER(p1->tgid, p2->tgid);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001110 case TPGID:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001111 return SPACESHIP_NUMBER(p1->tpgid, p2->tpgid);
Christian Göttsche9a822152021-03-21 19:40:56 +01001112 case TTY:
1113 /* Order no tty last */
1114 return SPACESHIP_DEFAULTSTR(p1->tty_name, p2->tty_name, "\x7F");
Hisham Muhammadbe1700c2015-03-16 01:43:04 -03001115 case USER:
Christian Göttsche397b5c42020-11-04 17:46:24 +01001116 return SPACESHIP_NULLSTR(p1->user, p2->user);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +00001117 default:
Christian Göttsche615fc932021-04-18 15:52:28 +02001118 assert(0 && "Process_compareByKey_Base: default key reached"); /* should never be reached */
Benny Baumann77db2402020-12-18 22:12:26 +01001119 return SPACESHIP_NUMBER(p1->pid, p2->pid);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +00001120 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00001121}