/
repair.c
255 lines (230 loc) · 6.3 KB
/
repair.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
244
245
246
247
248
249
250
251
252
253
254
255
#include <platform.h>
#include <repair.h>
#include <logging.h>
#if defined(__MINGW32__) || !defined(LMDB)
int repair_main(ARG_UNUSED int argc, ARG_UNUSED const char *const *const argv)
{
Log(LOG_LEVEL_ERR,
"cf-check repair not available on this platform/build");
return 1;
}
int repair_lmdb_default(ARG_UNUSED bool force)
{
Log(LOG_LEVEL_INFO,
"database repair not available on this platform/build");
return 0;
}
#else
#include <stdio.h>
#include <errno.h>
#include <lmdump.h>
#include <lmdb.h>
#include <diagnose.h>
#include <backup.h>
#include <sequence.h>
#include <utilities.h>
#include <diagnose.h>
#include <string_lib.h>
#include <replicate_lmdb.h>
static void print_usage(void)
{
printf("Usage: cf-check repair [-f] [FILE ...]\n");
printf("Example: cf-check repair /var/cfengine/state/cf_lastseen.lmdb\n");
printf("Options: -f|--force repair LMDB files that look OK ");
}
int remove_files(Seq *files)
{
assert(files != NULL);
size_t corruptions = SeqLength(files);
int failures = 0;
for (size_t i = 0; i < corruptions; ++i)
{
const char *filename = SeqAt(files, i);
assert(filename != NULL);
Log(LOG_LEVEL_INFO, "Removing: '%s'", filename);
if (unlink(filename) != 0)
{
Log(LOG_LEVEL_ERR,
"Failed to remove '%s' (%d - %s)",
filename,
errno,
strerror(errno));
++failures;
continue;
}
char *lock_file = StringConcatenate(2, filename, ".lock");
unlink(lock_file);
free(lock_file);
lock_file = StringConcatenate(2, filename, "-lock");
unlink(lock_file);
free(lock_file);
}
if (failures != 0)
{
Log(LOG_LEVEL_ERR, "Failed to remove %d files", failures);
}
return failures;
}
int repair_lmdb_file(const char *file)
{
char *dest_file = StringFormat("%s"REPAIR_FILE_EXTENSION, file);
pid_t child_pid = fork();
if (child_pid == 0)
{
/* child */
exit(replicate_lmdb(file, dest_file));
free(dest_file);
}
else
{
/* parent */
int status;
pid_t pid = waitpid(child_pid, &status, 0);
if (pid != child_pid)
{
/* real error that should never happen */
return -1;
}
if (WIFEXITED(status) && WEXITSTATUS(status) != CF_CHECK_OK
&& WEXITSTATUS(status) != CF_CHECK_LMDB_CORRUPT_PAGE)
{
Log(LOG_LEVEL_ERR, "Failed to repair file '%s', removing", file);
unlink(file);
free(dest_file);
return WEXITSTATUS(status);
}
else if (WIFSIGNALED(status))
{
Log(LOG_LEVEL_ERR, "Failed to repair file '%s', child process signaled (%d), removing",
file, WTERMSIG(status));
unlink(file);
free(dest_file);
return signal_to_cf_check_code(WTERMSIG(status));
}
else
{
/* replication successfull */
Log(LOG_LEVEL_INFO, "Replacing '%s' with the new copy", file);
if (rename(dest_file, file) != 0)
{
Log(LOG_LEVEL_ERR,
"Failed to replace file '%s' with the repaired copy: %s",
file, strerror(errno));
unlink(dest_file);
free(dest_file);
return -1;
}
free(dest_file);
return 0;
}
}
}
int repair_lmdb_files(Seq *files, bool force)
{
assert(files != NULL);
assert(SeqLength(files) > 0);
Seq *corrupt;
if (force)
{
corrupt = files;
}
else
{
const int corruptions = diagnose_files(files, &corrupt, false);
if (corruptions != 0)
{
assert(corrupt != NULL);
Log(LOG_LEVEL_NOTICE,
"%d corrupt database%s to fix",
corruptions,
corruptions != 1 ? "s" : "");
}
else
{
Log(LOG_LEVEL_INFO, "No corrupted LMDB files - nothing to do");
return 0;
}
}
int ret = 0;
const size_t length = SeqLength(corrupt);
assert(length > 0);
backup_files(corrupt);
for (int i = 0; i < length; ++i)
{
const char *file = SeqAt(files, i);
if (repair_lmdb_file(file) != 0)
{
ret++;
}
}
if (!force)
{
/* see 'if (force)' above */
SeqDestroy(corrupt);
}
if (ret == 0)
{
Log(LOG_LEVEL_NOTICE, "Database repair successful");
}
else
{
Log(LOG_LEVEL_ERR, "Database repair failed");
}
return ret;
}
int repair_main(int argc, const char *const *const argv)
{
size_t offset = 1;
bool force = false;
if (argc > 1 && argv[1] != NULL && argv[1][0] == '-')
{
if (matches_option(argv[1], "--force", "-f"))
{
offset++;
force = true;
}
else
{
print_usage();
printf("Unrecognized option: '%s'\n", argv[1]);
return 1;
}
}
Seq *files = argv_to_lmdb_files(argc, argv, offset);
if (files == NULL || SeqLength(files) == 0)
{
Log(LOG_LEVEL_ERR, "No database files to repair");
return 1;
}
const int ret = repair_lmdb_files(files, force);
SeqDestroy(files);
return ret;
}
int repair_lmdb_default(bool force)
{
// This function is used by cf-execd and cf-agent, not cf-check
// Consistency checks are not enabled by default (--skip-db-check=yes)
// This log message can be changed to verbose if it happens by default:
Log(LOG_LEVEL_INFO, "Running internal DB (LMDB) consistency checks");
Seq *files = default_lmdb_files();
if (files == NULL)
{
// Error message printed default_lmdb_files()
return 1;
}
if (SeqLength(files) == 0)
{
// First agent run - no LMDB files
Log(LOG_LEVEL_INFO, "Skipping local database repair, no lmdb files");
return 0;
}
const int ret = repair_lmdb_files(files, force);
SeqDestroy(files);
if (ret != 0)
{
Log(LOG_LEVEL_ERR, "Something went wrong during database repair");
Log(LOG_LEVEL_ERR, "Try running `cf-check repair` manually");
}
return ret;
}
#endif