X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fcommon%2Flockfile.c;fp=src%2Fcommon%2Flockfile.c;h=bf1ddf9e341c9f18ea3e08f4552f5fcd5c104357;hb=cea28ac8f3e9eb154a405efb645ed907ac086978;hp=a34838c553f13f795b8127d3d83947c95900b9be;hpb=f9431861198d04bfa0e14cbe65360412de67f40a;p=lttng-tools.git diff --git a/src/common/lockfile.c b/src/common/lockfile.c index a34838c55..bf1ddf9e3 100644 --- a/src/common/lockfile.c +++ b/src/common/lockfile.c @@ -12,49 +12,93 @@ #include +#include + #ifdef HAVE_FLOCK +#include + +static int lock_file(const char *filepath, int fd) +{ + int ret; + + /* + * Attempt to lock the file. If this fails, there is + * already a process using the same lock file running + * and we should exit. + */ + ret = flock(fd, LOCK_EX | LOCK_NB); + if (ret == -1) { + /* EWOULDBLOCK are expected if the file is locked: don't spam the logs. */ + if (errno != EWOULDBLOCK) { + PERROR("Failed to apply lock on lock file: file_path=`%s`", filepath); + } + } + + return ret; +} + #else /* HAVE_FLOCK */ -#include +static int lock_file(const char *filepath, int fd) +{ + int ret; + struct flock lock = {}; + + lock.l_whence = SEEK_SET; + lock.l_type = F_WRLCK; + + /* + * Attempt to lock the file. If this fails, there is + * already a process using the same lock file running + * and we should exit. + */ + ret = fcntl(fd, F_SETLK, &lock); + if (ret == -1) { + /* EAGAIN and EACCESS are expected if the file is locked: don't spam the logs. */ + if (errno != EAGAIN && errno != EACCES) { + PERROR("Failed to set lock on lock file: file_path=`%s`", filepath); + } + } + + return ret; +} + +#endif /* HAVE_FLOCK */ int utils_create_lock_file(const char *filepath) { - int ret; - int fd; - struct flock lock; + int ret, fd; assert(filepath); - 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); + PERROR("Failed to open lock file `%s`", filepath); fd = -1; goto error; } /* - * Attempt to lock the file. If this fails, there is - * already a process using the same lock file running - * and we should exit. + * Attempt to lock the file. If this fails, there is already a process using the same lock + * file running and we should exit. + * + * lock_file is chosen based on the build configuration, see implementations above. */ - lock.l_whence = SEEK_SET; - lock.l_type = F_WRLCK; - - ret = fcntl(fd, F_SETLK, &lock); + ret = lock_file(filepath, fd); if (ret == -1) { - PERROR("fcntl lock file"); - ERR("Could not get lock file %s, another instance is running.", filepath); + ERR("Could not get lock file `%s`, another instance is running.", filepath); + if (close(fd)) { - PERROR("close lock file"); + PERROR("Failed to close lock file fd: fd=%d", fd); } + fd = ret; goto error; } + DBG("Acquired lock file: file_path=`%s`", filepath); + error: return fd; } - -#endif /* HAVE_FLOCK */