-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockfile.c
62 lines (49 loc) · 1.32 KB
/
lockfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "lockfile.h"
#include "messages.h"
#include "mfst.h"
static int lockfile_fd = -1;
int open_lockfile(char *filename) {
int local_errno;
if((lockfile_fd = open(program_options.lock_file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
local_errno = errno;
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_OPEN_ERROR, strerror(errno));
return local_errno;
}
return 0;
}
int is_lockfile_locked() {
int retval = lockf(lockfile_fd, F_TEST, 0);
return (retval == -1 && (errno == EACCES || errno == EAGAIN));
}
int lock_lockfile() {
int local_errno;
if(lockf(lockfile_fd, F_TLOCK, 0) == -1) {
local_errno = errno;
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LOCKF_ERROR, strerror(errno));
errno = local_errno;
return -1;
}
return 0;
}
int unlock_lockfile() {
int local_errno;
if(lockf(lockfile_fd, F_ULOCK, 0) == -1) {
local_errno = errno;
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LOCKF_ERROR, strerror(errno));
errno = local_errno;
return -1;
}
return 0;
}
void close_lockfile() {
if(lockfile_fd != -1) {
close(lockfile_fd);
lockfile_fd = -1;
}
}