/
diminuto_lock.c
243 lines (198 loc) · 5.28 KB
/
diminuto_lock.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* vi: set ts=4 expandtab shiftwidth=4: */
/**
* @file
* @copyright Copyright 2008-2022 Digital Aggregates Corporation, Colorado, USA.
* @note Licensed under the terms in LICENSE.txt.
* @brief This is the implementation of the Lock feature.
* @author Chip Overclock <mailto:coverclock@diag.com>
* @see Diminuto <https://github.com/coverclock/com-diag-diminuto>
* @details
* This is the implementation of the Lock feature.
*
* See the unit tests unittest-lock.c, unittest-hangup-wan.c,
* unittest-hangup-too.c, unittest-hangup-tree.c, and unittest-hangup-fore.c
* for examples of patterns in how this feature might be used.
*/
#include "../src/diminuto_renameat2.h"
#include "../src/diminuto_lock.h"
#include "com/diag/diminuto/diminuto_lock.h"
#include "com/diag/diminuto/diminuto_log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <alloca.h>
int diminuto_lock_f(const char * file, const char * suffix, pid_t pid, unsigned int type)
{
int rc = -1;
int fd = -1;
FILE * fp = (FILE *)0;
char * path = (char *)0;
do {
path = (char *)alloca(strlen(file) + strlen(suffix) + 1 /* Include terminating NUL. */); /* No failure return. */
strcpy(path, file);
strcat(path, suffix);
if ((fd = mkstemp(path)) >= 0) {
/* Do nothing. */
} else if (errno == EEXIST) {
break;
} else {
diminuto_perror("diminuto_lock_f: mkstemp");
break;
}
if ((fp = fdopen(fd, "w")) == (FILE *)0) {
diminuto_perror("diminuto_lock_f: fdopen");
break;
}
if (fprintf(fp, "%d\n", pid) < 0) {
diminuto_perror("diminuto_lock_f: fprintf");
break;
}
if (fclose(fp) == EOF) {
diminuto_perror("diminuto_lock_f: fclose");
break;
}
fp = (FILE *)0;
fd = -1;
/*
* N.B. atomic.
*/
if (renameat2(AT_FDCWD, path, AT_FDCWD, file, type) >= 0) {
/* Do nothing. */
} else if ((type == RENAME_NOREPLACE) && (errno == EEXIST)) {
break;
} else {
diminuto_perror("diminuto_lock_f: renameat2");
break;
}
rc = 0;
} while (0);
if (fp == (FILE *)0) {
/* Do nothing. */
} else if (fclose(fp) == 0) {
fp = (FILE *)0;
fd = -1;
} else {
diminuto_perror("diminuto_lock_f: fclose");
}
if (fd < 0) {
/* Do nothing. */
} else if (close(fd) == 0) {
/* Do nothing. */
} else {
diminuto_perror("diminuto_lock_f: close");
}
if (path != (char *)0) {
(void)unlink(path);
}
return rc;
}
int diminuto_lock_lock_generic(const char * file, pid_t pid)
{
return diminuto_lock_f(file, "-lock-XXXXXX", pid, RENAME_NOREPLACE);
}
int diminuto_lock_lock(const char * file)
{
int rc = -1;
pid_t pid = -1;
if ((pid = getpid()) < 0) {
diminuto_perror("diminuto_lock_lock: getpid");
} else {
rc = diminuto_lock_lock_generic(file, pid);
}
return rc;
}
int diminuto_lock_prelock(const char * file)
{
int rc = -1;
int fd = -1;
/*
* N.B. atomic.
*/
if ((fd = open(file, O_CREAT | O_EXCL | O_WRONLY, 0600)) >= 0) {
rc = 0;
} else if (errno == EEXIST) {
/* Do nothing. */
} else {
diminuto_perror("diminuto_lock_prelock: open");
}
if (fd < 0) {
/* Do nothing. */
} else if (close(fd) == 0) {
/* Do nothing. */
} else {
diminuto_perror("diminuto_lock_prelock: close");
}
return rc;
}
int diminuto_lock_postlock_generic(const char * file, pid_t pid)
{
return diminuto_lock_f(file, "-post-XXXXXX", pid, RENAME_EXCHANGE);
}
int diminuto_lock_postlock(const char * file)
{
int rc = -1;
pid_t pid = -1;
if ((pid = getpid()) < 0) {
diminuto_perror("diminuto_lock_postlock: getpid");
} else {
rc = diminuto_lock_postlock_generic(file, pid);
}
return rc;
}
int diminuto_lock_unlock(const char * file)
{
int rc = 0;
if (unlink(file) >= 0) {
/* Do nothing. */
} else if (errno == ENOENT) {
rc = -1;
} else {
diminuto_perror("diminuto_lock_unlock: unlink");
rc = -1;
}
return rc;
}
pid_t diminuto_lock_check(const char * file)
{
pid_t pid = -1;
FILE * fp = (FILE *)0;
int rc = -1;
do {
if ((fp = fopen(file, "r")) == (FILE *)0) {
break;
}
rc = fscanf(fp, "%d\n", &pid);
if (rc != 1) {
pid = 0;
break;
}
} while (0);
if (fp == (FILE *)0) {
/* Do nothing. */
} else if (fclose(fp) == EOF) {
diminuto_perror("diminuto_lock_postlock: fclose");
} else {
fp = (FILE *)0;
}
return pid;
}
int diminuto_lock_file_generic(const char * file, pid_t pid)
{
return diminuto_lock_f(file, "-file-XXXXXX", pid, 0);
}
int diminuto_lock_file(const char * file)
{
int rc = -1;
pid_t pid = -1;
if ((pid = getpid()) < 0) {
diminuto_perror("diminuto_lock_file: getpid");
} else {
rc = diminuto_lock_file_generic(file, pid);
}
return rc;
}