blob: 3c404db324b38bdf605f03bf68e03822e13d8bd1 [file] [log] [blame]
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00001/*
2htop - Process.c
Hisham Muhammadb1b3f572015-03-21 16:52:54 -03003(C) 2004-2015 Hisham H. Muhammad
Nathan Scott500fb282020-08-20 09:35:24 +10004(C) 2020 Red Hat, Inc. All Rights Reserved.
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00005Released under the GNU GPL, see the COPYING file
6in the source distribution for its full text.
7*/
8
Hisham Muhammad84281bd2011-12-26 21:35:57 +00009#include "Process.h"
Hisham Muhammad3383d8e2015-01-21 23:27:31 -020010#include "Settings.h"
Hisham Muhammad272e2d92015-03-16 23:01:48 -030011
Explorer0935129712018-12-30 12:18:27 +080012#include "config.h"
13
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000014#include "CRT.h"
David Hunt5e602f12015-08-19 13:43:20 -030015#include "StringUtils.h"
Hisham Muhammad8fa33dc2008-03-09 02:33:23 +000016#include "RichString.h"
Hisham Muhammadb4f6b112014-11-27 20:10:23 -020017#include "Platform.h"
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000018
19#include <stdio.h>
20#include <sys/time.h>
21#include <sys/resource.h>
22#include <sys/param.h>
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000023#include <sys/stat.h>
24#include <unistd.h>
Hisham Muhammad84281bd2011-12-26 21:35:57 +000025#include <stdlib.h>
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000026#include <signal.h>
27#include <string.h>
28#include <stdbool.h>
29#include <pwd.h>
Hisham Muhammaddc262f42010-03-29 18:36:11 +000030#include <time.h>
Hisham Muhammad84281bd2011-12-26 21:35:57 +000031#include <assert.h>
Hisham Muhammad94280102015-08-20 00:32:47 -030032#include <math.h>
Kang-Che Sung (宋岡哲)c01f40e2018-02-26 21:15:05 +080033#ifdef MAJOR_IN_MKDEV
34#include <sys/mkdev.h>
Wataru Ashihara41754e52018-12-15 22:06:00 +090035#elif defined(MAJOR_IN_SYSMACROS)
Kang-Che Sung (宋岡哲)c01f40e2018-02-26 21:15:05 +080036#include <sys/sysmacros.h>
37#endif
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000038
Hisham Muhammadeb2803c2006-07-12 01:35:59 +000039static int Process_getuid = -1;
40
Hishamf1f805f2016-02-10 18:48:04 -020041char Process_pidFormat[20] = "%7d ";
Hisham Muhammad94280102015-08-20 00:32:47 -030042
43static char Process_titleBuffer[20][20];
44
45void Process_setupColumnWidths() {
46 int maxPid = Platform_getMaxPid();
47 if (maxPid == -1) return;
48 int digits = ceil(log10(maxPid));
49 assert(digits < 20);
50 for (int i = 0; Process_pidColumns[i].label; i++) {
51 assert(i < 20);
Hisham Muhammad09e241f2017-07-27 16:07:50 -030052 xSnprintf(Process_titleBuffer[i], 20, "%*s ", digits, Process_pidColumns[i].label);
Hisham Muhammad94280102015-08-20 00:32:47 -030053 Process_fields[Process_pidColumns[i].id].title = Process_titleBuffer[i];
54 }
Hisham Muhammad09e241f2017-07-27 16:07:50 -030055 xSnprintf(Process_pidFormat, sizeof(Process_pidFormat), "%%%dd ", digits);
Hisham Muhammad94280102015-08-20 00:32:47 -030056}
57
Hisham Muhammadbe1700c2015-03-16 01:43:04 -030058void Process_humanNumber(RichString* str, unsigned long number, bool coloring) {
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000059 char buffer[11];
60 int len;
Daniel Flanagandd334442019-10-31 11:39:12 -050061
Hisham Muhammada939cdf2014-04-24 15:00:09 -030062 int largeNumberColor = CRT_colors[LARGE_NUMBER];
63 int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
Benny Baumann40441dc2020-09-13 23:50:24 +020064 int processGigabytesColor = CRT_colors[PROCESS_GIGABYTES];
Hisham Muhammada939cdf2014-04-24 15:00:09 -030065 int processColor = CRT_colors[PROCESS];
66 if (!coloring) {
67 largeNumberColor = CRT_colors[PROCESS];
68 processMegabytesColor = CRT_colors[PROCESS];
Benny Baumann40441dc2020-09-13 23:50:24 +020069 processGigabytesColor = CRT_colors[PROCESS];
Hisham Muhammada939cdf2014-04-24 15:00:09 -030070 }
Daniel Flanagandd334442019-10-31 11:39:12 -050071
Benny Baumann40441dc2020-09-13 23:50:24 +020072 if (number < 1000) {
73 //Plain number, no markings
74 len = snprintf(buffer, 10, "%5lu ", number);
75 RichString_appendn(str, processColor, buffer, len);
76 } else if (number < 100000) {
77 //2 digit MB, 3 digit KB
Alan Barrf49f5452018-10-13 19:20:52 +010078 len = snprintf(buffer, 10, "%2lu", number/1000);
Hisham Muhammada939cdf2014-04-24 15:00:09 -030079 RichString_appendn(str, processMegabytesColor, buffer, len);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000080 number %= 1000;
Hisham Muhammad0a4ddab2014-04-21 19:00:12 -030081 len = snprintf(buffer, 10, "%03lu ", number);
Hisham Muhammada939cdf2014-04-24 15:00:09 -030082 RichString_appendn(str, processColor, buffer, len);
Benny Baumann40441dc2020-09-13 23:50:24 +020083 } else if (number < 1000 * ONE_K) {
84 //3 digit MB
85 number /= ONE_K;
86 len = snprintf(buffer, 10, "%4luM ", number);
87 RichString_appendn(str, processMegabytesColor, buffer, len);
88 } else if (number < 10000 * ONE_K) {
89 //1 digit GB, 3 digit MB
90 number /= ONE_K;
91 len = snprintf(buffer, 10, "%1lu", number/1000);
92 RichString_appendn(str, processGigabytesColor, buffer, len);
93 number %= 1000;
94 len = snprintf(buffer, 10, "%03luM ", number);
95 RichString_appendn(str, processMegabytesColor, buffer, len);
Benny Baumanne0e59972020-09-20 19:54:53 +020096 } else if (number < 100000 * ONE_K) {
Benny Baumann40441dc2020-09-13 23:50:24 +020097 //2 digit GB, 1 digit MB
98 number /= 100 * ONE_K;
99 len = snprintf(buffer, 10, "%2lu", number/10);
100 RichString_appendn(str, processGigabytesColor, buffer, len);
101 number %= 10;
102 len = snprintf(buffer, 10, ".%1luG ", number);
103 RichString_appendn(str, processMegabytesColor, buffer, len);
104 } else if (number < 1000 * ONE_M) {
105 //3 digit GB
106 number /= ONE_M;
107 len = snprintf(buffer, 10, "%4luG ", number);
108 RichString_appendn(str, processGigabytesColor, buffer, len);
109 } else if (number < 10000 * ONE_M) {
110 //1 digit TB, 3 digit GB
111 number /= ONE_M;
112 len = snprintf(buffer, 10, "%1lu", number/1000);
113 RichString_appendn(str, largeNumberColor, buffer, len);
114 number %= 1000;
115 len = snprintf(buffer, 10, "%03luG ", number);
116 RichString_appendn(str, processGigabytesColor, buffer, len);
117 } else {
118 //2 digit TB and above
119 len = snprintf(buffer, 10, "%4.1lfT ", (double)number/ONE_G);
120 RichString_appendn(str, largeNumberColor, buffer, len);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000121 }
122}
123
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300124void Process_colorNumber(RichString* str, unsigned long long number, bool coloring) {
Hisham Muhammad9b351402011-05-26 16:31:18 +0000125 char buffer[14];
Hisham Muhammada939cdf2014-04-24 15:00:09 -0300126
127 int largeNumberColor = CRT_colors[LARGE_NUMBER];
128 int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
129 int processColor = CRT_colors[PROCESS];
130 int processShadowColor = CRT_colors[PROCESS_SHADOW];
131 if (!coloring) {
132 largeNumberColor = CRT_colors[PROCESS];
133 processMegabytesColor = CRT_colors[PROCESS];
134 processShadowColor = CRT_colors[PROCESS];
135 }
136
Hisham Muhammade940aec2017-07-10 20:57:34 -0300137 if ((long long) number == -1LL) {
138 int len = snprintf(buffer, 13, " no perm ");
139 RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len);
adrien1018536941f2018-12-30 20:18:35 +0800140 } else if (number >= 100000LL * ONE_DECIMAL_T) {
141 xSnprintf(buffer, 13, "%11llu ", number / ONE_DECIMAL_G);
142 RichString_appendn(str, largeNumberColor, buffer, 12);
143 } else if (number >= 100LL * ONE_DECIMAL_T) {
144 xSnprintf(buffer, 13, "%11llu ", number / ONE_DECIMAL_M);
adrien1018f15d55c2018-12-18 21:05:09 +0800145 RichString_appendn(str, largeNumberColor, buffer, 8);
146 RichString_appendn(str, processMegabytesColor, buffer+8, 4);
adrien1018536941f2018-12-30 20:18:35 +0800147 } else if (number >= 10LL * ONE_DECIMAL_G) {
148 xSnprintf(buffer, 13, "%11llu ", number / ONE_DECIMAL_K);
Hisham Muhammada939cdf2014-04-24 15:00:09 -0300149 RichString_appendn(str, largeNumberColor, buffer, 5);
150 RichString_appendn(str, processMegabytesColor, buffer+5, 3);
151 RichString_appendn(str, processColor, buffer+8, 4);
Hisham Muhammad9b351402011-05-26 16:31:18 +0000152 } else {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300153 xSnprintf(buffer, 13, "%11llu ", number);
Hisham Muhammada939cdf2014-04-24 15:00:09 -0300154 RichString_appendn(str, largeNumberColor, buffer, 2);
155 RichString_appendn(str, processMegabytesColor, buffer+2, 3);
156 RichString_appendn(str, processColor, buffer+5, 3);
157 RichString_appendn(str, processShadowColor, buffer+8, 4);
Hisham Muhammad9b351402011-05-26 16:31:18 +0000158 }
159}
160
Hisham Muhammad272e2d92015-03-16 23:01:48 -0300161void Process_printTime(RichString* str, unsigned long long totalHundredths) {
162 unsigned long long totalSeconds = totalHundredths / 100;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000163
Hisham Muhammad272e2d92015-03-16 23:01:48 -0300164 unsigned long long hours = totalSeconds / 3600;
165 int minutes = (totalSeconds / 60) % 60;
166 int seconds = totalSeconds % 60;
167 int hundredths = totalHundredths - (totalSeconds * 100);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000168 char buffer[11];
Hisham Muhammad9c44f582011-12-14 23:29:07 +0000169 if (hours >= 100) {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300170 xSnprintf(buffer, 10, "%7lluh ", hours);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000171 RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000172 } else {
Hisham Muhammad9c44f582011-12-14 23:29:07 +0000173 if (hours) {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300174 xSnprintf(buffer, 10, "%2lluh", hours);
Hisham Muhammad9c44f582011-12-14 23:29:07 +0000175 RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300176 xSnprintf(buffer, 10, "%02d:%02d ", minutes, seconds);
Hisham Muhammad9c44f582011-12-14 23:29:07 +0000177 } else {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300178 xSnprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths);
Hisham Muhammad9c44f582011-12-14 23:29:07 +0000179 }
180 RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000181 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000182}
183
Hisham Muhammad93f091c2008-03-08 23:39:48 +0000184static inline void Process_writeCommand(Process* this, int attr, int baseattr, RichString* str) {
Tobias Geerinckx-Rice293eec42015-07-29 21:14:29 +0200185 int start = RichString_size(str), finish = 0;
186 char* comm = this->comm;
187
188 if (this->settings->highlightBaseName || !this->settings->showProgramPath) {
189 int i, basename = 0;
190 for (i = 0; i < this->basenameOffset; i++) {
191 if (comm[i] == '/') {
192 basename = i + 1;
193 } else if (comm[i] == ':') {
194 finish = i + 1;
195 break;
Hisham Muhammadf2a190b2014-02-27 17:11:23 -0300196 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000197 }
Tobias Geerinckx-Rice293eec42015-07-29 21:14:29 +0200198 if (!finish) {
199 if (this->settings->showProgramPath)
200 start += basename;
201 else
202 comm += basename;
203 finish = this->basenameOffset - basename;
204 }
205 finish += start - 1;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000206 }
Tobias Geerinckx-Rice293eec42015-07-29 21:14:29 +0200207
208 RichString_append(str, attr, comm);
209
210 if (this->settings->highlightBaseName)
211 RichString_setAttrn(str, baseattr, start, finish);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000212}
213
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300214void Process_outputRate(RichString* str, char* buffer, int n, double rate, int coloring) {
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200215 int largeNumberColor = CRT_colors[LARGE_NUMBER];
216 int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
217 int processColor = CRT_colors[PROCESS];
218 if (!coloring) {
219 largeNumberColor = CRT_colors[PROCESS];
220 processMegabytesColor = CRT_colors[PROCESS];
Hisham Muhammad2338ad52008-03-14 18:50:49 +0000221 }
Benny Baumann29ec1152020-09-07 11:53:58 +0200222 if (isnan(rate)) {
Hisham797bcd02016-02-20 02:22:57 -0200223 int len = snprintf(buffer, n, " no perm ");
224 RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len);
225 } else if (rate < ONE_K) {
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200226 int len = snprintf(buffer, n, "%7.2f B/s ", rate);
227 RichString_appendn(str, processColor, buffer, len);
adrien1018536941f2018-12-30 20:18:35 +0800228 } else if (rate < ONE_M) {
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200229 int len = snprintf(buffer, n, "%7.2f K/s ", rate / ONE_K);
230 RichString_appendn(str, processColor, buffer, len);
adrien1018536941f2018-12-30 20:18:35 +0800231 } else if (rate < ONE_G) {
232 int len = snprintf(buffer, n, "%7.2f M/s ", rate / ONE_M);
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200233 RichString_appendn(str, processMegabytesColor, buffer, len);
adrien1018536941f2018-12-30 20:18:35 +0800234 } else if (rate < ONE_T) {
235 int len = snprintf(buffer, n, "%7.2f G/s ", rate / ONE_G);
236 RichString_appendn(str, largeNumberColor, buffer, len);
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200237 } else {
adrien1018536941f2018-12-30 20:18:35 +0800238 int len = snprintf(buffer, n, "%7.2f T/s ", rate / ONE_T);
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200239 RichString_appendn(str, largeNumberColor, buffer, len);
240 }
Hisham Muhammad2338ad52008-03-14 18:50:49 +0000241}
242
Hisham Muhammad4c24a9b2015-03-31 23:23:10 -0300243void Process_writeField(Process* this, RichString* str, ProcessField field) {
Hisham Muhammad2f30cd12014-04-24 15:08:32 -0300244 char buffer[256]; buffer[255] = '\0';
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000245 int attr = CRT_colors[DEFAULT_COLOR];
Hisham Muhammad93f091c2008-03-08 23:39:48 +0000246 int baseattr = CRT_colors[PROCESS_BASENAME];
Hisham Muhammadd8e14802010-11-22 12:40:20 +0000247 int n = sizeof(buffer) - 1;
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200248 bool coloring = this->settings->highlightMegabytes;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000249
250 switch (field) {
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300251 case PERCENT_CPU: {
252 if (this->percent_cpu > 999.9) {
Daniel Flanagandd334442019-10-31 11:39:12 -0500253 xSnprintf(buffer, n, "%4u ", (unsigned int)this->percent_cpu);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300254 } else if (this->percent_cpu > 99.9) {
Daniel Flanagandd334442019-10-31 11:39:12 -0500255 xSnprintf(buffer, n, "%3u. ", (unsigned int)this->percent_cpu);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300256 } else {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300257 xSnprintf(buffer, n, "%4.1f ", this->percent_cpu);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300258 }
259 break;
260 }
261 case PERCENT_MEM: {
262 if (this->percent_mem > 99.9) {
Daniel Flanagandd334442019-10-31 11:39:12 -0500263 xSnprintf(buffer, n, "100. ");
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300264 } else {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300265 xSnprintf(buffer, n, "%4.1f ", this->percent_mem);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300266 }
267 break;
268 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000269 case COMM: {
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200270 if (this->settings->highlightThreads && Process_isThread(this)) {
Hisham Muhammad93f091c2008-03-08 23:39:48 +0000271 attr = CRT_colors[PROCESS_THREAD];
272 baseattr = CRT_colors[PROCESS_THREAD_BASENAME];
273 }
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200274 if (!this->settings->treeView || this->indent == 0) {
Hisham Muhammad93f091c2008-03-08 23:39:48 +0000275 Process_writeCommand(this, attr, baseattr, str);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000276 return;
277 } else {
278 char* buf = buffer;
279 int maxIndent = 0;
Hisham Muhammadca6b9232011-11-03 22:12:12 +0000280 bool lastItem = (this->indent < 0);
281 int indent = (this->indent < 0 ? -this->indent : this->indent);
Hisham Muhammadca6b9232011-11-03 22:12:12 +0000282
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000283 for (int i = 0; i < 32; i++)
Hishamfa1b5d12016-05-04 15:34:22 -0300284 if (indent & (1U << i))
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000285 maxIndent = i+1;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000286 for (int i = 0; i < maxIndent - 1; i++) {
Hisham Muhammad90518bf2019-02-10 00:36:34 +0100287 int written, ret;
Hisham Muhammadca6b9232011-11-03 22:12:12 +0000288 if (indent & (1 << i))
Hisham Muhammad90518bf2019-02-10 00:36:34 +0100289 ret = snprintf(buf, n, "%s ", CRT_treeStr[TREE_STR_VERT]);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000290 else
Hisham Muhammad90518bf2019-02-10 00:36:34 +0100291 ret = snprintf(buf, n, " ");
292 if (ret < 0 || ret >= n) {
293 written = n;
294 } else {
295 written = ret;
296 }
Hisham Muhammadca6b9232011-11-03 22:12:12 +0000297 buf += written;
298 n -= written;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000299 }
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200300 const char* draw = CRT_treeStr[lastItem ? (this->settings->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE];
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300301 xSnprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] );
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000302 RichString_append(str, CRT_colors[PROCESS_TREE], buffer);
Hisham Muhammad93f091c2008-03-08 23:39:48 +0000303 Process_writeCommand(this, attr, baseattr, str);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000304 return;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000305 }
306 }
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300307 case MAJFLT: Process_colorNumber(str, this->majflt, coloring); return;
308 case MINFLT: Process_colorNumber(str, this->minflt, coloring); return;
309 case M_RESIDENT: Process_humanNumber(str, this->m_resident * PAGE_SIZE_KB, coloring); return;
310 case M_SIZE: Process_humanNumber(str, this->m_size * PAGE_SIZE_KB, coloring); return;
311 case NICE: {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300312 xSnprintf(buffer, n, "%3ld ", this->nice);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300313 attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
314 : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
315 : attr;
316 break;
317 }
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300318 case NLWP: xSnprintf(buffer, n, "%4ld ", this->nlwp); break;
319 case PGRP: xSnprintf(buffer, n, Process_pidFormat, this->pgrp); break;
320 case PID: xSnprintf(buffer, n, Process_pidFormat, this->pid); break;
321 case PPID: xSnprintf(buffer, n, Process_pidFormat, this->ppid); break;
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300322 case PRIORITY: {
Ivan Kozik07086fc2016-09-06 09:22:38 +0000323 if(this->priority <= -100)
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300324 xSnprintf(buffer, n, " RT ");
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300325 else
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300326 xSnprintf(buffer, n, "%3ld ", this->priority);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300327 break;
328 }
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300329 case PROCESSOR: xSnprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break;
330 case SESSION: xSnprintf(buffer, n, Process_pidFormat, this->session); break;
331 case STARTTIME: xSnprintf(buffer, n, "%s", this->starttime_show); break;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000332 case STATE: {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300333 xSnprintf(buffer, n, "%c ", this->state);
Valmiky Arquissandas64e0d942014-10-14 02:30:17 +0100334 switch(this->state) {
335 case 'R':
336 attr = CRT_colors[PROCESS_R_STATE];
337 break;
338 case 'D':
339 attr = CRT_colors[PROCESS_D_STATE];
340 break;
Valmiky Arquissandas64e0d942014-10-14 02:30:17 +0100341 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000342 break;
343 }
Daniel Langec34be412018-10-07 11:16:12 +0200344 case ST_UID: xSnprintf(buffer, n, "%5d ", this->st_uid); break;
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300345 case TIME: Process_printTime(str, this->time); return;
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300346 case TGID: xSnprintf(buffer, n, Process_pidFormat, this->tgid); break;
347 case TPGID: xSnprintf(buffer, n, Process_pidFormat, this->tpgid); break;
348 case TTY_NR: xSnprintf(buffer, n, "%3u:%3u ", major(this->tty_nr), minor(this->tty_nr)); break;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000349 case USER: {
Hisham Muhammad02a30bf2010-02-25 01:43:18 +0000350 if (Process_getuid != (int) this->st_uid)
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000351 attr = CRT_colors[PROCESS_SHADOW];
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000352 if (this->user) {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300353 xSnprintf(buffer, n, "%-9s ", this->user);
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000354 } else {
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300355 xSnprintf(buffer, n, "%-9d ", this->st_uid);
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000356 }
Hisham Muhammad9b351402011-05-26 16:31:18 +0000357 if (buffer[9] != '\0') {
358 buffer[9] = ' ';
359 buffer[10] = '\0';
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000360 }
361 break;
362 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000363 default:
Hisham Muhammad09e241f2017-07-27 16:07:50 -0300364 xSnprintf(buffer, n, "- ");
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000365 }
366 RichString_append(str, attr, buffer);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000367}
368
Hisham Muhammad4c24a9b2015-03-31 23:23:10 -0300369void Process_display(Object* cast, RichString* out) {
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000370 Process* this = (Process*) cast;
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200371 ProcessField* fields = this->settings->fields;
Hisham Muhammadd8e14802010-11-22 12:40:20 +0000372 RichString_prune(out);
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000373 for (int i = 0; fields[i]; i++)
Hisham Muhammad4c24a9b2015-03-31 23:23:10 -0300374 As_Process(this)->writeField(this, out, fields[i]);
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200375 if (this->settings->shadowOtherUsers && (int)this->st_uid != Process_getuid)
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000376 RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
377 if (this->tag == true)
378 RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
Hisham Muhammada9c0ea32011-03-22 20:37:08 +0000379 assert(out->chlen > 0);
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000380}
381
Hisham Muhammad6f868b02015-02-20 14:52:10 -0200382void Process_done(Process* this) {
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000383 assert (this != NULL);
Hisham Muhammadf54a37b2014-05-03 17:49:05 -0300384 free(this->comm);
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000385}
386
Hisham Muhammad4c24a9b2015-03-31 23:23:10 -0300387ProcessClass Process_class = {
388 .super = {
389 .extends = Class(Object),
390 .display = Process_display,
391 .delete = Process_delete,
392 .compare = Process_compare
393 },
394 .writeField = Process_writeField,
Hisham Muhammad00b324b2012-12-05 15:12:20 +0000395};
396
Hisham Muhammad6f868b02015-02-20 14:52:10 -0200397void Process_init(Process* this, struct Settings_* settings) {
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200398 this->settings = settings;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000399 this->tag = false;
Hisham Muhammad9eb91212010-06-17 19:02:03 +0000400 this->showChildren = true;
Hisham Muhammadd8e14802010-11-22 12:40:20 +0000401 this->show = true;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000402 this->updated = false;
Hisham Muhammadcb297af2014-04-09 17:43:54 -0300403 this->basenameOffset = -1;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000404 if (Process_getuid == -1) Process_getuid = getuid();
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000405}
406
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000407void Process_toggleTag(Process* this) {
408 this->tag = this->tag == true ? false : true;
409}
410
411bool Process_setPriority(Process* this, int priority) {
Hisham Muhammad543d65c2017-07-26 15:40:55 -0300412 CRT_dropPrivileges();
Michael Kleinab3a7c22015-12-07 20:10:09 +0100413 int old_prio = getpriority(PRIO_PROCESS, this->pid);
414 int err = setpriority(PRIO_PROCESS, this->pid, priority);
Hisham Muhammad543d65c2017-07-26 15:40:55 -0300415 CRT_restorePrivileges();
Michael Kleinab3a7c22015-12-07 20:10:09 +0100416 if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
417 this->nice = priority;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000418 }
Michael Kleinab3a7c22015-12-07 20:10:09 +0100419 return (err == 0);
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000420}
421
Nathan Scott500fb282020-08-20 09:35:24 +1000422bool Process_changePriorityBy(Process* this, Arg delta) {
423 return Process_setPriority(this, this->nice + delta.i);
Hisham Muhammad47e881f2012-10-04 23:59:45 +0000424}
425
Nathan Scott500fb282020-08-20 09:35:24 +1000426bool Process_sendSignal(Process* this, Arg sgn) {
Hisham Muhammad543d65c2017-07-26 15:40:55 -0300427 CRT_dropPrivileges();
Nathan Scott500fb282020-08-20 09:35:24 +1000428 bool ok = (kill(this->pid, sgn.i) == 0);
Hisham Muhammad543d65c2017-07-26 15:40:55 -0300429 CRT_restorePrivileges();
Nathan Scott500fb282020-08-20 09:35:24 +1000430 return ok;
Hisham Muhammadda23c8c2008-03-09 08:58:38 +0000431}
432
Hisham Muhammad78d09f92014-04-25 19:41:23 -0300433long Process_pidCompare(const void* v1, const void* v2) {
Christian Göttsche18b1e9f2020-09-23 14:15:51 +0200434 const Process* p1 = (const Process*)v1;
435 const Process* p2 = (const Process*)v2;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000436 return (p1->pid - p2->pid);
437}
438
Hisham Muhammad78d09f92014-04-25 19:41:23 -0300439long Process_compare(const void* v1, const void* v2) {
Christian Göttsche18b1e9f2020-09-23 14:15:51 +0200440 const Process *p1, *p2;
441 const Settings *settings = ((const Process*)v1)->settings;
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200442 if (settings->direction == 1) {
Christian Göttsche18b1e9f2020-09-23 14:15:51 +0200443 p1 = (const Process*)v1;
444 p2 = (const Process*)v2;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000445 } else {
Christian Göttsche18b1e9f2020-09-23 14:15:51 +0200446 p2 = (const Process*)v1;
447 p1 = (const Process*)v2;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000448 }
Hisham Muhammad3383d8e2015-01-21 23:27:31 -0200449 switch (settings->sortKey) {
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000450 case PERCENT_CPU:
451 return (p2->percent_cpu > p1->percent_cpu ? 1 : -1);
452 case PERCENT_MEM:
453 return (p2->m_resident - p1->m_resident);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000454 case COMM:
455 return strcmp(p1->comm, p2->comm);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300456 case MAJFLT:
457 return (p2->majflt - p1->majflt);
458 case MINFLT:
459 return (p2->minflt - p1->minflt);
460 case M_RESIDENT:
461 return (p2->m_resident - p1->m_resident);
462 case M_SIZE:
463 return (p2->m_size - p1->m_size);
464 case NICE:
465 return (p1->nice - p2->nice);
Hisham Muhammadd357c672007-05-21 19:10:53 +0000466 case NLWP:
467 return (p1->nlwp - p2->nlwp);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300468 case PGRP:
469 return (p1->pgrp - p2->pgrp);
470 case PID:
471 return (p1->pid - p2->pid);
472 case PPID:
473 return (p1->ppid - p2->ppid);
474 case PRIORITY:
475 return (p1->priority - p2->priority);
Hisham Muhammad272e2d92015-03-16 23:01:48 -0300476 case PROCESSOR:
477 return (p1->processor - p2->processor);
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300478 case SESSION:
479 return (p1->session - p2->session);
Hisham Muhammad8d0fff22010-03-29 18:44:14 +0000480 case STARTTIME: {
481 if (p1->starttime_ctime == p2->starttime_ctime)
482 return (p1->pid - p2->pid);
483 else
484 return (p1->starttime_ctime - p2->starttime_ctime);
485 }
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300486 case STATE:
Vladimir Panteleev87be6232018-01-14 09:08:20 +0000487 return (Process_sortState(p1->state) - Process_sortState(p2->state));
Hisham Muhammadbe1700c2015-03-16 01:43:04 -0300488 case ST_UID:
489 return (p1->st_uid - p2->st_uid);
490 case TIME:
491 return ((p2->time) - (p1->time));
492 case TGID:
493 return (p1->tgid - p2->tgid);
494 case TPGID:
495 return (p1->tpgid - p2->tpgid);
496 case TTY_NR:
497 return (p1->tty_nr - p2->tty_nr);
498 case USER:
499 return strcmp(p1->user ? p1->user : "", p2->user ? p2->user : "");
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000500 default:
501 return (p1->pid - p2->pid);
502 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000503}