1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <errno.h>
4 #include <unsupported_api.h>
5
lockf(int fd,int op,off_t size)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
14 UNSUPPORTED_API_VOID(LITEOS_A);
15 switch (op) {
16 case F_TEST:
17 l.l_type = F_RDLCK;
18 if (fcntl(fd, F_GETLK, &l) < 0)
19 return -1;
20 if (l.l_type == F_UNLCK || l.l_pid == getpid())
21 return 0;
22 errno = EACCES;
23 return -1;
24 case F_ULOCK:
25 l.l_type = F_UNLCK;
26 case F_TLOCK:
27 return fcntl(fd, F_SETLK, &l);
28 case F_LOCK:
29 return fcntl(fd, F_SETLKW, &l);
30 }
31 errno = EINVAL;
32 return -1;
33 }
34
35 weak_alias(lockf, lockf64);
36