-
Notifications
You must be signed in to change notification settings - Fork 5
/
cruft.cc
472 lines (409 loc) · 11.9 KB
/
cruft.cc
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright © 2015 Alexandre Detiste <alexandre@detiste.be>
// SPDX-License-Identifier: GPL-2.0-or-later
#include <iostream>
#include <fstream>
#include <algorithm>
#include <ctime>
#include <thread>
#include <sys/stat.h>
#include <getopt.h>
#include <cstring>
#include <unistd.h>
#include <dirent.h>
#include "explain.h"
#include "filters.h"
#include "locate.h"
#include "dpkg.h"
#include "dpkg_exclude.h"
#include "shellexp.h"
#include "bugs.h"
using namespace std;
#ifdef BUSTER
#define LOCATE_DB "/var/lib/mlocate/mlocate.db"
#define UPDATEDB "updatedb.mlocate"
#else
#include "nolocate.h"
#define LOCATE_DB "/var/lib/plocate/plocate.db"
#define UPDATEDB "updatedb.plocate"
#endif
/*
static int read_mounts(const vector<string>& prunefs, vector<string>& mounts)
{
// this doesn't include "/", as it always exists
ifstream mtab("/proc/mounts");
string mount,type;
while (mtab.good())
{
getline(mtab,mount,' '); // discard device
getline(mtab,mount,' ');
getline(mtab,type,' ');
bool match=false;
if (mount=="/") match=true;
else if (mount.rfind("/sys/", 0) == 0) match=true;
else if (mount.rfind("/dev/", 0) == 0) match=true;
else {
for (const auto& elem: prunefs) {
string uppercase=type;
transform(uppercase.begin(), uppercase.end(), uppercase.begin(), ::toupper);
if (uppercase==elem) {
match=true;
break;
}
}
}
if (!match) {
//cerr << mount << " => " << type << endl;
mounts.emplace_back(mount);
}
getline(mtab,mount); // discard options
}
return 0;
}
*/
static bool updatedb()
{
/* return value is meant as an are_we_up_to_date flag
when running as non-root */
const string db = LOCATE_DB;
int rc_locate, rc_dpkg;
struct stat stat_locate, stat_dpkg;
rc_locate = stat(db.c_str(), &stat_locate);
rc_dpkg = stat("/var/lib/dpkg/status", &stat_dpkg);
if (rc_dpkg) {
cerr << "can't read /var/lib/dpkg/status timestamp !!!\n";
exit(1);
}
if (!rc_locate && stat_locate.st_mtim.tv_sec > stat_dpkg.st_mtim.tv_sec)
return true;
if (getuid()) return false;
if (system(UPDATEDB)) {
cerr << "updatedb failed\n";
exit(1);
}
return true;
}
static void one_file(const string& path)
{
struct stat buffer;
if (stat(path.c_str(), &buffer)) {
cerr << "file not found\n";
exit(1);
}
// is it a static file ?
dpkg_start("/");
if (query(path.c_str())) {
dpkg_end();
exit(0);
}
string infile = path;
if ((path.rfind("/bin/", 0) == 0)
or (path.rfind("/lib/", 0) == 0)
or (path.rfind("/sbin/", 0) == 0)) {
infile = "/usr" + infile;
}
char* file = realpath(infile.c_str(), nullptr);
vector<string> packages;
read_dpkg_header(packages);
dpkg_end();
// is it a dynamic file ?
vector<owner> globs;
read_filters("/etc/cruft/filters/", "/usr/share/cruft/ruleset", packages, globs);
for (const auto& gl: globs) {
if (myglob(file, gl.path)) {
cout << gl.package << '\n';
exit(0);
};
}
// match the dynamic "explain" filters
vector<owner> explain;
read_explain("/etc/cruft/explain/", packages, explain);
for (const auto& ex: explain) {
if (infile == ex.path) {
cout << ex.package << '\n';
exit(0);
}
}
cerr << "no matching package found\n";
exit(1);
}
static int one_package(const string& package)
{
vector<string> packages;
packages.push_back(package);
vector<owner> cruft;
read_filters("/etc/cruft/filters/", "/usr/share/cruft/ruleset", packages, cruft);
packages.push_back("binfmt-support");
packages.push_back("ucf");
read_explain("/etc/cruft/explain/", packages, cruft);
for (const auto& cr: cruft) {
if (cr.package == package) cout << cr.path << '\n';
}
return 0;
}
static clock_t beg = clock();
static void elapsed(const string& action)
{
if (getenv("ELAPSED") == nullptr) return;
clock_t end = clock();
clock_t elapsed_mseconds = (end - beg) * 1000 / CLOCKS_PER_SEC;
cerr << "elapsed " << action << ": " << elapsed_mseconds << '\n';
beg = end;
}
static const char* const default_explain_dir = "/etc/cruft/explain/";
static const char* const default_filter_dir = "/etc/cruft/filters/";
static const char* const default_ignore_file = "/etc/cruft/ignore";
static const char* const default_ruleset_file = "/usr/share/cruft/ruleset";
static const char* const default_bugs_file = "/usr/share/cruft/bugs";
static const char* const default_root_dir = "/";
static void print_help_message()
{
cout << "cruft-ng [OPTIONS] [file]\n\n";
cout << "if <file> is specified, this file is analysed\n";
cout << "if not, the whole system is analysed\n\n";
cout << "OPTIONS\n";
cout << " -p --package list volatile files only for this one package\n";
cout << " -E --explain directory for explain scripts (default: " << default_explain_dir << ")\n";
cout << " -F --filter directory for filters (default: " << default_filter_dir << ")\n";
cout << " -I --ignore path for ignore file (default: " << default_ignore_file << ")\n";
cout << " -R --ruleset path for ruleset file (default: " << default_ruleset_file << ")\n";
cout << " -B --bugs path for known bugs file (default: " << default_bugs_file << ")\n";
#ifndef BUSTER
cout << " -n --no-locate do not use locate\n";
cout << " -r --root root directory (default: " << default_root_dir << ", only works with --no-locate)\n";
#endif
cout << '\n';
cout << " -h --help this help message\n";
}
static void cruft(const string& ignore_file,
const string& filter_dir,
const string& ruleset_file,
const string& explain_dir,
const string& bugs_file,
bool locate,
const string& root_dir)
{
bool debug = getenv("DEBUG") != nullptr;
const int SIZEBUF = 200;
char buf[SIZEBUF];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
setlocale(LC_TIME, "");
strftime(buf, sizeof(buf), "%c", timeinfo);
cout << "cruft report: " << buf << '\n';
if (locate) {
bool updated = updatedb();
if (!updated) {
cerr << "warning: plocate database is outdated" << endl << flush;
}
elapsed("updatedb");
}
vector<string> fs;
#ifndef BUSTER
thread thr_plocate(locate ? read_locate : read_nolocate, ref(fs), ignore_file, root_dir);
#else
thread thr_plocate(read_locate, ref(fs), ignore_file, root_dir);
#endif
vector<string> packages;
vector<string> dpkg;
dpkg_start(root_dir);
thread thr_dpkg(read_dpkg, ref(packages), ref(dpkg), false, root_dir);
thr_plocate.join();
thr_dpkg.join();
dpkg_end();
elapsed("plocate + dpkg");
map<string, bug> bugs;
read_bugs(bugs, bugs_file);
// match two main data sources
vector<string> cruft;
vector<string> missing;
for (auto left=fs.begin(), right=dpkg.begin(); left != fs.end() && right != dpkg.end();)
{
//cerr << "[" << *left << "=" << *right << "]" << endl;
if (*left==*right) {
left++;
right++;
} else if (*left < *right) {
cruft.push_back(*left);
left++;
} else {
missing.push_back(*right);
right++;
}
if (right == dpkg.end()) while(left !=fs.end() ) {cruft.push_back(*left); left++; }
if (left == fs.end() ) while(right !=dpkg.end()) {missing.push_back(*right); right++;}
}
elapsed("main set match");
if (debug) cerr << missing.size() << " files in missing database\n";
if (debug) cerr << cruft.size() << " files in cruft database\n\n";
// https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=619086
vector<string> excludes;
read_dpkg_excludes(excludes);
elapsed("read excludes");
vector<string> missing2;
unsigned long count_stat = 0;
for (const auto& miss: missing) {
bool match=false;
for (const auto& ex: excludes) {
match=myglob(miss,ex);
if (match) break;
}
if (!match) {
// file may exist on tmpfs
// e.g.: /var/cache/apt/archives/partial
struct stat stat_buffer;
if ( stat(miss.c_str(), &stat_buffer) == 0) {
count_stat += 1;
if (debug) cerr << miss << " was not in plocate database\n";
} else {
missing2.push_back(miss);
}
}
}
elapsed("missing2");
if (debug) cerr << "count stat():" << count_stat << '\n';
// match the globs against reduced database
vector<owner> globs;
read_filters(filter_dir, ruleset_file, packages, globs);
elapsed("read filters");
vector<string> cruft3;
vector<bool> used_globs(globs.size(), false);
for (const auto& cr: cruft) {
bool match=false;
for (size_t i = 0; i < globs.size(); i++) {
match=myglob(cr, globs[i].path);
if (match) {
used_globs[i] = true;
break;
}
}
if (!match) cruft3.push_back(cr);
}
if (debug)
for (size_t i = 0; i < globs.size(); i++)
if (!used_globs[i])
cout << "unused ruleset: " << globs[i].package << " " << globs[i].path << endl;
elapsed("extra vs globs");
if (debug) cerr << cruft3.size() << " files in cruft3 database\n\n";
// set CRUFT_ROOT for explain scripts
setenv("CRUFT_ROOT", root_dir == "/" ? "" : root_dir.c_str(), 0);
// match the dynamic "explain" filters
vector<owner> explain;
read_explain(explain_dir, packages, explain);
elapsed("read explain");
vector<string> cruft4;
for (const auto& cr: cruft3) {
bool match=false;
for (const auto& ex: explain) {
match=(cr==ex.path);
if (match) break;
}
if (!match) cruft4.push_back(cr);
}
elapsed("extra vs explain");
if (debug) cerr << cruft4.size() << " files in cruft4 database\n";
//TODO: some smarter algo when run as non-root
// like checking the R/X bits of parent dir
cout << "---- missing: dpkg ----\n";
if (geteuid() == 0 ) for (const auto& miss: missing2) {
cout << " " << miss << '\n';
}
//TODO: split by filesystem
cout << "---- unexplained: / ----\n";
for (const auto& cr: cruft4) {
cout << " " << cr;
auto bug = bugs.find(cr);
if (bug != bugs.end()) {
cout << " (Bug: #" << bug->second.bugno << ")";
}
cout << '\n';
}
cout << "\nend.\n";
exit(0);
}
int main(int argc, char *argv[])
{
bool do_one_package = false;
bool locate = true;
string package = "";
string explain_dir = default_explain_dir;
string filter_dir = default_filter_dir;
string ignore_file = default_ignore_file;
string ruleset_file = default_ruleset_file;
string bugs_file = default_bugs_file;
string root_dir = default_root_dir;
const struct option long_options[] =
{
{"help", no_argument, nullptr, 'h'},
{"no-locate", no_argument, nullptr, 'n'},
{"package", required_argument, nullptr, 'p'},
{"explain", required_argument, nullptr, 'E'},
{"filter", required_argument, nullptr, 'F'},
{"ignore", required_argument, nullptr, 'I'},
{"ruleset", required_argument, nullptr, 'R'},
{"bugs", required_argument, nullptr, 'B'},
{"root", required_argument, nullptr, 'r'},
{0, 0, 0, 0}
};
int opt, opti = 0;
while ((opt = getopt_long(argc, argv, "p:E:F:hI:nR:B:r:", long_options, &opti)) != 0) {
if (opt == EOF)
break;
switch (opt) {
case 'p':
do_one_package = true;
package = optarg;
break;
case 'E':
explain_dir = optarg;
if (!explain_dir.empty() && explain_dir.back() != '/')
explain_dir += '/';
break;
case 'F':
filter_dir = optarg;
if (!filter_dir.empty() && filter_dir.back() != '/')
filter_dir += '/';
break;
case 'h':
print_help_message();
exit(0);
case 'I':
ignore_file = optarg;
break;
case 'n':
locate = false;
break;
case 'R':
ruleset_file = optarg;
break;
case 'B':
bugs_file = optarg;
break;
case '?':
print_help_message();
exit(1);
case 'r':
root_dir = optarg;
if (!root_dir.empty() && root_dir.back() != '/')
root_dir += '/';
break;
default:
cerr << "Invalid getopt return value: " << opt << "\n";
break;
}
}
if (do_one_package) exit(one_package(package));
if (optind < argc) {
if (optind + 1 == argc)
one_file(argv[1]);
cerr << "Invalid non-option arguments:";
while (optind < argc)
cerr << " " << argv[optind++];
cerr << '\n';
print_help_message();
exit(1);
}
// else: standard cruft report
cruft(ignore_file, filter_dir, ruleset_file, explain_dir, bugs_file, locate, root_dir);
}