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-Tag: v2.13.12~6 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=f9431861198d04bfa0e14cbe65360412de67f40a;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.c with more platform-specific code. Signed-off-by: Jérémie Galarneau Change-Id: Ibd566d8710380fe378a8f3df9454e21e83655b62 --- diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index eddce5c54..155bdcd88 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/Makefile.am b/src/common/Makefile.am index df6a5b63b..6212d48cb 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -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 index 000000000..a34838c55 --- /dev/null +++ b/src/common/lockfile.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2012 David Goulet + * Copyright (C) 2013 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#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; + + 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 index 000000000..1f8e01ddb --- /dev/null +++ b/src/common/lockfile.h @@ -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.c b/src/common/utils.c index bc4f6112f..eb1bbbb28 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -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. * diff --git a/src/common/utils.h b/src/common/utils.h index e09c0a9ae..dc170e3de 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -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);