rewind (or fseek) on /proc files fails -- sometimes?

Robert G. Brown (rgb@phy.duke.edu)
Thu, 15 Jul 1999 05:36:21 -0400 (EDT)


Dear kernel persons:

I've been working for a while on (yet another) proc-distributing
daemon called procstatd (available in alpha at

http://www.phy.duke.edu/~rgb/procstatd.tar.gz

if anybody wants to play with it or the associated tkperl GUI for system
monitoring). In the daemon I open various files in /proc, e.g.
/proc/stat or /proc/loadavg and parse their contents into fields to
serve to any who request them. Every epoch (say, 10 seconds) I then
use rewind() on read all of the open file descriptors (to avoid the
overhead of a file close/open) and reread/reparse them and deliver
either raw values or digested rates. None of which has to do with my
question.

My question is:

Why do some of the files in proc correctly rewind (or fseek() to the
beginning) while others, particularly /proc/loadavg, don't? To be
explicit, on most of the files I parse, a fragment like:

/* PROC_NET_DEV */
errno = 0;
if(stat_fd[PROC_NET_DEV]){
rewind(stat_fd[PROC_NET_DEV]); /* void, so tough to check errors */
} else {
return;
}
if(errno == EBADF){
fprintf(stderr,"Error: The /proc/net/dev file descriptor/stream is not seekable.\n");
fclose(stat_fd[PROC_NET_DEV]);
fprintf(stderr,"Closing and reopening /proc/net/dev.\n");
stat_fd[PROC_NET_DEV] = fopen("/proc/net/dev","r");
}

works fine; the error branch is never taken and the rewound file
correctly yields a current snapshot of the dynamic network data. On the
other hand, I have to use:

errno = 0;
if(stat_fd[PROC_LOADAVG]){
/*
* For no good reason that I can see, a one line /proc file won't rewind
* correctly. So we close and reopen it.
*/
fclose(stat_fd[PROC_LOADAVG]);
stat_fd[PROC_LOADAVG] = fopen("/proc/loadavg","r");
} else {
return;
}

to get current data from /proc/loadavg. If I use rewind as above I
reread the same data (from the time I originally opened and read the
file) over and over again, with NO error message on either rewind or the
fseek equivalent.

Is this an artifact of the file having only a single line?

Is this a bug in the kernel (something different about the way this
particular /proc file is built)?

Is this a "feature/bug" of rewind and fseek; that they just don't work
if one reads only a one line file?

Some of these questions I can probably answer myself with some obvious
tests (like adding a header line to /proc/loadavg, seeing what rewind
does for an arbitrary one line file) but in case somebody on the list
knows the answer (or it appears to be a real bug that needs attention) I
thought I'd post first.

rgb

Robert G. Brown http://www.phy.duke.edu/~rgb/
Duke University Dept. of Physics, Box 90305
Durham, N.C. 27708-0305
Phone: 1-919-660-2567 Fax: 919-660-2525 email:rgb@phy.duke.edu

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/