flopen(3bsd)

πŸ“¦
version 0.12.2 Β· 2 versions
  • 0.12.2 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.11.5 Β· ubuntu jammy
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– flopen(3bsd) πŸ’–

okay bestie, welcome to the flopen page from section 3bsd of the local manual β€” yes it's literally called flopen(3bsd), that's the whole name, we're not being weird about it ✨


🌸 NAME 🌸

flopen, flopenat β€” Reliably open and lock a file

no fr, that's the whole vibe: open it AND lock it, no cap πŸ”’


πŸŽ€ LIBRARY πŸŽ€

Utility functions from BSD systems (libbsd, -lbsd)


✨ SYNOPSIS ✨

#include <sys/fcntl.h>
#include <libutil.h>

(See libbsd(7) for include usage.)

int
flopen(const char *path, int flags);

int
flopen(const char *path, int flags, mode_t mode);

int
flopenat(int fd, const char *path, int flags);

int
flopenat(int fd, const char *path, int flags, mode_t mode);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the flopen() function opens or creates a file and acquires an exclusive lock on it πŸ’… it is essentially equivalent with calling open() with the same parameters followed by flock() with an operation argument of LOCK_EX, except that flopen() will attempt to detect and handle races that may occur between opening / creating the file and locking it 🫢

the way that makes it chef's kiss for opening lock files, PID files, spool files, mailboxes and other kinds of files which are used for synchronization between processes β€” it's giving main character energy for process coordination fr fr πŸ‘‘

If flags includes O_NONBLOCK and the file is already locked, flopen() will fail and set errno to EWOULDBLOCK 😭 (no waiting around for you, bestie, it's busy)

As with open(), the additional mode argument is required if flags includes O_CREAT ⚠️

The flopenat() function is equivalent to the flopen() function except in the case where the path specifies a relative path. In this case the file to be opened is determined relative to the directory associated with the file descriptor fd instead of the current working directory 🌈

If flopenat() is passed the special value AT_FDCWD in the fd parameter, the current working directory is used and the behavior is identical to a call to flopen() ✨


πŸ’œ RETURN VALUES πŸ’œ

If successful, flopen() returns a valid file descriptor πŸŽ‰ Otherwise, it returns -1, and sets errno as described in flock(2) and open(2) πŸ’€


πŸ“ SEE ALSO πŸ“


πŸ‘‘ AUTHORS πŸ‘‘

The flopen function and this manual page were written by Dag-Erling Smørgrav <des@FreeBSD.org> 🌟 absolute icon, we love to see it


and that's the page, bestie!! this cutie came to you from Debian, dated July 28, 2017, and it lives in the flopen(3bsd) manual πŸ’– go lock some files responsibly, mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from