From 77e7fddf47ca285c4d4d161745ddaf2bca6b46b2 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Wed, 14 Oct 2015 16:11:48 -0400 Subject: [PATCH] Port: Replace flock with fnctl MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- src/common/utils.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/common/utils.c b/src/common/utils.c index ca4eb4448..52fb25dbe 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -504,11 +504,13 @@ int utils_create_lock_file(const char *filepath) { int ret; int fd; + struct flock lock; assert(filepath); - fd = open(filepath, O_CREAT, - O_WRONLY | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + memset(&lock, 0, sizeof(lock)); + fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | + S_IRGRP | S_IWGRP); if (fd < 0) { PERROR("open lock file %s", filepath); ret = -1; @@ -520,8 +522,12 @@ int utils_create_lock_file(const char *filepath) * already a process using the same lock file running * and we should exit. */ - ret = flock(fd, LOCK_EX | LOCK_NB); - if (ret) { + lock.l_whence = SEEK_SET; + lock.l_type = F_WRLCK; + + ret = fcntl(fd, F_SETLK, &lock); + if (ret == -1) { + PERROR("fcntl lock file"); ERR("Could not get lock file %s, another instance is running.", filepath); if (close(fd)) { -- 2.34.1