#define _GNU_SOURCE #include #include #include #include #include static const char *lock_names[] = { [F_UNLCK] = "unlocked", [F_RDLCK] = "read lock in place", [F_WRLCK] = "write lock in place", }; int main(void) { int fd = open("foo", O_RDWR); struct flock fl = { .l_type = F_WRLCK }; int ret = fcntl(fd, F_OFD_GETLK, &fl); printf("get lock: %i, %s; %s\n", ret, strerror(errno), lock_names[fl.l_type]); ret = fcntl(fd, F_OFD_SETLK, &(struct flock){ .l_type = F_WRLCK }); printf("set wr lock: %i, %s\n", ret, strerror(errno)); close(fd); return 0; }