2009-06-02 19:28:19 +00:00
|
|
|
/*
|
|
|
|
htop - OpenFilesScreen.c
|
|
|
|
(C) 2005-2006 Hisham H. Muhammad
|
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "OpenFilesScreen.h"
|
|
|
|
|
|
|
|
#include "CRT.h"
|
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "ListItem.h"
|
2012-11-10 00:31:37 +00:00
|
|
|
#include "IncSet.h"
|
2015-08-19 16:43:20 +00:00
|
|
|
#include "StringUtils.h"
|
2015-03-23 18:26:56 +00:00
|
|
|
#include "FunctionBar.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2009-06-02 19:28:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-02 19:28:19 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
/*{
|
2009-06-02 19:28:19 +00:00
|
|
|
#include "Process.h"
|
|
|
|
#include "Panel.h"
|
|
|
|
|
2014-04-22 21:45:47 +00:00
|
|
|
typedef struct OpenFiles_Data_ {
|
|
|
|
char* data[256];
|
|
|
|
} OpenFiles_Data;
|
|
|
|
|
2009-06-02 19:28:19 +00:00
|
|
|
typedef struct OpenFiles_ProcessData_ {
|
2014-04-22 21:45:47 +00:00
|
|
|
OpenFiles_Data data;
|
2009-10-16 20:15:01 +00:00
|
|
|
int error;
|
2014-04-22 21:45:47 +00:00
|
|
|
struct OpenFiles_FileData_* files;
|
2009-06-02 19:28:19 +00:00
|
|
|
} OpenFiles_ProcessData;
|
|
|
|
|
|
|
|
typedef struct OpenFiles_FileData_ {
|
2014-04-22 21:45:47 +00:00
|
|
|
OpenFiles_Data data;
|
2009-06-02 19:28:19 +00:00
|
|
|
struct OpenFiles_FileData_* next;
|
|
|
|
} OpenFiles_FileData;
|
|
|
|
|
|
|
|
typedef struct OpenFilesScreen_ {
|
|
|
|
Process* process;
|
2011-03-22 22:09:42 +00:00
|
|
|
pid_t pid;
|
2009-06-02 19:28:19 +00:00
|
|
|
Panel* display;
|
|
|
|
} OpenFilesScreen;
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
static const char* OpenFilesScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL};
|
2009-06-02 19:28:19 +00:00
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
static const char* OpenFilesScreenKeys[] = {"F3", "F4", "F5", "Esc"};
|
2009-06-02 19:28:19 +00:00
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
static int OpenFilesScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27};
|
2009-06-02 19:28:19 +00:00
|
|
|
|
|
|
|
OpenFilesScreen* OpenFilesScreen_new(Process* process) {
|
|
|
|
OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen));
|
|
|
|
this->process = process;
|
2015-03-23 18:26:56 +00:00
|
|
|
FunctionBar* bar = FunctionBar_new(OpenFilesScreenFunctions, OpenFilesScreenKeys, OpenFilesScreenEvents);
|
|
|
|
this->display = Panel_new(0, 1, COLS, LINES-3, false, Class(ListItem), bar);
|
2011-03-22 22:09:42 +00:00
|
|
|
if (Process_isThread(process))
|
|
|
|
this->pid = process->tgid;
|
|
|
|
else
|
|
|
|
this->pid = process->pid;
|
2009-06-02 19:28:19 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFilesScreen_delete(OpenFilesScreen* this) {
|
|
|
|
Panel_delete((Object*)this->display);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2012-11-10 00:31:37 +00:00
|
|
|
static void OpenFilesScreen_draw(OpenFilesScreen* this, IncSet* inc) {
|
2009-06-02 19:28:19 +00:00
|
|
|
attrset(CRT_colors[METER_TEXT]);
|
|
|
|
mvhline(0, 0, ' ', COLS);
|
2012-11-10 00:31:37 +00:00
|
|
|
mvprintw(0, 0, "Snapshot of files open in process %d - %s", this->pid, this->process->comm);
|
2009-06-02 19:28:19 +00:00
|
|
|
attrset(CRT_colors[DEFAULT_COLOR]);
|
|
|
|
Panel_draw(this->display, true);
|
2012-11-10 00:31:37 +00:00
|
|
|
IncSet_drawBar(inc);
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 22:09:42 +00:00
|
|
|
static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
|
2009-06-02 19:28:19 +00:00
|
|
|
char command[1025];
|
2010-09-02 20:26:16 +00:00
|
|
|
snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid);
|
2009-06-02 19:28:19 +00:00
|
|
|
FILE* fd = popen(command, "r");
|
2014-01-16 20:51:16 +00:00
|
|
|
OpenFiles_ProcessData* pdata = calloc(1, sizeof(OpenFiles_ProcessData));
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFiles_FileData* fdata = NULL;
|
2014-04-22 21:45:47 +00:00
|
|
|
OpenFiles_Data* item = &(pdata->data);
|
2009-10-16 20:15:01 +00:00
|
|
|
if (!fd) {
|
2012-11-10 00:31:37 +00:00
|
|
|
pdata->error = 127;
|
|
|
|
return pdata;
|
2009-10-16 20:15:01 +00:00
|
|
|
}
|
2009-06-02 19:28:19 +00:00
|
|
|
while (!feof(fd)) {
|
|
|
|
int cmd = fgetc(fd);
|
2014-04-22 21:45:47 +00:00
|
|
|
if (cmd == EOF)
|
2009-06-02 19:28:19 +00:00
|
|
|
break;
|
|
|
|
char* entry = malloc(1024);
|
2010-03-03 21:10:51 +00:00
|
|
|
if (!fgets(entry, 1024, fd)) {
|
|
|
|
free(entry);
|
|
|
|
break;
|
|
|
|
}
|
2009-06-02 19:28:19 +00:00
|
|
|
char* newline = strrchr(entry, '\n');
|
|
|
|
*newline = '\0';
|
|
|
|
if (cmd == 'f') {
|
2014-04-22 21:45:47 +00:00
|
|
|
OpenFiles_FileData* nextFile = calloc(1, sizeof(OpenFiles_FileData));
|
2012-11-10 00:31:37 +00:00
|
|
|
if (fdata == NULL) {
|
|
|
|
pdata->files = nextFile;
|
2009-06-02 19:28:19 +00:00
|
|
|
} else {
|
2012-11-10 00:31:37 +00:00
|
|
|
fdata->next = nextFile;
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
fdata = nextFile;
|
2014-04-22 21:45:47 +00:00
|
|
|
item = &(fdata->data);
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
2014-04-24 15:16:51 +00:00
|
|
|
assert(cmd >= 0 && cmd <= 0xff);
|
2009-06-02 19:28:19 +00:00
|
|
|
item->data[cmd] = entry;
|
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
pdata->error = pclose(fd);
|
|
|
|
return pdata;
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 00:31:37 +00:00
|
|
|
static inline void addLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) {
|
|
|
|
Vector_add(lines, (Object*) ListItem_new(line, 0));
|
|
|
|
if (!incFilter || String_contains_i(line, incFilter))
|
|
|
|
Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1));
|
|
|
|
}
|
|
|
|
|
2014-04-22 21:45:47 +00:00
|
|
|
static inline void OpenFiles_Data_clear(OpenFiles_Data* data) {
|
|
|
|
for (int i = 0; i < 255; i++)
|
|
|
|
if (data->data[i])
|
|
|
|
free(data->data[i]);
|
|
|
|
}
|
|
|
|
|
2012-11-10 00:31:37 +00:00
|
|
|
static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* inc) {
|
2009-06-02 19:28:19 +00:00
|
|
|
Panel* panel = this->display;
|
2012-11-10 00:31:37 +00:00
|
|
|
int idx = Panel_getSelectedIndex(panel);
|
2009-06-02 19:28:19 +00:00
|
|
|
Panel_prune(panel);
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(this->pid);
|
|
|
|
if (pdata->error == 127) {
|
|
|
|
addLine("Could not execute 'lsof'. Please make sure it is available in your $PATH.", lines, panel, IncSet_filter(inc));
|
|
|
|
} else if (pdata->error == 1) {
|
|
|
|
addLine("Failed listing open files.", lines, panel, IncSet_filter(inc));
|
2009-06-02 19:28:19 +00:00
|
|
|
} else {
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFiles_FileData* fdata = pdata->files;
|
|
|
|
while (fdata) {
|
2009-06-02 19:28:19 +00:00
|
|
|
char entry[1024];
|
2014-04-22 21:45:47 +00:00
|
|
|
char** data = fdata->data.data;
|
2009-06-02 19:28:19 +00:00
|
|
|
sprintf(entry, "%5s %4s %10s %10s %10s %s",
|
2014-04-22 21:45:47 +00:00
|
|
|
data['f'] ? data['f'] : "",
|
|
|
|
data['t'] ? data['t'] : "",
|
|
|
|
data['D'] ? data['D'] : "",
|
|
|
|
data['s'] ? data['s'] : "",
|
|
|
|
data['i'] ? data['i'] : "",
|
|
|
|
data['n'] ? data['n'] : "");
|
2012-11-10 00:31:37 +00:00
|
|
|
addLine(entry, lines, panel, IncSet_filter(inc));
|
2014-04-22 21:45:47 +00:00
|
|
|
OpenFiles_Data_clear(&fdata->data);
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFiles_FileData* old = fdata;
|
|
|
|
fdata = fdata->next;
|
2009-06-02 19:28:19 +00:00
|
|
|
free(old);
|
|
|
|
}
|
2014-04-22 21:45:47 +00:00
|
|
|
OpenFiles_Data_clear(&pdata->data);
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
free(pdata);
|
|
|
|
Vector_insertionSort(lines);
|
2011-11-18 06:08:56 +00:00
|
|
|
Vector_insertionSort(panel->items);
|
2010-02-25 01:43:18 +00:00
|
|
|
Panel_setSelected(panel, idx);
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFilesScreen_run(OpenFilesScreen* this) {
|
|
|
|
Panel* panel = this->display;
|
|
|
|
Panel_setHeader(panel, " FD TYPE DEVICE SIZE NODE NAME");
|
2012-11-10 00:31:37 +00:00
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
FunctionBar* bar = panel->defaultBar;
|
2012-11-10 00:31:37 +00:00
|
|
|
IncSet* inc = IncSet_new(bar);
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE);
|
2012-11-10 00:31:37 +00:00
|
|
|
|
|
|
|
OpenFilesScreen_scan(this, lines, inc);
|
|
|
|
OpenFilesScreen_draw(this, inc);
|
2009-06-02 19:28:19 +00:00
|
|
|
|
|
|
|
bool looping = true;
|
|
|
|
while (looping) {
|
2012-11-10 00:31:37 +00:00
|
|
|
|
2009-06-02 19:28:19 +00:00
|
|
|
Panel_draw(panel, true);
|
2012-11-10 00:31:37 +00:00
|
|
|
|
|
|
|
if (inc->active)
|
|
|
|
move(LINES-1, CRT_cursorX);
|
2009-06-02 19:28:19 +00:00
|
|
|
int ch = getch();
|
2012-11-10 00:31:37 +00:00
|
|
|
|
2009-06-02 19:28:19 +00:00
|
|
|
if (ch == KEY_MOUSE) {
|
|
|
|
MEVENT mevent;
|
|
|
|
int ok = getmouse(&mevent);
|
|
|
|
if (ok == OK)
|
|
|
|
if (mevent.y >= panel->y && mevent.y < LINES - 1) {
|
|
|
|
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV);
|
|
|
|
ch = 0;
|
|
|
|
} if (mevent.y == LINES - 1)
|
2015-03-23 20:04:53 +00:00
|
|
|
ch = IncSet_synthesizeEvent(inc, mevent.x);
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
|
|
|
|
if (inc->active) {
|
|
|
|
IncSet_handleKey(inc, ch, panel, IncSet_getListItemValue, lines);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-06-02 19:28:19 +00:00
|
|
|
switch(ch) {
|
|
|
|
case ERR:
|
|
|
|
continue;
|
2012-11-10 00:31:37 +00:00
|
|
|
case KEY_F(3):
|
|
|
|
case '/':
|
2015-03-23 20:04:53 +00:00
|
|
|
IncSet_activate(inc, INC_SEARCH, panel);
|
2012-11-10 00:31:37 +00:00
|
|
|
break;
|
|
|
|
case KEY_F(4):
|
|
|
|
case '\\':
|
2015-03-23 20:04:53 +00:00
|
|
|
IncSet_activate(inc, INC_FILTER, panel);
|
2012-11-10 00:31:37 +00:00
|
|
|
break;
|
2009-06-02 19:28:19 +00:00
|
|
|
case KEY_F(5):
|
|
|
|
clear();
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFilesScreen_scan(this, lines, inc);
|
|
|
|
OpenFilesScreen_draw(this, inc);
|
2009-06-02 19:28:19 +00:00
|
|
|
break;
|
|
|
|
case '\014': // Ctrl+L
|
|
|
|
clear();
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFilesScreen_draw(this, inc);
|
2009-06-02 19:28:19 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
case 27:
|
2009-06-08 21:08:02 +00:00
|
|
|
case KEY_F(10):
|
2009-06-02 19:28:19 +00:00
|
|
|
looping = false;
|
|
|
|
break;
|
|
|
|
case KEY_RESIZE:
|
|
|
|
Panel_resize(panel, COLS, LINES-2);
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFilesScreen_draw(this, inc);
|
2009-06-02 19:28:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Panel_onKey(panel, ch);
|
|
|
|
}
|
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
Vector_delete(lines);
|
2012-11-10 00:31:37 +00:00
|
|
|
IncSet_delete(inc);
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|