common: move utils_create_lock_file to its own file
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 12 Dec 2023 21:13:59 +0000 (16:13 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 21 Dec 2023 16:31:07 +0000 (11:31 -0500)
A follow-up change introduces platform-specific implementations of this
functions. Moving the function to a separate file makes it possible to
add other implementations without polluting utils.cpp with more
platform-specific code.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: Ibd566d8710380fe378a8f3df9454e21e83655b62

src/bin/lttng-sessiond/main.cpp
src/common/Makefile.am
src/common/lockfile.cpp [new file with mode: 0644]
src/common/lockfile.hpp [new file with mode: 0644]
src/common/utils.cpp
src/common/utils.hpp

index 7f0995a9b0e8388bad9ed4617404825fcf5a06e0..3e5d8eeeaca01791c742ca2d4c58fa3f733c14fb 100644 (file)
@@ -52,6 +52,7 @@
 #include <common/futex.hpp>
 #include <common/ini-config/ini-config.hpp>
 #include <common/kernel-consumer/kernel-consumer.hpp>
+#include <common/lockfile.hpp>
 #include <common/logging-utils.hpp>
 #include <common/path.hpp>
 #include <common/relayd/relayd.hpp>
index 34478734763012c3be58dc7629e5fc3911b762bf..34fd83452802f5b8caa21a347ce4264e693f1e88 100644 (file)
@@ -93,6 +93,7 @@ libcommon_lgpl_la_SOURCES = \
        io-hint.hpp \
        kernel-probe.cpp \
        location.cpp \
+       lockfile.cpp lockfile.hpp \
        locked-reference.hpp \
        logging-utils.hpp logging-utils.cpp \
        log-level-rule.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
diff --git a/src/common/lockfile.hpp b/src/common/lockfile.hpp
new file mode 100644 (file)
index 0000000..1f8e01d
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef COMMON_LOCKFILE_H
+#define COMMON_LOCKFILE_H
+
+/*
+ * Create lock file to the given path and filename.
+ * Returns the associated file descriptor, -1 on error.
+ *
+ * Note that on systems that don't support flock, POSIX file locks are used.
+ * As such, the file lock is dropped whenever any of the file descriptors
+ * associated to the file's description is closed.
+ *
+ * For instance, the lock file is dropped if the process forks+exits or
+ * forks+execve as the child process closes a file descriptor referencing
+ * the file description of 'filepath'.
+ */
+int utils_create_lock_file(const char *filepath);
+
+#endif /* COMMON_LOCKFILE_H */
index 7cd61c049c3ffeae0b7bdcf74dc5a0eb9a6c58da..62c9c22d52686e442edad15ce3a4a17a8edb0a75 100644 (file)
@@ -254,49 +254,6 @@ error:
        return ret;
 }
 
-/*
- * Create lock file to the given path and filename.
- * Returns the associated file descriptor, -1 on error.
- */
-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;
-}
-
 /*
  * Create directory using the given path and mode.
  *
index b190a9b2852c52c352e75ac337818ba7e5e78f76..850c29ba88bf9ca3fe96b071861a7d67847c1b73 100644 (file)
@@ -50,7 +50,6 @@ size_t utils_get_current_time_str(const char *format, char *dst, size_t len)
 
 int utils_get_group_id(const char *name, bool warn, gid_t *gid);
 char *utils_generate_optstring(const struct option *long_options, size_t opt_count);
-int utils_create_lock_file(const char *filepath);
 int utils_recursive_rmdir(const char *path);
 int utils_truncate_stream_file(int fd, off_t length);
 int utils_show_help(int section, const char *page_name, const char *help_msg);
This page took 0.028018 seconds and 4 git commands to generate.