xref: /dragonfly/lib/libutil/flopen.3 (revision c541a65d)
1.\"-
2.\" Copyright (c) 2007 Dag-Erling Co�dan Sm�rgrav
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.\" $FreeBSD: src/lib/libutil/flopen.3,v 1.7 2008/10/20 18:02:16 des Exp $
27.\"
28.Dd May 10, 2007
29.Dt FLOPEN 3
30.Os
31.Sh NAME
32.Nm flopen
33.Nd "Reliably open and lock a file"
34.Sh LIBRARY
35.Lb libutil
36.Sh SYNOPSIS
37.In sys/fcntl.h
38.In libutil.h
39.Ft int
40.Fn flopen "const char *path" "int flags"
41.Ft int
42.Fn flopen "const char *path" "int flags" "mode_t mode"
43.Sh DESCRIPTION
44The
45.Fn flopen
46function opens or creates a file and acquires an exclusive lock on it.
47It is essentially equivalent with calling
48.Fn open
49with the same parameters followed by an
50.Fn fcntl
51.Dv F_SETLK
52or
53.Dv F_SETLKW
54operation with lock type
55.Dv F_WRLCK ,
56except that
57.Fn flopen
58will attempt to detect and handle races that may occur between opening
59/ creating the file and locking it.
60Thus, it is well suited for opening lock files, PID files, spool
61files, mailboxes and other kinds of files which are used for
62synchronization between processes.
63.Pp
64If
65.Va flags
66includes
67.Dv O_NONBLOCK
68and the file is already locked,
69.Fn flopen
70will fail and set
71.Va errno
72to
73.Dv EWOULDBLOCK .
74.Pp
75As with
76.Fn open ,
77the additional
78.Va mode
79argument is required if
80.Va flags
81includes
82.Dv O_CREAT .
83.Sh RETURN VALUES
84If successful,
85.Fn flopen
86returns a valid file descriptor.
87Otherwise, it returns -1, and sets
88.Va errno
89as described in
90.Xr fcntl 2
91and
92.Xr open 2 .
93.Sh SEE ALSO
94.Xr errno 2 ,
95.Xr fcntl 2 ,
96.Xr open 2
97.Sh HISTORY
98The
99.Fn flopen
100function first appeared in
101.Fx 6.3 .
102.Sh AUTHORS
103.An -nosplit
104The
105.Nm
106function and this manual page were written by
107.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
108