Debian Codegraph @

src/analyze/analyze-dlopen-metadata.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-json.h"
4
5 #include "alloc-util.h"
6 #include "analyze.h"
7 #include "analyze-dlopen-metadata.h"
8 #include "chase.h"
9 #include "elf-util.h"
10 #include "fd-util.h"
11 #include "format-table.h"
12 #include "json-util.h"
13 #include "strv.h"
14
15 int verb_dlopen_metadata(int argc, char *argv[], uintptr_t _data, void *userdata) {
16 int r;
17
18 _cleanup_free_ char *abspath = NULL;
19 _cleanup_close_ int fd = -EBADF;
20 fd = chase_and_open(argv[1], arg_root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, &abspath);
21 if (fd < 0)
22 return log_error_errno(fd, "Could not open \"%s\": %m", argv[1]);
23
24 _cleanup_(sd_json_variant_unrefp) sd_json_variant *dlopen_metadata = NULL;
25 r = parse_elf_object(
26 fd,
27 abspath,
28 arg_root,
29 /* fork_disable_dump= */ false,
30 /* ret= */ NULL,
31 NULL,
32 &dlopen_metadata);
33 if (r < 0)
34 return log_error_errno(r, "Parsing \"%s\" as ELF object failed: %m", abspath);
35
36 if (!dlopen_metadata)
37 return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "%s does not contain any .note.dlopen sections", argv[1]);
38
39 if (sd_json_format_enabled(arg_json_format_flags))
40 return sd_json_variant_dump(dlopen_metadata, arg_json_format_flags, stdout, NULL);
41
42 _cleanup_(table_unrefp) Table *t = NULL;
43 t = table_new("feature", "description", "soname", "priority");
44 if (!t)
45 return log_oom();
46
47 table_set_ersatz_string(t, TABLE_ERSATZ_NA);
48
49 sd_json_variant *z;
50 JSON_VARIANT_ARRAY_FOREACH(z, dlopen_metadata) {
51 _cleanup_strv_free_ char **sonames = NULL;
52
53 r = sd_json_variant_strv(sd_json_variant_by_key(z, "soname"), &sonames);
54 if (r < 0)
55 return log_error_errno(r, "Failed to extract sonames from dlopen metadata: %m");
56
57 r = table_add_many(
58 t,
59 TABLE_STRING, sd_json_variant_string(sd_json_variant_by_key(z, "feature")),
60 TABLE_STRING, sd_json_variant_string(sd_json_variant_by_key(z, "description")),
61 TABLE_STRV_WRAPPED, sonames,
62 TABLE_STRING, sd_json_variant_string(sd_json_variant_by_key(z, "priority")));
63 if (r < 0)
64 return table_log_add_error(r);
65 }
66
67 r = table_print_with_pager(t, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
68 if (r < 0)
69 return table_log_print_error(r);
70
71 return 0;
72 }