5b4e1b492b867a2a67115f86612d1467668c3799
[dragonfly.git] / lib / libutil / flopen.3
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: head/lib/libutil/flopen.3 229784 2012-01-07 16:13:56Z uqs $
27 .\"
28 .Dd June 6, 2009
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
44 The
45 .Fn flopen
46 function opens or creates a file and acquires an exclusive lock on it.
47 It is essentially equivalent with calling
48 .Fn open
49 with the same parameters followed by
50 .Fn flock
51 with an
52 .Va operation
53 argument of
54 .Dv LOCK_EX ,
55 except that
56 .Fn flopen
57 will attempt to detect and handle races that may occur between opening
58 / creating the file and locking it.
59 Thus, it is well suited for opening lock files, PID files, spool
60 files, mailboxes and other kinds of files which are used for
61 synchronization between processes.
62 .Pp
63 If
64 .Va flags
65 includes
66 .Dv O_NONBLOCK
67 and the file is already locked,
68 .Fn flopen
69 will fail and set
70 .Va errno
71 to
72 .Er EWOULDBLOCK .
73 .Pp
74 As with
75 .Fn open ,
76 the additional
77 .Va mode
78 argument is required if
79 .Va flags
80 includes
81 .Dv O_CREAT .
82 .Sh RETURN VALUES
83 If successful,
84 .Fn flopen
85 returns a valid file descriptor.
86 Otherwise, it returns -1, and sets
87 .Va errno
88 as described in
89 .Xr flock 2
90 and
91 .Xr open 2 .
92 .Sh SEE ALSO
93 .Xr errno 2 ,
94 .Xr flock 2 ,
95 .Xr open 2
96 .Sh AUTHORS
97 .An -nosplit
98 The
99 .Nm
100 function and this manual page were written by
101 .An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .