Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <errno.h> |
| 4 | #include "libc.h" |
| 5 | |
| 6 | int lockf(int fd, int op, off_t size) |
| 7 | { |
| 8 | struct flock l = { |
| 9 | .l_type = F_WRLCK, |
| 10 | .l_whence = SEEK_CUR, |
| 11 | .l_len = size, |
| 12 | }; |
| 13 | switch (op) { |
| 14 | case F_TEST: |
| 15 | l.l_type = F_RDLCK; |
| 16 | if (fcntl(fd, F_GETLK, &l) < 0) |
| 17 | return -1; |
| 18 | if (l.l_type == F_UNLCK || l.l_pid == getpid()) |
| 19 | return 0; |
| 20 | errno = EACCES; |
| 21 | return -1; |
| 22 | case F_ULOCK: |
| 23 | l.l_type = F_UNLCK; |
| 24 | case F_TLOCK: |
| 25 | return fcntl(fd, F_SETLK, &l); |
| 26 | case F_LOCK: |
| 27 | return fcntl(fd, F_SETLKW, &l); |
| 28 | } |
| 29 | errno = EINVAL; |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | LFS64(lockf); |