| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: LGPL-2.1-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <common/error.hpp> |
| 10 | #include <common/lockfile.hpp> |
| 11 | #include <common/macros.hpp> |
| 12 | |
| 13 | #include <fcntl.h> |
| 14 | |
| 15 | #ifdef HAVE_FLOCK |
| 16 | |
| 17 | #include <sys/file.h> |
| 18 | |
| 19 | static int lock_file(const char *filepath, int fd) |
| 20 | { |
| 21 | int ret; |
| 22 | |
| 23 | /* |
| 24 | * Attempt to lock the file. If this fails, there is |
| 25 | * already a process using the same lock file running |
| 26 | * and we should exit. |
| 27 | */ |
| 28 | ret = flock(fd, LOCK_EX | LOCK_NB); |
| 29 | if (ret == -1) { |
| 30 | /* EWOULDBLOCK are expected if the file is locked: don't spam the logs. */ |
| 31 | if (errno != EWOULDBLOCK) { |
| 32 | PERROR("Failed to apply lock on lock file: file_path=`%s`", filepath); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | #else /* HAVE_FLOCK */ |
| 40 | |
| 41 | static int lock_file(const char *filepath, int fd) |
| 42 | { |
| 43 | int ret; |
| 44 | struct flock lock = {}; |
| 45 | |
| 46 | lock.l_whence = SEEK_SET; |
| 47 | lock.l_type = F_WRLCK; |
| 48 | |
| 49 | /* |
| 50 | * Attempt to lock the file. If this fails, there is |
| 51 | * already a process using the same lock file running |
| 52 | * and we should exit. |
| 53 | */ |
| 54 | ret = fcntl(fd, F_SETLK, &lock); |
| 55 | if (ret == -1) { |
| 56 | /* EAGAIN and EACCESS are expected if the file is locked: don't spam the logs. */ |
| 57 | if (errno != EAGAIN && errno != EACCES) { |
| 58 | PERROR("Failed to set lock on lock file: file_path=`%s`", filepath); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | #endif /* HAVE_FLOCK */ |
| 66 | |
| 67 | int utils_create_lock_file(const char *filepath) |
| 68 | { |
| 69 | int ret, fd; |
| 70 | |
| 71 | LTTNG_ASSERT(filepath); |
| 72 | |
| 73 | fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 74 | if (fd < 0) { |
| 75 | PERROR("Failed to open lock file `%s`", filepath); |
| 76 | fd = -1; |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Attempt to lock the file. If this fails, there is already a process using the same lock |
| 82 | * file running and we should exit. |
| 83 | * |
| 84 | * lock_file is chosen based on the build configuration, see implementations above. |
| 85 | */ |
| 86 | ret = lock_file(filepath, fd); |
| 87 | if (ret == -1) { |
| 88 | ERR("Could not get lock file `%s`, another instance is running.", filepath); |
| 89 | |
| 90 | if (close(fd)) { |
| 91 | PERROR("Failed to close lock file fd: fd=%d", fd); |
| 92 | } |
| 93 | |
| 94 | fd = ret; |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | DBG_FMT("Acquired lock file: file_path={}", filepath); |
| 99 | |
| 100 | error: |
| 101 | return fd; |
| 102 | } |