/*
htop - Process.c
(C) 2004-2015 Hisham H. Muhammad
(C) 2020 Red Hat, Inc. All Rights Reserved.
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
#include "Process.h"
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/resource.h>
#include "CRT.h"
#include "Macros.h"
#include "Platform.h"
#include "ProcessList.h"
#include "DynamicColumn.h"
#include "RichString.h"
#include "Settings.h"
#include "XUtils.h"
#if defined(MAJOR_IN_MKDEV)
#include <sys/mkdev.h>
#endif
/* Used to identify kernel threads in Comm and Exe columns */
static const char* const kthreadID = "KTHREAD";
static uid_t Process_getuid = (uid_t)-1;
int Process_pidDigits = PROCESS_MIN_PID_DIGITS;
int Process_uidDigits = PROCESS_MIN_UID_DIGITS;
void Process_setupColumnWidths() {
int maxPid = Platform_getMaxPid();
if (maxPid == -1)
return;
if (maxPid < (int)pow(10, PROCESS_MIN_PID_DIGITS)) {
Process_pidDigits = PROCESS_MIN_PID_DIGITS;
return;
}
Process_pidDigits = ceil(log10(maxPid));
assert(Process_pidDigits <= PROCESS_MAX_PID_DIGITS);
}
void Process_setUidColumnWidth(uid_t maxUid) {
if (maxUid < (uid_t)pow(10, PROCESS_MIN_UID_DIGITS)) {
Process_uidDigits = PROCESS_MIN_UID_DIGITS;
return;
}
Process_uidDigits = ceil(log10(maxUid));
assert(Process_uidDigits <= PROCESS_MAX_UID_DIGITS);
}
void Process_printBytes(RichString* str, unsigned long long number, bool coloring) {
char buffer[16];
int len;
int largeNumberColor = coloring ? CRT_colors[LARGE_NUMBER] : CRT_colors[PROCESS];
int processMegabytesColor = coloring ? CRT_colors[PROCESS_MEGABYTES] : CRT_colors[PROCESS];
int processGigabytesColor = coloring ? CRT_colors[PROCESS_GIGABYTES] : CRT_colors[PROCESS];
int shadowColor = coloring ? CRT_colors[PROCESS_SHADOW] : CRT_colors[PROCESS];
int processColor = CRT_colors[PROCESS];
if (number == ULLONG_MAX) {
//Invalid number
RichString_appendAscii(str, shadowColor, " N/A ");
return;
}
number /= ONE_K;
if (number < 1000) {
//Plain number, no markings
len = xSnprintf(buffer, sizeof(buffer), "%5llu ", number);
RichString_appendnAscii(str, processColor, buffer, len);
}