+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 */