common: move utils_create_lock_file to its own file
[lttng-tools.git] / src / common / lockfile.cpp
diff --git a/src/common/lockfile.cpp b/src/common/lockfile.cpp
new file mode 100644 (file)
index 0000000..5dce37d
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
+ * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include <common/error.hpp>
+#include <common/lockfile.hpp>
+#include <common/macros.hpp>
+
+#ifdef HAVE_FLOCK
+
+#else /* HAVE_FLOCK */
+
+#include <fcntl.h>
+
+int utils_create_lock_file(const char *filepath)
+{
+       int ret;
+       int fd;
+       struct flock lock;
+
+       LTTNG_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);
+               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.
+        */
+       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)) {
+                       PERROR("close lock file");
+               }
+               fd = ret;
+               goto error;
+       }
+
+error:
+       return fd;
+}
+
+#endif /* HAVE_FLOCK */
\ No newline at end of file
This page took 0.023865 seconds and 4 git commands to generate.