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