Merge pull request #239 from StoneBam/list-locks

This commit is contained in:
BenBE 2020-11-14 16:03:17 +01:00 committed by GitHub
commit 9e976b899b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 461 additions and 10 deletions

View File

@ -27,6 +27,7 @@ in the source distribution for its full text.
#include "MainPanel.h"
#include "OpenFilesScreen.h"
#include "Process.h"
#include "ProcessLocksScreen.h"
#include "ProvideCurses.h"
#include "ScreenManager.h"
#include "SignalsPanel.h"
@ -371,6 +372,17 @@ static Htop_Reaction actionLsof(State* st) {
return HTOP_REFRESH | HTOP_REDRAW_BAR;
}
static Htop_Reaction actionShowLocks(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel);
if (!p) return HTOP_OK;
ProcessLocksScreen* pls = ProcessLocksScreen_new(p);
InfoScreen_run((InfoScreen*)pls);
ProcessLocksScreen_delete((Object*)pls);
clear();
CRT_enableDelay();
return HTOP_REFRESH | HTOP_REDRAW_BAR;
}
static Htop_Reaction actionStrace(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel);
if (!p) return HTOP_OK;
@ -435,6 +447,7 @@ static const struct { const char* key; const char* info; } helpRight[] = {
{ .key = " e: ", .info = "show process environment" },
{ .key = " i: ", .info = "set IO priority" },
{ .key = " l: ", .info = "list open files with lsof" },
{ .key = " x: ", .info = "list file locks of process" },
{ .key = " s: ", .info = "trace syscalls with strace" },
{ .key = " w: ", .info = "wrap process command in multiple lines" },
{ .key = " F2 C S: ", .info = "setup" },
@ -620,6 +633,7 @@ void Action_setBindings(Htop_Action* keys) {
keys['S'] = actionSetup;
keys['C'] = actionSetup;
keys[KEY_F(2)] = actionSetup;
keys['x'] = actionShowLocks;
keys['l'] = actionLsof;
keys['s'] = actionStrace;
keys[' '] = actionTag;

View File

@ -13,9 +13,7 @@ in the source distribution for its full text.
#include <sys/stat.h>
#include "Compat.h"
#ifndef HAVE_FSTATAT
#include "XUtils.h"
#endif
int Compat_fstatat(int dirfd,
@ -44,3 +42,27 @@ int Compat_fstatat(int dirfd,
#endif
}
int Compat_readlinkat(int dirfd,
const char* dirpath,
const char* pathname,
char* buf,
size_t bufsize) {
#ifdef HAVE_READLINKAT
(void)dirpath;
return readlinkat(dirfd, pathname, buf, bufsize);
#else
(void)dirfd;
char path[4096];
xSnprintf(path, sizeof(path), "%s/%s", dirpath, pathname);
return readlink(path, buf, bufsize);
#endif
}

View File

@ -7,6 +7,7 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include <stddef.h>
#include <sys/stat.h>
@ -16,4 +17,10 @@ int Compat_fstatat(int dirfd,
struct stat* statbuf,
int flags);
int Compat_readlinkat(int dirfd,
const char* dirpath,
const char* pathname,
char* buf,
size_t bufsize);
#endif /* HEADER_Compat */

View File

@ -53,6 +53,7 @@ myhtopsources = \
Panel.c \
Process.c \
ProcessList.c \
ProcessLocksScreen.c \
RichString.c \
ScreenManager.c \
Settings.c \
@ -105,6 +106,7 @@ myhtopheaders = \
Panel.h \
Process.h \
ProcessList.h \
ProcessLocksScreen.h \
ProvideCurses.h \
RichString.h \
ScreenManager.h \

116
ProcessLocksScreen.c Normal file
View File

@ -0,0 +1,116 @@
/*
htop - ProcessLocksScreen.c
(C) 2020 htop dev team
Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
#include "ProcessLocksScreen.h"
#include <dirent.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "CRT.h"
#include "Compat.h"
#include "FunctionBar.h"
#include "IncSet.h"
#include "Platform.h"
#include "ProcessList.h"
#include "XUtils.h"
ProcessLocksScreen* ProcessLocksScreen_new(const Process* process) {
ProcessLocksScreen* this = xMalloc(sizeof(ProcessLocksScreen));
Object_setClass(this, Class(ProcessLocksScreen));
if (Process_isThread(process))
this->pid = process->tgid;
else
this->pid = process->pid;
return (ProcessLocksScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, " ID TYPE EXCLUSION READ/WRITE DEVICE:INODE START END FILENAME");
}
void ProcessLocksScreen_delete(Object* this) {
free(InfoScreen_done((InfoScreen*)this));
}
static void ProcessLocksScreen_draw(InfoScreen* this) {
InfoScreen_drawTitled(this, "Snapshot of file locks of process %d - %s", ((ProcessLocksScreen*)this)->pid, this->process->comm);
}
static inline void FileLocks_Data_clear(FileLocks_Data* data) {
free(data->locktype);
free(data->exclusive);
free(data->readwrite);
free(data->filename);
}
static void ProcessLocksScreen_scan(InfoScreen* this) {
Panel* panel = this->display;
int idx = Panel_getSelectedIndex(panel);
Panel_prune(panel);
FileLocks_ProcessData* pdata = Platform_getProcessLocks(((ProcessLocksScreen*)this)->pid);
if (!pdata) {
InfoScreen_addLine(this, "This feature is not supported on your platform.");
} else if (pdata->error) {
InfoScreen_addLine(this, "Could not determine file locks.");
} else {
FileLocks_LockData* ldata = pdata->locks;
if (!ldata) {
InfoScreen_addLine(this, "No locks have been found for the selected process.");
}
while (ldata) {
FileLocks_Data* data = &ldata->data;
char entry[512];
if (ULLONG_MAX == data->end) {
xSnprintf(entry, sizeof(entry), "%10d %-10s %-10s %-10s %02x:%02x:%020"PRIu64" %20"PRIu64" %20s %s",
data->id,
data->locktype, data->exclusive, data->readwrite,
data->dev[0], data->dev[1], data->inode,
data->start, "<END OF FILE>",
data->filename ? data->filename : "<N/A>"
);
} else {
xSnprintf(entry, sizeof(entry), "%10d %-10s %-10s %-10s %02x:%02x:%020"PRIu64" %20"PRIu64" %20"PRIu64" %s",
data->id,
data->locktype, data->exclusive, data->readwrite,
data->dev[0], data->dev[1], data->inode,
data->start, data->end,
data->filename ? data->filename : "<N/A>"
);
}
InfoScreen_addLine(this, entry);
FileLocks_Data_clear(&ldata->data);
FileLocks_LockData* old = ldata;
ldata = ldata->next;
free(old);
}
}
free(pdata);
Vector_insertionSort(this->lines);
Vector_insertionSort(panel->items);
Panel_setSelected(panel, idx);
}
const InfoScreenClass ProcessLocksScreen_class = {
.super = {
.extends = Class(Object),
.delete = ProcessLocksScreen_delete
},
.scan = ProcessLocksScreen_scan,
.draw = ProcessLocksScreen_draw
};

48
ProcessLocksScreen.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef HEADER_ProcessLocksScreen
#define HEADER_ProcessLocksScreen
/*
htop - ProcessLocksScreen.h
(C) 2020 htop dev team
Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include "InfoScreen.h"
#include <stdlib.h>
typedef struct ProcessLocksScreen_ {
InfoScreen super;
pid_t pid;
} ProcessLocksScreen;
typedef struct FileLocks_Data_ {
char* locktype;
char* exclusive;
char* readwrite;
char* filename;
int id;
unsigned int dev[2];
uint64_t inode;
uint64_t start;
uint64_t end;
} FileLocks_Data;
typedef struct FileLocks_LockData_ {
FileLocks_Data data;
struct FileLocks_LockData_* next;
} FileLocks_LockData;
typedef struct FileLocks_ProcessData_ {
bool error;
struct FileLocks_LockData_* locks;
} FileLocks_ProcessData;
extern const InfoScreenClass ProcessLocksScreen_class;
ProcessLocksScreen* ProcessLocksScreen_new(const Process* process);
void ProcessLocksScreen_delete(Object* this);
#endif

View File

@ -88,7 +88,7 @@ AC_TYPE_UID_T
# ----------------------------------------------------------------------
AC_FUNC_CLOSEDIR_VOID
AC_FUNC_STAT
AC_CHECK_FUNCS([fstatat memmove strncasecmp strstr strdup])
AC_CHECK_FUNCS([fstatat memmove readlinkat strdup strncasecmp strstr])
save_cflags="${CFLAGS}"
CFLAGS="${CFLAGS} -std=c99"

View File

@ -17,6 +17,7 @@ in the source distribution for its full text.
#include "DateMeter.h"
#include "DateTimeMeter.h"
#include "HostnameMeter.h"
#include "ProcessLocksScreen.h"
#include "UptimeMeter.h"
#include "zfs/ZfsArcMeter.h"
#include "zfs/ZfsCompressedArcMeter.h"
@ -311,6 +312,17 @@ char* Platform_getProcessEnv(pid_t pid) {
return env;
}
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
(void)pid;
(void)inode;
return NULL;
}
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
(void)pid;
return NULL;
}
bool Platform_getDiskIO(DiskIOData* data) {
// TODO
(void)data;

View File

@ -8,12 +8,17 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include <stdbool.h>
#include <sys/types.h>
#include "Action.h"
#include "SignalsPanel.h"
#include "CPUMeter.h"
#include "DiskIOMeter.h"
#include "BatteryMeter.h"
#include "CPUMeter.h"
#include "DarwinProcess.h"
#include "DiskIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
extern ProcessField Platform_defaultFields[];
@ -49,6 +54,10 @@ void Platform_setZfsCompressedArcValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
char* Platform_getInodeFilename(pid_t pid, ino_t inode);
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int *bytesReceived,

View File

@ -206,6 +206,17 @@ char* Platform_getProcessEnv(pid_t pid) {
return NULL;
}
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
(void)pid;
(void)inode;
return NULL;
}
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
(void)pid;
return NULL;
}
bool Platform_getDiskIO(DiskIOData* data) {
// TODO
(void)data;

View File

@ -8,9 +8,13 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include <stdbool.h>
#include <sys/types.h>
#include "Action.h"
#include "BatteryMeter.h"
#include "DiskIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
extern ProcessFieldData Process_fields[];
@ -41,6 +45,10 @@ void Platform_setSwapValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
char* Platform_getInodeFilename(pid_t pid, ino_t inode);
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int *bytesReceived,

View File

@ -243,6 +243,17 @@ char* Platform_getProcessEnv(pid_t pid) {
return env;
}
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
(void)pid;
(void)inode;
return NULL;
}
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
(void)pid;
return NULL;
}
bool Platform_getDiskIO(DiskIOData* data) {
if (devstat_checkversion(NULL) < 0)

View File

@ -7,9 +7,13 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include <stdbool.h>
#include <sys/types.h>
#include "Action.h"
#include "BatteryMeter.h"
#include "DiskIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
extern ProcessFieldData Process_fields[];
@ -44,6 +48,10 @@ void Platform_setZfsCompressedArcValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
char* Platform_getInodeFilename(pid_t pid, ino_t inode);
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int *bytesReceived,

View File

@ -123,6 +123,9 @@ will display the list of file descriptors opened by the process.
Display the command line of the selected process in a separate screen, wrapped
onto multiple lines as needed.
.TP
.B x
Display the active file locks of the selected process in a separate screen.
.TP
.B F1, h, ?
Go to the help screen
.TP

View File

@ -11,11 +11,16 @@ in the source distribution for its full text.
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include "BatteryMeter.h"
#include "ClockMeter.h"
#include "Compat.h"
#include "CPUMeter.h"
#include "DateMeter.h"
#include "DateTimeMeter.h"
@ -304,6 +309,120 @@ char* Platform_getProcessEnv(pid_t pid) {
return env;
}
/*
* Return the absolute path of a file given its pid&inode number
*
* Based on implementation of lslocks from util-linux:
* https://sources.debian.org/src/util-linux/2.36-3/misc-utils/lslocks.c/#L162
*/
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
struct stat sb;
struct dirent *de;
DIR *dirp;
size_t len;
int fd;
char path[PATH_MAX];
char sym[PATH_MAX];
char* ret = NULL;
memset(path, 0, sizeof(path));
memset(sym, 0, sizeof(sym));
xSnprintf(path, sizeof(path), "%s/%d/fd/", PROCDIR, pid);
if (strlen(path) >= (sizeof(path) - 2))
return NULL;
if (!(dirp = opendir(path)))
return NULL;
if ((fd = dirfd(dirp)) < 0 )
goto out;
while ((de = readdir(dirp))) {
if (String_eq(de->d_name, ".") || String_eq(de->d_name, ".."))
continue;
/* care only for numerical descriptors */
if (!strtoull(de->d_name, (char **) NULL, 10))
continue;
if (!Compat_fstatat(fd, path, de->d_name, &sb, 0) && inode != sb.st_ino)
continue;
if ((len = Compat_readlinkat(fd, path, de->d_name, sym, sizeof(sym) - 1)) < 1)
goto out;
sym[len] = '\0';
ret = xStrdup(sym);
break;
}
out:
closedir(dirp);
return ret;
}
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
FileLocks_ProcessData* pdata = xCalloc(1, sizeof(FileLocks_ProcessData));
FILE* f = fopen(PROCDIR "/locks", "r");
if (!f) {
pdata->error = true;
return pdata;
}
char buffer[1024];
FileLocks_LockData** data_ref = &pdata->locks;
while(fgets(buffer, sizeof(buffer), f)) {
if (!strchr(buffer, '\n'))
continue;
int lock_id;
char lock_type[16];
char lock_excl[16];
char lock_rw[16];
pid_t lock_pid;
unsigned int lock_dev[2];
uint64_t lock_inode;
char lock_start[25];
char lock_end[25];
if (10 != sscanf(buffer, "%d: %15s %15s %15s %d %x:%x:%"PRIu64" %24s %24s",
&lock_id, lock_type, lock_excl, lock_rw, &lock_pid,
&lock_dev[0], &lock_dev[1], &lock_inode,
lock_start, lock_end))
continue;
if (pid != lock_pid)
continue;
FileLocks_LockData* ldata = xCalloc(1, sizeof(FileLocks_LockData));
FileLocks_Data* data = &ldata->data;
data->id = lock_id;
data->locktype = xStrdup(lock_type);
data->exclusive = xStrdup(lock_excl);
data->readwrite = xStrdup(lock_rw);
data->filename = Platform_getInodeFilename(lock_pid, lock_inode);
data->dev[0] = lock_dev[0];
data->dev[1] = lock_dev[1];
data->inode = lock_inode;
data->start = strtoull(lock_start, NULL, 10);
if (!String_eq(lock_end, "EOF")) {
data->end = strtoull(lock_end, NULL, 10);
} else {
data->end = ULLONG_MAX;
}
*data_ref = ldata;
data_ref = &ldata->next;
}
fclose(f);
return pdata;
}
void Platform_getPressureStall(const char *file, bool some, double* ten, double* sixty, double* threehundred) {
*ten = *sixty = *threehundred = 0;
char procname[128+1];

View File

@ -14,6 +14,7 @@ in the source distribution for its full text.
#include "DiskIOMeter.h"
#include "Meter.h"
#include "Process.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
extern ProcessField Platform_defaultFields[];
@ -45,8 +46,13 @@ void Platform_setZramValues(Meter* this);
void Platform_setZfsArcValues(Meter* this);
void Platform_setZfsCompressedArcValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
char* Platform_getInodeFilename(pid_t pid, ino_t inode);
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
void Platform_getPressureStall(const char *file, bool some, double* ten, double* sixty, double* threehundred);
bool Platform_getDiskIO(DiskIOData* data);

View File

@ -288,6 +288,17 @@ char* Platform_getProcessEnv(pid_t pid) {
return env;
}
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
(void)pid;
(void)inode;
return NULL;
}
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
(void)pid;
return NULL;
}
bool Platform_getDiskIO(DiskIOData* data) {
// TODO
(void)data;

View File

@ -8,9 +8,13 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include <stdbool.h>
#include <sys/types.h>
#include "Action.h"
#include "BatteryMeter.h"
#include "DiskIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
extern ProcessFieldData Process_fields[];
@ -42,6 +46,10 @@ void Platform_setSwapValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
char* Platform_getInodeFilename(pid_t pid, ino_t inode);
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int *bytesReceived,

View File

@ -264,6 +264,17 @@ char* Platform_getProcessEnv(pid_t pid) {
return envBuilder.env;
}
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
(void)pid;
(void)inode;
return NULL;
}
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
(void)pid;
return NULL;
}
bool Platform_getDiskIO(DiskIOData* data) {
// TODO
(void)data;

View File

@ -9,14 +9,19 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include <libproc.h>
#include <signal.h>
#include <stdbool.h>
#include <sys/mkdev.h>
#include <sys/proc.h>
#include <sys/types.h>
#include "Action.h"
#include "BatteryMeter.h"
#include "DiskIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include <signal.h>
#include <sys/mkdev.h>
#include <sys/proc.h>
#include <libproc.h>
#define kill(pid, signal) kill(pid / 1024, signal)
@ -64,6 +69,10 @@ void Platform_setZfsCompressedArcValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
char* Platform_getInodeFilename(pid_t pid, ino_t inode);
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int *bytesReceived,

View File

@ -141,6 +141,17 @@ char* Platform_getProcessEnv(pid_t pid) {
return NULL;
}
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
(void)pid;
(void)inode;
return NULL;
}
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
(void)pid;
return NULL;
}
bool Platform_getDiskIO(DiskIOData* data) {
(void)data;
return false;

View File

@ -11,6 +11,7 @@ in the source distribution for its full text.
#include "Action.h"
#include "BatteryMeter.h"
#include "DiskIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "UnsupportedProcess.h"
@ -48,6 +49,10 @@ bool Process_isThread(const Process* this);
char* Platform_getProcessEnv(pid_t pid);
char* Platform_getInodeFilename(pid_t pid, ino_t inode);
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int *bytesReceived,