According to Daniel Berrange, fcntl() locks have better portable semantics than lockf().
Use an exclusive lock on the first byte with fcntl(). Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> --- util/oslib-posix.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index da1d4a3201..26b11490b9 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -92,6 +92,11 @@ bool qemu_write_pidfile(const char *pidfile, Error **errp) { int pidfd; char pidstr[32]; + struct flock lock = { + .l_type = F_WRLCK, + .l_whence = SEEK_SET, + .l_len = 1, + }; pidfd = qemu_open(pidfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); if (pidfd == -1) { @@ -99,10 +104,11 @@ bool qemu_write_pidfile(const char *pidfile, Error **errp) return false; } - if (lockf(pidfd, F_TLOCK, 0)) { + if (fcntl(pidfd, F_SETLK, &lock)) { error_setg_errno(errp, errno, "Cannot lock pid file"); goto fail; } + if (ftruncate(pidfd, 0)) { error_setg_errno(errp, errno, "Failed to truncate pid file"); goto fail; -- 2.18.0.129.ge3331758f1