-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_file.c
121 lines (101 loc) · 2.05 KB
/
lock_file.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <role/glob.h>
static int get_pid(char *buf, pid_t *pid)
{
if (sscanf(buf, "%u", pid) < 1)
return LIBROLE_IO_ERROR;
return LIBROLE_OK;
}
static int check_link_count(const char *file)
{
struct stat sb;
if (stat (file, &sb) != 0) {
return LIBROLE_UNKNOWN_ERROR;
}
if (sb.st_nlink != 2) {
return LIBROLE_UNKNOWN_ERROR;
}
return LIBROLE_OK;
}
static int do_lock(const char *file, const char *lock)
{
int fd;
pid_t pid;
ssize_t len;
int result;
char buf[32];
fd = open(file, O_CREAT | O_EXCL | O_WRONLY, 0600);
if (-1 == fd) {
return 0;
}
pid = getpid();
snprintf(buf, sizeof buf, "%lu", (unsigned long) pid);
len = (ssize_t) strlen (buf) + 1;
if (write(fd, buf, len) != len) {
close (fd);
unlink(file);
return ;
}
close (fd);
if (link(file, lock) == 0) {
result = check_link_count (file);
unlink(file);
return result;
}
fd = open(lock, O_RDWR);
if (fd == -1) {
unlink(file);
errno = EINVAL;
return LIBROLE_IO_ERROR;
}
len = read(fd, buf, sizeof (buf) - 1);
close(fd);
if (len <= 0) {
unlink(file);
errno = EINVAL;
return LIBROLE_IO_ERROR;
}
buf[len] = '\0';
if ((result = get_pid(buf, &pid)) != LIBROLE_OK) {
unlink(file);
errno = EINVAL;
return result;
}
if (kill(pid, 0) == 0) {
unlink(file);
errno = EEXIST;
return LIBROLE_UNKNOWN_ERROR;
}
if (unlink(lock) != 0) {
unlink(file);
return LIBROLE_IO_ERROR;
}
result = 0;
if ((link(file, lock) == 0) && (check_link_count(file) == LIBROLE_OK))
result = LIBROLE_OK;
unlink(file);
return result;
}
int librole_lock(const char *file)
{
char lock[1024];
if (snprintf(lock, sizeof lock, "%s.lock", file) < 1)
return LIBROLE_UNKNOWN_ERROR;
return do_lock(file, lock);
}
int librole_unlock(const char *file)
{
char lock[1024];
if (snprintf(lock, sizeof lock, "%s.lock", file) < 1)
return LIBROLE_UNKNOWN_ERROR;
unlink(lock);
return LIBROLE_OK;
}