From: Willy Tarreau <willy@w.ods.org>
To: Ulrich Drepper <drepper@redhat.com>
Cc: Linux Kernel <linux-kernel@vger.kernel.org>,
Andrew Morton <akpm@osdl.org>, Linus Torvalds <torvalds@osdl.org>
Subject: Re: [PATCH] O_DIRECTORY|O_CREAT handling
Date: Sun, 21 Dec 2003 11:51:10 +0100 [thread overview]
Message-ID: <20031221105110.GA1323@alpha.home.local> (raw)
In-Reply-To: <3FE56A97.3060901@redhat.com>
Hi Ulrich,
On Sun, Dec 21, 2003 at 01:40:39AM -0800, Ulrich Drepper wrote:
>
> Some programs create temporary directory which they afterward use as the
> working directory or changed root. I.e., we have code like this:
>
> mkdir ("/some/dirRANDOM");
> chdir ("/some/dirRANDOM");
>
> or
>
> mkdir ("/some/dirRANDOM");
> fd = open ("/some/dirRANDOM", O_DIRECTORY);
> fchdir (fd);
>
> or
>
> mkdir ("/some/dirRANDOM");
> chroot ("/some/dirRANDOM");
>
> All these pieces of code have an obvious flaw, a race. There is no
> atomic way to do what we want.
Although I agree on the race, I fail to see in what case it matters.
In my programs, I often use mkdir() to get temporary directories instead
of temporary files, just because of the atomicity of the test-and-set which
mkdir() provides. Basically, I do :
base_dir="/tmp/tmpdir";
do {
rnd=random();
sprintf(dir, "%s%d", base_dir, rnd);
} while (!mkdir(dir, 0700);
/* now I'm guaranteed that I'm the first to get this dir, */
/* and only my UID can work in it */
chdir(dir);
So the only race would be someone working with the same UID (or root) removing
the directory and replacing it with another one (or a symlink or anything)
between mkdir() and chdir(). But don't see any use or consequence to this.
> Now combine these two problems. How about making this work?
>
> fd = open ("/some/dirRANDOM", O_RDONLY|O_CREAT|O_DIRECTORY|O_EXCL, 0700);
> fchdir (fd);
It would be interesting, of course, but is it portable to other systems ? If
it is not, very few people will use it, unfortunately. But if others already
do it right, then why not include it ?
Cheers,
Willy
next prev parent reply other threads:[~2003-12-21 10:51 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-12-21 9:40 Ulrich Drepper
2003-12-21 10:51 ` Willy Tarreau [this message]
2003-12-28 18:03 ` Andrew Pimlott
2003-12-28 18:25 ` viro
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=20031221105110.GA1323@alpha.home.local \
--to=willy@w.ods.org \
--cc=akpm@osdl.org \
--cc=drepper@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@osdl.org \
/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
all inboxes | Powered by JetHome®