OpenFilesScreen.c 4.29 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) {
Hisham Muhammad's avatar
Hisham Muhammad committed
79
   char command[1025];
Hisham Muhammad's avatar
Hisham Muhammad committed
80
   snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid);
Hisham Muhammad's avatar
Hisham Muhammad committed
81
   FILE* fd = popen(command, "r");
Hisham's avatar
Hisham committed
82
   OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData));
83
   OpenFiles_FileData* fdata = NULL;
Hisham Muhammad's avatar
Hisham Muhammad committed
84
   OpenFiles_Data* item = &(pdata->data);
85
   if (!fd) {
86
87
      pdata->error = 127;
      return pdata;
88
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
89
90
   while (!feof(fd)) {
      int cmd = fgetc(fd);
Hisham Muhammad's avatar
Hisham Muhammad committed
91
      if (cmd == EOF)
Hisham Muhammad's avatar
Hisham Muhammad committed
92
         break;
Hisham's avatar
Hisham committed
93
      char* entry = xMalloc(1024);
Hisham Muhammad's avatar
Hisham Muhammad committed
94
95
96
97
      if (!fgets(entry, 1024, fd)) {
         free(entry);
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
98
99
100
      char* newline = strrchr(entry, '\n');
      *newline = '\0';
      if (cmd == 'f') {
Hisham's avatar
Hisham committed
101
         OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData));
102
103
         if (fdata == NULL) {
            pdata->files = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
104
         } else {
105
            fdata->next = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
106
         }
107
         fdata = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
108
         item = &(fdata->data);
Hisham Muhammad's avatar
Hisham Muhammad committed
109
      }
110
      assert(cmd >= 0 && cmd <= 0xff);
Hisham Muhammad's avatar
Hisham Muhammad committed
111
112
      item->data[cmd] = entry;
   }
113
114
   pdata->error = pclose(fd);
   return pdata;
Hisham Muhammad's avatar
Hisham Muhammad committed
115
116
}

Hisham Muhammad's avatar
Hisham Muhammad committed
117
118
119
120
121
122
static inline void OpenFiles_Data_clear(OpenFiles_Data* data) {
   for (int i = 0; i < 255; i++)
      if (data->data[i])
         free(data->data[i]);
}

123
void OpenFilesScreen_scan(InfoScreen* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
124
   Panel* panel = this->display;
125
   int idx = Panel_getSelectedIndex(panel);
Hisham Muhammad's avatar
Hisham Muhammad committed
126
   Panel_prune(panel);
127
   OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(((OpenFilesScreen*)this)->pid);
128
   if (pdata->error == 127) {
129
      InfoScreen_addLine(this, "Could not execute 'lsof'. Please make sure it is available in your $PATH.");
130
   } else if (pdata->error == 1) {
131
      InfoScreen_addLine(this, "Failed listing open files.");
Hisham Muhammad's avatar
Hisham Muhammad committed
132
   } else {
133
134
      OpenFiles_FileData* fdata = pdata->files;
      while (fdata) {
Hisham Muhammad's avatar
Hisham Muhammad committed
135
         char entry[1024];
Hisham Muhammad's avatar
Hisham Muhammad committed
136
         char** data = fdata->data.data;
Hisham Muhammad's avatar
Hisham Muhammad committed
137
         sprintf(entry, "%5s %4s %10s %10s %10s %s",
Hisham Muhammad's avatar
Hisham Muhammad committed
138
139
140
141
142
143
            data['f'] ? data['f'] : "",
            data['t'] ? data['t'] : "",
            data['D'] ? data['D'] : "",
            data['s'] ? data['s'] : "",
            data['i'] ? data['i'] : "",
            data['n'] ? data['n'] : "");
144
         InfoScreen_addLine(this, entry);
Hisham Muhammad's avatar
Hisham Muhammad committed
145
         OpenFiles_Data_clear(&fdata->data);
146
147
         OpenFiles_FileData* old = fdata;
         fdata = fdata->next;
Hisham Muhammad's avatar
Hisham Muhammad committed
148
149
         free(old);
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
150
      OpenFiles_Data_clear(&pdata->data);
Hisham Muhammad's avatar
Hisham Muhammad committed
151
   }
152
   free(pdata);
153
   Vector_insertionSort(this->lines);
154
   Vector_insertionSort(panel->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
155
   Panel_setSelected(panel, idx);
Hisham Muhammad's avatar
Hisham Muhammad committed
156
}