The curious case of O_DIRECTORY|O_CREAT
The curious case of O_DIRECTORY|O_CREAT
Posted Mar 28, 2023 16:19 UTC (Tue) by NYKevin (subscriber, #129325)In reply to: The curious case of O_DIRECTORY|O_CREAT by josh
Parent article: The curious case of O_DIRECTORY|O_CREAT
0. Thread A opens the parent directory. This may or may not race with something, but let's call it out of scope for now and just assume that it happens.
1. Thread A calls mkdirat and creates /path/to/foo/
2. Thread B renames it to /path/to/bar/ (or removes it or whatever)
3. Now if thread A tries to openat /path/to/foo/, it gets ENOENT, so it would know to start over and try again.
Alternatively:
2. Thread B renames it to /path/to/bar/
3. Thread B creates a new /path/to/foo/
4. Thread A successfully openats /path/to/foo/. It's a different directory than the one that it created, but from thread A's perspective, this makes no practical difference. One newly-created directory is just as good as another, right?
Alternatively alternatively:
3. Thread B creates /path/to/foo as a symlink to something else.
4. Thread A tries to openat /path/to/foo/, but it fails because of O_NOFOLLOW. A knows that something is wrong and aborts the operation.