summaryrefslogtreecommitdiffstats
path: root/src/analyze/analyze-fdstore.c
blob: 13db7f50883134a8f83c4be7618931bba6d2dad0 (plain)
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "analyze-fdstore.h"
#include "analyze.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "fd-util.h"
#include "format-table.h"

static int dump_fdstore(sd_bus *bus, const char *arg) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *unit = NULL;
        int r;

        assert(bus);
        assert(arg);

        r = unit_name_mangle_with_suffix(arg, NULL, UNIT_NAME_MANGLE_GLOB, ".service", &unit);
        if (r < 0)
                return log_error_errno(r, "Failed to mangle name '%s': %m", arg);

        r = bus_call_method(
                        bus,
                        bus_systemd_mgr,
                        "DumpUnitFileDescriptorStore",
                        &error,
                        &reply,
                        "s", unit);
        if (r < 0)
                return log_error_errno(r, "Failed to call DumpUnitFileDescriptorStore: %s",
                                       bus_error_message(&error, r));

        r = sd_bus_message_enter_container(reply, 'a', "(suuutuusu)");
        if (r < 0)
                return bus_log_parse_error(r);

        table = table_new("fdname", "type", "devno", "inode", "rdevno", "path", "flags");
        if (!table)
                return log_oom();

        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);

        (void) table_set_align_percent(table, TABLE_HEADER_CELL(3), 100);

        for (;;) {
                uint32_t mode, major, minor, rmajor, rminor, flags;
                const char *fdname, *path;
                uint64_t inode;

                r = sd_bus_message_read(
                                reply,
                                "(suuutuusu)",
                                &fdname,
                                &mode,
                                &major, &minor,
                                &inode,
                                &rmajor, &rminor,
                                &path,
                                &flags);
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;

                r = table_add_many(
                                table,
                                TABLE_STRING, fdname,
                                TABLE_MODE_INODE_TYPE, mode,
                                TABLE_DEVNUM, makedev(major, minor),
                                TABLE_UINT64, inode,
                                TABLE_DEVNUM, makedev(rmajor, rminor),
                                TABLE_PATH, path,
                                TABLE_STRING, accmode_to_string(flags));
                if (r < 0)
                        return table_log_add_error(r);
        }

        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return r;

        if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_get_rows(table) <= 0)
                log_info("No file descriptors in fdstore of '%s'.", unit);
        else {
                r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */true);
                if (r < 0)
                        return log_error_errno(r, "Failed to output table: %m");
        }

        return EXIT_SUCCESS;
}

int verb_fdstore(int argc, char *argv[], void *userdata) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        int r;

        r = acquire_bus(&bus, NULL);
        if (r < 0)
                return bus_log_connect_error(r, arg_transport);

        STRV_FOREACH(arg, strv_skip(argv, 1)) {
                r = dump_fdstore(bus, *arg);
                if (r < 0)
                        return r;
        }

        return EXIT_SUCCESS;
}