89b41a2000-02-04Per Hedbor /*
0917d32013-03-04Anders Johansson  * $Id$
89b41a2000-02-04Per Hedbor  */ inherit "wizard";
1ed2a32001-08-24Henrik Grubbström (Grubba)  // This file uses stuff from spider... import spider;
bebbad2000-08-16Andreas Lange #include <stat.h> #include <roxen.h> //<locale-token project="admin_tasks">LOCALE</locale-token> #define LOCALE(X,Y) _STR_LOCALE("admin_tasks",X,Y)
89b41a2000-02-04Per Hedbor 
bebbad2000-08-16Andreas Lange constant action="debug_info";
89b41a2000-02-04Per Hedbor 
1ed2a32001-08-24Henrik Grubbström (Grubba) 
bebbad2000-08-16Andreas Lange string name= LOCALE(21, "Open files"); string doc = LOCALE(22, "Show a list of all open files and network connections.");
89b41a2000-02-04Per Hedbor  // Debug functions. List _all_ open filedescriptors
1ed2a32001-08-24Henrik Grubbström (Grubba)  string fix_port(string p)
89b41a2000-02-04Per Hedbor {
1ed2a32001-08-24Henrik Grubbström (Grubba)  array(string) a = p/" "; if (a[0] == "0.0.0.0") { a[0] = "*"; }
e50a4c2011-01-24Martin Stjernholm  if (has_value (a[0], ":")) a[0] = "[" + a[0] + "]";
1ed2a32001-08-24Henrik Grubbström (Grubba)  if (a[1] == "0") { a[1] = "ANY";
89b41a2000-02-04Per Hedbor  }
1ed2a32001-08-24Henrik Grubbström (Grubba)  return a * ":";
89b41a2000-02-04Per Hedbor }
bebbad2000-08-16Andreas Lange string parse( RequestID id )
89b41a2000-02-04Per Hedbor { return
80cd682003-11-17Anders Johansson  ("<font size='+1'><b>" +LOCALE(23, "Active filedescriptors")+ "</b></font>\n"+
1ed2a32001-08-24Henrik Grubbström (Grubba)  sprintf("<pre><b>%-5s %-9s %-10s %-10s</b>\n\n", "fd", "type", "mode", "details")+
89b41a2000-02-04Per Hedbor 
8b72882014-11-27Henrik Grubbström (Grubba)  (Array.map(Stdio.get_all_active_fd(),
89b41a2000-02-04Per Hedbor  lambda(int fd) {
1ed2a32001-08-24Henrik Grubbström (Grubba)  object f = Stdio.File(fd); object stat = f->stat();
bafe422006-01-24Jonas Wallden  if (!stat) return sprintf("%-5s %-9s %-10s %-12s", (string) fd, "Unknown", "?", "(error " + f->errno() + ")");
bebbad2000-08-16Andreas Lange 
1ed2a32001-08-24Henrik Grubbström (Grubba)  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 = "-";
89b41a2000-02-04Per Hedbor 
4495fd2007-02-16Martin Stjernholm  format_details:
1ed2a32001-08-24Henrik Grubbström (Grubba)  if (stat->isreg) { if (stat->size == 1) { details = "1 byte"; } else { details = sprintf("%d bytes", stat->size); }
f716b02001-08-24Henrik Grubbström (Grubba)  if (stat->ino) { details += sprintf(", inode: %d", stat->ino); }
1ed2a32001-08-24Henrik Grubbström (Grubba)  } else if (stat->issock) {
4495fd2007-02-16Martin Stjernholm 
1ed2a32001-08-24Henrik Grubbström (Grubba)  string local_port = f->query_address(1);
4495fd2007-02-16Martin Stjernholm  if (local_port) local_port = fix_port (local_port); else {
4bfabd2011-01-24Martin Stjernholm  if ((<System.EBADF, System.ENOTCONN, #if constant (System.EAFNOSUPPORT) System.EAFNOSUPPORT, #endif System.EINVAL>)[f->errno()]) {
4495fd2007-02-16Martin Stjernholm  // 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;
1ed2a32001-08-24Henrik Grubbström (Grubba)  }
4495fd2007-02-16Martin Stjernholm  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)";
1ed2a32001-08-24Henrik Grubbström (Grubba)  } else {
d6ba4f2001-08-24Johan Sundström  details = sprintf("%s &lt;=&gt; %s",
4495fd2007-02-16Martin Stjernholm  local_port, remote_port);
1ed2a32001-08-24Henrik Grubbström (Grubba)  } } return sprintf("%-5s %-9s %-10s %-12s", (string)fd, type, stat->mode_string, details);
89b41a2000-02-04Per Hedbor  })*"\n")+
bebbad2000-08-16Andreas Lange  "</pre><p><cf-ok/></p>");
89b41a2000-02-04Per Hedbor }