htop/OpenFilesScreen.c

157 lines
4.3 KiB
C
Raw Permalink Normal View History

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 "IncSet.h"
#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
/*{
#include "InfoScreen.h"
2009-06-02 19:28:19 +00:00
typedef struct OpenFiles_Data_ {
char* data[256];
} OpenFiles_Data;
2009-06-02 19:28:19 +00:00
typedef struct OpenFiles_ProcessData_ {
OpenFiles_Data data;
2009-10-16 20:15:01 +00:00
int error;
struct OpenFiles_FileData_* files;
2009-06-02 19:28:19 +00:00
} OpenFiles_ProcessData;
typedef struct OpenFiles_FileData_ {
OpenFiles_Data data;
2009-06-02 19:28:19 +00:00
struct OpenFiles_FileData_* next;
} OpenFiles_FileData;
typedef struct OpenFilesScreen_ {
InfoScreen super;
pid_t pid;
2009-06-02 19:28:19 +00:00
} OpenFilesScreen;
}*/
InfoScreenClass OpenFilesScreen_class = {
.super = {
.extends = Class(Object),
.delete = OpenFilesScreen_delete
},
.scan = OpenFilesScreen_scan,
.draw = OpenFilesScreen_draw
};
2009-06-02 19:28:19 +00:00
OpenFilesScreen* OpenFilesScreen_new(Process* process) {
2016-02-02 14:53:02 +00:00
OpenFilesScreen* this = xMalloc(sizeof(OpenFilesScreen));
Object_setClass(this, Class(OpenFilesScreen));
if (Process_isThread(process))
this->pid = process->tgid;
else
this->pid = process->pid;
return (OpenFilesScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, " FD TYPE DEVICE SIZE NODE NAME");
2009-06-02 19:28:19 +00:00
}
void OpenFilesScreen_delete(Object* this) {
free(InfoScreen_done((InfoScreen*)this));
2009-06-02 19:28:19 +00:00
}
void OpenFilesScreen_draw(InfoScreen* this) {
InfoScreen_drawTitled(this, "Snapshot of files open in process %d - %s", ((OpenFilesScreen*)this)->pid, this->process->comm);
2009-06-02 19:28:19 +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");
2016-02-02 14:53:02 +00:00
OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData));
OpenFiles_FileData* fdata = NULL;
OpenFiles_Data* item = &(pdata->data);
2009-10-16 20:15:01 +00:00
if (!fd) {
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);
if (cmd == EOF)
2009-06-02 19:28:19 +00:00
break;
2016-02-02 14:53:02 +00:00
char* entry = xMalloc(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') {
2016-02-02 14:53:02 +00:00
OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData));
if (fdata == NULL) {
pdata->files = nextFile;
2009-06-02 19:28:19 +00:00
} else {
fdata->next = nextFile;
2009-06-02 19:28:19 +00:00
}
fdata = nextFile;
item = &(fdata->data);
2009-06-02 19:28:19 +00:00
}
assert(cmd >= 0 && cmd <= 0xff);
2009-06-02 19:28:19 +00:00
item->data[cmd] = entry;
}
pdata->error = pclose(fd);
return pdata;
2009-06-02 19:28:19 +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]);
}
void OpenFilesScreen_scan(InfoScreen* this) {
2009-06-02 19:28:19 +00:00
Panel* panel = this->display;
int idx = Panel_getSelectedIndex(panel);
2009-06-02 19:28:19 +00:00
Panel_prune(panel);
OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(((OpenFilesScreen*)this)->pid);
if (pdata->error == 127) {
InfoScreen_addLine(this, "Could not execute 'lsof'. Please make sure it is available in your $PATH.");
} else if (pdata->error == 1) {
InfoScreen_addLine(this, "Failed listing open files.");
2009-06-02 19:28:19 +00:00
} else {
OpenFiles_FileData* fdata = pdata->files;
while (fdata) {
2009-06-02 19:28:19 +00:00
char entry[1024];
char** data = fdata->data.data;
2009-06-02 19:28:19 +00:00
sprintf(entry, "%5s %4s %10s %10s %10s %s",
data['f'] ? data['f'] : "",
data['t'] ? data['t'] : "",
data['D'] ? data['D'] : "",
data['s'] ? data['s'] : "",
data['i'] ? data['i'] : "",
data['n'] ? data['n'] : "");
InfoScreen_addLine(this, entry);
OpenFiles_Data_clear(&fdata->data);
OpenFiles_FileData* old = fdata;
fdata = fdata->next;
2009-06-02 19:28:19 +00:00
free(old);
}
OpenFiles_Data_clear(&pdata->data);
2009-06-02 19:28:19 +00:00
}
free(pdata);
Vector_insertionSort(this->lines);
Vector_insertionSort(panel->items);
2010-02-25 01:43:18 +00:00
Panel_setSelected(panel, idx);
2009-06-02 19:28:19 +00:00
}