bf1ddf9e341c9f18ea3e08f4552f5fcd5c104357
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.h>
10 #include <common/lockfile.h>
11 #include <common/macros.h>
21 static int lock_file(const char *filepath
, int fd
)
26 * Attempt to lock the file. If this fails, there is
27 * already a process using the same lock file running
30 ret
= flock(fd
, LOCK_EX
| LOCK_NB
);
32 /* EWOULDBLOCK are expected if the file is locked: don't spam the logs. */
33 if (errno
!= EWOULDBLOCK
) {
34 PERROR("Failed to apply lock on lock file: file_path=`%s`", filepath
);
41 #else /* HAVE_FLOCK */
43 static int lock_file(const char *filepath
, int fd
)
46 struct flock lock
= {};
48 lock
.l_whence
= SEEK_SET
;
49 lock
.l_type
= F_WRLCK
;
52 * Attempt to lock the file. If this fails, there is
53 * already a process using the same lock file running
56 ret
= fcntl(fd
, F_SETLK
, &lock
);
58 /* EAGAIN and EACCESS are expected if the file is locked: don't spam the logs. */
59 if (errno
!= EAGAIN
&& errno
!= EACCES
) {
60 PERROR("Failed to set lock on lock file: file_path=`%s`", filepath
);
67 #endif /* HAVE_FLOCK */
69 int utils_create_lock_file(const char *filepath
)
75 fd
= open(filepath
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
77 PERROR("Failed to open lock file `%s`", filepath
);
83 * Attempt to lock the file. If this fails, there is already a process using the same lock
84 * file running and we should exit.
86 * lock_file is chosen based on the build configuration, see implementations above.
88 ret
= lock_file(filepath
, fd
);
90 ERR("Could not get lock file `%s`, another instance is running.", filepath
);
93 PERROR("Failed to close lock file fd: fd=%d", fd
);
100 DBG("Acquired lock file: file_path=`%s`", filepath
);
This page took 0.032095 seconds and 4 git commands to generate.