Skip to contentSkip to conversation

procfs uglyness caused by "cat"

DISCUSSION Herbert Rosmanith 2 participants 2 messages Mar 14, 2006

lore
Share & tools
lore
#0 lore raw

hello list,

when doing "cat /proc/uptime" or similar files, the routine which prepares the buffer gets called twice.

this is because "cat" reads two times from the file, as a system-call-trace shows:

    # strace -f cat /proc/uptime
    ...
    read(3, "5241.09 5082.74\n", 1024)      = 16
    ...
    read(3, "", 1024)                       = 0
    ...

this leads to uptime_read_proc() being called two times, the second time with parameter off=16, but since this is ignored, the work is done twice: clock_...gettime(), cputime_to_timespec(), sprintf(), proc_calc_metrics() and so on.

insert a printk-statement if you don't beleive this.

btw, there's no wrong information produced because of this, because *page points to the same location both calls.

a simple way to get rid of this:

static int uptime_read_proc(char *page, char **start, off_t off,
int count, int *eof, void *data)

{
        struct timespec uptime;
        struct timespec idle;
        int len;
        cputime_t idletime;

+ if (off)
+ return 0;

        cputime_add(init_task.utime, init_task.stime);
        do_posix_clock_monotonic_gettime(&uptime);
        cputime_to_timespec(idletime, &idle);
        len = sprintf(page,"%lu.%02lu %lu.%02lu\n",

...
and so on.

this affects possibly all /proc files which ignore the offset parameter and are evaluated(?) with "cat".

regards,
h.rosmanith

#1 lore raw

Robert Hancock explained what's wrong with your proposal of:
+ if (off)
+ return 0;

For an alternative that seems to work better, see the processing behind the /proc/<pid>/cpuset file, by grep'ing for seq_file in kernel/cpuset.c.

Essentially, it composes the result string on the open, and then lets user code read and seek over that string at will.

Signature · 3 lines
                  I won't rest till it's the best ...
                  Programmer, Linux Scalability
                  Paul Jackson <[email protected]> 1.925.600.0401