Linux: use merge command line helpers

This commit is contained in:
Christian Göttsche 2021-05-18 22:36:03 +02:00 committed by BenBE
parent 6ad4f345dc
commit 7892ac68fb
1 changed files with 17 additions and 45 deletions

View File

@ -1012,11 +1012,10 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
return false; return false;
if (amtRead == 0) { if (amtRead == 0) {
if (process->state == 'Z') { if (process->state != 'Z') {
process->cmdlineBasenameEnd = 0;
} else {
process->isKernelThread = true; process->isKernelThread = true;
} }
Process_updateCmdline(process, NULL, 0, 0);
return true; return true;
} }
@ -1130,25 +1129,14 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
tokenEnd = lastChar + 1; tokenEnd = lastChar + 1;
} }
ProcessMergedCommand *mc = &process->mergedCommand; Process_updateCmdline(process, command, tokenStart, tokenEnd);
if (!process->cmdline || !String_eq(command, process->cmdline)) {
free_and_xStrdup(&process->cmdline, command);
process->cmdlineBasenameStart = tokenStart;
process->cmdlineBasenameEnd = tokenEnd;
mc->cmdlineChanged = true;
}
/* /proc/[pid]/comm could change, so should be updated */ /* /proc/[pid]/comm could change, so should be updated */
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';
if (!process->procComm || !String_eq(command, process->procComm)) { Process_updateComm(process, command);
free_and_xStrdup(&process->procComm, command);
mc->commChanged = true;
}
} else if (process->procComm) { } else if (process->procComm) {
free(process->procComm); Process_updateComm(process, NULL);
process->procComm = NULL;
mc->commChanged = true;
} }
char filename[MAX_NAME + 1]; char filename[MAX_NAME + 1];
@ -1166,33 +1154,23 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
if (!process->procExe || if (!process->procExe ||
(!process->procExeDeleted && !String_eq(filename, process->procExe)) || (!process->procExeDeleted && !String_eq(filename, process->procExe)) ||
(process->procExeDeleted && !String_startsWith(filename, process->procExe))) { (process->procExeDeleted && !String_startsWith(filename, process->procExe))) {
free_and_xStrdup(&process->procExe, filename);
/* exe is guaranteed to contain at least one /, but validate anyway */
while (amtRead && filename[--amtRead] != '/')
;
process->procExeBasenameOffset = amtRead + 1;
mc->exeChanged = true;
const char* deletedMarker = " (deleted)"; const char* deletedMarker = " (deleted)";
if (strlen(process->procExe) > strlen(deletedMarker)) { const size_t markerLen = strlen(deletedMarker);
process->procExeDeleted = String_eq(process->procExe + strlen(process->procExe) - strlen(deletedMarker), deletedMarker); const size_t filenameLen = strlen(filename);
if (process->procExeDeleted) { if (filenameLen > markerLen) {
process->procExe[strlen(process->procExe) - strlen(deletedMarker)] = '\0'; process->procExeDeleted = String_eq(filename + filenameLen - markerLen, deletedMarker);
if (String_eq(process->procExe, "/")) if (process->procExeDeleted)
process->procExeBasenameOffset = 0; filename[filenameLen - markerLen] = '\0';
}
} }
Process_updateExe(process, filename);
} }
} else if (process->procExe) { } else if (process->procExe) {
free(process->procExe); Process_updateExe(process, NULL);
process->procExe = NULL;
process->procExeBasenameOffset = 0;
process->procExeDeleted = false; process->procExeDeleted = false;
mc->exeChanged = true;
} }
return true; return true;
@ -1449,17 +1427,11 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
LinuxProcessList_readCwd(lp, procFd); LinuxProcessList_readCwd(lp, procFd);
} }
if (proc->state == 'Z' && (proc->cmdlineBasenameEnd == 0)) { if (proc->state == 'Z' && !proc->cmdline) {
proc->cmdlineBasenameEnd = -1; Process_updateCmdline(proc, command, 0, 0);
free_and_xStrdup(&proc->cmdline, command);
proc->cmdlineBasenameStart = 0;
proc->mergedCommand.commChanged = true;
} else if (Process_isThread(proc)) { } else if (Process_isThread(proc)) {
if (settings->showThreadNames || Process_isKernelThread(proc)) { if (settings->showThreadNames || Process_isKernelThread(proc)) {
proc->cmdlineBasenameEnd = -1; Process_updateCmdline(proc, command, 0, 0);
free_and_xStrdup(&proc->cmdline, command);
proc->cmdlineBasenameStart = 0;
proc->mergedCommand.commChanged = true;
} }
if (Process_isKernelThread(proc)) { if (Process_isKernelThread(proc)) {