Move CWD field handling to platform-neutral code

This commit is contained in:
Benny Baumann 2021-05-25 19:02:12 +02:00
parent c408add108
commit b6ff5c8a2e
6 changed files with 32 additions and 27 deletions

View File

@ -777,6 +777,20 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
Process_printLeftAlignedField(str, attr, procExe, TASK_COMM_LEN - 1); Process_printLeftAlignedField(str, attr, procExe, TASK_COMM_LEN - 1);
return; return;
} }
case CWD: {
const char* cwd;
if (!this->procCwd) {
attr = CRT_colors[PROCESS_SHADOW];
cwd = "N/A";
} else if (String_startsWith(this->procCwd, "/proc/") && strstr(this->procCwd, " (deleted)") != NULL) {
attr = CRT_colors[PROCESS_SHADOW];
cwd = "main thread terminated";
} else {
cwd = this->procCwd;
}
Process_printLeftAlignedField(str, attr, cwd, 25);
return;
}
case ELAPSED: Process_printTime(str, /* convert to hundreds of a second */ this->processList->realtimeMs / 10 - 100 * this->starttime_ctime, coloring); return; case ELAPSED: Process_printTime(str, /* convert to hundreds of a second */ this->processList->realtimeMs / 10 - 100 * this->starttime_ctime, coloring); return;
case MAJFLT: Process_printCount(str, this->majflt, coloring); return; case MAJFLT: Process_printCount(str, this->majflt, coloring); return;
case MINFLT: Process_printCount(str, this->minflt, coloring); return; case MINFLT: Process_printCount(str, this->minflt, coloring); return;
@ -915,6 +929,7 @@ void Process_done(Process* this) {
free(this->cmdline); free(this->cmdline);
free(this->procComm); free(this->procComm);
free(this->procExe); free(this->procExe);
free(this->procCwd);
free(this->mergedCommand.str); free(this->mergedCommand.str);
free(this->tty_name); free(this->tty_name);
} }
@ -1074,6 +1089,8 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField
const char *exe2 = p2->procExe ? (p2->procExe + p2->procExeBasenameOffset) : (Process_isKernelThread(p2) ? kthreadID : ""); const char *exe2 = p2->procExe ? (p2->procExe + p2->procExeBasenameOffset) : (Process_isKernelThread(p2) ? kthreadID : "");
return SPACESHIP_NULLSTR(exe1, exe2); return SPACESHIP_NULLSTR(exe1, exe2);
} }
case CWD:
return SPACESHIP_NULLSTR(p1->procCwd, p2->procCwd);
case ELAPSED: case ELAPSED:
r = -SPACESHIP_NUMBER(p1->starttime_ctime, p2->starttime_ctime); r = -SPACESHIP_NUMBER(p1->starttime_ctime, p2->starttime_ctime);
return r != 0 ? r : SPACESHIP_NUMBER(p1->pid, p2->pid); return r != 0 ? r : SPACESHIP_NUMBER(p1->pid, p2->pid);

View File

@ -17,7 +17,9 @@ in the source distribution for its full text.
#include "RichString.h" #include "RichString.h"
#define PROCESS_FLAG_IO 0x0001 #define PROCESS_FLAG_IO 0x00000001
#define PROCESS_FLAG_CWD 0x00000002
#define DEFAULT_HIGHLIGHT_SECS 5 #define DEFAULT_HIGHLIGHT_SECS 5
typedef enum ProcessField_ { typedef enum ProcessField_ {
@ -49,6 +51,7 @@ typedef enum ProcessField_ {
ELAPSED = 54, ELAPSED = 54,
PROC_COMM = 124, PROC_COMM = 124,
PROC_EXE = 125, PROC_EXE = 125,
CWD = 126,
/* Platform specific fields, defined in ${platform}/ProcessField.h */ /* Platform specific fields, defined in ${platform}/ProcessField.h */
PLATFORM_PROCESS_FIELDS PLATFORM_PROCESS_FIELDS
@ -151,6 +154,9 @@ typedef struct Process_ {
/* The main process executable */ /* The main process executable */
char *procExe; char *procExe;
/* The process/thread working directory */
char *procCwd;
/* Offset in procExe of the process basename */ /* Offset in procExe of the process basename */
int procExeBasenameOffset; int procExeBasenameOffset;

View File

@ -99,7 +99,7 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
[SECATTR] = { .name = "SECATTR", .title = " Security Attribute ", .description = "Security attribute of the process (e.g. SELinux or AppArmor)", .flags = PROCESS_FLAG_LINUX_SECATTR, }, [SECATTR] = { .name = "SECATTR", .title = " Security Attribute ", .description = "Security attribute of the process (e.g. SELinux or AppArmor)", .flags = PROCESS_FLAG_LINUX_SECATTR, },
[PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process from /proc/[pid]/comm", .flags = 0, }, [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process from /proc/[pid]/comm", .flags = 0, },
[PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process from /proc/[pid]/exe", .flags = 0, }, [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process from /proc/[pid]/exe", .flags = 0, },
[CWD] = { .name ="CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_LINUX_CWD, }, [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
}; };
Process* LinuxProcess_new(const Settings* settings) { Process* LinuxProcess_new(const Settings* settings) {
@ -116,7 +116,6 @@ void Process_delete(Object* cast) {
#ifdef HAVE_OPENVZ #ifdef HAVE_OPENVZ
free(this->ctid); free(this->ctid);
#endif #endif
free(this->cwd);
free(this->secattr); free(this->secattr);
free(this); free(this);
} }
@ -259,20 +258,6 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
xSnprintf(buffer, n, "%5lu ", lp->ctxt_diff); xSnprintf(buffer, n, "%5lu ", lp->ctxt_diff);
break; break;
case SECATTR: snprintf(buffer, n, "%-30s ", lp->secattr ? lp->secattr : "?"); break; case SECATTR: snprintf(buffer, n, "%-30s ", lp->secattr ? lp->secattr : "?"); break;
case CWD: {
const char* cwd;
if (!lp->cwd) {
attr = CRT_colors[PROCESS_SHADOW];
cwd = "N/A";
} else if (String_startsWith(lp->cwd, "/proc/") && strstr(lp->cwd, " (deleted)") != NULL) {
attr = CRT_colors[PROCESS_SHADOW];
cwd = "main thread terminated";
} else {
cwd = lp->cwd;
}
Process_printLeftAlignedField(str, attr, cwd, 25);
return;
}
default: default:
Process_writeField(this, str, field); Process_writeField(this, str, field);
return; return;
@ -364,8 +349,6 @@ static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proce
return SPACESHIP_NUMBER(p1->ctxt_diff, p2->ctxt_diff); return SPACESHIP_NUMBER(p1->ctxt_diff, p2->ctxt_diff);
case SECATTR: case SECATTR:
return SPACESHIP_NULLSTR(p1->secattr, p2->secattr); return SPACESHIP_NULLSTR(p1->secattr, p2->secattr);
case CWD:
return SPACESHIP_NULLSTR(p1->cwd, p2->cwd);
default: default:
return Process_compareByKey_Base(v1, v2, key); return Process_compareByKey_Base(v1, v2, key);
} }

View File

@ -27,7 +27,6 @@ in the source distribution for its full text.
#define PROCESS_FLAG_LINUX_CTXT 0x00004000 #define PROCESS_FLAG_LINUX_CTXT 0x00004000
#define PROCESS_FLAG_LINUX_SECATTR 0x00008000 #define PROCESS_FLAG_LINUX_SECATTR 0x00008000
#define PROCESS_FLAG_LINUX_LRS_FIX 0x00010000 #define PROCESS_FLAG_LINUX_LRS_FIX 0x00010000
#define PROCESS_FLAG_LINUX_CWD 0x00020000
#define PROCESS_FLAG_LINUX_DELAYACCT 0x00040000 #define PROCESS_FLAG_LINUX_DELAYACCT 0x00040000
typedef struct LinuxProcess_ { typedef struct LinuxProcess_ {
@ -100,7 +99,6 @@ typedef struct LinuxProcess_ {
unsigned long ctxt_diff; unsigned long ctxt_diff;
char* secattr; char* secattr;
unsigned long long int last_mlrs_calctime; unsigned long long int last_mlrs_calctime;
char* cwd;
} LinuxProcess; } LinuxProcess;
extern int pageSize; extern int pageSize;

View File

@ -911,17 +911,17 @@ static void LinuxProcessList_readCwd(LinuxProcess* process, openat_arg_t procFd)
#endif #endif
if (r < 0) { if (r < 0) {
free(process->cwd); free(process->super.procCwd);
process->cwd = NULL; process->super.procCwd = NULL;
return; return;
} }
pathBuffer[r] = '\0'; pathBuffer[r] = '\0';
if (process->cwd && String_eq(process->cwd, pathBuffer)) if (process->super.procCwd && String_eq(process->super.procCwd, pathBuffer))
return; return;
free_and_xStrdup(&process->cwd, pathBuffer); free_and_xStrdup(&process->super.procCwd, pathBuffer);
} }
#ifdef HAVE_DELAYACCT #ifdef HAVE_DELAYACCT
@ -1434,7 +1434,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
LinuxProcessList_readSecattrData(lp, procFd); LinuxProcessList_readSecattrData(lp, procFd);
} }
if (settings->flags & PROCESS_FLAG_LINUX_CWD) { if (settings->flags & PROCESS_FLAG_CWD) {
LinuxProcessList_readCwd(lp, procFd); LinuxProcessList_readCwd(lp, procFd);
} }

View File

@ -44,7 +44,8 @@ in the source distribution for its full text.
M_PSSWP = 121, \ M_PSSWP = 121, \
CTXT = 122, \ CTXT = 122, \
SECATTR = 123, \ SECATTR = 123, \
CWD = 126, \ \
DUMMY_BUMP_FIELD = CWD, \
// End of list // End of list