The check-then-create race
The check-then-create race
Answer
/* RACE: another process can create the file in this gap */ if (access(LOCKPATH, F_OK) != 0) { fd = open(LOCKPATH, O_WRONLY | O_CREAT, 0600); write(fd, pidbuf, n); }
Two processes can both reach the `open` having both seen no lockfile, and both believe they hold the lock. The window is small, and an attacker who can trigger the code repeatedly does not care how small.
Stallings & Brown 5e ch11 §11.1–11.6