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) {
assert (this != NULL);
free(this->cmdline);
free(this->procComm);
free(this->procExe);
free(this->tty_name);
}

View File

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

View File

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

View File

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