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
|
/*
* revdep - reverse dependency checker for rawnix
* main.c - entry point, argument parsing, check logic
*
* Copyright (c) 2025 zorz@gmx.com
* Licensed under the BSD 2-Clause License. See revdep.h.
*
* Concept based on revdep by James Buren for CRUX Linux.
*/
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE
#include "revdep.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <gelf.h>
/* installed Python version (major.minor), NULL if not found */
static char *py_version;
/* installed Perl version (X.Y.Z), NULL if not found */
static char *perl_version;
/*
* The system library path the way musl's dynamic linker builds it:
* from /etc/ld-musl-x86_64.path, entries separated by ':' or newlines,
* if that file exists; otherwise musl's default /lib:/usr/local/lib:
* /usr/lib. An existing but empty file means no system path, as for
* musl.
*/
static void
load_sys_path(strvec_t *dirs)
{
FILE *fp;
char line[4096];
size_t len;
fp = fopen(MUSL_PATH_FILE, "r");
if (!fp) {
strsplit(MUSL_SYS_PATH, ':', dirs);
return;
}
while (fgets(line, sizeof(line), fp)) {
len = strlen(line);
if (len > 0 && line[len - 1] == '\n')
line[--len] = '\0';
strsplit(line, ':', dirs);
}
fclose(fp);
}
static void
usage(void)
{
printf("revdep %s - reverse dependency checker for rawnix\n\n",
VERSION);
printf("usage: revdep [-r] [-i pkg,...] [-v] [package ...]\n");
printf(" revdep -h | -V\n\n");
printf("modes:\n");
printf(" (default) check packages for broken libraries\n");
printf(" -r reverse: show what depends on a package\n\n");
printf("options:\n");
printf(" -i pkg,... ignore packages (comma-separated)\n");
printf(" -v increase verbosity (up to -vvvv)\n");
printf(" -h show this help\n");
printf(" -V show the version\n\n");
printf("verbosity:\n");
printf(" (none) broken package names only\n");
printf(" -v status for each package checked\n");
printf(" -vv include broken file paths\n");
printf(" -vvv include missing library names\n");
printf(" -vvvv debug trace\n\n");
printf("files:\n");
printf(" /var/lib/pkg/db package database\n");
printf(" /etc/revdep.d/ per-package library directories\n");
printf(" %s system library path\n", MUSL_PATH_FILE);
}
/* -------------------------------------------------------------------- */
/* Forward check: are this package's ELF files satisfied? */
/* -------------------------------------------------------------------- */
static bool
check_file(const pkg_t *pkg, const char *path, const strvec_t *sys_dirs)
{
elfinfo_t *elf;
size_t i;
bool ok = true;
msg(V_DEBUG, " %s:%s: checking\n", pkg->name, path);
if (!is_regfile(path))
return true;
elf = cache_lookup(path);
if (!elf)
return true; /* not an ELF, skip */
for (i = 0; i < elf->needed.count; i++) {
const char *lib = elf->needed.items[i];
if (!lib_find(elf, pkg, lib, sys_dirs)) {
msg(V_DETAIL, " %s: %s requires %s (MISSING)\n",
pkg->name, path, lib);
ok = false;
}
}
if (!ok)
msg(V_FILES, " %s: %s: broken\n", pkg->name, path);
return ok;
}
static bool
check_package(const pkg_t *pkg, const strvec_t *sys_dirs)
{
size_t i;
bool ok = true;
if (pkg->ignore)
return true;
for (i = 0; i < pkg->files.count; i++) {
if (!check_file(pkg, pkg->files.items[i], sys_dirs))
ok = false;
}
/* Python versioned path check */
if (py_version && py_check_package(pkg, py_version))
ok = false;
/* Perl versioned path check */
if (perl_version && perl_check_package(pkg, perl_version))
ok = false;
return ok;
}
static int
run_check(pkgvec_t *pkgs, const strvec_t *sys_dirs,
int argc, char **argv, int first)
{
size_t i;
int ret = 0;
bool ok;
if (first >= argc) {
/* check all packages */
msg(V_NORMAL, ">> checking %zu packages\n", pkgs->count);
for (i = 0; i < pkgs->count; i++) {
ok = check_package(pkgs->items[i], sys_dirs);
if (!ok) {
if (verbosity >= V_NORMAL)
msg(V_NORMAL, "%s: BROKEN\n",
pkgs->items[i]->name);
else
msg(V_QUIET, "%s\n",
pkgs->items[i]->name);
ret = 1;
} else {
msg(V_NORMAL, "%s: ok\n",
pkgs->items[i]->name);
}
}
} else {
/* check specific packages */
int count = argc - first;
msg(V_NORMAL, ">> checking %d package%s\n",
count, count > 1 ? "s" : "");
for (i = first; i < (size_t)argc; i++) {
pkg_t *pkg = pkgvec_find(pkgs, argv[i]);
if (!pkg) {
fprintf(stderr, "revdep: %s: not found in db\n",
argv[i]);
ret = 1;
continue;
}
ok = check_package(pkg, sys_dirs);
if (!ok) {
if (verbosity >= V_NORMAL)
msg(V_NORMAL, "%s: BROKEN\n",
pkg->name);
else
msg(V_QUIET, "%s\n", pkg->name);
ret = 1;
} else {
msg(V_NORMAL, "%s: ok\n", pkg->name);
}
}
}
return ret;
}
/* -------------------------------------------------------------------- */
/* Reverse mode: what packages depend on <pkg>'s shared libraries? */
/* */
/* 1. Collect all .so files owned by the target package(s). */
/* 2. Walk every other package's ELF files. */
/* 3. If any DT_NEEDED matches a target .so filename, report it. */
/* -------------------------------------------------------------------- */
static bool
is_soname(const char *base)
{
const char *p = base;
while ((p = strstr(p, ".so")) != NULL) {
if (p != base && (p[3] == '\0' || p[3] == '.'))
return true;
p += 3;
}
return false;
}
static void
collect_sonames(const pkg_t *pkg, strvec_t *sonames)
{
size_t i;
const char *path, *base;
for (i = 0; i < pkg->files.count; i++) {
path = pkg->files.items[i];
/* extract basename */
base = strrchr(path, '/');
base = base ? base + 1 : path;
/* only shared objects: name.so or name.so.N... */
if (!is_soname(base))
continue;
/* avoid duplicates */
if (!strvec_contains(sonames, base))
strvec_push(sonames, base);
}
}
static int
run_reverse(pkgvec_t *pkgs, const strvec_t *sys_dirs,
int argc, char **argv, int first)
{
strvec_t sonames;
size_t i, j, k;
int ret = 0;
bool found_any = false;
(void)sys_dirs;
if (first >= argc) {
fprintf(stderr, "revdep: -r requires at least one package\n");
return 1;
}
strvec_init(&sonames);
/* collect .so basenames from all target packages */
for (i = first; i < (size_t)argc; i++) {
pkg_t *pkg = pkgvec_find(pkgs, argv[i]);
if (!pkg) {
fprintf(stderr, "revdep: %s: not found in db\n",
argv[i]);
ret = 1;
continue;
}
collect_sonames(pkg, &sonames);
}
if (sonames.count == 0) {
msg(V_NORMAL, ">> no shared libraries found in target%s\n",
(argc - first) > 1 ? "s" : "");
strvec_free(&sonames);
return ret;
}
msg(V_NORMAL, ">> %zu shared librar%s provided by target%s\n",
sonames.count,
sonames.count == 1 ? "y" : "ies",
(argc - first) > 1 ? "s" : "");
for (i = 0; i < sonames.count; i++)
msg(V_FILES, " %s\n", sonames.items[i]);
msg(V_NORMAL, ">> scanning %zu packages for dependants\n",
pkgs->count);
/* scan every package */
for (i = 0; i < pkgs->count; i++) {
pkg_t *pkg = pkgs->items[i];
bool depends = false;
/* skip if this is one of the target packages */
bool is_target = false;
for (j = first; j < (size_t)argc; j++) {
if (strcmp(pkg->name, argv[j]) == 0) {
is_target = true;
break;
}
}
if (is_target || pkg->ignore)
continue;
/* check each file */
for (j = 0; j < pkg->files.count && !depends; j++) {
const char *path = pkg->files.items[j];
elfinfo_t *elf;
if (!is_regfile(path))
continue;
elf = cache_lookup(path);
if (!elf)
continue;
for (k = 0; k < elf->needed.count; k++) {
if (strvec_contains(&sonames,
elf->needed.items[k])) {
depends = true;
msg(V_FILES,
" %s: %s needs %s\n",
pkg->name, path,
elf->needed.items[k]);
/* keep scanning files for -vv */
if (verbosity < V_FILES)
break;
}
}
}
if (depends) {
if (verbosity >= V_NORMAL)
msg(V_NORMAL, "%s: depends\n", pkg->name);
else
msg(V_QUIET, "%s\n", pkg->name);
found_any = true;
}
}
if (!found_any)
msg(V_NORMAL, ">> no dependants found\n");
strvec_free(&sonames);
return ret;
}
/* -------------------------------------------------------------------- */
/* main */
/* -------------------------------------------------------------------- */
int
main(int argc, char **argv)
{
int opt, ret;
bool reverse = false;
strvec_t ignores;
strvec_t sys_dirs;
pkgvec_t pkgs;
size_t i;
strvec_init(&ignores);
strvec_init(&sys_dirs);
pkgvec_init(&pkgs);
/* libelf init */
if (elf_version(EV_CURRENT) == EV_NONE) {
fprintf(stderr, "revdep: libelf init failed\n");
return 1;
}
while ((opt = getopt(argc, argv, "ri:vhV")) != -1) {
switch (opt) {
case 'r':
reverse = true;
break;
case 'i':
strsplit(optarg, ',', &ignores);
break;
case 'v':
if (verbosity < V_DEBUG)
verbosity++;
break;
case 'h':
usage();
return 0;
case 'V':
printf("revdep %s\n", VERSION);
return 0;
default:
fprintf(stderr,
"usage: revdep [-r] [-i pkg,...] [-v] "
"[package ...]\n");
return 1;
}
}
/* read package database */
if (!db_read(PKG_DB_PATH, &pkgs)) {
fprintf(stderr, "revdep: cannot read %s\n", PKG_DB_PATH);
ret = 1;
goto out;
}
msg(V_NORMAL, ">> %zu packages in database\n", pkgs.count);
/* detect installed Python version for path checks */
py_version = py_installed_version(&pkgs);
if (py_version)
msg(V_DEBUG, ">> python version: %s\n", py_version);
/* detect installed Perl version for path checks */
perl_version = perl_installed_version(&pkgs);
if (perl_version)
msg(V_DEBUG, ">> perl version: %s\n", perl_version);
/* read per-package dirs */
db_read_dirs(REVDEP_DIR, &pkgs);
/* mark ignored packages */
for (i = 0; i < ignores.count; i++) {
pkg_t *pkg = pkgvec_find(&pkgs, ignores.items[i]);
if (pkg)
pkg->ignore = true;
}
/* system library directories — musl's own */
load_sys_path(&sys_dirs);
for (i = 0; i < sys_dirs.count; i++)
msg(V_DEBUG, ">> system library dir: %s\n", sys_dirs.items[i]);
/* dispatch */
if (reverse)
ret = run_reverse(&pkgs, &sys_dirs, argc, argv, optind);
else
ret = run_check(&pkgs, &sys_dirs, argc, argv, optind);
out:
cache_clear();
free(py_version);
free(perl_version);
strvec_free(&sys_dirs);
strvec_free(&ignores);
pkgvec_free(&pkgs);
return ret;
}
|