Move procComm and procExe to main Process structure

This commit is contained in:
Benny Baumann 2021-01-30 15:31:59 +01:00 committed by BenBE
parent 10790f0a54
commit d74e8b7554
5 changed files with 31 additions and 27 deletions

View File

@ -515,6 +515,8 @@ void Process_display(const Object* cast, RichString* out) {
void Process_done(Process* this) { void Process_done(Process* this) {
assert (this != NULL); assert (this != NULL);
free(this->cmdline); free(this->cmdline);
free(this->procComm);
free(this->procExe);
free(this->tty_name); free(this->tty_name);
} }

View File

@ -102,6 +102,12 @@ typedef struct Process_ {
*/ */
char* cmdline; char* cmdline;
/* The process' "command" name */
char *procComm;
/* The main process executable */
char *procExe;
/* Offset in comm of the process basename */ /* Offset in comm of the process basename */
int basenameOffset; int basenameOffset;

View File

@ -131,8 +131,6 @@ void Process_delete(Object* cast) {
#endif #endif
free(this->cwd); free(this->cwd);
free(this->secattr); free(this->secattr);
free(this->procExe);
free(this->procComm);
free(this->mergedCommand.str); free(this->mergedCommand.str);
free(this); free(this);
} }
@ -387,8 +385,8 @@ void LinuxProcess_makeCommandStr(Process* this) {
/* Establish some shortcuts to data we need */ /* Establish some shortcuts to data we need */
const char *cmdline = this->cmdline; const char *cmdline = this->cmdline;
const char *procExe = lp->procExe; const char *procComm = this->procComm;
const char *procComm = lp->procComm; const char *procExe = this->procExe;
char *strStart = mc->str; char *strStart = mc->str;
char *str = strStart; char *str = strStart;
@ -685,9 +683,9 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
} }
case PROC_COMM: { case PROC_COMM: {
const char* procComm; const char* procComm;
if (lp->procComm) { if (this->procComm) {
attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM]; attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM];
procComm = lp->procComm; procComm = this->procComm;
} else { } else {
attr = CRT_colors[PROCESS_SHADOW]; attr = CRT_colors[PROCESS_SHADOW];
procComm = Process_isKernelThread(lp) ? kthreadID : "N/A"; procComm = Process_isKernelThread(lp) ? kthreadID : "N/A";
@ -698,11 +696,11 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
} }
case PROC_EXE: { case PROC_EXE: {
const char* procExe; const char* procExe;
if (lp->procExe) { if (this->procExe) {
attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_BASENAME : PROCESS_BASENAME]; attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_BASENAME : PROCESS_BASENAME];
if (lp->procExeDeleted) if (lp->procExeDeleted)
attr = CRT_colors[FAILED_READ]; attr = CRT_colors[FAILED_READ];
procExe = lp->procExe + lp->procExeBasenameOffset; procExe = this->procExe + lp->procExeBasenameOffset;
} else { } else {
attr = CRT_colors[PROCESS_SHADOW]; attr = CRT_colors[PROCESS_SHADOW];
procExe = Process_isKernelThread(lp) ? kthreadID : "N/A"; procExe = Process_isKernelThread(lp) ? kthreadID : "N/A";
@ -816,13 +814,13 @@ static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proce
case SECATTR: case SECATTR:
return SPACESHIP_NULLSTR(p1->secattr, p2->secattr); return SPACESHIP_NULLSTR(p1->secattr, p2->secattr);
case PROC_COMM: { case PROC_COMM: {
const char *comm1 = p1->procComm ? p1->procComm : (Process_isKernelThread(p1) ? kthreadID : ""); const char *comm1 = v1->procComm ? v1->procComm : (Process_isKernelThread(p1) ? kthreadID : "");
const char *comm2 = p2->procComm ? p2->procComm : (Process_isKernelThread(p2) ? kthreadID : ""); const char *comm2 = v2->procComm ? v2->procComm : (Process_isKernelThread(p2) ? kthreadID : "");
return strcmp(comm1, comm2); return strcmp(comm1, comm2);
} }
case PROC_EXE: { case PROC_EXE: {
const char *exe1 = p1->procExe ? (p1->procExe + p1->procExeBasenameOffset) : (Process_isKernelThread(p1) ? kthreadID : ""); const char *exe1 = v1->procExe ? (v1->procExe + p1->procExeBasenameOffset) : (Process_isKernelThread(p1) ? kthreadID : "");
const char *exe2 = p2->procExe ? (p2->procExe + p2->procExeBasenameOffset) : (Process_isKernelThread(p2) ? kthreadID : ""); const char *exe2 = v2->procExe ? (v2->procExe + p2->procExeBasenameOffset) : (Process_isKernelThread(p2) ? kthreadID : "");
return strcmp(exe1, exe2); return strcmp(exe1, exe2);
} }
case CWD: case CWD:

View File

@ -63,8 +63,6 @@ typedef struct LinuxProcessMergedCommand_ {
typedef struct LinuxProcess_ { typedef struct LinuxProcess_ {
Process super; Process super;
char *procComm;
char *procExe;
int procExeLen; int procExeLen;
int procExeBasenameOffset; int procExeBasenameOffset;
bool procExeDeleted; bool procExeDeleted;

View File

@ -1144,13 +1144,13 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
if ((amtRead = xReadfileat(procFd, "comm", command, sizeof(command))) > 0) { if ((amtRead = xReadfileat(procFd, "comm", command, sizeof(command))) > 0) {
command[amtRead - 1] = '\0'; command[amtRead - 1] = '\0';
lp->mergedCommand.maxLen += amtRead - 1; /* accommodate comm */ lp->mergedCommand.maxLen += amtRead - 1; /* accommodate comm */
if (!lp->procComm || !String_eq(command, lp->procComm)) { if (!process->procComm || !String_eq(command, process->procComm)) {
free_and_xStrdup(&lp->procComm, command); free_and_xStrdup(&process->procComm, command);
lp->mergedCommand.commChanged = true; lp->mergedCommand.commChanged = true;
} }
} else if (lp->procComm) { } else if (process->procComm) {
free(lp->procComm); free(process->procComm);
lp->procComm = NULL; process->procComm = NULL;
lp->mergedCommand.commChanged = true; lp->mergedCommand.commChanged = true;
} }
@ -1167,8 +1167,8 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
if (amtRead > 0) { if (amtRead > 0) {
filename[amtRead] = 0; filename[amtRead] = 0;
lp->mergedCommand.maxLen += amtRead; /* accommodate exe */ lp->mergedCommand.maxLen += amtRead; /* accommodate exe */
if (!lp->procExe || !String_eq(filename, lp->procExe)) { if (!process->procExe || !String_eq(filename, process->procExe)) {
free_and_xStrdup(&lp->procExe, filename); free_and_xStrdup(&process->procExe, filename);
lp->procExeLen = amtRead; lp->procExeLen = amtRead;
/* exe is guaranteed to contain at least one /, but validate anyway */ /* exe is guaranteed to contain at least one /, but validate anyway */
while (amtRead && filename[--amtRead] != '/') while (amtRead && filename[--amtRead] != '/')
@ -1177,17 +1177,17 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
lp->mergedCommand.exeChanged = true; lp->mergedCommand.exeChanged = true;
const char* deletedMarker = " (deleted)"; const char* deletedMarker = " (deleted)";
if (strlen(lp->procExe) > strlen(deletedMarker)) { if (strlen(process->procExe) > strlen(deletedMarker)) {
lp->procExeDeleted = String_eq(lp->procExe + strlen(lp->procExe) - strlen(deletedMarker), deletedMarker); lp->procExeDeleted = String_eq(process->procExe + strlen(process->procExe) - strlen(deletedMarker), deletedMarker);
if (lp->procExeDeleted && strlen(lp->procExe) - strlen(deletedMarker) == 1 && lp->procExe[0] == '/') { if (lp->procExeDeleted && strlen(process->procExe) - strlen(deletedMarker) == 1 && process->procExe[0] == '/') {
lp->procExeBasenameOffset = 0; lp->procExeBasenameOffset = 0;
} }
} }
} }
} else if (lp->procExe) { } else if (process->procExe) {
free(lp->procExe); free(process->procExe);
lp->procExe = NULL; process->procExe = NULL;
lp->procExeLen = 0; lp->procExeLen = 0;
lp->procExeBasenameOffset = 0; lp->procExeBasenameOffset = 0;
lp->procExeDeleted = false; lp->procExeDeleted = false;