| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | htop - Process.c |
| Hisham Muhammad | b1b3f57 | 2015-03-21 16:52:54 -0300 | [diff] [blame] | 3 | (C) 2004-2015 Hisham H. Muhammad |
| Nathan Scott | 500fb28 | 2020-08-20 09:35:24 +1000 | [diff] [blame] | 4 | (C) 2020 Red Hat, Inc. All Rights Reserved. |
| Daniel Lange | 94ad111 | 2021-09-22 11:33:00 +0200 | [diff] [blame] | 5 | Released under the GNU GPLv2+, see the COPYING file |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 6 | in the source distribution for its full text. |
| 7 | */ |
| 8 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 9 | #include "config.h" // IWYU pragma: keep |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 10 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 11 | #include "Process.h" |
| 12 | |
| 13 | #include <assert.h> |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 14 | #include <math.h> |
| 15 | #include <signal.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| mayurdahibhate | 1b74dfe | 2021-04-29 20:42:43 +0530 | [diff] [blame] | 19 | #include <string.h> |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 20 | #include <time.h> |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 21 | #include <sys/resource.h> |
| Explorer09 | 3512971 | 2018-12-30 12:18:27 +0800 | [diff] [blame] | 22 | |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 23 | #include "CRT.h" |
| Benny Baumann | e56089e | 2023-11-28 15:15:03 +0100 | [diff] [blame] | 24 | #include "Hashtable.h" |
| 25 | #include "Machine.h" |
| Christian Göttsche | 7cf5277 | 2020-11-18 14:26:30 +0100 | [diff] [blame] | 26 | #include "Macros.h" |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 27 | #include "ProcessTable.h" |
| Sohaib Mohamed | 6f2021f | 2021-07-11 03:11:29 +0200 | [diff] [blame] | 28 | #include "DynamicColumn.h" |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 29 | #include "RichString.h" |
| Christian Göttsche | da49489 | 2023-01-10 19:40:04 +0100 | [diff] [blame] | 30 | #include "Scheduling.h" |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 31 | #include "Settings.h" |
| Benny Baumann | e56089e | 2023-11-28 15:15:03 +0100 | [diff] [blame] | 32 | #include "Table.h" |
| Benny Baumann | 872e542 | 2020-10-14 20:21:09 +0200 | [diff] [blame] | 33 | #include "XUtils.h" |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 34 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 35 | #if defined(MAJOR_IN_MKDEV) |
| Kang-Che Sung (宋岡哲) | c01f40e | 2018-02-26 21:15:05 +0800 | [diff] [blame] | 36 | #include <sys/mkdev.h> |
| Kang-Che Sung (宋岡哲) | c01f40e | 2018-02-26 21:15:05 +0800 | [diff] [blame] | 37 | #endif |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 38 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 39 | |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 40 | /* Used to identify kernel threads in Comm and Exe columns */ |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 41 | static const char* const kthreadID = "KTHREAD"; |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 42 | |
| Christian Göttsche | a63cfc8 | 2020-10-13 14:26:40 +0200 | [diff] [blame] | 43 | void Process_fillStarttimeBuffer(Process* this) { |
| 44 | struct tm date; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 45 | time_t now = this->super.host->realtime.tv_sec; |
| Christian Göttsche | a63cfc8 | 2020-10-13 14:26:40 +0200 | [diff] [blame] | 46 | (void) localtime_r(&this->starttime_ctime, &date); |
| arza | 78c7b1c | 2022-10-20 17:20:51 +0300 | [diff] [blame] | 47 | |
| 48 | strftime(this->starttime_show, |
| 49 | sizeof(this->starttime_show) - 1, |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 50 | (this->starttime_ctime > now - 86400) ? "%R " : (this->starttime_ctime > now - 364 * 86400) ? "%b%d " : " %Y ", |
| arza | 78c7b1c | 2022-10-20 17:20:51 +0300 | [diff] [blame] | 51 | &date); |
| Christian Göttsche | a63cfc8 | 2020-10-13 14:26:40 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 54 | /* |
| 55 | * TASK_COMM_LEN is defined to be 16 for /proc/[pid]/comm in man proc(5), but it is |
| 56 | * not available in an userspace header - so define it. |
| 57 | * |
| 58 | * Note: This is taken from LINUX headers, but implicitly taken for other platforms |
| 59 | * for sake of brevity. |
| 60 | * |
| 61 | * Note: when colorizing a basename with the comm prefix, the entire basename |
| 62 | * (not just the comm prefix) is colorized for better readability, and it is |
| Benny Baumann | ea23eee | 2023-06-16 11:12:47 +0200 | [diff] [blame] | 63 | * implicit that only up to (TASK_COMM_LEN - 1) could be comm. |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 64 | */ |
| 65 | #define TASK_COMM_LEN 16 |
| Tobias Geerinckx-Rice | 293eec4 | 2015-07-29 21:14:29 +0200 | [diff] [blame] | 66 | |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 67 | static bool findCommInCmdline(const char* comm, const char* cmdline, int cmdlineBasenameStart, int* pCommStart, int* pCommEnd) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 68 | /* Try to find procComm in tokenized cmdline - this might in rare cases |
| 69 | * mis-identify a string or fail, if comm or cmdline had been unsuitably |
| 70 | * modified by the process */ |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 71 | const char* tokenBase; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 72 | size_t tokenLen; |
| 73 | const size_t commLen = strlen(comm); |
| 74 | |
| 75 | if (cmdlineBasenameStart < 0) |
| 76 | return false; |
| 77 | |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 78 | for (const char* token = cmdline + cmdlineBasenameStart; *token;) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 79 | for (tokenBase = token; *token && *token != '\n'; ++token) { |
| 80 | if (*token == '/') { |
| 81 | tokenBase = token + 1; |
| Hisham Muhammad | f2a190b | 2014-02-27 17:11:23 -0300 | [diff] [blame] | 82 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 83 | } |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 84 | tokenLen = token - tokenBase; |
| 85 | |
| 86 | if ((tokenLen == commLen || (tokenLen > commLen && commLen == (TASK_COMM_LEN - 1))) && |
| 87 | strncmp(tokenBase, comm, commLen) == 0) { |
| 88 | *pCommStart = tokenBase - cmdline; |
| 89 | *pCommEnd = token - cmdline; |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | if (*token) { |
| 94 | do { |
| 95 | ++token; |
| 96 | } while (*token && '\n' == *token); |
| Tobias Geerinckx-Rice | 293eec4 | 2015-07-29 21:14:29 +0200 | [diff] [blame] | 97 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 98 | } |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 99 | return false; |
| 100 | } |
| Tobias Geerinckx-Rice | 293eec4 | 2015-07-29 21:14:29 +0200 | [diff] [blame] | 101 | |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 102 | static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int cmdlineBaseOffset, const char* exe, int exeBaseOffset, int exeBaseLen) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 103 | int matchLen; /* matching length to be returned */ |
| 104 | char delim; /* delimiter following basename */ |
| Tobias Geerinckx-Rice | 293eec4 | 2015-07-29 21:14:29 +0200 | [diff] [blame] | 105 | |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 106 | /* cmdline prefix is an absolute path: it must match whole exe. */ |
| 107 | if (cmdline[0] == '/') { |
| 108 | matchLen = exeBaseLen + exeBaseOffset; |
| 109 | if (strncmp(cmdline, exe, matchLen) == 0) { |
| 110 | delim = cmdline[matchLen]; |
| 111 | if (delim == 0 || delim == '\n' || delim == ' ') { |
| 112 | return matchLen; |
| 113 | } |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /* cmdline prefix is a relative path: We need to first match the basename at |
| 119 | * cmdlineBaseOffset and then reverse match the cmdline prefix with the exe |
| 120 | * suffix. But there is a catch: Some processes modify their cmdline in ways |
| 121 | * that make htop's identification of the basename in cmdline unreliable. |
| 122 | * For e.g. /usr/libexec/gdm-session-worker modifies its cmdline to |
| 123 | * "gdm-session-worker [pam/gdm-autologin]" and htop ends up with |
| 124 | * proccmdlineBasenameEnd at "gdm-autologin]". This issue could arise with |
| 125 | * chrome as well as it stores in cmdline its concatenated argument vector, |
| 126 | * without NUL delimiter between the arguments (which may contain a '/') |
| 127 | * |
| 128 | * So if needed, we adjust cmdlineBaseOffset to the previous (if any) |
| 129 | * component of the cmdline relative path, and retry the procedure. */ |
| 130 | bool delimFound; /* if valid basename delimiter found */ |
| 131 | do { |
| 132 | /* match basename */ |
| 133 | matchLen = exeBaseLen + cmdlineBaseOffset; |
| 134 | if (cmdlineBaseOffset < exeBaseOffset && |
| 135 | strncmp(cmdline + cmdlineBaseOffset, exe + exeBaseOffset, exeBaseLen) == 0) { |
| 136 | delim = cmdline[matchLen]; |
| 137 | if (delim == 0 || delim == '\n' || delim == ' ') { |
| 138 | int i, j; |
| 139 | /* reverse match the cmdline prefix and exe suffix */ |
| 140 | for (i = cmdlineBaseOffset - 1, j = exeBaseOffset - 1; |
| 141 | i >= 0 && j >= 0 && cmdline[i] == exe[j]; --i, --j) |
| 142 | ; |
| 143 | |
| 144 | /* full match, with exe suffix being a valid relative path */ |
| 145 | if (i < 0 && j >= 0 && exe[j] == '/') |
| 146 | return matchLen; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* Try to find the previous potential cmdlineBaseOffset - it would be |
| 151 | * preceded by '/' or nothing, and delimited by ' ' or '\n' */ |
| 152 | for (delimFound = false, cmdlineBaseOffset -= 2; cmdlineBaseOffset > 0; --cmdlineBaseOffset) { |
| 153 | if (delimFound) { |
| 154 | if (cmdline[cmdlineBaseOffset - 1] == '/') { |
| 155 | break; |
| 156 | } |
| 157 | } else if (cmdline[cmdlineBaseOffset] == ' ' || cmdline[cmdlineBaseOffset] == '\n') { |
| 158 | delimFound = true; |
| 159 | } |
| 160 | } |
| 161 | } while (delimFound); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* stpcpy, but also converts newlines to spaces */ |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 167 | static inline char* stpcpyWithNewlineConversion(char* dstStr, const char* srcStr) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 168 | for (; *srcStr; ++srcStr) { |
| 169 | *dstStr++ = (*srcStr == '\n') ? ' ' : *srcStr; |
| 170 | } |
| 171 | *dstStr = 0; |
| 172 | return dstStr; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * This function makes the merged Command string. It also stores the offsets of the |
| 177 | * basename, comm w.r.t the merged Command string - these offsets will be used by |
| 178 | * Process_writeCommand() for coloring. The merged Command string is also |
| Christian Göttsche | 1ef8c0e | 2021-12-16 15:57:37 +0100 | [diff] [blame] | 179 | * returned by Process_getCommand() for searching, sorting and filtering. |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 180 | */ |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 181 | void Process_makeCommandStr(Process* this, const Settings* settings) { |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 182 | ProcessMergedCommand* mc = &this->mergedCommand; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 183 | |
| 184 | bool showMergedCommand = settings->showMergedCommand; |
| 185 | bool showProgramPath = settings->showProgramPath; |
| 186 | bool searchCommInCmdline = settings->findCommInCmdline; |
| 187 | bool stripExeFromCmdline = settings->stripExeFromCmdline; |
| Benny Baumann | 7bfd62b | 2021-07-17 20:59:50 +0200 | [diff] [blame] | 188 | bool showThreadNames = settings->showThreadNames; |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 189 | bool shadowDistPathPrefix = settings->shadowDistPathPrefix; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 190 | |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 191 | uint64_t settingsStamp = settings->lastUpdate; |
| 192 | |
| Benny Baumann | a61a2e6 | 2021-04-18 18:10:04 +0200 | [diff] [blame] | 193 | /* Nothing to do to (Re)Generate the Command string, if the process is: |
| 194 | * - a kernel thread, or |
| 195 | * - a zombie from before being under htop's watch, or |
| 196 | * - a user thread and showThreadNames is not set */ |
| 197 | if (Process_isKernelThread(this)) |
| 198 | return; |
| marcluque | d8dfbbd | 2021-10-12 00:45:09 +0200 | [diff] [blame] | 199 | if (this->state == ZOMBIE && !this->mergedCommand.str) |
| Benny Baumann | a61a2e6 | 2021-04-18 18:10:04 +0200 | [diff] [blame] | 200 | return; |
| Benny Baumann | a61a2e6 | 2021-04-18 18:10:04 +0200 | [diff] [blame] | 201 | |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 202 | /* this->mergedCommand.str needs updating only if its state or contents changed. |
| 203 | * Its content is based on the fields cmdline, comm, and exe. */ |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 204 | if (mc->lastUpdate >= settingsStamp) |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 205 | return; |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 206 | |
| 207 | mc->lastUpdate = settingsStamp; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 208 | |
| Stefan | f66f04e | 2023-03-30 11:25:46 +0200 | [diff] [blame] | 209 | /* The field separator "│" has been chosen such that it will not match any |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 210 | * valid string used for searching or filtering */ |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 211 | const char* SEPARATOR = CRT_treeStr[TREE_STR_VERT]; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 212 | const int SEPARATOR_LEN = strlen(SEPARATOR); |
| 213 | |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 214 | /* Accommodate the column text, two field separators and terminating NUL */ |
| 215 | size_t maxLen = 2 * SEPARATOR_LEN + 1; |
| 216 | maxLen += this->cmdline ? strlen(this->cmdline) : strlen("(zombie)"); |
| 217 | maxLen += this->procComm ? strlen(this->procComm) : 0; |
| 218 | maxLen += this->procExe ? strlen(this->procExe) : 0; |
| Benny Baumann | 7ef58f2 | 2021-05-17 23:15:24 +0200 | [diff] [blame] | 219 | |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 220 | free(mc->str); |
| 221 | mc->str = xCalloc(1, maxLen); |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 222 | |
| 223 | /* Reset all locations that need extra handling when actually displaying */ |
| 224 | mc->highlightCount = 0; |
| 225 | memset(mc->highlights, 0, sizeof(mc->highlights)); |
| 226 | |
| 227 | size_t mbMismatch = 0; |
| Benny Baumann | 9a78155 | 2021-05-15 21:54:46 +0200 | [diff] [blame] | 228 | #define WRITE_HIGHLIGHT(_offset, _length, _attr, _flags) \ |
| 229 | do { \ |
| 230 | /* Check if we still have capacity */ \ |
| 231 | assert(mc->highlightCount < ARRAYSIZE(mc->highlights)); \ |
| 232 | if (mc->highlightCount >= ARRAYSIZE(mc->highlights)) \ |
| Christian Göttsche | 5dec947 | 2021-08-22 17:14:36 +0200 | [diff] [blame] | 233 | break; \ |
| Benny Baumann | 9a78155 | 2021-05-15 21:54:46 +0200 | [diff] [blame] | 234 | \ |
| 235 | mc->highlights[mc->highlightCount].offset = str - strStart + (_offset) - mbMismatch; \ |
| 236 | mc->highlights[mc->highlightCount].length = _length; \ |
| 237 | mc->highlights[mc->highlightCount].attr = _attr; \ |
| 238 | mc->highlights[mc->highlightCount].flags = _flags; \ |
| 239 | mc->highlightCount++; \ |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 240 | } while (0) |
| 241 | |
| Benny Baumann | 9a78155 | 2021-05-15 21:54:46 +0200 | [diff] [blame] | 242 | #define WRITE_SEPARATOR \ |
| 243 | do { \ |
| 244 | WRITE_HIGHLIGHT(0, 1, CRT_colors[FAILED_READ], CMDLINE_HIGHLIGHT_FLAG_SEPARATOR); \ |
| 245 | mbMismatch += SEPARATOR_LEN - 1; \ |
| 246 | str = stpcpy(str, SEPARATOR); \ |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 247 | } while (0) |
| 248 | |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 249 | #define CHECK_AND_MARK(str_, prefix_) \ |
| 250 | if (String_startsWith(str_, prefix_)) { \ |
| 251 | WRITE_HIGHLIGHT(0, strlen(prefix_), CRT_colors[PROCESS_SHADOW], CMDLINE_HIGHLIGHT_FLAG_PREFIXDIR); \ |
| 252 | break; \ |
| 253 | } else (void)0 |
| 254 | |
| 255 | #define CHECK_AND_MARK_DIST_PATH_PREFIXES(str_) \ |
| 256 | do { \ |
| 257 | if ((str_)[0] != '/') { \ |
| 258 | break; \ |
| 259 | } \ |
| 260 | switch ((str_)[1]) { \ |
| 261 | case 'b': \ |
| 262 | CHECK_AND_MARK(str_, "/bin/"); \ |
| 263 | break; \ |
| 264 | case 'l': \ |
| 265 | CHECK_AND_MARK(str_, "/lib/"); \ |
| 266 | CHECK_AND_MARK(str_, "/lib32/"); \ |
| 267 | CHECK_AND_MARK(str_, "/lib64/"); \ |
| 268 | CHECK_AND_MARK(str_, "/libx32/"); \ |
| 269 | break; \ |
| 270 | case 's': \ |
| 271 | CHECK_AND_MARK(str_, "/sbin/"); \ |
| 272 | break; \ |
| 273 | case 'u': \ |
| 274 | if (String_startsWith(str_, "/usr/")) { \ |
| 275 | switch ((str_)[5]) { \ |
| 276 | case 'b': \ |
| 277 | CHECK_AND_MARK(str_, "/usr/bin/"); \ |
| 278 | break; \ |
| 279 | case 'l': \ |
| 280 | CHECK_AND_MARK(str_, "/usr/libexec/"); \ |
| 281 | CHECK_AND_MARK(str_, "/usr/lib/"); \ |
| 282 | CHECK_AND_MARK(str_, "/usr/lib32/"); \ |
| 283 | CHECK_AND_MARK(str_, "/usr/lib64/"); \ |
| 284 | CHECK_AND_MARK(str_, "/usr/libx32/"); \ |
| 285 | \ |
| 286 | CHECK_AND_MARK(str_, "/usr/local/bin/"); \ |
| 287 | CHECK_AND_MARK(str_, "/usr/local/lib/"); \ |
| 288 | CHECK_AND_MARK(str_, "/usr/local/sbin/"); \ |
| 289 | break; \ |
| 290 | case 's': \ |
| 291 | CHECK_AND_MARK(str_, "/usr/sbin/"); \ |
| 292 | break; \ |
| 293 | } \ |
| 294 | } \ |
| 295 | break; \ |
| 296 | } \ |
| 297 | } while (0) |
| 298 | |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 299 | const int baseAttr = Process_isThread(this) ? CRT_colors[PROCESS_THREAD_BASENAME] : CRT_colors[PROCESS_BASENAME]; |
| 300 | const int commAttr = Process_isThread(this) ? CRT_colors[PROCESS_THREAD_COMM] : CRT_colors[PROCESS_COMM]; |
| Christian Göttsche | 8154125 | 2021-04-04 18:07:26 +0200 | [diff] [blame] | 301 | const int delExeAttr = CRT_colors[FAILED_READ]; |
| 302 | const int delLibAttr = CRT_colors[PROCESS_TAG]; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 303 | |
| 304 | /* Establish some shortcuts to data we need */ |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 305 | const char* cmdline = this->cmdline; |
| 306 | const char* procComm = this->procComm; |
| 307 | const char* procExe = this->procExe; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 308 | |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 309 | char* strStart = mc->str; |
| 310 | char* str = strStart; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 311 | |
| 312 | int cmdlineBasenameStart = this->cmdlineBasenameStart; |
| 313 | int cmdlineBasenameEnd = this->cmdlineBasenameEnd; |
| 314 | |
| 315 | if (!cmdline) { |
| 316 | cmdlineBasenameStart = 0; |
| 317 | cmdlineBasenameEnd = 0; |
| 318 | cmdline = "(zombie)"; |
| 319 | } |
| 320 | |
| 321 | assert(cmdlineBasenameStart >= 0); |
| 322 | assert(cmdlineBasenameStart <= (int)strlen(cmdline)); |
| 323 | |
| 324 | if (!showMergedCommand || !procExe || !procComm) { /* fall back to cmdline */ |
| Jan Kończak | 8c99683 | 2022-01-05 14:27:51 +0100 | [diff] [blame] | 325 | if ((showMergedCommand || (Process_isUserlandThread(this) && showThreadNames)) && procComm && strlen(procComm)) { /* set column to or prefix it with comm */ |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 326 | if (strncmp(cmdline + cmdlineBasenameStart, procComm, MINIMUM(TASK_COMM_LEN - 1, strlen(procComm))) != 0) { |
| 327 | WRITE_HIGHLIGHT(0, strlen(procComm), commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM); |
| 328 | str = stpcpy(str, procComm); |
| 329 | |
| Benny Baumann | 4010458 | 2022-10-22 19:19:39 +0200 | [diff] [blame] | 330 | if (!showMergedCommand) |
| Jan Kończak | 8c99683 | 2022-01-05 14:27:51 +0100 | [diff] [blame] | 331 | return; |
| 332 | |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 333 | WRITE_SEPARATOR; |
| 334 | } |
| 335 | } |
| 336 | |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 337 | if (shadowDistPathPrefix && showProgramPath) |
| 338 | CHECK_AND_MARK_DIST_PATH_PREFIXES(cmdline); |
| 339 | |
| Benny Baumann | 2824e29 | 2021-05-15 21:55:14 +0200 | [diff] [blame] | 340 | if (cmdlineBasenameEnd > cmdlineBasenameStart) |
| 341 | WRITE_HIGHLIGHT(showProgramPath ? cmdlineBasenameStart : 0, cmdlineBasenameEnd - cmdlineBasenameStart, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME); |
| Benny Baumann | b7248f6 | 2021-10-06 16:38:45 +0200 | [diff] [blame] | 342 | |
| 343 | if (this->procExeDeleted) |
| 344 | WRITE_HIGHLIGHT(showProgramPath ? cmdlineBasenameStart : 0, cmdlineBasenameEnd - cmdlineBasenameStart, delExeAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED); |
| 345 | else if (this->usesDeletedLib) |
| 346 | WRITE_HIGHLIGHT(showProgramPath ? cmdlineBasenameStart : 0, cmdlineBasenameEnd - cmdlineBasenameStart, delLibAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED); |
| 347 | |
| Benny Baumann | 2824e29 | 2021-05-15 21:55:14 +0200 | [diff] [blame] | 348 | (void)stpcpyWithNewlineConversion(str, cmdline + (showProgramPath ? 0 : cmdlineBasenameStart)); |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 349 | |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | int exeLen = strlen(this->procExe); |
| 354 | int exeBasenameOffset = this->procExeBasenameOffset; |
| 355 | int exeBasenameLen = exeLen - exeBasenameOffset; |
| 356 | |
| 357 | assert(exeBasenameOffset >= 0); |
| 358 | assert(exeBasenameOffset <= (int)strlen(procExe)); |
| 359 | |
| 360 | bool haveCommInExe = false; |
| Christian Göttsche | ce27f83 | 2021-08-08 16:04:26 +0200 | [diff] [blame] | 361 | if (procExe && procComm && (!Process_isUserlandThread(this) || showThreadNames)) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 362 | haveCommInExe = strncmp(procExe + exeBasenameOffset, procComm, TASK_COMM_LEN - 1) == 0; |
| 363 | } |
| 364 | |
| 365 | /* Start with copying exe */ |
| 366 | if (showProgramPath) { |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 367 | if (shadowDistPathPrefix) |
| 368 | CHECK_AND_MARK_DIST_PATH_PREFIXES(procExe); |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 369 | if (haveCommInExe) |
| 370 | WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM); |
| 371 | WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME); |
| 372 | if (this->procExeDeleted) |
| Christian Göttsche | 8154125 | 2021-04-04 18:07:26 +0200 | [diff] [blame] | 373 | WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, delExeAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED); |
| 374 | else if (this->usesDeletedLib) |
| 375 | WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, delLibAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED); |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 376 | str = stpcpy(str, procExe); |
| 377 | } else { |
| 378 | if (haveCommInExe) |
| 379 | WRITE_HIGHLIGHT(0, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM); |
| 380 | WRITE_HIGHLIGHT(0, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME); |
| 381 | if (this->procExeDeleted) |
| Christian Göttsche | 8154125 | 2021-04-04 18:07:26 +0200 | [diff] [blame] | 382 | WRITE_HIGHLIGHT(0, exeBasenameLen, delExeAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED); |
| 383 | else if (this->usesDeletedLib) |
| 384 | WRITE_HIGHLIGHT(0, exeBasenameLen, delLibAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED); |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 385 | str = stpcpy(str, procExe + exeBasenameOffset); |
| 386 | } |
| 387 | |
| 388 | bool haveCommInCmdline = false; |
| 389 | int commStart = 0; |
| 390 | int commEnd = 0; |
| 391 | |
| 392 | /* Try to match procComm with procExe's basename: This is reliable (predictable) */ |
| 393 | if (searchCommInCmdline) { |
| 394 | /* commStart/commEnd will be adjusted later along with cmdline */ |
| Christian Göttsche | ce27f83 | 2021-08-08 16:04:26 +0200 | [diff] [blame] | 395 | haveCommInCmdline = (!Process_isUserlandThread(this) || showThreadNames) && findCommInCmdline(procComm, cmdline, cmdlineBasenameStart, &commStart, &commEnd); |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | int matchLen = matchCmdlinePrefixWithExeSuffix(cmdline, cmdlineBasenameStart, procExe, exeBasenameOffset, exeBasenameLen); |
| 399 | |
| 400 | bool haveCommField = false; |
| 401 | |
| Christian Göttsche | ce27f83 | 2021-08-08 16:04:26 +0200 | [diff] [blame] | 402 | if (!haveCommInExe && !haveCommInCmdline && procComm && (!Process_isUserlandThread(this) || showThreadNames)) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 403 | WRITE_SEPARATOR; |
| 404 | WRITE_HIGHLIGHT(0, strlen(procComm), commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM); |
| 405 | str = stpcpy(str, procComm); |
| 406 | haveCommField = true; |
| 407 | } |
| 408 | |
| 409 | if (matchLen) { |
| Benny Baumann | 9eed309 | 2022-05-30 22:38:59 +0200 | [diff] [blame] | 410 | if (stripExeFromCmdline) { |
| 411 | /* strip the matched exe prefix */ |
| 412 | cmdline += matchLen; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 413 | |
| Benny Baumann | 9eed309 | 2022-05-30 22:38:59 +0200 | [diff] [blame] | 414 | commStart -= matchLen; |
| 415 | commEnd -= matchLen; |
| 416 | } else { |
| 417 | matchLen = 0; |
| 418 | } |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if (!matchLen || (haveCommField && *cmdline)) { |
| 422 | /* cmdline will be a separate field */ |
| 423 | WRITE_SEPARATOR; |
| 424 | } |
| 425 | |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 426 | if (shadowDistPathPrefix) |
| 427 | CHECK_AND_MARK_DIST_PATH_PREFIXES(cmdline); |
| 428 | |
| Christian Göttsche | ce27f83 | 2021-08-08 16:04:26 +0200 | [diff] [blame] | 429 | if (!haveCommInExe && haveCommInCmdline && !haveCommField && (!Process_isUserlandThread(this) || showThreadNames)) |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 430 | WRITE_HIGHLIGHT(commStart, commEnd - commStart, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM); |
| 431 | |
| 432 | /* Display cmdline if it hasn't been consumed by procExe */ |
| 433 | if (*cmdline) |
| 434 | (void)stpcpyWithNewlineConversion(str, cmdline); |
| 435 | |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 436 | #undef CHECK_AND_MARK_DIST_PATH_PREFIXES |
| 437 | #undef CHECK_AND_MARK |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 438 | #undef WRITE_SEPARATOR |
| 439 | #undef WRITE_HIGHLIGHT |
| 440 | } |
| 441 | |
| 442 | void Process_writeCommand(const Process* this, int attr, int baseAttr, RichString* str) { |
| 443 | (void)baseAttr; |
| 444 | |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 445 | const ProcessMergedCommand* mc = &this->mergedCommand; |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 446 | const char* mergedCommand = mc->str; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 447 | |
| 448 | int strStart = RichString_size(str); |
| 449 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 450 | const Settings* settings = this->super.host->settings; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 451 | const bool highlightBaseName = settings->highlightBaseName; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 452 | const bool highlightSeparator = true; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 453 | const bool highlightDeleted = settings->highlightDeletedExe; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 454 | |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 455 | if (!mergedCommand) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 456 | int len = 0; |
| 457 | const char* cmdline = this->cmdline; |
| 458 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 459 | if (highlightBaseName || !settings->showProgramPath) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 460 | int basename = 0; |
| 461 | for (int i = 0; i < this->cmdlineBasenameEnd; i++) { |
| 462 | if (cmdline[i] == '/') { |
| 463 | basename = i + 1; |
| 464 | } else if (cmdline[i] == ':') { |
| 465 | len = i + 1; |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | if (len == 0) { |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 470 | if (settings->showProgramPath) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 471 | strStart += basename; |
| 472 | } else { |
| 473 | cmdline += basename; |
| 474 | } |
| 475 | len = this->cmdlineBasenameEnd - basename; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | RichString_appendWide(str, attr, cmdline); |
| 480 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 481 | if (settings->highlightBaseName) { |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 482 | RichString_setAttrn(str, baseAttr, strStart, len); |
| 483 | } |
| 484 | |
| 485 | return; |
| 486 | } |
| 487 | |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 488 | RichString_appendWide(str, attr, mergedCommand); |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 489 | |
| 490 | for (size_t i = 0, hlCount = CLAMP(mc->highlightCount, 0, ARRAYSIZE(mc->highlights)); i < hlCount; i++) { |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 491 | const ProcessCmdlineHighlight* hl = &mc->highlights[i]; |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 492 | |
| 493 | if (!hl->length) |
| 494 | continue; |
| 495 | |
| 496 | if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_SEPARATOR) |
| 497 | if (!highlightSeparator) |
| 498 | continue; |
| 499 | |
| 500 | if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_BASENAME) |
| 501 | if (!highlightBaseName) |
| 502 | continue; |
| 503 | |
| 504 | if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_DELETED) |
| 505 | if (!highlightDeleted) |
| 506 | continue; |
| 507 | |
| Christian Göttsche | 623ee31 | 2021-05-23 16:04:43 +0200 | [diff] [blame] | 508 | if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_PREFIXDIR) |
| 509 | if (!highlightDeleted) |
| 510 | continue; |
| 511 | |
| Benny Baumann | bcb18ef | 2021-04-10 13:31:39 +0200 | [diff] [blame] | 512 | RichString_setAttrn(str, hl->attr, strStart + hl->offset, hl->length); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 513 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| marcluque | d8dfbbd | 2021-10-12 00:45:09 +0200 | [diff] [blame] | 516 | static inline char processStateChar(ProcessState state) { |
| 517 | switch (state) { |
| 518 | case UNKNOWN: return '?'; |
| 519 | case RUNNABLE: return 'U'; |
| 520 | case RUNNING: return 'R'; |
| 521 | case QUEUED: return 'Q'; |
| 522 | case WAITING: return 'W'; |
| 523 | case UNINTERRUPTIBLE_WAIT: return 'D'; |
| 524 | case BLOCKED: return 'B'; |
| 525 | case PAGING: return 'P'; |
| 526 | case STOPPED: return 'T'; |
| 527 | case TRACED: return 't'; |
| 528 | case ZOMBIE: return 'Z'; |
| 529 | case DEFUNCT: return 'X'; |
| 530 | case IDLE: return 'I'; |
| 531 | case SLEEPING: return 'S'; |
| 532 | default: |
| 533 | assert(0); |
| 534 | return '!'; |
| 535 | } |
| 536 | } |
| 537 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 538 | static void Process_rowWriteField(const Row* super, RichString* str, RowField field) { |
| 539 | const Process* this = (const Process*) super; |
| 540 | assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); |
| 541 | Process_writeField(this, str, field); |
| 542 | } |
| 543 | |
| 544 | void Process_writeField(const Process* this, RichString* str, RowField field) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 545 | const Row* super = (const Row*) &this->super; |
| 546 | const Machine* host = super->host; |
| 547 | const Settings* settings = host->settings; |
| Benny Baumann | 2aacbf8 | 2023-11-29 21:27:07 +0100 | [diff] [blame] | 548 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 549 | bool coloring = settings->highlightMegabytes; |
| Benny Baumann | 2aacbf8 | 2023-11-29 21:27:07 +0100 | [diff] [blame] | 550 | char buffer[256]; buffer[255] = '\0'; |
| 551 | int attr = CRT_colors[DEFAULT_COLOR]; |
| 552 | size_t n = sizeof(buffer) - 1; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 553 | |
| 554 | switch (field) { |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 555 | case COMM: { |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 556 | int baseattr = CRT_colors[PROCESS_BASENAME]; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 557 | if (settings->highlightThreads && Process_isThread(this)) { |
| Hisham Muhammad | 93f091c | 2008-03-08 23:39:48 +0000 | [diff] [blame] | 558 | attr = CRT_colors[PROCESS_THREAD]; |
| 559 | baseattr = CRT_colors[PROCESS_THREAD_BASENAME]; |
| 560 | } |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 561 | const ScreenSettings* ss = settings->ss; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 562 | if (!ss->treeView || super->indent == 0) { |
| Hisham Muhammad | 93f091c | 2008-03-08 23:39:48 +0000 | [diff] [blame] | 563 | Process_writeCommand(this, attr, baseattr, str); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 564 | return; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 565 | } |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 566 | |
| 567 | char* buf = buffer; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 568 | const bool lastItem = (super->indent < 0); |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 569 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 570 | for (uint32_t indent = (super->indent < 0 ? -super->indent : super->indent); indent > 1; indent >>= 1) { |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 571 | int written, ret; |
| Christian Göttsche | 9631bc9 | 2022-06-21 21:10:47 +0200 | [diff] [blame] | 572 | if (indent & 1U) { |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 573 | ret = xSnprintf(buf, n, "%s ", CRT_treeStr[TREE_STR_VERT]); |
| 574 | } else { |
| 575 | ret = xSnprintf(buf, n, " "); |
| 576 | } |
| 577 | if (ret < 0 || (size_t)ret >= n) { |
| 578 | written = n; |
| 579 | } else { |
| 580 | written = ret; |
| 581 | } |
| 582 | buf += written; |
| 583 | n -= written; |
| 584 | } |
| 585 | |
| 586 | const char* draw = CRT_treeStr[lastItem ? TREE_STR_BEND : TREE_STR_RTEE]; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 587 | xSnprintf(buf, n, "%s%s ", draw, super->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] ); |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 588 | RichString_appendWide(str, CRT_colors[PROCESS_TREE], buffer); |
| 589 | Process_writeCommand(this, attr, baseattr, str); |
| 590 | return; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 591 | } |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 592 | case PROC_COMM: { |
| 593 | const char* procComm; |
| 594 | if (this->procComm) { |
| 595 | attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM]; |
| 596 | procComm = this->procComm; |
| 597 | } else { |
| 598 | attr = CRT_colors[PROCESS_SHADOW]; |
| 599 | procComm = Process_isKernelThread(this) ? kthreadID : "N/A"; |
| 600 | } |
| 601 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 602 | Row_printLeftAlignedField(str, attr, procComm, TASK_COMM_LEN - 1); |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | case PROC_EXE: { |
| 606 | const char* procExe; |
| 607 | if (this->procExe) { |
| 608 | attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_BASENAME : PROCESS_BASENAME]; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 609 | if (settings->highlightDeletedExe) { |
| Benny Baumann | de1d063 | 2021-06-10 23:23:03 +0200 | [diff] [blame] | 610 | if (this->procExeDeleted) |
| 611 | attr = CRT_colors[FAILED_READ]; |
| 612 | else if (this->usesDeletedLib) |
| 613 | attr = CRT_colors[PROCESS_TAG]; |
| 614 | } |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 615 | procExe = this->procExe + this->procExeBasenameOffset; |
| 616 | } else { |
| 617 | attr = CRT_colors[PROCESS_SHADOW]; |
| 618 | procExe = Process_isKernelThread(this) ? kthreadID : "N/A"; |
| 619 | } |
| 620 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 621 | Row_printLeftAlignedField(str, attr, procExe, TASK_COMM_LEN - 1); |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 622 | return; |
| 623 | } |
| Benny Baumann | b6ff5c8 | 2021-05-25 19:02:12 +0200 | [diff] [blame] | 624 | case CWD: { |
| 625 | const char* cwd; |
| 626 | if (!this->procCwd) { |
| 627 | attr = CRT_colors[PROCESS_SHADOW]; |
| 628 | cwd = "N/A"; |
| 629 | } else if (String_startsWith(this->procCwd, "/proc/") && strstr(this->procCwd, " (deleted)") != NULL) { |
| 630 | attr = CRT_colors[PROCESS_SHADOW]; |
| 631 | cwd = "main thread terminated"; |
| 632 | } else { |
| 633 | cwd = this->procCwd; |
| 634 | } |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 635 | Row_printLeftAlignedField(str, attr, cwd, 25); |
| Benny Baumann | b6ff5c8 | 2021-05-25 19:02:12 +0200 | [diff] [blame] | 636 | return; |
| 637 | } |
| Benny Baumann | b83ce85 | 2022-04-03 12:57:23 +0200 | [diff] [blame] | 638 | case ELAPSED: { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 639 | const uint64_t rt = host->realtimeMs; |
| Benny Baumann | b83ce85 | 2022-04-03 12:57:23 +0200 | [diff] [blame] | 640 | const uint64_t st = this->starttime_ctime * 1000; |
| 641 | const uint64_t dt = |
| 642 | rt < st ? 0 : |
| 643 | rt - st; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 644 | Row_printTime(str, /* convert to hundreds of a second */ dt / 10, coloring); |
| Benny Baumann | b83ce85 | 2022-04-03 12:57:23 +0200 | [diff] [blame] | 645 | return; |
| 646 | } |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 647 | case MAJFLT: Row_printCount(str, this->majflt, coloring); return; |
| 648 | case MINFLT: Row_printCount(str, this->minflt, coloring); return; |
| 649 | case M_RESIDENT: Row_printKBytes(str, this->m_resident, coloring); return; |
| 650 | case M_VIRT: Row_printKBytes(str, this->m_virt, coloring); return; |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 651 | case NICE: |
| Nico Sonack | 11bf6a7 | 2024-04-20 21:30:40 +0200 | [diff] [blame] | 652 | if (this->nice == PROCESS_NICE_UNKNOWN) { |
| 653 | xSnprintf(buffer, n, "N/A "); |
| 654 | attr = CRT_colors[PROCESS_SHADOW]; |
| 655 | } else { |
| 656 | xSnprintf(buffer, n, "%3ld ", this->nice); |
| 657 | attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY] |
| 658 | : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY] |
| 659 | : CRT_colors[PROCESS_SHADOW]; |
| 660 | } |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 661 | break; |
| 662 | case NLWP: |
| 663 | if (this->nlwp == 1) |
| 664 | attr = CRT_colors[PROCESS_SHADOW]; |
| 665 | |
| 666 | xSnprintf(buffer, n, "%4ld ", this->nlwp); |
| 667 | break; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 668 | case PERCENT_CPU: Row_printPercentage(this->percent_cpu, buffer, n, Row_fieldWidths[PERCENT_CPU], &attr); break; |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 669 | case PERCENT_NORM_CPU: { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 670 | float cpuPercentage = this->percent_cpu / host->activeCPUs; |
| 671 | Row_printPercentage(cpuPercentage, buffer, n, Row_fieldWidths[PERCENT_CPU], &attr); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 672 | break; |
| 673 | } |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 674 | case PERCENT_MEM: Row_printPercentage(this->percent_mem, buffer, n, 4, &attr); break; |
| Christian Göttsche | 9f68c8d | 2020-12-15 19:44:52 +0100 | [diff] [blame] | 675 | case PGRP: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pgrp); break; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 676 | case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, Process_getPid(this)); break; |
| 677 | case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, Process_getParent(this)); break; |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 678 | case PRIORITY: |
| 679 | if (this->priority <= -100) |
| Hisham Muhammad | 09e241f | 2017-07-27 16:07:50 -0300 | [diff] [blame] | 680 | xSnprintf(buffer, n, " RT "); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 681 | else |
| Hisham Muhammad | 09e241f | 2017-07-27 16:07:50 -0300 | [diff] [blame] | 682 | xSnprintf(buffer, n, "%3ld ", this->priority); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 683 | break; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 684 | case PROCESSOR: xSnprintf(buffer, n, "%3d ", Settings_cpuId(settings, this->processor)); break; |
| Christian Göttsche | da49489 | 2023-01-10 19:40:04 +0100 | [diff] [blame] | 685 | case SCHEDULERPOLICY: { |
| 686 | const char* schedPolStr = "N/A"; |
| 687 | #ifdef SCHEDULER_SUPPORT |
| 688 | if (this->scheduling_policy >= 0) |
| 689 | schedPolStr = Scheduling_formatPolicy(this->scheduling_policy); |
| 690 | #endif |
| 691 | xSnprintf(buffer, n, "%-5s ", schedPolStr); |
| 692 | break; |
| 693 | } |
| Christian Göttsche | 9f68c8d | 2020-12-15 19:44:52 +0100 | [diff] [blame] | 694 | case SESSION: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->session); break; |
| Hisham Muhammad | 09e241f | 2017-07-27 16:07:50 -0300 | [diff] [blame] | 695 | case STARTTIME: xSnprintf(buffer, n, "%s", this->starttime_show); break; |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 696 | case STATE: |
| marcluque | d8dfbbd | 2021-10-12 00:45:09 +0200 | [diff] [blame] | 697 | xSnprintf(buffer, n, "%c ", processStateChar(this->state)); |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 698 | switch (this->state) { |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 699 | case RUNNABLE: |
| 700 | case RUNNING: |
| 701 | case TRACED: |
| 702 | attr = CRT_colors[PROCESS_RUN_STATE]; |
| 703 | break; |
| marcluque | d8dfbbd | 2021-10-12 00:45:09 +0200 | [diff] [blame] | 704 | |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 705 | case BLOCKED: |
| 706 | case DEFUNCT: |
| 707 | case STOPPED: |
| 708 | case UNINTERRUPTIBLE_WAIT: |
| 709 | case ZOMBIE: |
| 710 | attr = CRT_colors[PROCESS_D_STATE]; |
| 711 | break; |
| marcluque | d8dfbbd | 2021-10-12 00:45:09 +0200 | [diff] [blame] | 712 | |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 713 | case QUEUED: |
| 714 | case WAITING: |
| 715 | case IDLE: |
| 716 | case SLEEPING: |
| 717 | attr = CRT_colors[PROCESS_SHADOW]; |
| 718 | break; |
| marcluque | d8dfbbd | 2021-10-12 00:45:09 +0200 | [diff] [blame] | 719 | |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 720 | case UNKNOWN: |
| 721 | case PAGING: |
| 722 | break; |
| Valmiky Arquissandas | 64e0d94 | 2014-10-14 02:30:17 +0100 | [diff] [blame] | 723 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 724 | break; |
| Silke Hofstra | 696f79f | 2021-08-16 22:50:36 +0200 | [diff] [blame] | 725 | case ST_UID: xSnprintf(buffer, n, "%*d ", Process_uidDigits, this->st_uid); break; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 726 | case TIME: Row_printTime(str, this->time, coloring); return; |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 727 | case TGID: |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 728 | if (Process_getThreadGroup(this) == Process_getPid(this)) |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 729 | attr = CRT_colors[PROCESS_SHADOW]; |
| 730 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 731 | xSnprintf(buffer, n, "%*d ", Process_pidDigits, Process_getThreadGroup(this)); |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 732 | break; |
| Christian Göttsche | 9f68c8d | 2020-12-15 19:44:52 +0100 | [diff] [blame] | 733 | case TPGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->tpgid); break; |
| Christian Göttsche | 9a82215 | 2021-03-21 19:40:56 +0100 | [diff] [blame] | 734 | case TTY: |
| 735 | if (!this->tty_name) { |
| Christian Göttsche | a3c8285 | 2021-01-27 15:11:42 +0100 | [diff] [blame] | 736 | attr = CRT_colors[PROCESS_SHADOW]; |
| Christian Göttsche | 9a82215 | 2021-03-21 19:40:56 +0100 | [diff] [blame] | 737 | xSnprintf(buffer, n, "(no tty) "); |
| Christian Göttsche | a3c8285 | 2021-01-27 15:11:42 +0100 | [diff] [blame] | 738 | } else { |
| Christian Göttsche | 9a82215 | 2021-03-21 19:40:56 +0100 | [diff] [blame] | 739 | const char* name = String_startsWith(this->tty_name, "/dev/") ? (this->tty_name + strlen("/dev/")) : this->tty_name; |
| 740 | xSnprintf(buffer, n, "%-8s ", name); |
| Christian Göttsche | a3c8285 | 2021-01-27 15:11:42 +0100 | [diff] [blame] | 741 | } |
| 742 | break; |
| Christian Göttsche | fee744a | 2021-01-27 15:11:46 +0100 | [diff] [blame] | 743 | case USER: |
| Christian Göttsche | b4d5b5c | 2023-05-05 20:23:15 +0200 | [diff] [blame] | 744 | if (this->elevated_priv == TRI_ON) |
| Christian Göttsche | e3481a9 | 2022-10-29 19:21:12 +0200 | [diff] [blame] | 745 | attr = CRT_colors[PROCESS_PRIV]; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 746 | else if (host->htopUserId != this->st_uid) |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 747 | attr = CRT_colors[PROCESS_SHADOW]; |
| Christian Göttsche | 5359eae | 2020-12-23 12:21:29 +0100 | [diff] [blame] | 748 | |
| Hisham Muhammad | eb2803c | 2006-07-12 01:35:59 +0000 | [diff] [blame] | 749 | if (this->user) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 750 | Row_printLeftAlignedField(str, attr, this->user, 10); |
| Christian Göttsche | 5359eae | 2020-12-23 12:21:29 +0100 | [diff] [blame] | 751 | return; |
| Hisham Muhammad | eb2803c | 2006-07-12 01:35:59 +0000 | [diff] [blame] | 752 | } |
| Christian Göttsche | 5359eae | 2020-12-23 12:21:29 +0100 | [diff] [blame] | 753 | |
| Silke Hofstra | 696f79f | 2021-08-16 22:50:36 +0200 | [diff] [blame] | 754 | xSnprintf(buffer, n, "%-10d ", this->st_uid); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 755 | break; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 756 | default: |
| Sohaib Mohamed | 6f2021f | 2021-07-11 03:11:29 +0200 | [diff] [blame] | 757 | if (DynamicColumn_writeField(this, str, field)) |
| 758 | return; |
| Christian Göttsche | 615fc93 | 2021-04-18 15:52:28 +0200 | [diff] [blame] | 759 | assert(0 && "Process_writeField: default key reached"); /* should never be reached */ |
| Hisham Muhammad | 09e241f | 2017-07-27 16:07:50 -0300 | [diff] [blame] | 760 | xSnprintf(buffer, n, "- "); |
| Sohaib Mohamed | 6f2021f | 2021-07-11 03:11:29 +0200 | [diff] [blame] | 761 | break; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 762 | } |
| Benny Baumann | 2aacbf8 | 2023-11-29 21:27:07 +0100 | [diff] [blame] | 763 | |
| Christian Göttsche | a2be57d | 2021-04-14 20:54:38 +0200 | [diff] [blame] | 764 | RichString_appendAscii(str, attr, buffer); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| Hisham Muhammad | 6f868b0 | 2015-02-20 14:52:10 -0200 | [diff] [blame] | 767 | void Process_done(Process* this) { |
| Sohaib Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 768 | assert(this != NULL); |
| Benny Baumann | 02431c4 | 2020-12-19 16:21:08 +0100 | [diff] [blame] | 769 | free(this->cmdline); |
| Benny Baumann | d74e8b7 | 2021-01-30 15:31:59 +0100 | [diff] [blame] | 770 | free(this->procComm); |
| 771 | free(this->procExe); |
| Benny Baumann | b6ff5c8 | 2021-05-25 19:02:12 +0200 | [diff] [blame] | 772 | free(this->procCwd); |
| Benny Baumann | cdb660a | 2021-04-10 11:10:50 +0200 | [diff] [blame] | 773 | free(this->mergedCommand.str); |
| Christian Göttsche | 9a82215 | 2021-03-21 19:40:56 +0100 | [diff] [blame] | 774 | free(this->tty_name); |
| Hisham Muhammad | da23c8c | 2008-03-09 08:58:38 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| Benny Baumann | c0d0202 | 2021-04-24 12:06:49 +0200 | [diff] [blame] | 777 | /* This function returns the string displayed in Command column, so that sorting |
| 778 | * happens on what is displayed - whether comm, full path, basename, etc.. So |
| 779 | * this follows Process_writeField(COMM) and Process_writeCommand */ |
| Christian Göttsche | 1ef8c0e | 2021-12-16 15:57:37 +0100 | [diff] [blame] | 780 | const char* Process_getCommand(const Process* this) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 781 | const Settings* settings = this->super.host->settings; |
| 782 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 783 | if ((Process_isUserlandThread(this) && settings->showThreadNames) || !this->mergedCommand.str) { |
| Benny Baumann | c0d0202 | 2021-04-24 12:06:49 +0200 | [diff] [blame] | 784 | return this->cmdline; |
| 785 | } |
| 786 | |
| 787 | return this->mergedCommand.str; |
| Narendran Gopalakrishnan | 09fe94d | 2020-10-17 16:24:45 +0530 | [diff] [blame] | 788 | } |
| 789 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 790 | static const char* Process_getSortKey(const Process* this) { |
| 791 | return Process_getCommand(this); |
| Hisham Muhammad | da23c8c | 2008-03-09 08:58:38 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| Sohaib Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 794 | const char* Process_rowGetSortKey(Row* super) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 795 | const Process* this = (const Process*) super; |
| 796 | assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); |
| 797 | return Process_getSortKey(this); |
| Hisham Muhammad | da23c8c | 2008-03-09 08:58:38 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 800 | /* Test whether display must highlight this row (if the htop UID matches) */ |
| 801 | static bool Process_isHighlighted(const Process* this) { |
| 802 | const Machine* host = this->super.host; |
| 803 | const Settings* settings = host->settings; |
| 804 | return settings->shadowOtherUsers && this->st_uid != host->htopUserId; |
| 805 | } |
| 806 | |
| 807 | bool Process_rowIsHighlighted(const Row* super) { |
| 808 | const Process* this = (const Process*) super; |
| 809 | assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); |
| 810 | return Process_isHighlighted(this); |
| 811 | } |
| 812 | |
| 813 | /* Test whether display must follow parent process (if this thread is hidden) */ |
| 814 | static bool Process_isVisible(const Process* p, const Settings* settings) { |
| 815 | if (settings->hideUserlandThreads) |
| 816 | return !Process_isThread(p); |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | bool Process_rowIsVisible(const Row* super, const Table* table) { |
| 821 | const Process* this = (const Process*) super; |
| 822 | assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); |
| 823 | return Process_isVisible(this, table->host->settings); |
| 824 | } |
| 825 | |
| 826 | /* Test whether display must filter out this process (various mechanisms) */ |
| 827 | static bool Process_matchesFilter(const Process* this, const Table* table) { |
| 828 | const Machine* host = table->host; |
| 829 | if (host->userId != (uid_t) -1 && this->st_uid != host->userId) |
| 830 | return true; |
| 831 | |
| 832 | const char* incFilter = table->incFilter; |
| 833 | if (incFilter && !String_contains_i(Process_getCommand(this), incFilter, true)) |
| 834 | return true; |
| 835 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 836 | const ProcessTable* pt = (const ProcessTable*) host->activeTable; |
| 837 | assert(Object_isA((const Object*) pt, (const ObjectClass*) &ProcessTable_class)); |
| 838 | if (pt->pidMatchList && !Hashtable_get(pt->pidMatchList, Process_getThreadGroup(this))) |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 839 | return true; |
| 840 | |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 841 | return false; |
| 842 | } |
| 843 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 844 | bool Process_rowMatchesFilter(const Row* super, const Table* table) { |
| 845 | const Process* this = (const Process*) super; |
| 846 | assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); |
| 847 | return Process_matchesFilter(this, table); |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 848 | } |
| 849 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 850 | void Process_init(Process* this, const Machine* host) { |
| 851 | Row_init(&this->super, host); |
| 852 | |
| 853 | this->cmdlineBasenameEnd = -1; |
| Christian Göttsche | 3716fc7 | 2023-09-24 14:43:30 +0200 | [diff] [blame] | 854 | this->st_uid = (uid_t)-1; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static bool Process_setPriority(Process* this, int priority) { |
| Christian Göttsche | 36880cd | 2021-01-21 20:27:37 +0100 | [diff] [blame] | 858 | if (Settings_isReadonly()) |
| 859 | return false; |
| 860 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 861 | int old_prio = getpriority(PRIO_PROCESS, Process_getPid(this)); |
| 862 | int err = setpriority(PRIO_PROCESS, Process_getPid(this), priority); |
| Benny Baumann | 82157f5 | 2021-02-16 19:44:59 +0100 | [diff] [blame] | 863 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 864 | if (err == 0 && old_prio != getpriority(PRIO_PROCESS, Process_getPid(this))) { |
| Michael Klein | ab3a7c2 | 2015-12-07 20:10:09 +0100 | [diff] [blame] | 865 | this->nice = priority; |
| Hisham Muhammad | da23c8c | 2008-03-09 08:58:38 +0000 | [diff] [blame] | 866 | } |
| Michael Klein | ab3a7c2 | 2015-12-07 20:10:09 +0100 | [diff] [blame] | 867 | return (err == 0); |
| Hisham Muhammad | da23c8c | 2008-03-09 08:58:38 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 870 | bool Process_rowChangePriorityBy(Row* super, Arg delta) { |
| 871 | Process* this = (Process*) super; |
| 872 | assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); |
| Nathan Scott | 500fb28 | 2020-08-20 09:35:24 +1000 | [diff] [blame] | 873 | return Process_setPriority(this, this->nice + delta.i); |
| Hisham Muhammad | 47e881f | 2012-10-04 23:59:45 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 876 | static bool Process_sendSignal(Process* this, Arg sgn) { |
| 877 | return kill(Process_getPid(this), sgn.i) == 0; |
| 878 | } |
| 879 | |
| 880 | bool Process_rowSendSignal(Row* super, Arg sgn) { |
| 881 | Process* this = (Process*) super; |
| 882 | assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); |
| 883 | return Process_sendSignal(this, sgn); |
| Hisham Muhammad | da23c8c | 2008-03-09 08:58:38 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| Christian Göttsche | 90ea3ac | 2020-12-23 13:02:32 +0100 | [diff] [blame] | 886 | int Process_compare(const void* v1, const void* v2) { |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 887 | const Process* p1 = (const Process*)v1; |
| 888 | const Process* p2 = (const Process*)v2; |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 889 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 890 | const ScreenSettings* ss = p1->super.host->settings->ss; |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 891 | |
| Hisham Muhammad | 72ba20f | 2021-08-31 15:38:52 +1000 | [diff] [blame] | 892 | ProcessField key = ScreenSettings_getActiveSortKey(ss); |
| Hisham Muhammad | e8c6994 | 2020-12-17 19:08:56 -0300 | [diff] [blame] | 893 | |
| Christian Göttsche | 90ea3ac | 2020-12-23 13:02:32 +0100 | [diff] [blame] | 894 | int result = Process_compareByKey(p1, p2, key); |
| Benny Baumann | 77db240 | 2020-12-18 22:12:26 +0100 | [diff] [blame] | 895 | |
| 896 | // Implement tie-breaker (needed to make tree mode more stable) |
| 897 | if (!result) |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 898 | return SPACESHIP_NUMBER(Process_getPid(p1), Process_getPid(p2)); |
| Benny Baumann | 77db240 | 2020-12-18 22:12:26 +0100 | [diff] [blame] | 899 | |
| Hisham Muhammad | 72ba20f | 2021-08-31 15:38:52 +1000 | [diff] [blame] | 900 | return (ScreenSettings_getActiveDirection(ss) == 1) ? result : -result; |
| Benny Baumann | 77db240 | 2020-12-18 22:12:26 +0100 | [diff] [blame] | 901 | } |
| 902 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 903 | int Process_compareByParent(const Row* r1, const Row* r2) { |
| Nathan Scott | 5d778ea | 2024-01-17 14:04:24 +1100 | [diff] [blame] | 904 | int result = SPACESHIP_NUMBER( |
| 905 | r1->isRoot ? 0 : Row_getGroupOrParent(r1), |
| 906 | r2->isRoot ? 0 : Row_getGroupOrParent(r2) |
| 907 | ); |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 908 | |
| 909 | if (result != 0) |
| 910 | return result; |
| 911 | |
| 912 | return Process_compare(r1, r2); |
| 913 | } |
| 914 | |
| Christian Göttsche | 90ea3ac | 2020-12-23 13:02:32 +0100 | [diff] [blame] | 915 | int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key) { |
| Benny Baumann | 77db240 | 2020-12-18 22:12:26 +0100 | [diff] [blame] | 916 | int r; |
| 917 | |
| Christian Göttsche | 89473cc | 2020-12-15 19:44:48 +0100 | [diff] [blame] | 918 | switch (key) { |
| Hisham Muhammad | 5d48ab8 | 2006-07-11 06:13:32 +0000 | [diff] [blame] | 919 | case PERCENT_CPU: |
| Christian Göttsche | 15eab20 | 2020-10-30 17:02:20 +0100 | [diff] [blame] | 920 | case PERCENT_NORM_CPU: |
| Explorer09 | 076b913 | 2023-08-17 04:21:05 +0800 | [diff] [blame] | 921 | return compareRealNumbers(p1->percent_cpu, p2->percent_cpu); |
| Hisham Muhammad | 5d48ab8 | 2006-07-11 06:13:32 +0000 | [diff] [blame] | 922 | case PERCENT_MEM: |
| Daniel Lange | 4531b31 | 2021-01-21 14:27:23 +0100 | [diff] [blame] | 923 | return SPACESHIP_NUMBER(p1->m_resident, p2->m_resident); |
| Hisham Muhammad | 5d48ab8 | 2006-07-11 06:13:32 +0000 | [diff] [blame] | 924 | case COMM: |
| Narendran Gopalakrishnan | 09fe94d | 2020-10-17 16:24:45 +0530 | [diff] [blame] | 925 | return SPACESHIP_NULLSTR(Process_getCommand(p1), Process_getCommand(p2)); |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 926 | case PROC_COMM: { |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 927 | const char* comm1 = p1->procComm ? p1->procComm : (Process_isKernelThread(p1) ? kthreadID : ""); |
| 928 | const char* comm2 = p2->procComm ? p2->procComm : (Process_isKernelThread(p2) ? kthreadID : ""); |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 929 | return SPACESHIP_NULLSTR(comm1, comm2); |
| 930 | } |
| 931 | case PROC_EXE: { |
| Benny Baumann | 976c612 | 2021-07-14 19:24:18 +0200 | [diff] [blame] | 932 | const char* exe1 = p1->procExe ? (p1->procExe + p1->procExeBasenameOffset) : (Process_isKernelThread(p1) ? kthreadID : ""); |
| 933 | const char* exe2 = p2->procExe ? (p2->procExe + p2->procExeBasenameOffset) : (Process_isKernelThread(p2) ? kthreadID : ""); |
| Benny Baumann | aa8552b | 2021-04-18 19:25:56 +0200 | [diff] [blame] | 934 | return SPACESHIP_NULLSTR(exe1, exe2); |
| 935 | } |
| Benny Baumann | b6ff5c8 | 2021-05-25 19:02:12 +0200 | [diff] [blame] | 936 | case CWD: |
| 937 | return SPACESHIP_NULLSTR(p1->procCwd, p2->procCwd); |
| Christian Göttsche | 550a141 | 2021-05-02 13:29:39 +0200 | [diff] [blame] | 938 | case ELAPSED: |
| 939 | r = -SPACESHIP_NUMBER(p1->starttime_ctime, p2->starttime_ctime); |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 940 | return r != 0 ? r : SPACESHIP_NUMBER(Process_getPid(p1), Process_getPid(p2)); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 941 | case MAJFLT: |
| Daniel Lange | 4531b31 | 2021-01-21 14:27:23 +0100 | [diff] [blame] | 942 | return SPACESHIP_NUMBER(p1->majflt, p2->majflt); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 943 | case MINFLT: |
| Daniel Lange | 4531b31 | 2021-01-21 14:27:23 +0100 | [diff] [blame] | 944 | return SPACESHIP_NUMBER(p1->minflt, p2->minflt); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 945 | case M_RESIDENT: |
| Daniel Lange | 4531b31 | 2021-01-21 14:27:23 +0100 | [diff] [blame] | 946 | return SPACESHIP_NUMBER(p1->m_resident, p2->m_resident); |
| Christian Göttsche | fa002c0 | 2020-11-20 17:09:34 +0100 | [diff] [blame] | 947 | case M_VIRT: |
| Daniel Lange | 4531b31 | 2021-01-21 14:27:23 +0100 | [diff] [blame] | 948 | return SPACESHIP_NUMBER(p1->m_virt, p2->m_virt); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 949 | case NICE: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 950 | return SPACESHIP_NUMBER(p1->nice, p2->nice); |
| Hisham Muhammad | d357c67 | 2007-05-21 19:10:53 +0000 | [diff] [blame] | 951 | case NLWP: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 952 | return SPACESHIP_NUMBER(p1->nlwp, p2->nlwp); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 953 | case PGRP: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 954 | return SPACESHIP_NUMBER(p1->pgrp, p2->pgrp); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 955 | case PID: |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 956 | return SPACESHIP_NUMBER(Process_getPid(p1), Process_getPid(p2)); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 957 | case PPID: |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 958 | return SPACESHIP_NUMBER(Process_getParent(p1), Process_getParent(p2)); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 959 | case PRIORITY: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 960 | return SPACESHIP_NUMBER(p1->priority, p2->priority); |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 961 | case PROCESSOR: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 962 | return SPACESHIP_NUMBER(p1->processor, p2->processor); |
| Christian Göttsche | da49489 | 2023-01-10 19:40:04 +0100 | [diff] [blame] | 963 | case SCHEDULERPOLICY: |
| 964 | return SPACESHIP_NUMBER(p1->scheduling_policy, p2->scheduling_policy); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 965 | case SESSION: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 966 | return SPACESHIP_NUMBER(p1->session, p2->session); |
| 967 | case STARTTIME: |
| 968 | r = SPACESHIP_NUMBER(p1->starttime_ctime, p2->starttime_ctime); |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 969 | return r != 0 ? r : SPACESHIP_NUMBER(Process_getPid(p1), Process_getPid(p2)); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 970 | case STATE: |
| marcluque | d8dfbbd | 2021-10-12 00:45:09 +0200 | [diff] [blame] | 971 | return SPACESHIP_NUMBER(p1->state, p2->state); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 972 | case ST_UID: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 973 | return SPACESHIP_NUMBER(p1->st_uid, p2->st_uid); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 974 | case TIME: |
| Daniel Lange | 4531b31 | 2021-01-21 14:27:23 +0100 | [diff] [blame] | 975 | return SPACESHIP_NUMBER(p1->time, p2->time); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 976 | case TGID: |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 977 | return SPACESHIP_NUMBER(Process_getThreadGroup(p1), Process_getThreadGroup(p2)); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 978 | case TPGID: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 979 | return SPACESHIP_NUMBER(p1->tpgid, p2->tpgid); |
| Christian Göttsche | 9a82215 | 2021-03-21 19:40:56 +0100 | [diff] [blame] | 980 | case TTY: |
| 981 | /* Order no tty last */ |
| 982 | return SPACESHIP_DEFAULTSTR(p1->tty_name, p2->tty_name, "\x7F"); |
| Hisham Muhammad | be1700c | 2015-03-16 01:43:04 -0300 | [diff] [blame] | 983 | case USER: |
| Christian Göttsche | 397b5c4 | 2020-11-04 17:46:24 +0100 | [diff] [blame] | 984 | return SPACESHIP_NULLSTR(p1->user, p2->user); |
| Hisham Muhammad | 5d48ab8 | 2006-07-11 06:13:32 +0000 | [diff] [blame] | 985 | default: |
| Christian Göttsche | fa9f260 | 2021-12-08 13:02:18 +0100 | [diff] [blame] | 986 | CRT_debug("Process_compareByKey_Base() called with key %d", key); |
| Christian Göttsche | 615fc93 | 2021-04-18 15:52:28 +0200 | [diff] [blame] | 987 | assert(0 && "Process_compareByKey_Base: default key reached"); /* should never be reached */ |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 988 | return SPACESHIP_NUMBER(Process_getPid(p1), Process_getPid(p2)); |
| Hisham Muhammad | 5d48ab8 | 2006-07-11 06:13:32 +0000 | [diff] [blame] | 989 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 990 | } |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 991 | |
| 992 | void Process_updateComm(Process* this, const char* comm) { |
| 993 | if (!this->procComm && !comm) |
| 994 | return; |
| 995 | |
| 996 | if (this->procComm && comm && String_eq(this->procComm, comm)) |
| 997 | return; |
| 998 | |
| 999 | free(this->procComm); |
| 1000 | this->procComm = comm ? xStrdup(comm) : NULL; |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 1001 | |
| 1002 | this->mergedCommand.lastUpdate = 0; |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | static int skipPotentialPath(const char* cmdline, int end) { |
| 1006 | if (cmdline[0] != '/') |
| 1007 | return 0; |
| 1008 | |
| 1009 | int slash = 0; |
| 1010 | for (int i = 1; i < end; i++) { |
| Benny Baumann | 0d85af2 | 2021-07-14 19:18:27 +0200 | [diff] [blame] | 1011 | if (cmdline[i] == '/' && cmdline[i + 1] != '\0') { |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 1012 | slash = i + 1; |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
| Benny Baumann | 0d85af2 | 2021-07-14 19:18:27 +0200 | [diff] [blame] | 1016 | if (cmdline[i] == ' ' && cmdline[i - 1] != '\\') |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 1017 | return slash; |
| 1018 | |
| Benny Baumann | 0d85af2 | 2021-07-14 19:18:27 +0200 | [diff] [blame] | 1019 | if (cmdline[i] == ':' && cmdline[i + 1] == ' ') |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 1020 | return slash; |
| 1021 | } |
| 1022 | |
| 1023 | return slash; |
| 1024 | } |
| 1025 | |
| 1026 | void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd) { |
| 1027 | assert(basenameStart >= 0); |
| 1028 | assert((cmdline && basenameStart < (int)strlen(cmdline)) || (!cmdline && basenameStart == 0)); |
| Christian Göttsche | c408add | 2021-05-23 15:53:23 +0200 | [diff] [blame] | 1029 | assert((basenameEnd > basenameStart) || (basenameEnd == 0 && basenameStart == 0)); |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 1030 | assert((cmdline && basenameEnd <= (int)strlen(cmdline)) || (!cmdline && basenameEnd == 0)); |
| 1031 | |
| 1032 | if (!this->cmdline && !cmdline) |
| 1033 | return; |
| 1034 | |
| 1035 | if (this->cmdline && cmdline && String_eq(this->cmdline, cmdline)) |
| 1036 | return; |
| 1037 | |
| 1038 | free(this->cmdline); |
| 1039 | this->cmdline = cmdline ? xStrdup(cmdline) : NULL; |
| Christian Göttsche | 32cb302 | 2024-01-27 23:22:31 +0100 | [diff] [blame] | 1040 | if (Process_isKernelThread(this)) { |
| 1041 | /* kernel threads have no basename */ |
| 1042 | this->cmdlineBasenameStart = 0; |
| 1043 | this->cmdlineBasenameEnd = 0; |
| 1044 | } else { |
| 1045 | this->cmdlineBasenameStart = (basenameStart || !cmdline) ? basenameStart : skipPotentialPath(cmdline, basenameEnd); |
| 1046 | this->cmdlineBasenameEnd = basenameEnd; |
| 1047 | } |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 1048 | |
| 1049 | this->mergedCommand.lastUpdate = 0; |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | void Process_updateExe(Process* this, const char* exe) { |
| 1053 | if (!this->procExe && !exe) |
| 1054 | return; |
| 1055 | |
| 1056 | if (this->procExe && exe && String_eq(this->procExe, exe)) |
| 1057 | return; |
| 1058 | |
| 1059 | free(this->procExe); |
| 1060 | if (exe) { |
| 1061 | this->procExe = xStrdup(exe); |
| 1062 | const char* lastSlash = strrchr(exe, '/'); |
| 1063 | this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (lastSlash - exe + 1) : 0; |
| 1064 | } else { |
| 1065 | this->procExe = NULL; |
| 1066 | this->procExeBasenameOffset = 0; |
| 1067 | } |
| Benny Baumann | 2999fff | 2022-05-29 23:03:56 +0200 | [diff] [blame] | 1068 | |
| 1069 | this->mergedCommand.lastUpdate = 0; |
| Christian Göttsche | 05fb681 | 2021-05-18 22:29:25 +0200 | [diff] [blame] | 1070 | } |
| Christian Göttsche | 3ba6952 | 2021-12-04 19:57:47 +0100 | [diff] [blame] | 1071 | |
| Benny Baumann | edf319e | 2022-02-27 20:29:40 +0100 | [diff] [blame] | 1072 | void Process_updateCPUFieldWidths(float percentage) { |
| Explorer09 | 95f77eb | 2024-03-21 14:59:40 +0800 | [diff] [blame] | 1073 | if (!isgreaterequal(percentage, 99.9F)) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 1074 | Row_updateFieldWidth(PERCENT_CPU, 4); |
| 1075 | Row_updateFieldWidth(PERCENT_NORM_CPU, 4); |
| Benny Baumann | edf319e | 2022-02-27 20:29:40 +0100 | [diff] [blame] | 1076 | return; |
| 1077 | } |
| 1078 | |
| Kumar | 0af08bc | 2022-04-14 16:35:02 +0530 | [diff] [blame] | 1079 | // Add additional two characters, one for "." and another for precision. |
| 1080 | uint8_t width = ceil(log10(percentage + 0.1)) + 2; |
| Benny Baumann | edf319e | 2022-02-27 20:29:40 +0100 | [diff] [blame] | 1081 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 1082 | Row_updateFieldWidth(PERCENT_CPU, width); |
| 1083 | Row_updateFieldWidth(PERCENT_NORM_CPU, width); |
| Benny Baumann | edf319e | 2022-02-27 20:29:40 +0100 | [diff] [blame] | 1084 | } |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 1085 | |
| 1086 | const ProcessClass Process_class = { |
| 1087 | .super = { |
| 1088 | .super = { |
| 1089 | .extends = Class(Row), |
| 1090 | .display = Row_display, |
| 1091 | .delete = Process_delete, |
| 1092 | .compare = Process_compare |
| 1093 | }, |
| 1094 | .isHighlighted = Process_rowIsHighlighted, |
| 1095 | .isVisible = Process_rowIsVisible, |
| 1096 | .matchesFilter = Process_rowMatchesFilter, |
| 1097 | .sortKeyString = Process_rowGetSortKey, |
| 1098 | .compareByParent = Process_compareByParent, |
| 1099 | .writeField = Process_rowWriteField |
| 1100 | }, |
| 1101 | }; |