2009-06-02 19:28:19 +00:00
|
|
|
/*
|
|
|
|
htop - OpenFilesScreen.c
|
|
|
|
(C) 2005-2006 Hisham H. Muhammad
|
2021-09-22 09:33:00 +00:00
|
|
|
Released under the GNU GPLv2+, see the COPYING file
|
2009-06-02 19:28:19 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "OpenFilesScreen.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2022-01-11 21:25:29 +00:00
|
|
|
#include <errno.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <fcntl.h>
|
2021-09-03 04:20:15 +00:00
|
|
|
#include <inttypes.h>
|
2009-06-02 19:28:19 +00:00
|
|
|
#include <stdio.h>
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <stdlib.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-06-02 19:28:19 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2021-09-03 04:20:15 +00:00
|
|
|
#include <sys/stat.h>
|
2009-06-02 19:28:19 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "Macros.h"
|
|
|
|
#include "Panel.h"
|
|
|
|
#include "ProvideCurses.h"
|
|
|
|
#include "Vector.h"
|
|
|
|
#include "XUtils.h"
|
|
|
|
|
2009-06-02 19:28:19 +00:00
|
|
|
|
2020-10-07 17:02:23 +00:00
|
|
|
typedef struct OpenFiles_Data_ {
|
2021-09-03 04:16:43 +00:00
|
|
|
char* data[8];
|
2020-10-07 17:02:23 +00:00
|
|
|
} OpenFiles_Data;
|
|
|
|
|
|
|
|
typedef struct OpenFiles_ProcessData_ {
|
|
|
|
OpenFiles_Data data;
|
|
|
|
int error;
|
|
|
|
struct OpenFiles_FileData_* files;
|
|
|
|
} OpenFiles_ProcessData;
|
|
|
|
|
|
|
|
typedef struct OpenFiles_FileData_ {
|
|
|
|
OpenFiles_Data data;
|
|
|
|
struct OpenFiles_FileData_* next;
|
|
|
|
} OpenFiles_FileData;
|
|
|
|
|
|
|
|
static size_t getIndexForType(char type) {
|
|
|
|
switch (type) {
|
|
|
|
case 'f':
|
|
|
|
return 0;
|
|
|
|
case 'a':
|
|
|
|
return 1;
|
|
|
|
case 'D':
|
|
|
|
return 2;
|
|
|
|
case 'i':
|
|
|
|
return 3;
|
|
|
|
case 'n':
|
|
|
|
return 4;
|
|
|
|
case 's':
|
|
|
|
return 5;
|
|
|
|
case 't':
|
|
|
|
return 6;
|
2021-09-03 04:16:43 +00:00
|
|
|
case 'o':
|
|
|
|
return 7;
|
2020-10-07 17:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* should never reach here */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* getDataForType(const OpenFiles_Data* data, char type) {
|
|
|
|
size_t index = getIndexForType(type);
|
|
|
|
return data->data[index] ? data->data[index] : "";
|
|
|
|
}
|
2009-06-02 19:28:19 +00:00
|
|
|
|
2020-10-07 17:02:23 +00:00
|
|
|
OpenFilesScreen* OpenFilesScreen_new(const Process* process) {
|
2016-02-02 14:53:02 +00:00
|
|
|
OpenFilesScreen* this = xMalloc(sizeof(OpenFilesScreen));
|
2016-01-12 08:00:58 +00:00
|
|
|
Object_setClass(this, Class(OpenFilesScreen));
|
2020-11-01 00:09:51 +00:00
|
|
|
if (Process_isThread(process)) {
|
2011-03-22 22:09:42 +00:00
|
|
|
this->pid = process->tgid;
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2011-03-22 22:09:42 +00:00
|
|
|
this->pid = process->pid;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2021-09-03 04:16:43 +00:00
|
|
|
return (OpenFilesScreen*) InfoScreen_init(&this->super, process, NULL, LINES - 2, " FD TYPE MODE DEVICE SIZE OFFSET NODE NAME");
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
void OpenFilesScreen_delete(Object* this) {
|
|
|
|
free(InfoScreen_done((InfoScreen*)this));
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:02:23 +00:00
|
|
|
static void OpenFilesScreen_draw(InfoScreen* this) {
|
2020-10-17 10:54:45 +00:00
|
|
|
InfoScreen_drawTitled(this, "Snapshot of files open in process %d - %s", ((OpenFilesScreen*)this)->pid, Process_getCommand(this->process));
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 22:09:42 +00:00
|
|
|
static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
|
2016-02-02 14:53:02 +00:00
|
|
|
OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData));
|
2020-09-15 20:07:52 +00:00
|
|
|
|
|
|
|
int fdpair[2] = {0, 0};
|
2018-03-16 14:39:03 +00:00
|
|
|
if (pipe(fdpair) == -1) {
|
|
|
|
pdata->error = 1;
|
2012-11-10 00:31:37 +00:00
|
|
|
return pdata;
|
2009-10-16 20:15:01 +00:00
|
|
|
}
|
2020-09-15 20:07:52 +00:00
|
|
|
|
2018-03-16 14:39:03 +00:00
|
|
|
pid_t child = fork();
|
|
|
|
if (child == -1) {
|
2020-09-15 20:07:52 +00:00
|
|
|
close(fdpair[1]);
|
|
|
|
close(fdpair[0]);
|
2018-03-16 14:39:03 +00:00
|
|
|
pdata->error = 1;
|
|
|
|
return pdata;
|
|
|
|
}
|
2020-09-15 20:07:52 +00:00
|
|
|
|
2018-03-16 14:39:03 +00:00
|
|
|
if (child == 0) {
|
|
|
|
close(fdpair[0]);
|
|
|
|
dup2(fdpair[1], STDOUT_FILENO);
|
|
|
|
close(fdpair[1]);
|
|
|
|
int fdnull = open("/dev/null", O_WRONLY);
|
2020-11-01 00:09:51 +00:00
|
|
|
if (fdnull < 0) {
|
2018-03-16 14:39:03 +00:00
|
|
|
exit(1);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 14:39:03 +00:00
|
|
|
dup2(fdnull, STDERR_FILENO);
|
|
|
|
close(fdnull);
|
2020-09-15 20:02:57 +00:00
|
|
|
char buffer[32] = {0};
|
|
|
|
xSnprintf(buffer, sizeof(buffer), "%d", pid);
|
2022-05-17 11:15:37 +00:00
|
|
|
// Use of NULL in variadic functions must have a pointer cast.
|
|
|
|
// The NULL constant is not required by standard to have a pointer type.
|
|
|
|
execlp("lsof", "lsof", "-P", "-o", "-p", buffer, "-F", (char *)NULL);
|
2018-03-16 14:39:03 +00:00
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
close(fdpair[1]);
|
2020-09-15 20:07:52 +00:00
|
|
|
|
|
|
|
OpenFiles_Data* item = &(pdata->data);
|
|
|
|
OpenFiles_FileData* fdata = NULL;
|
2021-09-03 04:20:15 +00:00
|
|
|
bool lsofIncludesFileSize = false;
|
2020-09-15 20:07:52 +00:00
|
|
|
|
2018-03-16 14:39:03 +00:00
|
|
|
FILE* fd = fdopen(fdpair[0], "r");
|
2020-09-15 17:56:52 +00:00
|
|
|
if (!fd) {
|
|
|
|
pdata->error = 1;
|
|
|
|
return pdata;
|
|
|
|
}
|
2016-06-19 21:55:35 +00:00
|
|
|
for (;;) {
|
|
|
|
char* line = String_readLine(fd);
|
|
|
|
if (!line) {
|
2010-03-03 21:10:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-15 20:07:52 +00:00
|
|
|
|
2016-06-19 21:55:35 +00:00
|
|
|
unsigned char cmd = line[0];
|
2020-10-07 17:02:23 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case 'f': /* file descriptor */
|
|
|
|
{
|
2016-02-02 14:53:02 +00:00
|
|
|
OpenFiles_FileData* nextFile = xCalloc(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);
|
2020-10-07 17:02:23 +00:00
|
|
|
} /* FALLTHRU */
|
|
|
|
case 'a': /* file access mode */
|
|
|
|
case 'D': /* file's major/minor device number */
|
|
|
|
case 'i': /* file's inode number */
|
|
|
|
case 'n': /* file name, comment, Internet address */
|
|
|
|
case 's': /* file's size */
|
|
|
|
case 't': /* file's type */
|
2021-09-03 04:18:30 +00:00
|
|
|
{
|
|
|
|
size_t index = getIndexForType(cmd);
|
|
|
|
free_and_xStrdup(&item->data[index], line + 1);
|
|
|
|
break;
|
|
|
|
}
|
2021-09-03 04:16:43 +00:00
|
|
|
case 'o': /* file's offset */
|
2020-10-07 17:02:23 +00:00
|
|
|
{
|
|
|
|
size_t index = getIndexForType(cmd);
|
2021-09-03 04:18:30 +00:00
|
|
|
if (String_startsWith(line + 1, "0t")) {
|
|
|
|
free_and_xStrdup(&item->data[index], line + 3);
|
|
|
|
} else {
|
|
|
|
free_and_xStrdup(&item->data[index], line + 1);
|
|
|
|
}
|
2020-10-07 17:02:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'c': /* process command name */
|
|
|
|
case 'd': /* file's device character code */
|
|
|
|
case 'g': /* process group ID */
|
|
|
|
case 'G': /* file flags */
|
|
|
|
case 'k': /* link count */
|
|
|
|
case 'l': /* file's lock status */
|
|
|
|
case 'L': /* process login name */
|
|
|
|
case 'p': /* process ID */
|
|
|
|
case 'P': /* protocol name */
|
|
|
|
case 'R': /* parent process ID */
|
|
|
|
case 'T': /* TCP/TPI information, identified by prefixes */
|
|
|
|
case 'u': /* process user ID */
|
|
|
|
/* ignore */
|
|
|
|
break;
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
2021-09-03 04:20:15 +00:00
|
|
|
|
|
|
|
if (cmd == 's')
|
|
|
|
lsofIncludesFileSize = true;
|
|
|
|
|
2016-06-19 21:55:35 +00:00
|
|
|
free(line);
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
2019-06-23 17:13:05 +00:00
|
|
|
fclose(fd);
|
2020-09-15 20:07:52 +00:00
|
|
|
|
2018-03-16 14:39:03 +00:00
|
|
|
int wstatus;
|
2022-01-11 21:25:29 +00:00
|
|
|
while (waitpid(child, &wstatus, 0) == -1)
|
|
|
|
if (errno != EINTR) {
|
|
|
|
pdata->error = 1;
|
|
|
|
return pdata;
|
|
|
|
}
|
2020-09-15 20:07:52 +00:00
|
|
|
|
2020-11-01 00:09:51 +00:00
|
|
|
if (!WIFEXITED(wstatus)) {
|
2018-03-16 14:39:03 +00:00
|
|
|
pdata->error = 1;
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2018-03-16 14:39:03 +00:00
|
|
|
pdata->error = WEXITSTATUS(wstatus);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2020-09-15 20:07:52 +00:00
|
|
|
|
2021-09-03 04:20:15 +00:00
|
|
|
/* We got all information we need; no post-processing needed */
|
|
|
|
if (lsofIncludesFileSize)
|
|
|
|
return pdata;
|
|
|
|
|
|
|
|
/* On linux, `lsof -o -F` omits SIZE, so add it back. */
|
|
|
|
/* On macOS, `lsof -o -F` includes SIZE, so this block isn't needed. If no open files have a filesize, this will still run, unfortunately. */
|
|
|
|
size_t fileSizeIndex = getIndexForType('s');
|
|
|
|
for (fdata = pdata->files; fdata != NULL; fdata = fdata->next) {
|
|
|
|
item = &fdata->data;
|
|
|
|
const char* filename = getDataForType(item, 'n');
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (stat(filename, &st) == 0) {
|
|
|
|
char fileSizeBuf[21]; /* 20 (long long) + 1 (NULL) */
|
|
|
|
xSnprintf(fileSizeBuf, sizeof(fileSizeBuf), "%"PRIu64, st.st_size); /* st.st_size is long long on macOS, long on linux */
|
|
|
|
free_and_xStrdup(&item->data[fileSizeIndex], fileSizeBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-10 00:31:37 +00:00
|
|
|
return pdata;
|
2009-06-02 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:02:23 +00:00
|
|
|
static void OpenFiles_Data_clear(OpenFiles_Data* data) {
|
|
|
|
for (size_t i = 0; i < ARRAYSIZE(data->data); i++)
|
|
|
|
free(data->data[i]);
|
2014-04-22 21:45:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:02:23 +00:00
|
|
|
static void OpenFilesScreen_scan(InfoScreen* this) {
|
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);
|
2016-01-12 08:00:58 +00:00
|
|
|
OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(((OpenFilesScreen*)this)->pid);
|
2012-11-10 00:31:37 +00:00
|
|
|
if (pdata->error == 127) {
|
2016-01-12 08:00:58 +00:00
|
|
|
InfoScreen_addLine(this, "Could not execute 'lsof'. Please make sure it is available in your $PATH.");
|
2012-11-10 00:31:37 +00:00
|
|
|
} else if (pdata->error == 1) {
|
2016-01-12 08:00:58 +00:00
|
|
|
InfoScreen_addLine(this, "Failed listing open files.");
|
2009-06-02 19:28:19 +00:00
|
|
|
} else {
|
2012-11-10 00:31:37 +00:00
|
|
|
OpenFiles_FileData* fdata = pdata->files;
|
|
|
|
while (fdata) {
|
2020-10-07 17:02:23 +00:00
|
|
|
OpenFiles_Data* data = &fdata->data;
|
|
|
|
size_t lenN = strlen(getDataForType(data, 'n'));
|
2021-09-03 04:16:43 +00:00
|
|
|
size_t sizeEntry = 5 + 7 + 4 + 10 + 10 + 10 + 10 + lenN + 8 /*spaces*/ + 1 /*null*/;
|
2018-02-18 13:38:49 +00:00
|
|
|
char entry[sizeEntry];
|
2021-09-03 04:16:43 +00:00
|
|
|
xSnprintf(entry, sizeof(entry), "%5.5s %-7.7s %-4.4s %-10.10s %10.10s %10.10s %10.10s %s",
|
2020-10-07 17:02:23 +00:00
|
|
|
getDataForType(data, 'f'),
|
|
|
|
getDataForType(data, 't'),
|
|
|
|
getDataForType(data, 'a'),
|
|
|
|
getDataForType(data, 'D'),
|
|
|
|
getDataForType(data, 's'),
|
2021-09-03 04:16:43 +00:00
|
|
|
getDataForType(data, 'o'),
|
2020-10-07 17:02:23 +00:00
|
|
|
getDataForType(data, 'i'),
|
|
|
|
getDataForType(data, 'n'));
|
2016-01-12 08:00:58 +00:00
|
|
|
InfoScreen_addLine(this, entry);
|
2020-10-07 17:02:23 +00:00
|
|
|
OpenFiles_Data_clear(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);
|
2016-01-12 08:00:58 +00:00
|
|
|
Vector_insertionSort(this->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
|
|
|
}
|
2020-10-07 17:02:23 +00:00
|
|
|
|
|
|
|
const InfoScreenClass OpenFilesScreen_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Object),
|
|
|
|
.delete = OpenFilesScreen_delete
|
|
|
|
},
|
|
|
|
.scan = OpenFilesScreen_scan,
|
|
|
|
.draw = OpenFilesScreen_draw
|
|
|
|
};
|