DragonFlyBSD: add EXE and COMM columns and use merged command line helpers

This commit is contained in:
Christian Göttsche 2021-05-19 17:44:37 +02:00 committed by BenBE
parent d445676f09
commit 72724d42f3
3 changed files with 62 additions and 20 deletions

View File

@ -44,6 +44,8 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
[TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, .defaultSortDesc = true, },
[NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
[TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, },
[PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process", .flags = 0, },
[PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process", .flags = 0, },
[JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, },
[JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, },
};
@ -69,7 +71,7 @@ static void DragonFlyBSDProcess_writeField(const Process* this, RichString* str,
size_t n = sizeof(buffer) - 1;
switch (field) {
// add Platform-specific fields here
case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, (fp->kernel ? -1 : this->pid)); break;
case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, Process_isKernelThread(this) ? -1 : this->pid); break;
case JID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, fp->jid); break;
case JAIL: Process_printLeftAlignedField(str, attr, fp->jname, 11); return;
default:

View File

@ -262,29 +262,68 @@ static inline void DragonFlyBSDProcessList_scanMemoryInfo(ProcessList* pl) {
pl->usedSwap *= pageSizeKb;
}
static char* DragonFlyBSDProcessList_readProcessName(kvm_t* kd, const struct kinfo_proc* kproc, int* basenameEnd) {
//static void DragonFlyBSDProcessList_updateExe(const struct kinfo_proc* kproc, Process* proc) {
// const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, kproc->kp_pid };
// char buffer[2048];
// size_t size = sizeof(buffer);
// if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
// Process_updateExe(proc, NULL);
// return;
// }
//
// /* Kernel threads return an empty buffer */
// if (buffer[0] == '\0') {
// Process_updateExe(proc, NULL);
// return;
// }
//
// Process_updateExe(proc, buffer);
//}
static void DragonFlyBSDProcessList_updateExe(const struct kinfo_proc* kproc, Process* proc) {
if (Process_isKernelThread(proc))
return;
char path[32];
xSnprintf(path, sizeof(path), "/proc/%d/file", kproc->kp_pid);
char target[PATH_MAX];
ssize_t ret = readlink(path, target, sizeof(target) - 1);
if (ret <= 0)
return;
target[ret] = '\0';
Process_updateExe(proc, target);
}
static void DragonFlyBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
Process_updateComm(proc, kproc->kp_comm);
char** argv = kvm_getargv(kd, kproc, 0);
if (!argv) {
return xStrdup(kproc->kp_comm);
if (!argv || !argv[0]) {
Process_updateCmdline(proc, kproc->kp_comm, 0, strlen(kproc->kp_comm));
return;
}
int len = 0;
size_t len = 0;
for (int i = 0; argv[i]; i++) {
len += strlen(argv[i]) + 1;
}
char* comm = xMalloc(len);
char* at = comm;
*basenameEnd = 0;
char* cmdline = xMalloc(len);
char* at = cmdline;
int end = 0;
for (int i = 0; argv[i]; i++) {
at = stpcpy(at, argv[i]);
if (!*basenameEnd) {
*basenameEnd = at - comm;
if (end == 0) {
end = at - cmdline;
}
*at = ' ';
at++;
*at++ = ' ';
}
at--;
*at = '\0';
return comm;
Process_updateCmdline(proc, cmdline, 0, end);
}
static inline void DragonFlyBSDProcessList_scanJails(DragonFlyBSDProcessList* dfpl) {
@ -383,7 +422,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
Process* proc = ProcessList_getProcess(super, kproc->kp_ktaddr ? (pid_t)kproc->kp_ktaddr : kproc->kp_pid, &preExisting, DragonFlyBSDProcess_new);
DragonFlyBSDProcess* dfp = (DragonFlyBSDProcess*) proc;
proc->show = ! ((hideKernelThreads && Process_isKernelThread(dfp)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
if (!preExisting) {
dfp->jid = kproc->kp_jailid;
@ -417,9 +456,10 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
free_and_xStrdup(&proc->tty_name, name);
}
DragonFlyBSDProcessList_updateExe(kproc, proc);
DragonFlyBSDProcessList_updateProcessName(dfpl->kd, kproc, proc);
ProcessList_add(super, proc);
proc->cmdline = DragonFlyBSDProcessList_readProcessName(dfpl->kd, kproc, &proc->cmdlineBasenameEnd);
proc->mergedCommand.cmdlineChanged = true;
dfp->jname = DragonFlyBSDProcessList_readJailName(dfpl, kproc->kp_jailid);
} else {
@ -436,9 +476,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
proc->user = UsersTable_getRef(super->usersTable, proc->st_uid);
}
if (settings->updateProcessNames) {
free(proc->cmdline);
proc->cmdline = DragonFlyBSDProcessList_readProcessName(dfpl->kd, kproc, &proc->cmdlineBasenameEnd);
proc->mergedCommand.cmdlineChanged = true;
DragonFlyBSDProcessList_updateProcessName(dfpl->kd, kproc, proc);
}
}
@ -526,7 +564,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
if (kproc->kp_flags & P_JAILED)
proc->state = 'J';
if (Process_isKernelThread(dfp))
if (Process_isKernelThread(proc))
super->kernelThreads++;
super->totalTasks++;

View File

@ -11,6 +11,8 @@ in the source distribution for its full text.
#define PLATFORM_PROCESS_FIELDS \
JID = 100, \
JAIL = 101, \
\
DUMMY_BUMP_FIELD = PROC_EXE, \
// End of list