aboutsummaryrefslogtreecommitdiffstats
path: root/OpenFilesScreen.c
diff options
context:
space:
mode:
Diffstat (limited to 'OpenFilesScreen.c')
-rw-r--r--OpenFilesScreen.c41
1 files changed, 34 insertions, 7 deletions
diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c
index a772bba..9ea333b 100644
--- a/OpenFilesScreen.c
+++ b/OpenFilesScreen.c
@@ -76,16 +76,35 @@ void OpenFilesScreen_draw(InfoScreen* this) {
}
static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
- char command[1025];
- xSnprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid);
- FILE* fd = popen(command, "r");
+ char buffer[1025];
+ xSnprintf(buffer, 1024, "%d", pid);
OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData));
OpenFiles_FileData* fdata = NULL;
OpenFiles_Data* item = &(pdata->data);
- if (!fd) {
- pdata->error = 127;
+ int fdpair[2];
+ if (pipe(fdpair) == -1) {
+ pdata->error = 1;
return pdata;
}
+ pid_t child = fork();
+ if (child == -1) {
+ pdata->error = 1;
+ return pdata;
+ }
+ if (child == 0) {
+ close(fdpair[0]);
+ dup2(fdpair[1], STDOUT_FILENO);
+ close(fdpair[1]);
+ int fdnull = open("/dev/null", O_WRONLY);
+ if (fdnull < 0)
+ exit(1);
+ dup2(fdnull, STDERR_FILENO);
+ close(fdnull);
+ execlp("lsof", "lsof", "-P", "-p", buffer, "-F", NULL);
+ exit(127);
+ }
+ close(fdpair[1]);
+ FILE* fd = fdopen(fdpair[0], "r");
for (;;) {
char* line = String_readLine(fd);
if (!line) {
@@ -105,7 +124,15 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
item->data[cmd] = xStrdup(line + 1);
free(line);
}
- pdata->error = pclose(fd);
+ int wstatus;
+ if (waitpid(child, &wstatus, 0) == -1) {
+ pdata->error = 1;
+ return pdata;
+ }
+ if (!WIFEXITED(wstatus))
+ pdata->error = 1;
+ else
+ pdata->error = WEXITSTATUS(wstatus);
return pdata;
}
@@ -130,7 +157,7 @@ void OpenFilesScreen_scan(InfoScreen* this) {
char** data = fdata->data.data;
int lenN = data['n'] ? strlen(data['n']) : 0;
int sizeEntry = 5 + 7 + 10 + 10 + 10 + lenN + 5 /*spaces*/ + 1 /*null*/;
- char* entry = xMalloc(sizeEntry);
+ char entry[sizeEntry];
xSnprintf(entry, sizeEntry, "%5.5s %7.7s %10.10s %10.10s %10.10s %s",
data['f'] ? data['f'] : "",
data['t'] ? data['t'] : "",

© 2014-2024 Faster IT GmbH | imprint | privacy policy