blob: a4ebaa52d4a45ae3e7ef6f5de92fb1ef11fd365c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* ISC license. */
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <skalibs/error.h>
#include <skalibs/djbunix.h>
int fd_lock (int fd, int w, int nb)
{
struct flock fl =
{
.l_type = w ? F_WRLCK : F_RDLCK,
.l_whence = SEEK_SET,
.l_start = 0,
.l_len = 0
} ;
int e = errno ;
int r ;
do r = fcntl(fd, nb ? F_SETLK : F_SETLKW, &fl) ;
while (r == -1 && errno == EINTR) ;
return r != -1 ? 1 :
errno == EACCES || error_isagain(errno) ? (errno = e, 0) :
-1 ;
}
|