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, 14 Mar 2024 18:54:21 +0000 (14:54 -0400)
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.c with more
platform-specific code.

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

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

index eddce5c5482e69fbd40f24191426f8137367b09e..155bdcd884c8833e63a369689cceed3fba50eaa1 100644 (file)
@@ -37,6 +37,7 @@
 #include <common/futex.h>
 #include <common/relayd/relayd.h>
 #include <common/utils.h>
+#include <common/lockfile.h>
 #include <common/path.h>
 #include <common/daemonize.h>
 #include <common/config/session-config.h>
index df6a5b63b1cef86c53dd18f6c861ce6604a32edd..6212d48cb1bfc47dcbb0c4619438769a6637693d 100644 (file)
@@ -84,6 +84,7 @@ libcommon_lgpl_la_SOURCES = \
        fd-handle.c fd-handle.h \
        kernel-probe.c \
        location.c \
+       lockfile.c lockfile.h \
        log-level-rule.c \
        mi-lttng.c mi-lttng.h \
        notification.c \
diff --git a/src/common/lockfile.c b/src/common/lockfile.c
new file mode 100644 (file)
index 0000000..a34838c
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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.h>
+#include <common/lockfile.h>
+#include <common/macros.h>
+
+#include <assert.h>
+
+#ifdef HAVE_FLOCK
+
+#else /* HAVE_FLOCK */
+
+#include <fcntl.h>
+
+int utils_create_lock_file(const char *filepath)
+{
+       int ret;
+       int fd;
+       struct flock lock;
+
+       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 */
diff --git a/src/common/lockfile.h b/src/common/lockfile.h
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 bc4f6112f9db4907e18dd9758f2a39105ca0a138..eb1bbbb28aa1d07945c90cd82ddca63d17841216 100644 (file)
@@ -264,52 +264,6 @@ error:
        return ret;
 }
 
-/*
- * Create lock file to the given path and filename.
- * Returns the associated file descriptor, -1 on error.
- */
-LTTNG_HIDDEN
-int utils_create_lock_file(const char *filepath)
-{
-       int ret;
-       int fd;
-       struct flock lock;
-
-       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 e09c0a9ae556f8ed3dc3a6d6a8a70f68cb69b5a9..dc170e3de50c8e84b7ce191f2a22f8884ad55231 100644 (file)
@@ -43,7 +43,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.030273 seconds and 4 git commands to generate.