Process.c 41 KB

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