9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: G. David Butler gdb@dbSystems.com
Subject: [9fans] create(2)/open(2) race for file creation
Date: Sun,  8 Feb 1998 15:10:44 -0600	[thread overview]
Message-ID: <19980208211044.q3huPNvrh-MD91yJbVWAiCvmKdH9Gu7fdQqs0A2tsJs@z> (raw)

>From: "G. David Butler" <gdb@dbSystems.com>
>
>Another alternative would be to change create(2) to simply call
>create(5) and return the results.  There will need to be some
>cleanup of programs that assume that a create on a existing file
>is OK, but if that is the case it is easy to change:
>
>if ((fd = create(file, mode, perm)) < 0) {
>	error...
>}
>
>to:
>
>if ((fd = create(file, mode, perm)) < 0 ||
>    (fd = open(file, mode|OTRUNC) < 0)) {
>	error...
>}
>
>In those programs.
>
>Any comments?

I'm surprised I haven't yet seen "What about union directories?"

If create(2) is changed then it could succeed even though a
file with that name exists in the union.  Then the above:

if ((fd = create(file, mode, perm)) < 0) {
	error...
}

Would need to become:

if ((fd = open(file, mode|OTRUNC)) < 0 ||
    (fd = create(file, mode, perm)) < 0 ||
    (fd = open(file, mode|OTRUNC)) < 0 ||
	error...
}

This is precisely the current create(2) call and the nasty
race is clear.

At this point an application could remove the OTRUNC from the
last open and know what is happening and deal with it as
appropriate.

So another advantage of changing create(2) to simply use create(5)
is the application now has more control over the behavior of
union directories.

For example one could mount a creatable directory before a
directory of readonly files and could have file updates "replace"
the readonly ones.  Currently one would get an error because you
can't write the file that exists.  To do that the application
would have to "know" the creatable directory exists and issue
create(2) to that directory.

In this case the application could do:

if ((fd = create(file, mode, perm)) < 0 ||
    (fd = open(file, mode|OTRUNC) < 0)) {
	error...
}

with no race.

David Butler
gdb@dbSystems.com




             reply	other threads:[~1998-02-08 21:10 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-02-08 21:10 G.David [this message]
  -- strict thread matches above, loose matches on Subject: below --
1998-02-26 23:30 Russ
1998-02-26 23:01 G.David
1998-02-26 22:16 G.David
1998-02-26 19:33 Eric
1998-02-26  0:24 G.David
1998-02-26  0:04 G.David
1998-02-25 23:42 G.David
1998-02-10 14:34 G.David
1998-02-10  8:59 forsyth
1998-02-09 21:21 G.David
1998-02-09 16:42 G.David
1998-02-09  8:05 Dan
1998-02-08 22:48 G.David
1998-02-08 22:18 Photon
1998-02-08 21:44 Rob
1998-02-08 16:52 G.David
1998-02-08 16:16 G.David
1998-02-08 16:10 Dave
1998-02-08 15:48 G.David
1998-02-08  5:09 G.David
1998-02-08  2:01 Rob
1998-02-08  0:27 G.David

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19980208211044.q3huPNvrh-MD91yJbVWAiCvmKdH9Gu7fdQqs0A2tsJs@z \
    --to=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).