diff -uNr qemu-3.1.0/lockf_implementation.h qemu-3.1.0.mod/lockf_implementation.h --- qemu-3.1.0/lockf_implementation.h 1970-01-01 03:00:00.000000000 +0300 +++ qemu-3.1.0.mod/lockf_implementation.h 2019-02-14 00:17:12.137667319 +0200 @@ -0,0 +1,56 @@ +#ifndef LOCKF_IMPLEMENTATION_H +#define LOCKF_IMPLEMENTATION_H + +// +// lockf() implementation from GNU Libc +// + +static int lockf (int fd, int cmd, off_t len) +{ + struct flock fl; + + memset ((char *) &fl, '\0', sizeof (fl)); + + /* lockf is always relative to the current file position. */ + fl.l_whence = SEEK_CUR; + fl.l_start = 0; + fl.l_len = len; + + switch (cmd) + { + case F_TEST: + /* Test the lock: return 0 if FD is unlocked or locked by this process; + return -1, set errno to EACCES, if another process holds the lock. */ + fl.l_type = F_RDLCK; + if (fcntl (fd, F_GETLK, &fl) < 0) + return -1; + if (fl.l_type == F_UNLCK || fl.l_pid == getpid ()) + return 0; + errno = EACCES; + return -1; + + case F_ULOCK: + fl.l_type = F_UNLCK; + cmd = F_SETLK; + break; + case F_LOCK: + fl.l_type = F_WRLCK; + cmd = F_SETLKW; + break; + case F_TLOCK: + fl.l_type = F_WRLCK; + cmd = F_SETLK; + break; + + default: + errno = EINVAL; + return -1; + } + + /* lockf() is a cancellation point but so is fcntl() if F_SETLKW is + used. Therefore we don't have to care about cancellation here, + the fcntl() function will take care of it. */ + return fcntl (fd, cmd, &fl); +} + +#endif diff -uNr qemu-3.1.0/os-posix.c qemu-3.1.0.mod/os-posix.c --- qemu-3.1.0/os-posix.c 2018-12-11 19:44:34.000000000 +0200 +++ qemu-3.1.0.mod/os-posix.c 2019-02-14 00:17:05.354295183 +0200 @@ -36,6 +36,7 @@ #include "qemu/error-report.h" #include "qemu/log.h" #include "qemu/cutils.h" +#include "lockf_implementation.h" #ifdef CONFIG_LINUX #include diff -uNr qemu-3.1.0/qga/main.c qemu-3.1.0.mod/qga/main.c --- qemu-3.1.0/qga/main.c 2018-12-11 19:44:34.000000000 +0200 +++ qemu-3.1.0.mod/qga/main.c 2019-02-14 00:17:18.731038388 +0200 @@ -45,6 +45,8 @@ #endif #endif +#include "lockf_implementation.h" + #ifndef _WIN32 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" #define QGA_STATE_RELATIVE_DIR "run" diff -uNr qemu-3.1.0/scsi/qemu-pr-helper.c qemu-3.1.0.mod/scsi/qemu-pr-helper.c --- qemu-3.1.0/scsi/qemu-pr-helper.c 2018-12-11 19:44:34.000000000 +0200 +++ qemu-3.1.0.mod/scsi/qemu-pr-helper.c 2019-02-14 00:17:24.634405524 +0200 @@ -24,6 +24,8 @@ #include #include +#include "lockf_implementation.h" + #ifdef CONFIG_LIBCAP #include #endif