]> git.baikalelectronics.ru Git - kernel.git/commit
seq_file: more atomicity in traverse()
authorAlexey Dobriyan <adobriyan@sw.ru>
Mon, 16 Jul 2007 06:40:39 +0000 (23:40 -0700)
committerLinus Torvalds <torvalds@woody.linux-foundation.org>
Mon, 16 Jul 2007 16:05:45 +0000 (09:05 -0700)
commit5069a9adf8b3d9f23fdcd57df89d58b9b62d090d
tree4905ee309e3147bb64f8a8a4446937424540fd5f
parent107b47cff922b838e3f73d4e603e4e0760b45d78
seq_file: more atomicity in traverse()

Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:

while read line; do
[do something with $line]
done </proc/$FILE

bash, to implement such loop does essentially

read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);

Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.

Two objects in list, with ->index'es being 0 and 1.  Current one is 1, as
bash prints second object line by line.

Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.

So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0.  seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.

Fix is to update ->index only after we're sure we saw enough objects down
the road.

Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/seq_file.c