Subject: Re: touch(1) enchancement, mkstemp(1) (fwd)
To: None <darrenr@cyber.com.au, tech-security@NetBSD.ORG>
From: Olaf Seibert <rhialto@polder.ubc.kun.nl>
List: tech-security
Date: 02/17/1998 22:08:00
A long time ago, I read about a trick regarding lockfiles. For
lockfiles, you essentially need an atomic test-and-set operation.

On V7, before O_EXCL existed, one usually made a mode 000 file with
creat(2). If the file already existed, it could not be opened, so the
open failed. Otherwise it was created atomically. This strategy failed
for the superuser since she can open all files.

The trick was not to use a lock FILE but a lock DIRECTORY, since
mkdir(2) is atomic and refuses to create directories that already
exist.

I checked the source for mkdir(1), and it does not attempt to be clever,
but directly calls mkdir(2) so this can also be used in shell scripts
where you cannot specify the O_* flags used for open(2).

Furthermore, this can be applied to unique temp files. Use mkdir to
create a guaranteed new directory, then make your temp files in there.

-Olaf.
--
___ Olaf 'Rhialto' Seibert      D787B44DFC896063 4CBB95A5BD1DAA96 
\X/ It's not easy having a good time    rhialto@polder.ubc.kun.nl