1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
/* 
 * $Id$ 
 */ 
inherit "wizard"; 
 
// This file uses stuff from spider... 
import spider; 
 
#include <stat.h> 
#include <roxen.h> 
//<locale-token project="admin_tasks">LOCALE</locale-token> 
#define LOCALE(X,Y)   _STR_LOCALE("admin_tasks",X,Y) 
 
 
constant action="debug_info"; 
 
 
string name= LOCALE(21, "Open files"); 
string doc = LOCALE(22,  
                    "Show a list of all open files and network connections."); 
 
// Debug functions.  List _all_ open filedescriptors 
 
string fix_port(string p) 
{ 
  array(string) a = p/" "; 
  if (a[0] == "0.0.0.0") { 
    a[0] = "*"; 
  } 
  if (has_value (a[0], ":")) 
    a[0] = "[" + a[0] + "]"; 
  if (a[1] == "0") { 
    a[1] = "ANY"; 
  } 
  return a * ":"; 
} 
 
string parse( RequestID id ) 
{ 
  return 
    ("<font size='+1'><b>" +LOCALE(23, "Active filedescriptors")+ "</b></font>\n"+ 
     sprintf("<pre><b>%-5s  %-9s  %-10s   %-10s</b>\n\n", 
             "fd", "type", "mode", "details")+ 
 
     (Array.map(Stdio.get_all_active_fd(), 
          lambda(int fd) 
          { 
            object f = Stdio.File(fd); 
            object stat = f->stat(); 
            if (!stat) 
              return sprintf("%-5s  %-9s  %-10s   %-12s", 
                             (string) fd, "Unknown", "?", "(error " + f->errno() + ")"); 
 
            string type = ([ 
              "reg":"File", 
              "dir":"Dir", 
              "lnk":"Link", 
              "chr":"Special", 
              "blk":"Device", 
              "fifo":"FIFO", 
              "sock":"Socket", 
              "unknown":"Unknown", 
            ])[stat->type] || "Unknown"; 
 
            // Doors aren't standardized yet... 
            if ((type == "Unknown") && 
                ((stat->mode & 0xf000) == 0xd000)) 
              type = "Door"; 
 
            string details = "-"; 
 
          format_details: 
            if (stat->isreg) { 
              if (stat->size == 1) { 
                details = "1 byte"; 
              } else { 
                details = sprintf("%d bytes", stat->size); 
              } 
              if (stat->ino) { 
                details += sprintf(", inode: %d", stat->ino); 
              } 
            } else if (stat->issock) { 
 
 
              string local_port = f->query_address(1); 
              if (local_port) 
                local_port = fix_port (local_port); 
              else { 
                if ((<System.EBADF, System.ENOTCONN, 
#if constant (System.EAFNOSUPPORT) 
                    System.EAFNOSUPPORT, 
#endif 
                    System.EINVAL>)[f->errno()]) { 
                  // A socket that getsockname(2) doesn't like. Assume 
                  // it's a unix socket if we get these errors. Don't 
                  // know how portable it is - only tested on Linux. 
                  // /mast 
                  type = "Unix"; 
                  break format_details; 
                } 
 
                local_port = 
                  "(Cannot get local port: "+ strerror (f->errno()) + ")"; 
              } 
 
              string remote_port = f->query_address(); 
              if (remote_port) 
                remote_port = fix_port (remote_port); 
              else if (f->errno() != System.ENOTCONN) 
                remote_port = 
                  "(Cannot get remote port: "+ strerror (f->errno()) + ")"; 
 
              if (!remote_port) { 
                type = "Port"; 
                details = local_port + " (listen)"; 
              } else { 
                details = sprintf("%s &lt;=&gt; %s", 
                                  local_port, remote_port); 
              } 
            } 
 
            return sprintf("%-5s  %-9s  %-10s   %-12s", 
                           (string)fd, 
                           type, 
                           stat->mode_string, 
                           details); 
          })*"\n")+ 
     "</pre><p><cf-ok/></p>"); 
}