OpenFilesScreen.c 4.93 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
/*
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.
*/

Hisham Muhammad's avatar
Hisham Muhammad committed
8
9
10
11
#include "OpenFilesScreen.h"

#include "CRT.h"
#include "ProcessList.h"
12
#include "IncSet.h"
David Hunt's avatar
David Hunt committed
13
#include "StringUtils.h"
14
#include "FunctionBar.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16

#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
17
18
19
20
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <unistd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
21
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24
25
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>

Hisham Muhammad's avatar
Hisham Muhammad committed
26
/*{
27
#include "InfoScreen.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
28

Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
31
32
typedef struct OpenFiles_Data_ {
   char* data[256];
} OpenFiles_Data;

Hisham Muhammad's avatar
Hisham Muhammad committed
33
typedef struct OpenFiles_ProcessData_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
34
   OpenFiles_Data data;
35
   int error;
Hisham Muhammad's avatar
Hisham Muhammad committed
36
   struct OpenFiles_FileData_* files;
Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
39
} OpenFiles_ProcessData;

typedef struct OpenFiles_FileData_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
40
   OpenFiles_Data data;
Hisham Muhammad's avatar
Hisham Muhammad committed
41
42
43
44
   struct OpenFiles_FileData_* next;
} OpenFiles_FileData;

typedef struct OpenFilesScreen_ {
45
   InfoScreen super;
46
   pid_t pid;
Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
49
50
} OpenFilesScreen;

}*/

51
52
53
54
55
56
57
58
InfoScreenClass OpenFilesScreen_class = {
   .super = {
      .extends = Class(Object),
      .delete = OpenFilesScreen_delete
   },
   .scan = OpenFilesScreen_scan,
   .draw = OpenFilesScreen_draw
};
Hisham Muhammad's avatar
Hisham Muhammad committed
59
60

OpenFilesScreen* OpenFilesScreen_new(Process* process) {
Hisham's avatar
Hisham committed
61
   OpenFilesScreen* this = xMalloc(sizeof(OpenFilesScreen));
62
   Object_setClass(this, Class(OpenFilesScreen));
63
64
65
66
   if (Process_isThread(process))
      this->pid = process->tgid;
   else
      this->pid = process->pid;
67
   return (OpenFilesScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, "   FD TYPE     DEVICE       SIZE       NODE NAME");
Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
}

70
71
void OpenFilesScreen_delete(Object* this) {
   free(InfoScreen_done((InfoScreen*)this));
Hisham Muhammad's avatar
Hisham Muhammad committed
72
73
}

74
75
void OpenFilesScreen_draw(InfoScreen* this) {
   InfoScreen_drawTitled(this, "Snapshot of files open in process %d - %s", ((OpenFilesScreen*)this)->pid, this->process->comm);
Hisham Muhammad's avatar
Hisham Muhammad committed
76
77
}

78
static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
79
80
   char buffer[1025];
   xSnprintf(buffer, 1024, "%d", pid);
Hisham's avatar
Hisham committed
81
   OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData));
82
   OpenFiles_FileData* fdata = NULL;
Hisham Muhammad's avatar
Hisham Muhammad committed
83
   OpenFiles_Data* item = &(pdata->data);
84
85
86
   int fdpair[2];
   if (pipe(fdpair) == -1) {
      pdata->error = 1;
87
      return pdata;
88
   }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
   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");
108
109
110
   for (;;) {
      char* line = String_readLine(fd);
      if (!line) {
Hisham Muhammad's avatar
Hisham Muhammad committed
111
112
         break;
      }
113
      unsigned char cmd = line[0];
Hisham Muhammad's avatar
Hisham Muhammad committed
114
      if (cmd == 'f') {
Hisham's avatar
Hisham committed
115
         OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData));
116
117
         if (fdata == NULL) {
            pdata->files = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
118
         } else {
119
            fdata->next = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
120
         }
121
         fdata = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
122
         item = &(fdata->data);
Hisham Muhammad's avatar
Hisham Muhammad committed
123
      }
124
125
      item->data[cmd] = xStrdup(line + 1);
      free(line);
Hisham Muhammad's avatar
Hisham Muhammad committed
126
   }
127
128
129
130
131
132
133
134
135
   int wstatus;
   if (waitpid(child, &wstatus, 0) == -1) {
      pdata->error = 1;
      return pdata;
   }
   if (!WIFEXITED(wstatus))
      pdata->error = 1;
   else
      pdata->error = WEXITSTATUS(wstatus);
136
   return pdata;
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
}

Hisham Muhammad's avatar
Hisham Muhammad committed
139
140
141
142
143
144
static inline void OpenFiles_Data_clear(OpenFiles_Data* data) {
   for (int i = 0; i < 255; i++)
      if (data->data[i])
         free(data->data[i]);
}

145
void OpenFilesScreen_scan(InfoScreen* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
146
   Panel* panel = this->display;
147
   int idx = Panel_getSelectedIndex(panel);
Hisham Muhammad's avatar
Hisham Muhammad committed
148
   Panel_prune(panel);
149
   OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(((OpenFilesScreen*)this)->pid);
150
   if (pdata->error == 127) {
151
      InfoScreen_addLine(this, "Could not execute 'lsof'. Please make sure it is available in your $PATH.");
152
   } else if (pdata->error == 1) {
153
      InfoScreen_addLine(this, "Failed listing open files.");
Hisham Muhammad's avatar
Hisham Muhammad committed
154
   } else {
155
156
      OpenFiles_FileData* fdata = pdata->files;
      while (fdata) {
Hisham Muhammad's avatar
Hisham Muhammad committed
157
         char** data = fdata->data.data;
158
159
         int lenN = data['n'] ? strlen(data['n']) : 0;
         int sizeEntry = 5 + 7 + 10 + 10 + 10 + lenN + 5 /*spaces*/ + 1 /*null*/;
160
         char entry[sizeEntry];
161
         xSnprintf(entry, sizeEntry, "%5.5s %7.7s %10.10s %10.10s %10.10s %s",
Hisham Muhammad's avatar
Hisham Muhammad committed
162
163
164
165
166
167
            data['f'] ? data['f'] : "",
            data['t'] ? data['t'] : "",
            data['D'] ? data['D'] : "",
            data['s'] ? data['s'] : "",
            data['i'] ? data['i'] : "",
            data['n'] ? data['n'] : "");
168
         InfoScreen_addLine(this, entry);
Hisham Muhammad's avatar
Hisham Muhammad committed
169
         OpenFiles_Data_clear(&fdata->data);
170
171
         OpenFiles_FileData* old = fdata;
         fdata = fdata->next;
Hisham Muhammad's avatar
Hisham Muhammad committed
172
173
         free(old);
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
174
      OpenFiles_Data_clear(&pdata->data);
Hisham Muhammad's avatar
Hisham Muhammad committed
175
   }
176
   free(pdata);
177
   Vector_insertionSort(this->lines);
178
   Vector_insertionSort(panel->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
179
   Panel_setSelected(panel, idx);
Hisham Muhammad's avatar
Hisham Muhammad committed
180
}