Re: rename("a","b") succeeds multiple times race

From: Chris Sykes (chris@sigsegv.plus.com)
Date: Thu Apr 24 2003 - 09:30:58 EST


On Thu, Apr 24, 2003 at 12:57:07PM +0100, Ian Jackson wrote:
> I'm running 2.2.25 on a dual PIII; I have a program that processes
> mail messages which are left in a queue directory as uniquely named
> files. The queue runners each `claim' a message by renaming it away
> from the initial filename, so that only one queue runner works on each
> message.
>
> However, this does not work because Linux erroneously allows several
> processes to simultaneously and `successfully' rename the same file.
> The filesystem in question is ext2.
>
> I ran the system under strace, and saw (for example) the following, in
> five straces of five different processes:
>
> 02:11:47.293131 rename("q1988na-000xqY", "proc.1988na-000xqY") = 0
> 02:11:47.354497 rename("q1988na-000xqY", "proc.1988na-000xqY") = 0
> 02:11:47.412207 rename("q1988na-000xqY", "proc.1988na-000xqY") = 0
> 02:11:47.414376 rename("q1988na-000xqY", "proc.1988na-000xqY") = 0
> 02:11:47.414559 rename("q1988na-000xqY", "proc.1988na-000xqY") = 0

In rename(2):
[1] "If newpath already exists it will be atomically replaced
    (subject to a few conditions - see ERRORS below), so that
    there is no point at which another process attempting to
    access newpath will find it missing."

 ...

[2] "However, when overwriting there will probably be a window
    in which both oldpath and newpath refer to the file being
    renamed."

Perhaps your program should link(2) the newpath to the oldpath, then
on success unlink(2) the oldpath. link(2) will fail should newpath
already exist. Of course this assumes that oldpath & newpath are on
the same filesystem.

-- 

(o- Chris Sykes -- GPG Key: http://www.sigsegv.plus.com/key.txt //\ "Don't worry. Everything is getting nicely out of control ..." V_/_ Douglas Adams - The Salmon of Doubt


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



This archive was generated by hypermail 2b29 : Wed Apr 30 2003 - 22:00:14 EST