• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <errno.h>
4 
lockf(int fd,int op,off_t size)5 int lockf(int fd, int op, off_t size)
6 {
7 	struct flock l = {
8 		.l_type = F_WRLCK,
9 		.l_whence = SEEK_CUR,
10 		.l_len = size,
11 	};
12 	switch (op) {
13 	case F_TEST:
14 		l.l_type = F_RDLCK;
15 		if (fcntl(fd, F_GETLK, &l) < 0)
16 			return -1;
17 		if (l.l_type == F_UNLCK || l.l_pid == getpid())
18 			return 0;
19 		errno = EACCES;
20 		return -1;
21 	case F_ULOCK:
22 		l.l_type = F_UNLCK;
23 	case F_TLOCK:
24 		return fcntl(fd, F_SETLK, &l);
25 	case F_LOCK:
26 		return fcntl(fd, F_SETLKW, &l);
27 	}
28 	errno = EINVAL;
29 	return -1;
30 }
31 
32 weak_alias(lockf, lockf64);
33