1:255.16-alt1
[systemd_ALT.git] / src / analyze / analyze-fdstore.c
blob13db7f50883134a8f83c4be7618931bba6d2dad0
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "analyze-fdstore.h"
4 #include "analyze.h"
5 #include "bus-error.h"
6 #include "bus-locator.h"
7 #include "fd-util.h"
8 #include "format-table.h"
10 static int dump_fdstore(sd_bus *bus, const char *arg) {
11 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
12 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
13 _cleanup_(table_unrefp) Table *table = NULL;
14 _cleanup_free_ char *unit = NULL;
15 int r;
17 assert(bus);
18 assert(arg);
20 r = unit_name_mangle_with_suffix(arg, NULL, UNIT_NAME_MANGLE_GLOB, ".service", &unit);
21 if (r < 0)
22 return log_error_errno(r, "Failed to mangle name '%s': %m", arg);
24 r = bus_call_method(
25 bus,
26 bus_systemd_mgr,
27 "DumpUnitFileDescriptorStore",
28 &error,
29 &reply,
30 "s", unit);
31 if (r < 0)
32 return log_error_errno(r, "Failed to call DumpUnitFileDescriptorStore: %s",
33 bus_error_message(&error, r));
35 r = sd_bus_message_enter_container(reply, 'a', "(suuutuusu)");
36 if (r < 0)
37 return bus_log_parse_error(r);
39 table = table_new("fdname", "type", "devno", "inode", "rdevno", "path", "flags");
40 if (!table)
41 return log_oom();
43 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
45 (void) table_set_align_percent(table, TABLE_HEADER_CELL(3), 100);
47 for (;;) {
48 uint32_t mode, major, minor, rmajor, rminor, flags;
49 const char *fdname, *path;
50 uint64_t inode;
52 r = sd_bus_message_read(
53 reply,
54 "(suuutuusu)",
55 &fdname,
56 &mode,
57 &major, &minor,
58 &inode,
59 &rmajor, &rminor,
60 &path,
61 &flags);
62 if (r < 0)
63 return bus_log_parse_error(r);
64 if (r == 0)
65 break;
67 r = table_add_many(
68 table,
69 TABLE_STRING, fdname,
70 TABLE_MODE_INODE_TYPE, mode,
71 TABLE_DEVNUM, makedev(major, minor),
72 TABLE_UINT64, inode,
73 TABLE_DEVNUM, makedev(rmajor, rminor),
74 TABLE_PATH, path,
75 TABLE_STRING, accmode_to_string(flags));
76 if (r < 0)
77 return table_log_add_error(r);
80 r = sd_bus_message_exit_container(reply);
81 if (r < 0)
82 return r;
84 if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_get_rows(table) <= 0)
85 log_info("No file descriptors in fdstore of '%s'.", unit);
86 else {
87 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */true);
88 if (r < 0)
89 return log_error_errno(r, "Failed to output table: %m");
92 return EXIT_SUCCESS;
95 int verb_fdstore(int argc, char *argv[], void *userdata) {
96 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
97 int r;
99 r = acquire_bus(&bus, NULL);
100 if (r < 0)
101 return bus_log_connect_error(r, arg_transport);
103 STRV_FOREACH(arg, strv_skip(argv, 1)) {
104 r = dump_fdstore(bus, *arg);
105 if (r < 0)
106 return r;
109 return EXIT_SUCCESS;