From: Linus Torvalds <torvalds@osdl.org>
To: Erik Andersen <andersen@codepoet.org>
Cc: Kalin KOZHUHAROV <kalin@ThinRope.net>,
Davide Libenzi <davidel@xmailserver.org>,
Robert Love <rml@ximian.com>, Chris Wedgwood <cw@f00f.org>,
Arjan van de Ven <arjanv@redhat.com>,
Russell Leighton <russ@elegant-software.com>,
Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: clone() <-> getpid() bug in 2.6?
Date: Sun, 6 Jun 2004 09:57:20 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.58.0406060937330.7010@ppc970.osdl.org> (raw)
In-Reply-To: <20040606075754.GA10642@codepoet.org>
On Sun, 6 Jun 2004, Erik Andersen wrote:
>
> http://sources.redhat.com/cgi-bin/cvsweb.cgi/~checkout~/libc/sysdeps/posix/tempname.c?rev=1.36&content-type=text/plain&cvsroot=glibc
>
> /* Get some more or less random data. */
> random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
> value += random_time_bits ^ __getpid ();
This one is historically at least a bit understandable: it's quite common
for various programs to create their lockfiles with the "unique"
identifier that is the pid of the locker. So the algorithm used to be
roughly (used by various things, ranging from tty subsystem locking to
whatever):
int fd, len;
int pid = getpid();
len = sprintf(name, "lockfile.%d", pid);
fd = open(name, O_EXCL | O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
if (errno == EEXIST) {
fprintf(stderr, "Stale lockfile %s. Remove me\n", name);
exit(1); /* Or just "unlink + repeat" */
}
perror("lockfile");
exit(1);
}
/* Write the pid into the lockfile, fsync it */
write(fd, name + 9, len - 9);
fsync(fd);
/* Try to move it atomically */
while (rename("lockfile", name) < 0) {
if (errno != EEXIST) {
perror("lockfile");
exit(1);
}
/* Try again later */
sleep(1);
}
... We now have an exclusive lock ..
and here "pid" is in fact a good system-wide unique identifier. It's not
random, and it works badly with networked filesystems (which is why you'll
find versions of this algorithm that use the hostname in addition to the
pid too), but it's "obvious".
I think the original "mktemp()" also used to start off with "pid" as a
"unique" identifier. Because while it's not random, it _is_ system-wide
unique, so getpid() actually makes _sense_ in that respect.
In other words: getpid() makes little sense as a random value, but it does
make some sense as a unique value. Maybe less now than it did 20 years ago
due to the prevalence of networking, but history is powerful.
Linus
next prev parent reply other threads:[~2004-06-06 16:57 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-06-05 15:28 Russell Leighton
2004-06-05 20:45 ` Linus Torvalds
2004-06-05 20:55 ` Arjan van de Ven
2004-06-05 21:13 ` Linus Torvalds
2004-06-05 21:48 ` Robert Love
2004-06-05 22:44 ` Linus Torvalds
2004-06-05 21:53 ` Chris Wedgwood
2004-06-05 22:47 ` Robert Love
2004-06-05 22:57 ` David S. Miller
2004-06-05 23:01 ` Linus Torvalds
2004-06-05 23:07 ` Davide Libenzi
2004-06-05 23:18 ` Linus Torvalds
2004-06-05 23:26 ` Davide Libenzi
2004-06-06 5:08 ` Kalin KOZHUHAROV
2004-06-06 5:13 ` Chris Wedgwood
2004-06-06 5:34 ` Kalin KOZHUHAROV
2004-06-06 6:07 ` Linus Torvalds
2004-06-06 6:43 ` Kalin KOZHUHAROV
2004-06-06 7:57 ` Erik Andersen
2004-06-06 16:57 ` Linus Torvalds [this message]
2004-06-06 18:53 ` Simon Kirby
2004-06-06 19:00 ` Linus Torvalds
2004-06-06 9:52 ` Bernd Eckenfels
2004-06-06 13:07 ` Paul Rolland
2004-06-06 17:20 ` Patrick J. LoPresti
2004-06-06 17:31 ` Paul Rolland
2004-06-06 17:43 ` Davide Libenzi
2004-06-06 18:17 ` Rik van Riel
2004-06-06 18:37 ` Patrick J. LoPresti
2004-06-06 16:33 ` chris
[not found] ` <200406062022.54320.vda@port.imtp.ilyichevsk.odessa.ua>
2004-06-06 17:55 ` Linus Torvalds
2004-06-07 18:20 ` Bruce Guenter
2004-06-08 11:06 ` Kalin KOZHUHAROV
2004-06-05 23:19 ` Robert Love
2004-06-06 14:29 ` Russell Leighton
2004-06-06 15:38 ` Using getpid() often, another way? [was Re: clone() <-> getpid() bug in 2.6?] Russell Leighton
2004-06-06 15:44 ` Robert Love
2004-06-07 0:20 ` Russell Leighton
2004-06-06 15:58 ` Arjan van de Ven
2004-06-06 23:49 ` Russell Leighton
2004-06-07 12:13 ` Arjan van de Ven
2004-06-07 13:48 ` Sean Neakums
2004-06-07 14:00 ` Christoph Hellwig
2004-06-07 14:10 ` Sean Neakums
2004-06-07 18:42 ` David Mosberger
2004-06-07 23:02 ` Russell Leighton
2004-06-07 23:27 ` David Mosberger
2004-06-08 6:01 ` Arjan van de Ven
2004-06-08 9:48 ` Eric W. Biederman
2004-06-07 0:09 ` Russell Leighton
2004-06-07 12:20 ` Arjan van de Ven
2004-06-06 17:19 ` Linus Torvalds
2004-06-12 9:15 ` Dominik Straßer
2004-06-12 13:47 ` Linus Torvalds
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=Pine.LNX.4.58.0406060937330.7010@ppc970.osdl.org \
--to=torvalds@osdl.org \
--cc=andersen@codepoet.org \
--cc=arjanv@redhat.com \
--cc=cw@f00f.org \
--cc=davidel@xmailserver.org \
--cc=kalin@ThinRope.net \
--cc=linux-kernel@vger.kernel.org \
--cc=rml@ximian.com \
--cc=russ@elegant-software.com \
/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®