Merge branch 'vendor/XZ'
[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: src/lib/libutil/flopen.3,v 1.7 2008/10/20 18:02:16 des Exp $
27 .\"
28 .Dd December 27, 2008
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 an
50 .Fn fcntl
51 .Dv F_SETLK
52 or
53 .Dv F_SETLKW
54 operation with lock type
55 .Dv F_WRLCK ,
56 except that
57 .Fn flopen
58 will attempt to detect and handle races that may occur between opening
59 / creating the file and locking it.
60 Thus, it is well suited for opening lock files, PID files, spool
61 files, mailboxes and other kinds of files which are used for
62 synchronization between processes.
63 .Pp
64 If
65 .Va flags
66 includes
67 .Dv O_NONBLOCK
68 and the file is already locked,
69 .Fn flopen
70 will fail and set
71 .Va errno
72 to
73 .Er EWOULDBLOCK .
74 .Pp
75 As with
76 .Fn open ,
77 the additional
78 .Va mode
79 argument is required if
80 .Va flags
81 includes
82 .Dv O_CREAT .
83 .Sh RETURN VALUES
84 If successful,
85 .Fn flopen
86 returns a valid file descriptor.
87 Otherwise, it returns -1, and sets
88 .Va errno
89 as described in
90 .Xr fcntl 2
91 and
92 .Xr open 2 .
93 .Sh SEE ALSO
94 .Xr errno 2 ,
95 .Xr fcntl 2 ,
96 .Xr open 2
97 .Sh HISTORY
98 The
99 .Fn flopen
100 function first appeared in
101 .Fx 6.3 .
102 .Sh AUTHORS
103 .An -nosplit
104 The
105 .Nm
106 function and this manual page were written by
107 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .