-
Notifications
You must be signed in to change notification settings - Fork 4
/
pstore-clean.c
323 lines (275 loc) · 8.3 KB
/
pstore-clean.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/mount.h>
#include <linux/fs.h>
#include <cutils/config_utils.h>
#include <cutils/fs.h>
#include <ctype.h>
#define LOG_TAG "pstore-clean"
#include <log/log.h>
#define MNT "/dev/pstore"
#define DST_DIR "/data/dontpanic"
#define DST DST_DIR"/apanic_console"
#define BUFFER_SIZE 4096
#define PSTORE_CLEAN_VENDOR_CONFIG_FILE "/vendor/etc/pstore-clean.conf"
/* This tool copies and clean kernel dumps from the pstore filesystem
* according to its configuration file (see above). Otherwise that
* data will accumulate in pstore backend (NVRAM, EFI variable, RAM,
* ...) and eventually fill storage. This behavior can be customized
* as all storage backends and file types do not have same
* constraints.
*/
typedef enum {
DEFAULT,
KEEP,
MOVE
} pstore_action;
static int filecopy(const char *srcfile, int dstfd)
{
int srcfd, read_ret, write_ret;
char buffer[BUFFER_SIZE];
srcfd = open(srcfile, O_RDONLY);
if (srcfd == -1) {
ALOGE("Open %s error: %s\n", srcfile, strerror(errno));
return -1;
}
do {
memset(buffer, 0, BUFFER_SIZE);
read_ret = read(srcfd, buffer, BUFFER_SIZE);
if (read_ret == -1) {
ALOGE("Read %s error: %s\n", srcfile, strerror(errno));
close(srcfd);
return -1;
}
write_ret = write(dstfd, buffer, read_ret);
if(write_ret == -1) {
ALOGE("Write error: %s\n", strerror(errno));
close(srcfd);
return -1;
}
} while(read_ret > 0 && write_ret > 0);
close(srcfd);
return 0;
}
static int dir_not_empty(char* path)
{
DIR* dir;
if ((dir = opendir(path)) == NULL) {
ALOGE("Opendir %s failed (%s)", MNT, strerror(errno));
return -1;
}
struct dirent* dent = NULL;
while ((dent = readdir(dir)) != NULL) {
if ((strcmp(dent->d_name, ".") != 0) && (strcmp(dent->d_name, "..") != 0)) {
closedir(dir);
return 1; //dir is not empty
}
}
closedir(dir);
return 0; //dir is empty
}
static int is_fs_mounted(char* path)
{
FILE* proc_mounts = NULL;
char part [PAGE_SIZE];
int is_mounted = 0;
memset(part, 0, PAGE_SIZE);
if ((proc_mounts = fopen("/proc/mounts", "r")) != NULL) {
while (fgets(part, PAGE_SIZE, proc_mounts) != NULL) {
if (strstr(part, path) != NULL) {
is_mounted = 1;
break;
}
}
fclose(proc_mounts);
}
return is_mounted;
}
static size_t getbasefilename(char *filename, char* basefilename,
size_t maxlen)
{
char separator = '-';
size_t filename_len = maxlen, i;
size_t lastoccurance = 0;
for (i = 0; i < maxlen; i++) {
if (filename[i] == separator) {
lastoccurance = i;
}
basefilename[i] = filename[i];
if (filename[i] == '\0') {
filename_len = i;
break;
}
}
if (lastoccurance) {
if (isdigit(filename[lastoccurance + 1])) {
basefilename[lastoccurance] = '\0';
filename_len = lastoccurance;
}
}
return filename_len;
}
static int perform_rule_check(cnode *root, char *filename,
pstore_action *action,
const char **dstpath)
{
const char *action_str = NULL;
cnode *node = NULL;
if (!root || !filename || !action) {
return -1;
}
if (!(node = config_find(root, filename))) {
ALOGI("no rule for %s", filename);
return -1;
}
if (!(action_str = config_str(node, "action", NULL))) {
ALOGE("Could not retrieve action for rule: %s", node->name);
return -1;
}
if (!strcmp(action_str, "move")) {
if (!(*dstpath = config_str(node, "destination", NULL))) {
*dstpath = "/dev/null";
}
*action = MOVE;
} else if (!strcmp(action_str, "keep")) {
*action = KEEP;
} else {
*action = DEFAULT;
}
return 0;
}
static void cleanup_dir(const char *path)
{
char srcpath[PATH_MAX];
struct dirent* dent;
DIR* dir;
if ((dir = opendir(path)) == NULL) {
ALOGE("Opendir %s failed (%s)", path, strerror(errno));
return;
}
while ((dent = readdir(dir)) != NULL) {
/* Only remove regular files */
if (dent->d_type != DT_REG)
continue;
snprintf(srcpath, sizeof(srcpath), "%s/%s", path, dent->d_name);
if (unlink(srcpath) != 0)
ALOGE("Remove %s failed (%s)", srcpath, strerror(errno));
}
closedir(dir);
}
int main()
{
int n = 0;
int status;
int dstfd = -1, fd = -1;
struct dirent **namelist = NULL;
char srcpath[PATH_MAX];
const char *dstpath;
char destpath[PATH_MAX];
char basefile[FILENAME_MAX];
int namelist_len = 0;
pstore_action action;
umask(0027);
struct dirent* dent = NULL;
cnode *rulesroot = NULL;
cnode *root = config_node("", "");
if (root) {
config_load_file(root, PSTORE_CLEAN_VENDOR_CONFIG_FILE);
if (!(rulesroot = config_find(root, "pstore_rules"))) {
ALOGI("pstore_rules not specified in config file");
}
}
if (!is_fs_mounted(MNT)) {
ALOGE("Pstore fs isn't mounted. Exiting.");
status = -1;
goto error1;
}
status = dir_not_empty(MNT);
if (status == -1) {
ALOGE("Opendir %s failed (%s)", MNT, strerror(errno));
goto error2;
}
else if (status == 0) {
ALOGI("Pstore is clean.");
goto done;
}
ALOGW("Kernel pstore crash dump found, copying to " DST "\n");
cleanup_dir(DST_DIR);
if ((namelist_len = scandir(MNT, &namelist, NULL, alphasort)) < 1) {
ALOGE("Failed to list " MNT " (%s)", strerror(errno));
goto error2;
}
for (n = namelist_len - 1; n >= 0; n--) {
dent = namelist[n];
if ((strcmp(dent->d_name, ".") == 0) || (strcmp(dent->d_name, "..") == 0))
continue;
getbasefilename(dent->d_name, basefile, FILENAME_MAX);
if (perform_rule_check(rulesroot, basefile, &action, &dstpath)) {
action = DEFAULT;
}
snprintf(srcpath, sizeof(srcpath), "%s/%s", MNT, dent->d_name);
switch (action) {
case MOVE:
snprintf(destpath, sizeof(destpath), "%s/%s", dstpath, dent->d_name);
if ((fd = open(destpath, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0) {
ALOGE("Failed to open %s (%s)", destpath, strerror(errno));
}
else if (filecopy(srcpath, fd) == -1) {
ALOGE("Could not move %s to %s on rule: %s (%s)",
srcpath, dstpath, dent->d_name, strerror(errno));
close(fd);
}
else {
close(fd);
}
if (unlink(srcpath) != 0) {
ALOGE("Remove %s failed (%s)", srcpath, strerror(errno));
goto error3;
}
break;
case KEEP:
/* nothing to do for now, as the file is supposed to remain
in pstore and be excluded from apanic_console */
break;
default:
/* default rules, fallback */
if (dstfd == -1 &&
(dstfd = open(DST, O_CREAT | O_APPEND | O_WRONLY, 0666)) < 0) {
ALOGE("Failed to open " DST " (%s)", strerror(errno));
goto error3;
}
snprintf(destpath, sizeof(destpath), "------ %s ------\n", dent->d_name);
write(dstfd, destpath, strlen(destpath));
if (filecopy(srcpath, dstfd) == -1) {
ALOGE("Copy file %s failed (%s)", srcpath, strerror(errno));
}
if (unlink(srcpath) != 0) {
ALOGE("Remove %s failed (%s)", srcpath, strerror(errno));
goto error3;
}
}
}
error3:
for (n = 0; n < namelist_len; n++) {
free(namelist[n]);
}
free(namelist);
error2:
if (dstfd >= 0)
close(dstfd);
done:
error1:
if (root) {
config_free(root);
free(root);
}
return status;
}