open_excl(3)

πŸ“¦
version 0.34 Β· 3 versions
, in alpine 3.24, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu resolute, ubuntu stonking

✨ NAME ✨

open_excl β€” open a file for exclusive writing πŸ’…

okay so bestie, welcome to the open_excl(3) page from the Library Functions Manual. This function is lowkey iconic: it opens a file for exclusive writing, periodt.

πŸ¦‹ SYNTAX πŸ¦‹

#include <libowfat/open.h>

int open_excl(const char *filename);

🌸 DESCRIPTION 🌸

So open_excl opens the file filename for writing and returns the file handle. But here's the vibe check: the file may not exist before the call to open_excl. Like, it has to be brand-new, bestie. The file will be created with mode 0600.

If there was an error creating the file, open_excl returns -1 and sets errno accordingly. So if you get -1, check errno, okay? 😭

Now the messy part: since open_excl relies on the O_EXCL flag to open, it does not work reliably over NFS (the NFS protocol is broken) and must be emulated using a lock file. The emulation tea: create a file with a unique file name and link(2) it to the lock file. Then stat the lock file and see if the link count is 2. That's it, that's the workaround. 🫢

πŸ‘‘ SEE ALSO πŸ‘‘


sign-off: this page is from the Library Functions Manual, titled open_excl(3), and there's no date on it. bye bestie 🫢✨