OpenFilesScreen.c 5.85 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
12
13
14
#include "OpenFilesScreen.h"

#include "CRT.h"
#include "ProcessList.h"
#include "ListItem.h"

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

Hisham Muhammad's avatar
Hisham Muhammad committed
24
/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
25
26
27
28
29
30
31
#include "Process.h"
#include "Panel.h"
#include "FunctionBar.h"

typedef struct OpenFiles_ProcessData_ {
   char* data[256];
   struct OpenFiles_FileData_* files;
32
   int error;
Hisham Muhammad's avatar
Hisham Muhammad committed
33
34
35
36
37
38
39
40
41
} OpenFiles_ProcessData;

typedef struct OpenFiles_FileData_ {
   char* data[256];
   struct OpenFiles_FileData_* next;
} OpenFiles_FileData;

typedef struct OpenFilesScreen_ {
   Process* process;
42
   pid_t pid;
Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
45
46
47
48
49
   Panel* display;
   FunctionBar* bar;
   bool tracing;
} OpenFilesScreen;

}*/

50
static const char* ofsFunctions[] = {"Refresh", "Done   ", NULL};
Hisham Muhammad's avatar
Hisham Muhammad committed
51

52
static const char* ofsKeys[] = {"F5", "Esc"};
Hisham Muhammad's avatar
Hisham Muhammad committed
53

54
static int ofsEvents[] = {KEY_F(5), 27};
Hisham Muhammad's avatar
Hisham Muhammad committed
55
56
57
58
59

OpenFilesScreen* OpenFilesScreen_new(Process* process) {
   OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen));
   this->process = process;
   this->display = Panel_new(0, 1, COLS, LINES-3, LISTITEM_CLASS, true, ListItem_compare);
60
   this->bar = FunctionBar_new(ofsFunctions, ofsKeys, ofsEvents);
Hisham Muhammad's avatar
Hisham Muhammad committed
61
   this->tracing = true;
62
63
64
65
   if (Process_isThread(process))
      this->pid = process->tgid;
   else
      this->pid = process->pid;
Hisham Muhammad's avatar
Hisham Muhammad committed
66
67
68
69
70
71
72
73
74
75
76
77
   return this;
}

void OpenFilesScreen_delete(OpenFilesScreen* this) {
   Panel_delete((Object*)this->display);
   FunctionBar_delete((Object*)this->bar);
   free(this);
}

static void OpenFilesScreen_draw(OpenFilesScreen* this) {
   attrset(CRT_colors[METER_TEXT]);
   mvhline(0, 0, ' ', COLS);
78
   mvprintw(0, 0, "Files open in process %d - %s", this->pid, this->process->comm);
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
81
82
83
   attrset(CRT_colors[DEFAULT_COLOR]);
   Panel_draw(this->display, true);
   FunctionBar_draw(this->bar, NULL);
}

84
static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
Hisham Muhammad's avatar
Hisham Muhammad committed
85
   char command[1025];
Hisham Muhammad's avatar
Hisham Muhammad committed
86
   snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid);
Hisham Muhammad's avatar
Hisham Muhammad committed
87
88
89
90
91
   FILE* fd = popen(command, "r");
   OpenFiles_ProcessData* process = calloc(sizeof(OpenFiles_ProcessData), 1);
   OpenFiles_FileData* file = NULL;
   OpenFiles_ProcessData* item = process;
   bool anyRead = false;
92
93
94
95
   if (!fd) {
      process->error = 127;
      return process;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
96
97
   while (!feof(fd)) {
      int cmd = fgetc(fd);
98
      if (cmd == EOF && !anyRead)
Hisham Muhammad's avatar
Hisham Muhammad committed
99
100
101
         break;
      anyRead = true;
      char* entry = malloc(1024);
Hisham Muhammad's avatar
Hisham Muhammad committed
102
103
104
105
      if (!fgets(entry, 1024, fd)) {
         free(entry);
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
      char* newline = strrchr(entry, '\n');
      *newline = '\0';
      if (cmd == 'f') {
         OpenFiles_FileData* nextFile = calloc(sizeof(OpenFiles_ProcessData), 1);
         if (file == NULL) {
            process->files = nextFile;
         } else {
            file->next = nextFile;
         }
         file = nextFile;
         item = (OpenFiles_ProcessData*) file;
      }
      item->data[cmd] = entry;
   }
120
   process->error = pclose(fd);
Hisham Muhammad's avatar
Hisham Muhammad committed
121
122
123
124
125
   return process;
}

static void OpenFilesScreen_scan(OpenFilesScreen* this) {
   Panel* panel = this->display;
Hisham Muhammad's avatar
Hisham Muhammad committed
126
   int idx = MAX(Panel_getSelectedIndex(panel), 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
127
   Panel_prune(panel);
128
   OpenFiles_ProcessData* process = OpenFilesScreen_getProcessData(this->pid);
129
   if (process->error == 127) {
Hisham Muhammad's avatar
Hisham Muhammad committed
130
      Panel_add(panel, (Object*) ListItem_new("Could not execute 'lsof'. Please make sure it is available in your $PATH.", 0));
131
132
   } else if (process->error == 1) {
      Panel_add(panel, (Object*) ListItem_new("Failed listing open files.", 0));
Hisham Muhammad's avatar
Hisham Muhammad committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
   } else {
      OpenFiles_FileData* file = process->files;
      while (file) {
         char entry[1024];
         sprintf(entry, "%5s %4s %10s %10s %10s %s",
            file->data['f'] ? file->data['f'] : "",
            file->data['t'] ? file->data['t'] : "",
            file->data['D'] ? file->data['D'] : "",
            file->data['s'] ? file->data['s'] : "",
            file->data['i'] ? file->data['i'] : "",
            file->data['n'] ? file->data['n'] : "");
         Panel_add(panel, (Object*) ListItem_new(entry, 0));
         for (int i = 0; i < 255; i++)
            if (file->data[i])
               free(file->data[i]);
         OpenFiles_FileData* old = file;
         file = file->next;
         free(old);
      }
      for (int i = 0; i < 255; i++)
         if (process->data[i])
            free(process->data[i]);
   }
   free(process);
157
   Vector_insertionSort(panel->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
158
   Panel_setSelected(panel, idx);
Hisham Muhammad's avatar
Hisham Muhammad committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
}

void OpenFilesScreen_run(OpenFilesScreen* this) {
   Panel* panel = this->display;
   Panel_setHeader(panel, "   FD TYPE     DEVICE       SIZE       NODE NAME");
   OpenFilesScreen_scan(this);
   OpenFilesScreen_draw(this);
   //CRT_disableDelay();
   
   bool looping = true;
   while (looping) {
      Panel_draw(panel, true);
      int ch = getch();
      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)
               ch = FunctionBar_synthesizeEvent(this->bar, mevent.x);
      }
      switch(ch) {
      case ERR:
         continue;
      case KEY_F(5):
         clear();
         OpenFilesScreen_scan(this);
         OpenFilesScreen_draw(this);
         break;
      case '\014': // Ctrl+L
         clear();
         OpenFilesScreen_draw(this);
         break;
      case 'q':
      case 27:
196
      case KEY_F(10):
Hisham Muhammad's avatar
Hisham Muhammad committed
197
198
199
200
201
202
203
204
205
206
207
208
         looping = false;
         break;
      case KEY_RESIZE:
         Panel_resize(panel, COLS, LINES-2);
         OpenFilesScreen_draw(this);
         break;
      default:
         Panel_onKey(panel, ch);
      }
   }
   //CRT_enableDelay();
}