11eaf0ac3Slogwang.\"- 21eaf0ac3Slogwang.\" Copyright (c) 2007 Dag-Erling Coïdan Smørgrav 31eaf0ac3Slogwang.\" All rights reserved. 41eaf0ac3Slogwang.\" 51eaf0ac3Slogwang.\" Redistribution and use in source and binary forms, with or without 61eaf0ac3Slogwang.\" modification, are permitted provided that the following conditions 71eaf0ac3Slogwang.\" are met: 81eaf0ac3Slogwang.\" 1. Redistributions of source code must retain the above copyright 91eaf0ac3Slogwang.\" notice, this list of conditions and the following disclaimer. 101eaf0ac3Slogwang.\" 2. Redistributions in binary form must reproduce the above copyright 111eaf0ac3Slogwang.\" notice, this list of conditions and the following disclaimer in the 121eaf0ac3Slogwang.\" documentation and/or other materials provided with the distribution. 131eaf0ac3Slogwang.\" 141eaf0ac3Slogwang.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151eaf0ac3Slogwang.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161eaf0ac3Slogwang.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171eaf0ac3Slogwang.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181eaf0ac3Slogwang.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191eaf0ac3Slogwang.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201eaf0ac3Slogwang.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211eaf0ac3Slogwang.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221eaf0ac3Slogwang.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231eaf0ac3Slogwang.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241eaf0ac3Slogwang.\" SUCH DAMAGE. 251eaf0ac3Slogwang.\" 261eaf0ac3Slogwang.\" $FreeBSD$ 271eaf0ac3Slogwang.\" 28*22ce4affSfengbojiang.Dd July 28, 2017 291eaf0ac3Slogwang.Dt FLOPEN 3 301eaf0ac3Slogwang.Os 311eaf0ac3Slogwang.Sh NAME 32*22ce4affSfengbojiang.Nm flopen , 33*22ce4affSfengbojiang.Nm flopenat 341eaf0ac3Slogwang.Nd "Reliably open and lock a file" 351eaf0ac3Slogwang.Sh LIBRARY 361eaf0ac3Slogwang.Lb libutil 371eaf0ac3Slogwang.Sh SYNOPSIS 381eaf0ac3Slogwang.In sys/fcntl.h 391eaf0ac3Slogwang.In libutil.h 401eaf0ac3Slogwang.Ft int 411eaf0ac3Slogwang.Fn flopen "const char *path" "int flags" 421eaf0ac3Slogwang.Ft int 431eaf0ac3Slogwang.Fn flopen "const char *path" "int flags" "mode_t mode" 44*22ce4affSfengbojiang.Ft int 45*22ce4affSfengbojiang.Fn flopenat "int fd" "const char *path" "int flags" 46*22ce4affSfengbojiang.Ft int 47*22ce4affSfengbojiang.Fn flopenat "int fd" "const char *path" "int flags" "mode_t mode" 481eaf0ac3Slogwang.Sh DESCRIPTION 491eaf0ac3SlogwangThe 501eaf0ac3Slogwang.Fn flopen 511eaf0ac3Slogwangfunction opens or creates a file and acquires an exclusive lock on it. 521eaf0ac3SlogwangIt is essentially equivalent with calling 531eaf0ac3Slogwang.Fn open 541eaf0ac3Slogwangwith the same parameters followed by 551eaf0ac3Slogwang.Fn flock 561eaf0ac3Slogwangwith an 57*22ce4affSfengbojiang.Fa operation 581eaf0ac3Slogwangargument of 591eaf0ac3Slogwang.Dv LOCK_EX , 601eaf0ac3Slogwangexcept that 611eaf0ac3Slogwang.Fn flopen 621eaf0ac3Slogwangwill attempt to detect and handle races that may occur between opening 631eaf0ac3Slogwang/ creating the file and locking it. 641eaf0ac3SlogwangThus, it is well suited for opening lock files, PID files, spool 651eaf0ac3Slogwangfiles, mailboxes and other kinds of files which are used for 661eaf0ac3Slogwangsynchronization between processes. 671eaf0ac3Slogwang.Pp 681eaf0ac3SlogwangIf 69*22ce4affSfengbojiang.Fa flags 701eaf0ac3Slogwangincludes 711eaf0ac3Slogwang.Dv O_NONBLOCK 721eaf0ac3Slogwangand the file is already locked, 731eaf0ac3Slogwang.Fn flopen 741eaf0ac3Slogwangwill fail and set 751eaf0ac3Slogwang.Va errno 761eaf0ac3Slogwangto 771eaf0ac3Slogwang.Dv EWOULDBLOCK . 781eaf0ac3Slogwang.Pp 791eaf0ac3SlogwangAs with 801eaf0ac3Slogwang.Fn open , 811eaf0ac3Slogwangthe additional 82*22ce4affSfengbojiang.Fa mode 831eaf0ac3Slogwangargument is required if 84*22ce4affSfengbojiang.Fa flags 851eaf0ac3Slogwangincludes 861eaf0ac3Slogwang.Dv O_CREAT . 87*22ce4affSfengbojiang.Pp 88*22ce4affSfengbojiangThe 89*22ce4affSfengbojiang.Fn flopenat 90*22ce4affSfengbojiangfunction is equivalent to the 91*22ce4affSfengbojiang.Fn flopen 92*22ce4affSfengbojiangfunction except in the case where the 93*22ce4affSfengbojiang.Fa path 94*22ce4affSfengbojiangspecifies a relative path. 95*22ce4affSfengbojiangIn this case the file to be opened is determined relative to the directory 96*22ce4affSfengbojiangassociated with the file descriptor 97*22ce4affSfengbojiang.Fa fd 98*22ce4affSfengbojianginstead of the current working directory. 99*22ce4affSfengbojiangIf 100*22ce4affSfengbojiang.Fn flopenat 101*22ce4affSfengbojiangis passed the special value 102*22ce4affSfengbojiang.Dv AT_FDCWD 103*22ce4affSfengbojiangin the 104*22ce4affSfengbojiang.Fa fd 105*22ce4affSfengbojiangparameter, the current working directory is used 106*22ce4affSfengbojiangand the behavior is identical to a call to 107*22ce4affSfengbojiang.Fn flopen . 1081eaf0ac3Slogwang.Sh RETURN VALUES 1091eaf0ac3SlogwangIf successful, 1101eaf0ac3Slogwang.Fn flopen 1111eaf0ac3Slogwangreturns a valid file descriptor. 1121eaf0ac3SlogwangOtherwise, it returns -1, and sets 1131eaf0ac3Slogwang.Va errno 1141eaf0ac3Slogwangas described in 1151eaf0ac3Slogwang.Xr flock 2 1161eaf0ac3Slogwangand 1171eaf0ac3Slogwang.Xr open 2 . 1181eaf0ac3Slogwang.Sh SEE ALSO 1191eaf0ac3Slogwang.Xr errno 2 , 1201eaf0ac3Slogwang.Xr flock 2 , 1211eaf0ac3Slogwang.Xr open 2 1221eaf0ac3Slogwang.Sh AUTHORS 1231eaf0ac3Slogwang.An -nosplit 1241eaf0ac3SlogwangThe 1251eaf0ac3Slogwang.Nm 1261eaf0ac3Slogwangfunction and this manual page were written by 1271eaf0ac3Slogwang.An Dag-Erling Sm\(/orgrav Aq Mt [email protected] . 128