IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New I'm just looking for a count of file descriptors.
Sockets, files, whatever.

I should have put this in the Linux forum, actually, since that's the system I'm doing this for.

Here's the situation:

On Solaris, if an accept() on a socket fails because of EMFILE (too many open files), the OS doesn't do anything to the connection attempt. If you close another file descriptor, you can then go back and accept() the waiting incoming connection.

On Linux, if the the accept() fails from EMFILE, the incoming connection is dropped and you can't retry it.

We already have accept() wrapped, so my solution is to check to make sure there are file descriptors left before actually calling accept(). I can get the max allowed with getrlimit(), no problem. I'm having issues figuring out how many are currently open, though.

One possibility is to count the number of files in /proc/<pid>/fd. I'd rather just use a system call.

I can even point to the spot in the kernel (fs/file_table.c, files_stat.nr_files) but I don't know how to actually GET that value.

There are indications that sysctl may be what I need; I'm still checking that. sysctl, unfortunately, isn't necessarily stable between kernel revisions.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New I am thinking either...
king:~# stat --help\nUsage: stat [OPTION] FILE...\nDisplay file or filesystem status.\n\n  -f, --filesystem      display filesystem status instead of file status\n  -c  --format=FORMAT   use the specified FORMAT instead of the default\n  -L, --dereference     follow links\n  -t, --terse           print the information in terse form\n      --help     display this help and exit\n      --version  output version information and exit\n\nThe valid format sequences for files (without --filesystem):\n\n  %A   Access rights in human readable form\n  %a   Access rights in octal\n  %b   Number of blocks allocated\n  %D   Device number in hex\n  %d   Device number in decimal\n  %F   File type\n  %f   raw mode in hex\n  %G   Group name of owner\n  %g   Group ID of owner\n  %h   Number of hard links\n  %i   Inode number\n  %N   Quoted File name with dereference if symbolic link\n  %n   File name\n  %o   IO block size\n  %s   Total size, in bytes\n  %T   Minor device type in hex\n  %t   Major device type in hex\n  %U   User name of owner\n  %u   User ID of owner\n  %X   Time of last access as seconds since Epoch\n  %x   Time of last access\n  %Y   Time of last modification as seconds since Epoch\n  %y   Time of last modification\n  %Z   Time of last change as seconds since Epoch\n  %z   Time of last change\n\nValid format sequences for file systems:\n\n  %a   Free blocks available to non-superuser\n  %b   Total data blocks in file system\n  %c   Total file nodes in file system\n  %d   Free file nodes in file system\n  %f   Free blocks in file system\n  %i   File System id in hex\n  %l   Maximum length of filenames\n  %n   File name\n  %s   Optimal transfer block size\n  %T   Type in human readable form\n  %t   Type in hex\n\nReport bugs to <bug-coreutils@gnu.org>.\nking:~#

or lsof
king:~# lsof -h\nlsof 4.64\n latest revision: [link|ftp://vic.cc.purdue.edu/pub/tools/unix/lsof/|ftp://vic.cc.purdue..../tools/unix/lsof/]\n latest FAQ: [link|ftp://vic.cc.purdue.edu/pub/tools/unix/lsof/FAQ|ftp://vic.cc.purdue....ols/unix/lsof/FAQ]\n latest man page: [link|ftp://vic.cc.purdue.edu/pub/tools/unix/lsof/lsof_man|ftp://vic.cc.purdue....nix/lsof/lsof_man]\n usage: [-?abhlnNoOPRstUvV] [-c c] [+|-d s] [+D D] [+|-f]\n [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+|-M] [-o [o]] [-p s]\n [+|-r [t]] [-S [t]] [-T [t]] [-u s] [+|-w] [--] [names]\nDefaults in parentheses; comma-separate set (s) items; dash-separate ranges.\n  -?|-h list help          -a AND selections (OR)     -b avoid kernel blocks\n  -c c  cmd c, /c/[bix]                               +d s  dir s files\n  -d s  select by FD set   +D D  dir D tree *SLOW?*    \n  -i select IPv[46] files  -l list UID numbers        -n no host names\n  -N select NFS files      -o list file offset        -O avoid overhead *RISKY*\n  -P no port names         -R list paRent PID         -s list file size\n  -t terse listing         -T disable TCP/TPI info    -U select Unix socket\n  -v list version info     -V verbose search          +|-w  Warnings (+)\n  -- end option scan\n  +f|-f  +filesystem or -file names   \n  -F [f] select fields; -F? for help  \n  +|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0)\n                                        +|-M   portMap registration (-)\n  -o o   o 0t offset digits (8)         -p s   select by PID set\n  -S [t] t second stat timeout (15)     -T qs  TCP/TPI Q,St info (s)\n  -g [s] select by process group ID set and print process group IDs\n  -i i   select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]\n  +|-r [t] repeat every t seconds (15); + until no files, - forever\n  -u s   exclude(^)|select login|UID set s\n  names  select named files or files on named file systems\nOnly root can list all files; /dev warnings disabled; kernel ID check disabled.\nking:~#


Either one should do your trick...

[link|mailto:curley95@attbi.com|greg] - Grand-Master Artist in IT
[link|http://www.iwethey.org/ed_curry/|REMEMBER ED CURRY!]   [link|http://pascal.rockford.com:8888/SSK@kQMsmc74S0Tw3KHQiRQmDem0gAIPAgM/edcurry/1//|ED'S GHOST SPEAKS!]
Heimatland Geheime Staatspolizei reminds:
These [link|http://www.whitehouse.gov/pcipb/cyberstrategy-draft.html|Civilian General Orders], please memorize them.
"Questions" will be asked at safety checkpoints.
New I need to do this in code, not at a shell prompt...
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New The non-syscall method....

Frankly, ls /proc/[0-9]*/fd/* | wc -l would be my\r\nexpedient.

\r\n\r\n

Otherwise, I'd suggest lsof, either as an OS call, or\r\npulling apart the sources to see where it's grabbing its\r\ninformation.

\r\n\r\n

Remember that /proc is system state, and that file\r\noperations on /proc may be the preferred operational method.

\r\n
--\r\n
Karsten M. Self [link|mailto:kmself@ix.netcom.com|kmself@ix.netcom.com]\r\n
[link|http://kmself.home.netcom.com/|http://kmself.home.netcom.com/]\r\n
What part of "gestalt" don't you understand?\r\n
[link|http://twiki.iwethey.org/twiki/bin/view/Main/|TWikIWETHEY] -- an experiment in collective intelligence. Stupidity. Whatever.\r\n
\r\n
   Keep software free.     Oppose the CBDTPA.     Kill S.2048 dead.\r\n[link|http://www.eff.org/alerts/20020322_eff_cbdtpa_alert.html|http://www.eff.org/alerts/20020322_eff_cbdtpa_alert.html]\r\n
New Remember that /proc is meant to be a human API
Its contents can and do change dramatically between releases of Linux, and that fact makes it very dangerous to rely on it in code.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Yabut
The alternative is sysctl, which also changes drastically between releases. In fact, the manpage for sysctl specifically says to use /proc instead.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New There is a fast /proc answer
Look in /proc/sys/fs/file-nr and interpret it as described in [link|http://www.cs.tau.ac.il/~didi/file-nr.html|http://www.cs.tau.ac...didi/file-nr.html]

That works for now, but apply usual /proc caveats.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New That's for the whole system (I think); I need by process
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New It is, my misread. Sorry.
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Note: current solution
I'm iterating /proc/<pid>/fd and counting the open descriptors.

Not ideal, but it works. It's also a little slow: 4ms when there are ~1000 open files for the process.

I'm still open to better suggestions... :-)
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Well... I just looked through *lsof* source tree...
... and found this:

/*\n * getlinksrc() - get the source path name for the /proc/<PID>/fd/<FD> link\n */\n\n\nstatic int\ngetlinksrc(ln, src, srcl, slash)\n        char *ln;                       /* link path */\n        char *src;                      /* link source path return address */\n        int srcl;                       /* length of src[] */\n        int *slash;                     /* leading slash flag: 0 = not present\n                                         *                     1 = present */\n{\n        char *cp;\n        int ll;\n\n        *slash = 0;\n        if ((ll = readlink(ln, src, srcl - 1)) < 1\n        ||  ll >= srcl)\n            return(-1);\n        src[ll] = '\\0';\n        if (*src == '/') {\n            *slash = 1;\n            return(ll);\n        }\n        if ((cp = strchr(src, ':'))) {\n            *cp = '\\0';\n            ll = strlen(src);\n        }\n        return(ll);\n}


Looks like they use proc too...

[link|mailto:curley95@attbi.com|greg] - Grand-Master Artist in IT
[link|http://www.iwethey.org/ed_curry/|REMEMBER ED CURRY!]   [link|http://pascal.rockford.com:8888/SSK@kQMsmc74S0Tw3KHQiRQmDem0gAIPAgM/edcurry/1//|ED'S GHOST SPEAKS!]
Heimatland Geheime Staatspolizei reminds:
These [link|http://www.whitehouse.gov/pcipb/cyberstrategy-draft.html|Civilian General Orders], please memorize them.
"Questions" will be asked at safety checkpoints.
New Well, joo find anything else... or give up looking?

[link|mailto:curley95@attbi.com|greg] - Grand-Master Artist in IT
[link|http://www.iwethey.org/ed_curry/|REMEMBER ED CURRY!]   [link|http://pascal.rockford.com:8888/SSK@kQMsmc74S0Tw3KHQiRQmDem0gAIPAgM/edcurry/1//|ED'S GHOST SPEAKS!]
Heimatland Geheime Staatspolizei reminds:
These [link|http://www.whitehouse.gov/pcipb/cyberstrategy-draft.html|Civilian General Orders], please memorize them.
"Questions" will be asked at safety checkpoints.
     I'm just looking for a count of file descriptors. - (admin) - (11)
         I am thinking either... - (folkert) - (1)
             I need to do this in code, not at a shell prompt... -NT - (admin)
         The non-syscall method.... - (kmself) - (2)
             Remember that /proc is meant to be a human API - (ben_tilly) - (1)
                 Yabut - (admin)
         There is a fast /proc answer - (ben_tilly) - (2)
             That's for the whole system (I think); I need by process -NT - (admin) - (1)
                 It is, my misread. Sorry. -NT - (ben_tilly)
         Note: current solution - (admin)
         Well... I just looked through *lsof* source tree... - (folkert)
         Well, joo find anything else... or give up looking? -NT - (folkert)

POLTERGEIST!
158 ms