United States-English |
|
|
HP-UX Reference > Llockf(2)HP-UX 11i Version 3: February 2007 |
|
NAMElockf — provide semaphores and record locking on files DESCRIPTIONThe lockf() function allows regions of a file to be used as semaphores (advisory locks) or restricts access to only the locking process (enforcement-mode record locks). Other processes that attempt to access the locked resource either return an error or sleep until the resource becomes unlocked. All locks for a process are released upon the first close of the file, even if the process still has the file opened, and all locks held by a process are released when the process terminates. fildes is an open file descriptor. The file descriptor must have been opened with write-only permission (O_WRONLY) or read-write permission (O_RDWR) in order to establish a lock with this function call (see open(2)). If the calling process is a member of a group that has the PRIV_LOCKRDONLY privilege (see getprivgrp(2)), it can also use lockf() to lock files opened with read-only permission (O_RDONLY). function is a control value that specifies the action to be taken. Permissible values for function are defined in <unistd.h> as follows: #define F_ULOCK 0 /* unlock a region */ #define F_LOCK 1 /* lock a region */ #define F_TLOCK 2 /* test and lock a region */ #define F_TEST 3 /* test region for lock */ All other values of function are reserved for future extensions and result in an error return if not implemented. F_TEST is used to detect whether a lock by another process is present on the specified region. lockf() returns zero if the region is accessible and -1 if it is not; in which case errno is set to EACCES. F_LOCK and F_TLOCK both lock a region of a file if the region is available. F_ULOCK removes locks from a region of the file. size is the number of contiguous bytes to be locked or unlocked. The resource to be locked starts at the current offset in the file, and extends forward for a positive size, and backward for a negative size (the preceding bytes up to but not including the current offset). If size is zero, the region from the current offset through the end of the largest possible file is 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 in order to be locked, because such locks can exist past the end of the file. Regions locked with F_LOCK or F_TLOCK can, in whole or in part, contain or be contained by a previously locked region for the same process. When this occurs or if adjacent regions occur, the regions are combined into a single region. If the request requires that a new element be added to the table of active locks but the table is already full, an error is returned, and the new region is not locked. F_LOCK and F_TLOCK requests differ only by the action taken if the resource is not available: F_LOCK causes the calling process to sleep until the resource is available, whereas F_TLOCK returns an EACCES error if the region is already locked by another process. F_ULOCK requests can, in whole or part, release one or more locked regions controlled by the process. When regions are not fully released, the remaining regions are still locked by the process. Releasing the center section of a locked region requires an additional element in the table of active locks. If this table is full, an EDEADLK error is returned, and the requested region is not released. Regular files with the file mode of S_ENFMT, not having the group execute bit set, will have an enforcement policy enabled. With enforcement enabled, reads and writes that would access a locked region sleep until the entire region is available if O_NDELAY is clear, but return -1 with errno set if O_NDELAY is set. File access by other system functions, such as exec(), are not subject to the enforcement policy. Locks on directories, pipes, and special files are advisory only; no enforcement policy is used. A potential for deadlock occurs if a process controlling a locked resource is put to sleep by accessing the locked resource of another process. Thus, calls to fcntl(), lockf(), read(), or write() (see fcntl(2), lockf(2), read(2), and write(2)) scan for a deadlock prior to sleeping on a locked resource. Deadlock is not checked for the wait() and pause() system calls (see wait(2) and pause(2)), so potential for deadlock is not eliminated. A creat() call or an open() call with the O_CREATE and O_TRUNC flags set on a regular file returns error EAGAIN if another process has locked part of the file and the file is currently in enforcement mode. NETWORKING FEATURESNFSThe advisory record-locking capabilities of lockf() are implemented throughout the network by the ``network lock daemon'' (see lockd(1M)). If the file server crashes and is rebooted, the lock daemon attempts to recover all locks associated with the crashed server. If a lock cannot be reclaimed, the process that held the lock is issued a SIGLOST signal. Only advisory record locking is implemented for NFS files. RETURN VALUEUpon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error. ERRORSlockf() fails if any of the following occur:
WARNINGSDeadlock conditions may arise when either the wait() or pause() system calls are used in conjunction with enforced locking (see wait(2) and pause(2) for details). When a file descriptor is closed, all locks on the file from the calling process are deleted, even if other file descriptors for that file (obtained through dup() or open(), for example) still exist. Unexpected results may occur in processes that use buffers in the user address space. The process may later read or write data which is or was locked. The standard I/O package, stdio(3S), is the most common source of unexpected buffering. In a hostile environment, locking can be misused by holding key public resources locked. This is particularly true with public read files that have enforcement enabled. It is not recommended that the PRIV_LOCKRDONLY capability be used because it is provided for backward compatibility only. This feature may be modified or dropped from future HP-UX releases. Locks default to advisory mode unless the setgid bit of the file permissions is set. Application UsageBecause in the future the variable errno will be set to EAGAIN rather than EACCES when a section of a file is already locked by another process, portable application programs should expect and test for either value. For example: if (lockf(fd, F_TLOCK, siz) == -1) if ((errno == EAGAIN) || (errno == EACCES)) /* * section locked by another process * check for either EAGAIN or EACCES * due to different implementations */ else if ... /* * check for other errors */ |
Printable version | ||
|