OpenFilesScreen.c 4.36 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
   }
89
90
91
   for (;;) {
      char* line = String_readLine(fd);
      if (!line) {
Hisham Muhammad's avatar
Hisham Muhammad committed
92
93
         break;
      }
94
      unsigned char cmd = line[0];
Hisham Muhammad's avatar
Hisham Muhammad committed
95
      if (cmd == 'f') {
Hisham's avatar
Hisham committed
96
         OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData));
97
98
         if (fdata == NULL) {
            pdata->files = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
99
         } else {
100
            fdata->next = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
101
         }
102
         fdata = nextFile;
Hisham Muhammad's avatar
Hisham Muhammad committed
103
         item = &(fdata->data);
Hisham Muhammad's avatar
Hisham Muhammad committed
104
      }
105
      assert(cmd >= 0 && cmd <= 0xff);
106
107
      item->data[cmd] = xStrdup(line + 1);
      free(line);
Hisham Muhammad's avatar
Hisham Muhammad committed
108
   }
109
110
   pdata->error = pclose(fd);
   return pdata;
Hisham Muhammad's avatar
Hisham Muhammad committed
111
112
}

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

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