LOCKF(3)                   POSIX Programmer's Manual                  LOCKF(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       lockf -- record locking on files

SSYYNNOOPPSSIISS
       #include <unistd.h>

       int lockf(int _f_i_l_d_e_s, int _f_u_n_c_t_i_o_n, off_t _s_i_z_e);

DDEESSCCRRIIPPTTIIOONN
       The _l_o_c_k_f() function shall lock sections of a file with advisory-mode
       locks. Calls to _l_o_c_k_f() from threads in other processes which attempt
       to lock the locked file section shall either return an error value or
       block until the section becomes unlocked. All the locks for a process
       are removed when the process terminates. Record locking with _l_o_c_k_f()
       shall be supported for regular files and may be supported for other
       files.

       The _f_i_l_d_e_s argument is an open file descriptor. To establish a lock
       with this function, the file descriptor shall be opened with write-only
       permission (O_WRONLY) or with read/write permission (O_RDWR).

       The _f_u_n_c_t_i_o_n argument is a control value which specifies the action to
       be taken. The permissible values for _f_u_n_c_t_i_o_n are defined in _<_u_n_i_s_t_d_._h_>
       as follows:

              +---------+----------------------------------------------+
              |FFuunnccttiioonn |                 DDeessccrriippttiioonn                  |
              +---------+----------------------------------------------+
              |F_ULOCK  | Unlock locked sections.                      |
              |F_LOCK   | Lock a section for exclusive use.            |
              |F_TLOCK  | Test and lock a section for exclusive use.   |
              |F_TEST   | Test a section for locks by other processes. |
              +---------+----------------------------------------------+
       F_TEST shall detect if a lock by another process is present on the
       specified section.

       F_LOCK and F_TLOCK shall both lock a section of a file if the section
       is available.

       F_ULOCK shall remove locks from a section of the file.

       The _s_i_z_e argument is the number of contiguous bytes to be locked or
       unlocked.  The section to be locked or unlocked starts at the current
       offset in the file and extends forward for a positive size or backward
       for a negative size (the preceding bytes up to but not including the
       current offset). If _s_i_z_e is 0, the section from the current offset
       through the largest possible file offset shall be locked (that is, from
       the current offset through the present or any future end-of-file). An
       area need not be allocated to the file to be locked because locks may
       exist past the end-of-file.

       The sections locked with F_LOCK or F_TLOCK may, in whole or in part,
       contain or be contained by a previously locked section for the same
       process. When this occurs, or if adjacent locked sections would occur,
       the sections shall be combined into a single locked section. If the
       request would cause the number of locks to exceed a system-imposed
       limit, the request shall fail.

       F_LOCK and F_TLOCK requests differ only by the action taken if the
       section is not available. F_LOCK shall block the calling thread until
       the section is available. F_TLOCK shall cause the function to fail if
       the section is already locked by another process.

       File locks shall be released on first close by the locking process of
       any file descriptor for the file.

       F_ULOCK requests may release (wholly or in part) one or more locked
       sections controlled by the process. Locked sections shall be unlocked
       starting at the current file offset through _s_i_z_e bytes or to the end-
       of-file if _s_i_z_e is (ooffff__tt)0. When all of a locked section is not
       released (that is, when the beginning or end of the area to be unlocked
       falls within a locked section), the remaining portions of that section
       shall remain locked by the process. Releasing the center portion of a
       locked section shall cause the remaining locked beginning and end
       portions to become two separate locked sections. If the request would
       cause the number of locks in the system to exceed a system-imposed
       limit, the request shall fail.

       A potential for deadlock occurs if the threads of a process controlling
       a locked section are blocked by accessing a locked section of another
       process. If the system detects that deadlock would occur, _l_o_c_k_f() shall
       fail with an [[EEDDEEAADDLLKK]] error.

       The interaction between _f_c_n_t_l() and _l_o_c_k_f() locks is unspecified.

       Blocking on a section shall be interrupted by any signal.

       An F_ULOCK request in which _s_i_z_e is non-zero and the offset of the last
       byte of the requested section is the maximum value for an object of
       type ooffff__tt, when the process has an existing lock in which _s_i_z_e is 0
       and which includes the last byte of the requested section, shall be
       treated as a request to unlock from the start of the requested section
       with a size equal to 0. Otherwise, an F_ULOCK request shall attempt to
       unlock only the requested section.

       Attempting to lock a section of a file that is associated with a
       buffered stream produces unspecified results.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _l_o_c_k_f() shall return 0. Otherwise, it shall
       return -1, set _e_r_r_n_o to indicate an error, and existing locks shall not
       be changed.

EERRRROORRSS
       The _l_o_c_k_f() function shall fail if:

       EEBBAADDFF  The _f_i_l_d_e_s argument is not a valid open file descriptor; or
              _f_u_n_c_t_i_o_n is F_LOCK or F_TLOCK and _f_i_l_d_e_s is not a valid file
              descriptor open for writing.

       EEAACCCCEESS or EEAAGGAAIINN
              The _f_u_n_c_t_i_o_n argument is F_TLOCK or F_TEST and the section is
              already locked by another process.

       EEDDEEAADDLLKK
              The _f_u_n_c_t_i_o_n argument is F_LOCK and a deadlock is detected.

       EEIINNTTRR  A signal was caught during execution of the function.

       EEIINNVVAALL The _f_u_n_c_t_i_o_n argument is not one of F_LOCK, F_TLOCK, F_TEST, or
              F_ULOCK; or _s_i_z_e plus the current file offset is less than 0.

       EEOOVVEERRFFLLOOWW
              The offset of the first, or if _s_i_z_e is not 0 then the last, byte
              in the requested section cannot be represented correctly in an
              object of type ooffff__tt.

       The _l_o_c_k_f() function may fail if:

       EEAAGGAAIINN The _f_u_n_c_t_i_o_n argument is F_LOCK or F_TLOCK and the file is
              mapped with _m_m_a_p().

       EEDDEEAADDLLKK or EENNOOLLCCKK
              The _f_u_n_c_t_i_o_n argument is F_LOCK, F_TLOCK, or F_ULOCK, and the
              request would cause the number of locks to exceed a system-
              imposed limit.

       EEOOPPNNOOTTSSUUPPPP or EEIINNVVAALL
              The implementation does not support the locking of files of the
              type indicated by the _f_i_l_d_e_s argument.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   LLoocckkiinngg aa PPoorrttiioonn ooff aa FFiillee
       In the following example, a file named //hhoommee//ccnndd//mmoodd11 is being
       modified. Other processes that use locking are prevented from changing
       it during this process. Only the first 10000 bytes are locked, and the
       lock call fails if another process has any part of this area locked
       already.

           ##iinncclluuddee <<ffccnnttll..hh>>
           ##iinncclluuddee <<uunniissttdd..hh>>

           int fildes;
           int status;
           ...
           fildes = open("/home/cnd/mod1", O_RDWR);
           status = lockf(fildes, F_TLOCK, (off_t)10000);

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       Record-locking should not be used in combination with the _f_o_p_e_n(),
       _f_r_e_a_d(), _f_w_r_i_t_e(), and other _s_t_d_i_o functions. Instead, the more
       primitive, non-buffered functions (such as _o_p_e_n()) should be used.
       Unexpected results may occur in processes that do buffering in the user
       address space. The process may later read/write data which is/was
       locked. The _s_t_d_i_o functions are the most common source of unexpected
       buffering.

       The _a_l_a_r_m() function may be used to provide a timeout facility in
       applications requiring it.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _a_l_a_r_m(), _c_h_m_o_d(), _c_l_o_s_e(), _c_r_e_a_t(), _f_c_n_t_l(), _f_o_p_e_n(), _m_m_a_p(), _o_p_e_n(),
       _r_e_a_d(), _w_r_i_t_e()

       The Base Definitions volume of POSIX.1-2008, <<uunniissttdd..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                             LOCKF(3)
