flopen(3bsd) 3bsd flopen(3bsd)

# [NAME](#NAME)

`flopen`, `flopenat` — Reliably open and lock a file

# [LIBRARY](#LIBRARY)

library “libbsd”

# [SYNOPSIS](#SYNOPSIS)

`#include <sys/fcntl.h>`  
`#include <libutil.h>` (See [libbsd(7)](/man/os/libbsd/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](#DESCRIPTION)

The [`flopen`](#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`](#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. Thus, it is well suited for opening lock files, PID files, spool files, mailboxes and other kinds of files which are used for synchronization between processes.

If `flags` includes `O_NONBLOCK` and the file is already locked, [`flopen`](#flopen~2)() will fail and set `errno` to `EWOULDBLOCK`.

As with [`open`](#open)(), the additional `mode` argument is required if `flags` includes `O_CREAT`.

The [`flopenat`](#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](#RETURN_VALUES)

If successful, `flopen`() returns a valid file descriptor. Otherwise, it returns -1, and sets `errno` as described in [flock(2)](/man/os/libbsd/flock.2) and [open(2)](/man/os/libbsd/open.2).

# [SEE ALSO](#SEE_ALSO)

[errno(2)](/man/os/libbsd/errno.2), [flock(2)](/man/os/libbsd/flock.2), [open(2)](/man/os/libbsd/open.2)

# [AUTHORS](#AUTHORS)

The `flopen` function and this manual page were written by Dag-Erling Smørgrav &lt;[des@FreeBSD.org](mailto:des@FreeBSD.org)&gt;.

July 28, 2017 Linux 4.19.0-gvisor

---

Document: flopen(3bsd) from libbsd 0.12.2-r9 (Chainguard OS)
Source: https://gitlab.freedesktop.org/libbsd/libbsd
