lkml.org 
[lkml]   [2003]   [Apr]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: rename("a","b") succeeds multiple times race
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

[unhandled content-type:application/pgp-signature]
\
 
 \ /
  Last update: 2005-03-22 13:34    [W:0.033 / U:0.744 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site