#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>
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 \
--- /dev/null
+/*
+ * 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 */
--- /dev/null
+/*
+ * 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 */
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.
*
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);