Login
You're viewing the functional.cafe public feed.
  • Nov 11, 2026, 9:11 PM

    POSIX/SUS seems to be unambiguous that if you mkdir with the directory already existing at the start, mkdir exits status 1 (and 26.04 Rust uutils does). But a race while mkdir is running is something you can argue about even within the POSIX specifications language, at least as far as I can see.

    Interested parties can peruse the relevant SUS page and argue about it: pubs.opengroup.org/onlinepubs/
    (edited to have the actual URL, thanks frames)

    💬 2🔄 0⭐ 3

Replies

  • Nov 11, 2026, 10:01 PM

    I guess an extremely close reading of POSIX requires mkdir(1) to fail if mkdir(2) does (and you didn't give -p), because it "shall perform actions equivalent to the mkdir() function", which fails on existing directories. But you can argue about what "created successfully" means if your mkdir(1) starts out with the directory not existing but mkdir(2) fails. Did your mkdir(1) successfully create the directory and so exits status 0?

    💬 2🔄 0⭐ 0
  • Nov 11, 2026, 10:45 PM

    Actually I'm wrong about the particular 'race' in the OpenZFS test suite. What OpenZFS is doing is exercising a code path where the directory is created but then a thing goes wrong that causes mkdir(2) to return EIO. This is not a race exactly but a filesystem internal thing, where the filesystem may do work but not fully guarantee the mkdir(2) results.

    💬 1🔄 0⭐ 0
  • Nov 11, 2026, 11:03 PM

    However it turns out that I'm right in a sense, because uutils mkdir(1) would exit status 0 if the directory existed after a failed mkdir(2) call for any reason, including a concurrent race with another mkdir.

    💬 3🔄 0⭐ 0
  • Nov 11, 2026, 9:27 PM

    @cks Seems a bit strange, POSIX-wise mkdir -p should exit successfully on EEXIST, while without it seems undefined (see exit status section of mkdir(1)).
    And without the -p option, mkdir(1) is stuck to being racy anyway and typically exits unsuccessfully when anything already exists.

    💬 1🔄 0⭐ 0
  • Nov 11, 2026, 9:36 PM
    @lanodan @cks

    i kinda read that exit status as defined, it's 0 if all directories were created successfully, which for EEXIST they weren't created (as they already existed); thus counting as an error (which -p makes an explicit exception for), thus exit >0

    and imo this also implies that for the race condition, all that matters is if mkdir.2 errors or not?
    💬 1🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Nov 11, 2026, 9:36 PM

    @lanodan @cks
    hmm so I guess if someone was using mkdir(1) as a locking primitive ("if I successfully created the directory then it must've not existed before me, and nobody else will be able to create it until I unlink it, therefore I own the lock") then uutils will screw them

    but I don't see anything in SUS[1] that'd require the behaviour needed for use as a locking primitive

    However...
    1/

    [1]: pubs.opengroup.org/onlinepubs/

    💬 1🔄 0⭐ 0
  • Nov 11, 2026, 9:38 PM

    @lanodan @cks
    However, it says

    > If the -m option is specified, [...] the directory shall at no time have permissions less restrictive than the -m mode option-argument

    So if you race:

    mkdir -m 0770 foo
    mkdir -m 0755 foo

    and both succeed, and the resulting directory permissions are either one of those, then that's a bug

    💬 0🔄 0⭐ 0
  • Nov 11, 2026, 10:31 PM

    @cks it should be possible to externally observe which mkdir created a directory if you pass `-m`

    💬 0🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0
  • Nov 12, 2026, 12:46 AM

    @wolf480pl If I'm reading the code of the uutils fix correctly¹, uutils was basically applying 'mkdir -p' processing to all errors, even if you did plain mkdir. In 'mkdir -p', you skip reporting errors on directories that exist afterward.

    (The commit message talks about EEXIST but as far as I can see they don't check that that's the error they got from the mkdir(), they apply the processing to all errors.)

    ¹ github.com/uutils/coreutils/co

    💬 1🔄 0⭐ 0
  • Nov 12, 2026, 12:54 AM

    @cks
    oh, I read your previous post backwards - I thought they were trying to detect after mkdir that the dir exists, and if it does exist, fail to replicate GNU's behaviour, and that this was a TOCTOU.

    But yeah if it used to succeed when the dir exists - that's a much easier mistake to make.

    💬 1🔄 0⭐ 0
  • Nov 12, 2026, 1:11 AM

    @wolf480pl @cks They still check path.is_dir() (`statx` on Linux) on recursive errors, instead of checking for EEXIST. So the current code is still buggy. The path might change after mkdir(2) fails and before statx(2) is called.

    💬 1🔄 0⭐ 0
  • Nov 12, 2026, 2:04 AM

    @collinfunk
    hmm but how do you tell apart:
    1. mkdir -p a/b tries to create a, fails from EACCESS
    2. someone else creates a
    3. mkdir checks if a exists, it does, so it goes on to create a/b

    from

    1. someone else creates a
    2. mkdir tries to create a, fails from EEXISTS
    3. mkdir goes on to create a/b

    ?

    Oh, there's supposed to be a chmod, which would have to be tried (and potentially fail) after discovering the directory exists, not before...

    @cks

    💬 2🔄 0⭐ 0
  • Nov 12, 2026, 2:19 AM

    @collinfunk
    also, it seems to me what they're doing is fundamentally wrong: they're trying to implement POSIX semantics on top of a high-level filesystem API from Rust's stdlib, that AFAIK doesn't even make any promises wrt atomicity, so it might be mapping to some sequence of syscalls now and a semantically different sequence of syscalls in the future...
    @cks

    💬 1🔄 0⭐ 0
  • Nov 12, 2026, 2:29 AM

    @wolf480pl @cks I have similar concerns about that, but figured I was probably just being paranoid. I wonder how bad it would be for portability to just use rustix syscall wrappers.

    💬 1🔄 0⭐ 0
  • Nov 12, 2026, 2:40 AM

    @collinfunk @wolf480pl In my view the only time 'mkdir -p' should fail is if at the end of its work, there is no 'a/b'. People using 'mkdir -p' care about whether or not the result exists, not how it got there. (Of course it could be renamed immediately afterward before your script does anything else, but so can anything.)

    I think POSIX can be read to be more narrow than this, but if so, deviation is likely to be extremely common in mkdirs in practice.

    💬 0🔄 0⭐ 0
  • Nov 12, 2026, 2:36 AM

    @wolf480pl @cks For 'mkdir -p', you generally don't check that the directory exists. You just create the directory, and try to enter it with chdir(2) or by opening a file descriptor (if you use a file descriptor, you'd use mkdirat(2) later to create the child relative to the parent that).

    💬 0🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0
  • Nov 12, 2026, 4:43 PM

    @gnomon @mwl The specific situation seems to be a fairly obscure corner case; I'm honestly kind of impressed the ZFS test suite has a test for it. The commit¹ and the comments in the test script² have more details, and I think people are unlikely to hit this in practice (and if you do, it looks like things are already going wrong).

    ¹ github.com/openzfs/zfs/commit/
    ² github.com/openzfs/zfs/blob/ma

    💬 0🔄 0⭐ 0
  • Nov 12, 2026, 5:55 AM

    @cks flatpak does. And uses EEXIST on a read only filesystem. At least NFS a current Linux produces EROFS if the inode is not yet cached in this case, as it can't know if the directory exists without asking the server.

    💬 0🔄 0⭐ 0