2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.hpp>
10 #include <common/lockfile.hpp>
11 #include <common/macros.hpp>
19 static int lock_file(const char *filepath
, int fd
)
24 * Attempt to lock the file. If this fails, there is
25 * already a process using the same lock file running
28 ret
= flock(fd
, LOCK_EX
| LOCK_NB
);
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
);
39 #else /* HAVE_FLOCK */
41 static int lock_file(const char *filepath
, int fd
)
44 struct flock lock
= {};
46 lock
.l_whence
= SEEK_SET
;
47 lock
.l_type
= F_WRLCK
;
50 * Attempt to lock the file. If this fails, there is
51 * already a process using the same lock file running
54 ret
= fcntl(fd
, F_SETLK
, &lock
);
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
);
65 #endif /* HAVE_FLOCK */
67 int utils_create_lock_file(const char *filepath
)
71 LTTNG_ASSERT(filepath
);
73 fd
= open(filepath
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
75 PERROR("Failed to open lock file `%s`", filepath
);
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.
84 * lock_file is chosen based on the build configuration, see implementations above.
86 ret
= lock_file(filepath
, fd
);
88 ERR("Could not get lock file `%s`, another instance is running.", filepath
);
91 PERROR("Failed to close lock file fd: fd=%d", fd
);
98 DBG_FMT("Acquired lock file: file_path={}", filepath
);
This page took 0.030396 seconds and 4 git commands to generate.