Process.c 19 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - Process.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2015 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

Hisham Muhammad's avatar
Hisham Muhammad committed
8
#include "Process.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
#include "Settings.h"
10

Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include "CRT.h"
David Hunt's avatar
David Hunt committed
12
#include "StringUtils.h"
13
#include "RichString.h"
14
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
17
18
19
20

#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/param.h>
#include <sys/stat.h>
Hisham's avatar
Hisham committed
21
#include <sys/types.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
22
#include <unistd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
23
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26
27
#include <signal.h>
#include <string.h>
#include <stdbool.h>
#include <pwd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
28
#include <time.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
29
#include <assert.h>
30
#include <math.h>
31
32
33
34
35
36
#ifdef MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#elif defined(MAJOR_IN_SYSMACROS) || \
   (defined(HAVE_SYS_SYSMACROS_H) && HAVE_SYS_SYSMACROS_H)
#include <sys/sysmacros.h>
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
37

sherpya's avatar
sherpya committed
38
39
40
41
42
#ifdef __ANDROID__
#define SYS_ioprio_get __NR_ioprio_get
#define SYS_ioprio_set __NR_ioprio_set
#endif

43
// On Linux, this works only with glibc 2.1+. On earlier versions
Hisham Muhammad's avatar
Hisham Muhammad committed
44
// the behavior is similar to have a hardcoded page size.
45
#ifndef PAGE_SIZE
Hisham Muhammad's avatar
Hisham Muhammad committed
46
#define PAGE_SIZE ( sysconf(_SC_PAGESIZE) )
47
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
48
#define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K )
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
51
#include "Object.h"
52

Hisham Muhammad's avatar
Hisham Muhammad committed
53
#include <sys/types.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
54
#include <inttypes.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
55

56
#define PROCESS_FLAG_IO 0x0001
57

58
typedef enum ProcessFields {
Hisham's avatar
Hisham committed
59
   NULL_PROCESSFIELD = 0,
60
61
62
63
64
65
66
67
68
69
70
71
72
   PID = 1,
   COMM = 2,
   STATE = 3,
   PPID = 4,
   PGRP = 5,
   SESSION = 6,
   TTY_NR = 7,
   TPGID = 8,
   MINFLT = 10,
   MAJFLT = 12,
   PRIORITY = 18,
   NICE = 19,
   STARTTIME = 21,
73
   PROCESSOR = 38,
74
75
76
77
78
79
80
81
82
83
   M_SIZE = 39,
   M_RESIDENT = 40,
   ST_UID = 46,
   PERCENT_CPU = 47,
   PERCENT_MEM = 48,
   USER = 49,
   TIME = 50,
   NLWP = 51,
   TGID = 52,
} ProcessField;
Hisham Muhammad's avatar
Hisham Muhammad committed
84

85
86
87
88
89
typedef struct ProcessPidColumn_ {
   int id;
   char* label;
} ProcessPidColumn;

Hisham Muhammad's avatar
Hisham Muhammad committed
90
91
92
typedef struct Process_ {
   Object super;

Hisham Muhammad's avatar
Hisham Muhammad committed
93
   struct Settings_* settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
94

95
   unsigned long long int time;
Hisham Muhammad's avatar
Hisham Muhammad committed
96
   pid_t pid;
97
98
   pid_t ppid;
   pid_t tgid;
Hisham Muhammad's avatar
Hisham Muhammad committed
99
   char* comm;
100
   int commLen;
Hisham Muhammad's avatar
Hisham Muhammad committed
101
   int indent;
102
103
104
105

   int basenameOffset;
   bool updated;

Hisham Muhammad's avatar
Hisham Muhammad committed
106
107
   char state;
   bool tag;
Hisham Muhammad's avatar
Hisham Muhammad committed
108
   bool showChildren;
109
   bool show;
110
111
112
   unsigned int pgrp;
   unsigned int session;
   unsigned int tty_nr;
113
   int tpgid;
114
   uid_t st_uid;
Hisham Muhammad's avatar
Hisham Muhammad committed
115
   unsigned long int flags;
116
   int processor;
Hisham Muhammad's avatar
Hisham Muhammad committed
117
118
119
120
121

   float percent_cpu;
   float percent_mem;
   char* user;

Hisham Muhammad's avatar
Hisham Muhammad committed
122
123
   long int priority;
   long int nice;
124
   long int nlwp;
Hisham Muhammad's avatar
Hisham Muhammad committed
125
126
   char starttime_show[8];
   time_t starttime_ctime;
Hisham Muhammad's avatar
Hisham Muhammad committed
127

128
129
   long m_size;
   long m_resident;
Hisham Muhammad's avatar
Hisham Muhammad committed
130
131
132
133
134

   int exit_signal;

   unsigned long int minflt;
   unsigned long int majflt;
135
   #ifdef DEBUG
Hisham Muhammad's avatar
Hisham Muhammad committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
   long int itrealvalue;
   unsigned long int vsize;
   long int rss;
   unsigned long int rlim;
   unsigned long int startcode;
   unsigned long int endcode;
   unsigned long int startstack;
   unsigned long int kstkesp;
   unsigned long int kstkeip;
   unsigned long int signal;
   unsigned long int blocked;
   unsigned long int sigignore;
   unsigned long int sigcatch;
   unsigned long int wchan;
   unsigned long int nswap;
   unsigned long int cnswap;
   #endif

Hisham Muhammad's avatar
Hisham Muhammad committed
154
155
} Process;

Hisham Muhammad's avatar
Hisham Muhammad committed
156
157
158
159
typedef struct ProcessFieldData_ {
   const char* name;
   const char* title;
   const char* description;
160
   uint64_t flags;
Hisham Muhammad's avatar
Hisham Muhammad committed
161
} ProcessFieldData;
Hisham Muhammad's avatar
Hisham Muhammad committed
162

163
// Implemented in platform-specific code:
Hisham Muhammad's avatar
Hisham Muhammad committed
164
165
void Process_writeField(Process* this, RichString* str, ProcessField field);
long Process_compare(const void* v1, const void* v2);
166
167
168
void Process_delete(Object* cast);
bool Process_isThread(Process* this);
extern ProcessFieldData Process_fields[];
169
170
extern ProcessPidColumn Process_pidColumns[];
extern char Process_pidFormat[20];
Hisham Muhammad's avatar
Hisham Muhammad committed
171

172
typedef Process*(*Process_New)(struct Settings_*);
173
174
175
176
177
178
179
180
typedef void (*Process_WriteField)(Process*, RichString*, ProcessField);

typedef struct ProcessClass_ {
   const ObjectClass super;
   const Process_WriteField writeField;
} ProcessClass;

#define As_Process(this_)              ((ProcessClass*)((this_)->super.klass))
181

182
183
#define Process_isChildOf(process_, pid_) (process_->tgid == pid_ || (process_->tgid == process_->pid && process_->ppid == pid_))

184
185
#define Process_sortState(state) ((state) == 'I' ? 0x100 : (state))

Hisham Muhammad's avatar
Hisham Muhammad committed
186
}*/
187

188
189
static int Process_getuid = -1;

190
#define ONE_K 1024L
Hisham Muhammad's avatar
Hisham Muhammad committed
191
192
193
#define ONE_M (ONE_K * ONE_K)
#define ONE_G (ONE_M * ONE_K)

194
#define ONE_DECIMAL_K 1000L
195
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
196
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
197

Hisham's avatar
Hisham committed
198
char Process_pidFormat[20] = "%7d ";
199
200
201
202
203
204
205
206
207
208

static char Process_titleBuffer[20][20];

void Process_setupColumnWidths() {
   int maxPid = Platform_getMaxPid();
   if (maxPid == -1) return;
   int digits = ceil(log10(maxPid));
   assert(digits < 20);
   for (int i = 0; Process_pidColumns[i].label; i++) {
      assert(i < 20);
209
      xSnprintf(Process_titleBuffer[i], 20, "%*s ", digits, Process_pidColumns[i].label);
210
211
      Process_fields[Process_pidColumns[i].id].title = Process_titleBuffer[i];
   }
212
   xSnprintf(Process_pidFormat, sizeof(Process_pidFormat), "%%%dd ", digits);
213
214
}

215
void Process_humanNumber(RichString* str, unsigned long number, bool coloring) {
Hisham Muhammad's avatar
Hisham Muhammad committed
216
217
   char buffer[11];
   int len;
218
219
220
221
222
223
224
225
   
   int largeNumberColor = CRT_colors[LARGE_NUMBER];
   int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
   int processColor = CRT_colors[PROCESS];
   if (!coloring) {
      largeNumberColor = CRT_colors[PROCESS];
      processMegabytesColor = CRT_colors[PROCESS];
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
226
 
227
   if(number >= (10 * ONE_DECIMAL_M)) {
228
229
230
      #ifdef __LP64__
      if(number >= (100 * ONE_DECIMAL_G)) {
         len = snprintf(buffer, 10, "%4ldT ", number / ONE_G);
231
         RichString_appendn(str, largeNumberColor, buffer, len);
232
         return;
233
      } else if (number >= (1000 * ONE_DECIMAL_M)) {
234
         len = snprintf(buffer, 10, "%4.1lfT ", (double)number / ONE_G);
235
         RichString_appendn(str, largeNumberColor, buffer, len);
236
237
         return;
      }
238
      #endif
239
      if(number >= (100 * ONE_DECIMAL_M)) {
240
         len = snprintf(buffer, 10, "%4ldG ", number / ONE_M);
241
         RichString_appendn(str, largeNumberColor, buffer, len);
242
         return;
243
      }
244
      len = snprintf(buffer, 10, "%4.1lfG ", (double)number / ONE_M);
245
      RichString_appendn(str, largeNumberColor, buffer, len);
246
      return;
247
   } else if (number >= 100000) {
Hisham Muhammad's avatar
Hisham Muhammad committed
248
      len = snprintf(buffer, 10, "%4ldM ", number / ONE_K);
249
      RichString_appendn(str, processMegabytesColor, buffer, len);
250
      return;
251
   } else if (number >= 1000) {
Hisham Muhammad's avatar
Hisham Muhammad committed
252
      len = snprintf(buffer, 10, "%2ld", number/1000);
253
      RichString_appendn(str, processMegabytesColor, buffer, len);
Hisham Muhammad's avatar
Hisham Muhammad committed
254
      number %= 1000;
255
      len = snprintf(buffer, 10, "%03lu ", number);
256
      RichString_appendn(str, processColor, buffer, len);
257
      return;
Hisham Muhammad's avatar
Hisham Muhammad committed
258
   }
259
   len = snprintf(buffer, 10, "%5lu ", number);
260
   RichString_appendn(str, processColor, buffer, len);
Hisham Muhammad's avatar
Hisham Muhammad committed
261
262
}

263
void Process_colorNumber(RichString* str, unsigned long long number, bool coloring) {
264
   char buffer[14];
265
266
267
268
269
270
271
272
273
274
275

   int largeNumberColor = CRT_colors[LARGE_NUMBER];
   int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
   int processColor = CRT_colors[PROCESS];
   int processShadowColor = CRT_colors[PROCESS_SHADOW];
   if (!coloring) {
      largeNumberColor = CRT_colors[PROCESS];
      processMegabytesColor = CRT_colors[PROCESS];
      processShadowColor = CRT_colors[PROCESS];
   }

276
277
278
279
   if ((long long) number == -1LL) {
      int len = snprintf(buffer, 13, "    no perm ");
      RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len);
   } else if (number > 10000000000) {
280
      xSnprintf(buffer, 13, "%11lld ", number / 1000);
281
282
283
      RichString_appendn(str, largeNumberColor, buffer, 5);
      RichString_appendn(str, processMegabytesColor, buffer+5, 3);
      RichString_appendn(str, processColor, buffer+8, 4);
284
   } else {
285
      xSnprintf(buffer, 13, "%11llu ", number);
286
287
288
289
      RichString_appendn(str, largeNumberColor, buffer, 2);
      RichString_appendn(str, processMegabytesColor, buffer+2, 3);
      RichString_appendn(str, processColor, buffer+5, 3);
      RichString_appendn(str, processShadowColor, buffer+8, 4);
290
291
292
   }
}

293
294
void Process_printTime(RichString* str, unsigned long long totalHundredths) {
   unsigned long long totalSeconds = totalHundredths / 100;
Hisham Muhammad's avatar
Hisham Muhammad committed
295

296
297
298
299
   unsigned long long hours = totalSeconds / 3600;
   int minutes = (totalSeconds / 60) % 60;
   int seconds = totalSeconds % 60;
   int hundredths = totalHundredths - (totalSeconds * 100);
Hisham Muhammad's avatar
Hisham Muhammad committed
300
   char buffer[11];
301
   if (hours >= 100) {
302
      xSnprintf(buffer, 10, "%7lluh ", hours);
Hisham Muhammad's avatar
Hisham Muhammad committed
303
304
      RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
   } else {
305
      if (hours) {
306
         xSnprintf(buffer, 10, "%2lluh", hours);
307
         RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
308
         xSnprintf(buffer, 10, "%02d:%02d ", minutes, seconds);
309
      } else {
310
         xSnprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths);
311
312
      }
      RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
313
314
315
   }
}

316
static inline void Process_writeCommand(Process* this, int attr, int baseattr, RichString* str) {
317
318
319
320
321
322
323
324
325
326
327
   int start = RichString_size(str), finish = 0;
   char* comm = this->comm;

   if (this->settings->highlightBaseName || !this->settings->showProgramPath) {
      int i, basename = 0;
      for (i = 0; i < this->basenameOffset; i++) {
         if (comm[i] == '/') {
            basename = i + 1;
         } else if (comm[i] == ':') {
            finish = i + 1;
            break;
328
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
329
      }
330
331
332
333
334
335
336
337
      if (!finish) {
         if (this->settings->showProgramPath)
            start += basename;
         else
            comm += basename;
         finish = this->basenameOffset - basename;
      }
      finish += start - 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
338
   }
339
340
341
342
343

   RichString_append(str, attr, comm);

   if (this->settings->highlightBaseName)
      RichString_setAttrn(str, baseattr, start, finish);
Hisham Muhammad's avatar
Hisham Muhammad committed
344
345
}

346
void Process_outputRate(RichString* str, char* buffer, int n, double rate, int coloring) {
Hisham Muhammad's avatar
Hisham Muhammad committed
347
348
349
350
351
352
353
   int largeNumberColor = CRT_colors[LARGE_NUMBER];
   int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
   int processColor = CRT_colors[PROCESS];
   if (!coloring) {
      largeNumberColor = CRT_colors[PROCESS];
      processMegabytesColor = CRT_colors[PROCESS];
   }
354
355
356
357
   if (rate == -1) {
      int len = snprintf(buffer, n, "    no perm ");
      RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len);
   } else if (rate < ONE_K) {
Hisham Muhammad's avatar
Hisham Muhammad committed
358
359
360
361
362
363
364
365
366
367
368
      int len = snprintf(buffer, n, "%7.2f B/s ", rate);
      RichString_appendn(str, processColor, buffer, len);
   } else if (rate < ONE_K * ONE_K) {
      int len = snprintf(buffer, n, "%7.2f K/s ", rate / ONE_K);
      RichString_appendn(str, processColor, buffer, len);
   } else if (rate < ONE_K * ONE_K * ONE_K) {
      int len = snprintf(buffer, n, "%7.2f M/s ", rate / ONE_K / ONE_K);
      RichString_appendn(str, processMegabytesColor, buffer, len);
   } else {
      int len = snprintf(buffer, n, "%7.2f G/s ", rate / ONE_K / ONE_K / ONE_K);
      RichString_appendn(str, largeNumberColor, buffer, len);
369
370
371
   }
}

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
void Process_printPercentage(float val, char* buffer, int n, int* attr) {
   if (val >= 0) {
      if (val < 100) {
         xSnprintf(buffer, n, "%4.1f ", val);
      } else if (val < 1000) {
         xSnprintf(buffer, n, "%3d. ", (unsigned int)val); 
      } else {
         xSnprintf(buffer, n, "%4d ", (unsigned int)val); 
      }
   } else {
      *attr = CRT_colors[PROCESS_SHADOW];
      xSnprintf(buffer, n, " N/A ");
   }
}

387
void Process_writeField(Process* this, RichString* str, ProcessField field) {
388
   char buffer[256]; buffer[255] = '\0';
Hisham Muhammad's avatar
Hisham Muhammad committed
389
   int attr = CRT_colors[DEFAULT_COLOR];
390
   int baseattr = CRT_colors[PROCESS_BASENAME];
391
   int n = sizeof(buffer) - 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
392
   bool coloring = this->settings->highlightMegabytes;
Hisham Muhammad's avatar
Hisham Muhammad committed
393
394

   switch (field) {
395
396
   case PERCENT_CPU: Process_printPercentage(this->percent_cpu, buffer, n, &attr); break;
   case PERCENT_MEM: Process_printPercentage(this->percent_mem, buffer, n, &attr); break;
Hisham Muhammad's avatar
Hisham Muhammad committed
397
   case COMM: {
Hisham Muhammad's avatar
Hisham Muhammad committed
398
      if (this->settings->highlightThreads && Process_isThread(this)) {
399
400
401
         attr = CRT_colors[PROCESS_THREAD];
         baseattr = CRT_colors[PROCESS_THREAD_BASENAME];
      }
402
403
      ScreenSettings* ss = this->settings->ss;
      if (!ss->treeView || this->indent == 0) {
404
         Process_writeCommand(this, attr, baseattr, str);
Hisham Muhammad's avatar
Hisham Muhammad committed
405
406
407
408
         return;
      } else {
         char* buf = buffer;
         int maxIndent = 0;
409
410
411
         bool lastItem = (this->indent < 0);
         int indent = (this->indent < 0 ? -this->indent : this->indent);

Hisham Muhammad's avatar
Hisham Muhammad committed
412
         for (int i = 0; i < 32; i++)
413
            if (indent & (1U << i))
Hisham Muhammad's avatar
Hisham Muhammad committed
414
               maxIndent = i+1;
415
          for (int i = 0; i < maxIndent - 1; i++) {
416
417
            int written;
            if (indent & (1 << i))
Hisham Muhammad's avatar
Hisham Muhammad committed
418
               written = snprintf(buf, n, "%s  ", CRT_treeStr[TREE_STR_VERT]);
Hisham Muhammad's avatar
Hisham Muhammad committed
419
            else
420
421
422
               written = snprintf(buf, n, "   ");
            buf += written;
            n -= written;
423
         }
424
         const char* draw = CRT_treeStr[lastItem ? (ss->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE];
425
         xSnprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] );
Hisham Muhammad's avatar
Hisham Muhammad committed
426
         RichString_append(str, CRT_colors[PROCESS_TREE], buffer);
427
         Process_writeCommand(this, attr, baseattr, str);
428
         return;
Hisham Muhammad's avatar
Hisham Muhammad committed
429
430
      }
   }
431
432
433
434
435
   case MAJFLT: Process_colorNumber(str, this->majflt, coloring); return;
   case MINFLT: Process_colorNumber(str, this->minflt, coloring); return;
   case M_RESIDENT: Process_humanNumber(str, this->m_resident * PAGE_SIZE_KB, coloring); return;
   case M_SIZE: Process_humanNumber(str, this->m_size * PAGE_SIZE_KB, coloring); return;
   case NICE: {
436
      xSnprintf(buffer, n, "%3ld ", this->nice);
437
438
439
440
441
      attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
           : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
           : attr;
      break;
   }
442
443
444
445
   case NLWP: xSnprintf(buffer, n, "%4ld ", this->nlwp); break;
   case PGRP: xSnprintf(buffer, n, Process_pidFormat, this->pgrp); break;
   case PID: xSnprintf(buffer, n, Process_pidFormat, this->pid); break;
   case PPID: xSnprintf(buffer, n, Process_pidFormat, this->ppid); break;
446
   case PRIORITY: {
447
      if(this->priority <= -100)
448
         xSnprintf(buffer, n, " RT ");
449
      else
450
         xSnprintf(buffer, n, "%3ld ", this->priority);
451
452
      break;
   }
453
454
455
   case PROCESSOR: xSnprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break;
   case SESSION: xSnprintf(buffer, n, Process_pidFormat, this->session); break;
   case STARTTIME: xSnprintf(buffer, n, "%s", this->starttime_show); break;
Hisham Muhammad's avatar
Hisham Muhammad committed
456
   case STATE: {
457
      xSnprintf(buffer, n, "%c ", this->state);
458
459
460
461
462
463
464
465
      switch(this->state) {
          case 'R':
              attr = CRT_colors[PROCESS_R_STATE];
              break;
          case 'D':
              attr = CRT_colors[PROCESS_D_STATE];
              break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
466
467
      break;
   }
468
   case ST_UID: xSnprintf(buffer, n, "%4d ", this->st_uid); break;
469
   case TIME: Process_printTime(str, this->time); return;
470
471
472
   case TGID: xSnprintf(buffer, n, Process_pidFormat, this->tgid); break;
   case TPGID: xSnprintf(buffer, n, Process_pidFormat, this->tpgid); break;
   case TTY_NR: xSnprintf(buffer, n, "%3u:%3u ", major(this->tty_nr), minor(this->tty_nr)); break;
Hisham Muhammad's avatar
Hisham Muhammad committed
473
   case USER: {
Hisham Muhammad's avatar
Hisham Muhammad committed
474
      if (Process_getuid != (int) this->st_uid)
Hisham Muhammad's avatar
Hisham Muhammad committed
475
         attr = CRT_colors[PROCESS_SHADOW];
476
      if (this->user) {
477
         xSnprintf(buffer, n, "%-9s ", this->user);
478
      } else {
479
         xSnprintf(buffer, n, "%-9d ", this->st_uid);
480
      }
481
482
483
      if (buffer[9] != '\0') {
         buffer[9] = ' ';
         buffer[10] = '\0';
Hisham Muhammad's avatar
Hisham Muhammad committed
484
485
486
487
      }
      break;
   }
   default:
488
      xSnprintf(buffer, n, "- ");
Hisham Muhammad's avatar
Hisham Muhammad committed
489
490
491
492
   }
   RichString_append(str, attr, buffer);
}

493
void Process_display(Object* cast, RichString* out) {
494
   Process* this = (Process*) cast;
495
   ProcessField* fields = this->settings->ss->fields;
496
   RichString_prune(out);
497
   for (int i = 0; fields[i]; i++)
498
      As_Process(this)->writeField(this, out, fields[i]);
Hisham Muhammad's avatar
Hisham Muhammad committed
499
   if (this->settings->shadowOtherUsers && (int)this->st_uid != Process_getuid)
500
501
502
      RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
   if (this->tag == true)
      RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
503
   assert(out->chlen > 0);
504
505
}

506
void Process_done(Process* this) {
507
   assert (this != NULL);
508
   free(this->comm);
509
510
}

511
512
513
514
515
516
517
518
ProcessClass Process_class = {
   .super = {
      .extends = Class(Object),
      .display = Process_display,
      .delete = Process_delete,
      .compare = Process_compare
   },
   .writeField = Process_writeField,
519
520
};

521
void Process_init(Process* this, struct Settings_* settings) {
Hisham Muhammad's avatar
Hisham Muhammad committed
522
   this->settings = settings;
523
   this->tag = false;
Hisham Muhammad's avatar
Hisham Muhammad committed
524
   this->showChildren = true;
525
   this->show = true;
526
   this->updated = false;
527
   this->basenameOffset = -1;
528
529
530
531
532
533
534
535
   if (Process_getuid == -1) Process_getuid = getuid();
}

void Process_toggleTag(Process* this) {
   this->tag = this->tag == true ? false : true;
}

bool Process_setPriority(Process* this, int priority) {
536
   CRT_dropPrivileges();
537
538
   int old_prio = getpriority(PRIO_PROCESS, this->pid);
   int err = setpriority(PRIO_PROCESS, this->pid, priority);
539
   CRT_restorePrivileges();
540
541
   if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
      this->nice = priority;
542
   }
543
   return (err == 0);
544
545
}

546
bool Process_changePriorityBy(Process* this, int delta) {
547
548
549
   return Process_setPriority(this, this->nice + delta);
}

550
void Process_sendSignal(Process* this, int sgn) {
551
   CRT_dropPrivileges();
552
   kill(this->pid, (int) sgn);
553
   CRT_restorePrivileges();
554
555
}

556
long Process_pidCompare(const void* v1, const void* v2) {
Hisham Muhammad's avatar
Hisham Muhammad committed
557
558
   Process* p1 = (Process*)v1;
   Process* p2 = (Process*)v2;
559
560
561
   return (p1->pid - p2->pid);
}

562
long Process_compare(const void* v1, const void* v2) {
563
   Process *p1, *p2;
Hisham Muhammad's avatar
Hisham Muhammad committed
564
   Settings *settings = ((Process*)v1)->settings;
565
566
   ScreenSettings* ss = settings->ss;
   if (ss->direction == 1) {
567
568
569
570
571
572
      p1 = (Process*)v1;
      p2 = (Process*)v2;
   } else {
      p2 = (Process*)v1;
      p1 = (Process*)v2;
   }
573
   switch (ss->sortKey) {
Hisham Muhammad's avatar
Hisham Muhammad committed
574
   case PERCENT_CPU:
575
      return (p2->percent_cpu > p1->percent_cpu ? 1 : -1);
Hisham Muhammad's avatar
Hisham Muhammad committed
576
   case PERCENT_MEM:
577
      return (p2->m_resident - p1->m_resident);
Hisham Muhammad's avatar
Hisham Muhammad committed
578
   case COMM:
579
      return strcmp(p1->comm, p2->comm);
580
581
582
583
584
585
586
587
588
589
   case MAJFLT:
      return (p2->majflt - p1->majflt);
   case MINFLT:
      return (p2->minflt - p1->minflt);
   case M_RESIDENT:
      return (p2->m_resident - p1->m_resident);
   case M_SIZE:
      return (p2->m_size - p1->m_size);
   case NICE:
      return (p1->nice - p2->nice);
590
591
   case NLWP:
      return (p1->nlwp - p2->nlwp);
592
593
594
595
596
597
598
599
   case PGRP:
      return (p1->pgrp - p2->pgrp);
   case PID:
      return (p1->pid - p2->pid);
   case PPID:
      return (p1->ppid - p2->ppid);
   case PRIORITY:
      return (p1->priority - p2->priority);
600
601
   case PROCESSOR:
      return (p1->processor - p2->processor);
602
603
   case SESSION:
      return (p1->session - p2->session);
604
605
606
607
608
609
   case STARTTIME: {
      if (p1->starttime_ctime == p2->starttime_ctime)
         return (p1->pid - p2->pid);
      else
         return (p1->starttime_ctime - p2->starttime_ctime);
   }
610
   case STATE:
611
      return (Process_sortState(p1->state) - Process_sortState(p2->state));
612
613
614
615
616
617
618
619
620
621
622
623
   case ST_UID:
      return (p1->st_uid - p2->st_uid);
   case TIME:
      return ((p2->time) - (p1->time));
   case TGID:
      return (p1->tgid - p2->tgid);
   case TPGID:
      return (p1->tpgid - p2->tpgid);
   case TTY_NR:
      return (p1->tty_nr - p2->tty_nr);
   case USER:
      return strcmp(p1->user ? p1->user : "", p2->user ? p2->user : "");
Hisham Muhammad's avatar
Hisham Muhammad committed
624
   default:
625
      return (p1->pid - p2->pid);
Hisham Muhammad's avatar
Hisham Muhammad committed
626
627
   }
}