Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,20 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
Process_printLeftAlignedField(str, attr, procExe, TASK_COMM_LEN - 1);
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 MAJFLT: Process_printCount(str, this->majflt, coloring); return;
case MINFLT: Process_printCount(str, this->minflt, coloring); return;
Expand Down Expand Up @@ -915,6 +929,7 @@ void Process_done(Process* this) {
free(this->cmdline);
free(this->procComm);
free(this->procExe);
free(this->procCwd);
free(this->mergedCommand.str);
free(this->tty_name);
}
Expand Down Expand Up @@ -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 : "");
return SPACESHIP_NULLSTR(exe1, exe2);
}
case CWD:
return SPACESHIP_NULLSTR(p1->procCwd, p2->procCwd);
case ELAPSED:
r = -SPACESHIP_NUMBER(p1->starttime_ctime, p2->starttime_ctime);
return r != 0 ? r : SPACESHIP_NUMBER(p1->pid, p2->pid);
Expand Down
8 changes: 7 additions & 1 deletion Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ in the source distribution for its full text.
#include "RichString.h"


#define PROCESS_FLAG_IO 0x0001
#define PROCESS_FLAG_IO 0x00000001
#define PROCESS_FLAG_CWD 0x00000002

#define DEFAULT_HIGHLIGHT_SECS 5

typedef enum ProcessField_ {
Expand Down Expand Up @@ -49,6 +51,7 @@ typedef enum ProcessField_ {
ELAPSED = 54,
PROC_COMM = 124,
PROC_EXE = 125,
CWD = 126,

/* Platform specific fields, defined in ${platform}/ProcessField.h */
PLATFORM_PROCESS_FIELDS
Expand Down Expand Up @@ -151,6 +154,9 @@ typedef struct Process_ {
/* The main process executable */
char *procExe;

/* The process/thread working directory */
char *procCwd;

/* Offset in procExe of the process basename */
int procExeBasenameOffset;

Expand Down
25 changes: 25 additions & 0 deletions darwin/DarwinProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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_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_CWD, },
[TRANSLATED] = { .name = "TRANSLATED", .title = "T ", .description = "Translation info (T translated, N native)", .flags = 0, },
};

Expand Down Expand Up @@ -106,6 +108,25 @@ static void DarwinProcess_updateExe(pid_t pid, Process* proc) {
Process_updateExe(proc, path);
}

static void DarwinProcess_updateCwd(pid_t pid, Process* proc) {
struct proc_vnodepathinfo vpi;

int r = proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &vpi, sizeof(vpi));
if (r <= 0) {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

if (!vpi.pvi_cdir.vip_path[0]) {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

free_and_xStrdup(&proc->procCwd, vpi.pvi_cdir.vip_path);
}

static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* proc) {
Process_updateComm(proc, k->kp_proc.p_comm);

Expand Down Expand Up @@ -294,6 +315,10 @@ void DarwinProcess_setFromKInfoProc(Process* proc, const struct kinfo_proc* ps,

DarwinProcess_updateExe(ep->p_pid, proc);
DarwinProcess_updateCmdLine(ps, proc);

if (proc->settings->flags & PROCESS_FLAG_CWD) {
DarwinProcess_updateCwd(ep->p_pid, proc);
}
}

/* Mutable information */
Expand Down
2 changes: 2 additions & 0 deletions darwin/ProcessField.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ in the source distribution for its full text.

#define PLATFORM_PROCESS_FIELDS \
TRANSLATED = 100, \
\
DUMMY_BUMP_FIELD = CWD, \
// End of list


Expand Down
1 change: 1 addition & 0 deletions dragonflybsd/DragonFlyBSDProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
[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, },
[CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
[JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, },
[JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, },
};
Expand Down
24 changes: 24 additions & 0 deletions dragonflybsd/DragonFlyBSDProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ static void DragonFlyBSDProcessList_updateExe(const struct kinfo_proc* kproc, Pr
Process_updateExe(proc, target);
}

static void DragonFlyBSDProcessList_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, kproc->kp_pid };
char buffer[2048];
size_t size = sizeof(buffer);
if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

/* Kernel threads return an empty buffer */
if (buffer[0] == '\0') {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

free_and_xStrdup(&proc->procCwd, buffer);
}

static void DragonFlyBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
Process_updateComm(proc, kproc->kp_comm);

Expand Down Expand Up @@ -459,6 +479,10 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
DragonFlyBSDProcessList_updateExe(kproc, proc);
DragonFlyBSDProcessList_updateProcessName(dfpl->kd, kproc, proc);

if (settings->flags & PROCESS_FLAG_CWD) {
DragonFlyBSDProcessList_updateCwd(kproc, proc);
}

ProcessList_add(super, proc);

dfp->jname = DragonFlyBSDProcessList_readJailName(dfpl, kproc->kp_jailid);
Expand Down
2 changes: 1 addition & 1 deletion dragonflybsd/ProcessField.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in the source distribution for its full text.
JID = 100, \
JAIL = 101, \
\
DUMMY_BUMP_FIELD = PROC_EXE, \
DUMMY_BUMP_FIELD = CWD, \
// End of list


Expand Down
1 change: 1 addition & 0 deletions freebsd/FreeBSDProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
[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, },
[CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
[JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, },
[JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, },
};
Expand Down
24 changes: 24 additions & 0 deletions freebsd/FreeBSDProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,26 @@ static void FreeBSDProcessList_updateExe(const struct kinfo_proc* kproc, Process
Process_updateExe(proc, buffer);
}

static void FreeBSDProcessList_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, kproc->ki_pid };
char buffer[2048];
size_t size = sizeof(buffer);
if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

/* Kernel threads return an empty buffer */
if (buffer[0] == '\0') {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

free_and_xStrdup(&proc->procCwd, buffer);
}

static void FreeBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
Process_updateComm(proc, kproc->ki_comm);

Expand Down Expand Up @@ -495,6 +515,10 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
FreeBSDProcessList_updateExe(kproc, proc);
FreeBSDProcessList_updateProcessName(fpl->kd, kproc, proc);

if (settings->flags & PROCESS_FLAG_CWD) {
FreeBSDProcessList_updateCwd(kproc, proc);
}

fp->jname = FreeBSDProcessList_readJailName(kproc);

proc->tty_nr = kproc->ki_tdev;
Expand Down
2 changes: 1 addition & 1 deletion freebsd/ProcessField.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in the source distribution for its full text.
JID = 100, \
JAIL = 101, \
\
DUMMY_BUMP_FIELD = PROC_EXE, \
DUMMY_BUMP_FIELD = CWD, \
// End of list


Expand Down
19 changes: 1 addition & 18 deletions linux/LinuxProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, },
[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, },
[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) {
Expand All @@ -116,7 +116,6 @@ void Process_delete(Object* cast) {
#ifdef HAVE_OPENVZ
free(this->ctid);
#endif
free(this->cwd);
free(this->secattr);
free(this);
}
Expand Down Expand Up @@ -259,20 +258,6 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
xSnprintf(buffer, n, "%5lu ", lp->ctxt_diff);
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:
Process_writeField(this, str, field);
return;
Expand Down Expand Up @@ -364,8 +349,6 @@ static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proce
return SPACESHIP_NUMBER(p1->ctxt_diff, p2->ctxt_diff);
case SECATTR:
return SPACESHIP_NULLSTR(p1->secattr, p2->secattr);
case CWD:
return SPACESHIP_NULLSTR(p1->cwd, p2->cwd);
default:
return Process_compareByKey_Base(v1, v2, key);
}
Expand Down
2 changes: 0 additions & 2 deletions linux/LinuxProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ in the source distribution for its full text.
#define PROCESS_FLAG_LINUX_CTXT 0x00004000
#define PROCESS_FLAG_LINUX_SECATTR 0x00008000
#define PROCESS_FLAG_LINUX_LRS_FIX 0x00010000
#define PROCESS_FLAG_LINUX_CWD 0x00020000
#define PROCESS_FLAG_LINUX_DELAYACCT 0x00040000

typedef struct LinuxProcess_ {
Expand Down Expand Up @@ -100,7 +99,6 @@ typedef struct LinuxProcess_ {
unsigned long ctxt_diff;
char* secattr;
unsigned long long int last_mlrs_calctime;
char* cwd;
} LinuxProcess;

extern int pageSize;
Expand Down
10 changes: 5 additions & 5 deletions linux/LinuxProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -911,17 +911,17 @@ static void LinuxProcessList_readCwd(LinuxProcess* process, openat_arg_t procFd)
#endif

if (r < 0) {
free(process->cwd);
process->cwd = NULL;
free(process->super.procCwd);
process->super.procCwd = NULL;
return;
}

pathBuffer[r] = '\0';

if (process->cwd && String_eq(process->cwd, pathBuffer))
if (process->super.procCwd && String_eq(process->super.procCwd, pathBuffer))
return;

free_and_xStrdup(&process->cwd, pathBuffer);
free_and_xStrdup(&process->super.procCwd, pathBuffer);
}

#ifdef HAVE_DELAYACCT
Expand Down Expand Up @@ -1434,7 +1434,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
LinuxProcessList_readSecattrData(lp, procFd);
}

if (settings->flags & PROCESS_FLAG_LINUX_CWD) {
if (settings->flags & PROCESS_FLAG_CWD) {
LinuxProcessList_readCwd(lp, procFd);
}

Expand Down
3 changes: 2 additions & 1 deletion linux/ProcessField.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ in the source distribution for its full text.
M_PSSWP = 121, \
CTXT = 122, \
SECATTR = 123, \
CWD = 126, \
\
DUMMY_BUMP_FIELD = CWD, \
// End of list


Expand Down
6 changes: 6 additions & 0 deletions openbsd/OpenBSDProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
.description = "comm string of the process",
.flags = 0,
},
[CWD] = {
.name = "CWD",
.title = "CWD ",
.description = "The current working directory of the process",
.flags = PROCESS_FLAG_CWD,
},

};

Expand Down
24 changes: 24 additions & 0 deletions openbsd/OpenBSDProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) {
}
}

static void OpenBSDProcessList_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
const int mib[] = { CTL_KERN, KERN_PROC_CWD, kproc->ki_pid };
char buffer[2048];
size_t size = sizeof(buffer);
if (sysctl(mib, 3, buffer, &size, NULL, 0) != 0) {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

/* Kernel threads return an empty buffer */
if (buffer[0] == '\0') {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}

free_and_xStrdup(&proc->procCwd, buffer);
}

static void OpenBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
Process_updateComm(proc, kproc->p_comm);

Expand Down Expand Up @@ -272,6 +292,10 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {

OpenBSDProcessList_updateProcessName(this->kd, kproc, proc);

if (settings->flags & PROCESS_FLAG_CWD) {
OpenBSDProcessList_updateCwd(kproc, proc);
}

proc->tty_nr = kproc->p_tdev;
const char* name = ((dev_t)kproc->p_tdev != NODEV) ? devname(kproc->p_tdev, S_IFCHR) : NULL;
if (!name || String_eq(name, "??")) {
Expand Down
2 changes: 1 addition & 1 deletion solaris/ProcessField.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ in the source distribution for its full text.
CONTID = 105, \
LWPID = 106, \
\
DUMMY_BUMP_FIELD = PROC_EXE, \
DUMMY_BUMP_FIELD = CWD, \
// End of list


Expand Down
Loading