Commit | Line | Data |
---|---|---|
51379287 JG |
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 | ||
e560c525 JG |
13 | #include <fcntl.h> |
14 | ||
51379287 JG |
15 | #ifdef HAVE_FLOCK |
16 | ||
e560c525 JG |
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 | ||
51379287 JG |
39 | #else /* HAVE_FLOCK */ |
40 | ||
e560c525 JG |
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 */ | |
51379287 JG |
66 | |
67 | int utils_create_lock_file(const char *filepath) | |
68 | { | |
e560c525 | 69 | int ret, fd; |
51379287 JG |
70 | |
71 | LTTNG_ASSERT(filepath); | |
72 | ||
51379287 JG |
73 | fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
74 | if (fd < 0) { | |
e560c525 | 75 | PERROR("Failed to open lock file `%s`", filepath); |
51379287 JG |
76 | fd = -1; |
77 | goto error; | |
78 | } | |
79 | ||
80 | /* | |
e560c525 JG |
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. | |
51379287 | 85 | */ |
e560c525 | 86 | ret = lock_file(filepath, fd); |
51379287 | 87 | if (ret == -1) { |
e560c525 JG |
88 | ERR("Could not get lock file `%s`, another instance is running.", filepath); |
89 | ||
51379287 | 90 | if (close(fd)) { |
e560c525 | 91 | PERROR("Failed to close lock file fd: fd=%d", fd); |
51379287 | 92 | } |
e560c525 | 93 | |
51379287 JG |
94 | fd = ret; |
95 | goto error; | |
96 | } | |
97 | ||
e560c525 JG |
98 | DBG_FMT("Acquired lock file: file_path={}", filepath); |
99 | ||
51379287 JG |
100 | error: |
101 | return fd; | |
e560c525 | 102 | } |