From: Jérémie Galarneau Date: Tue, 12 Dec 2023 21:13:59 +0000 (-0500) Subject: common: move utils_create_lock_file to its own file X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=513792873b851baef066648b41f9cbfe978063fc;p=lttng-tools.git common: move utils_create_lock_file to its own file 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 Change-Id: Ibd566d8710380fe378a8f3df9454e21e83655b62 --- diff --git a/src/bin/lttng-sessiond/main.cpp b/src/bin/lttng-sessiond/main.cpp index 7f0995a9b..3e5d8eeea 100644 --- a/src/bin/lttng-sessiond/main.cpp +++ b/src/bin/lttng-sessiond/main.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 344787347..34fd83452 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -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 index 000000000..5dce37d6f --- /dev/null +++ b/src/common/lockfile.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2012 David Goulet + * Copyright (C) 2013 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#include +#include + +#ifdef HAVE_FLOCK + +#else /* HAVE_FLOCK */ + +#include + +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 index 000000000..1f8e01ddb --- /dev/null +++ b/src/common/lockfile.hpp @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 Jérémie Galarneau + * + * 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 */ diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 7cd61c049..62c9c22d5 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -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. * diff --git a/src/common/utils.hpp b/src/common/utils.hpp index b190a9b28..850c29ba8 100644 --- a/src/common/utils.hpp +++ b/src/common/utils.hpp @@ -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);