---
name: "flopen"
section: "3"
platform: "linux"
package: "libbsd-dev"
version: "0.8.3-1"
release: "Debian 9 (Stretch)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/flopen"
---

# flopen(3)

reliably open and lock a file

## NAME

**flopen** – reliably open and lock a file

## LIBRARY

library “libbsd”

## SYNOPSIS

**#include \<sys/fcntl.h\>**

**#include \<bsd/libutil.h\>**

*int*\

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

*int*\

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

## DESCRIPTION

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. 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**() will fail and set *errno* to EWOULDBLOCK.

As with **open**(), the additional *mode* argument is required if *flags* includes O\_CREAT.

## RETURN VALUES

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

## SEE ALSO

[errno(2)](/errno/2.md), [flock(2)](/flock/2.md), [open(2)](/open/2.md)

## AUTHORS

The **flopen** function and this manual page were written by Dag-Erling Smørgrav ⟨des@FreeBSD.org⟩.
