| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/sysmacros.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "analyze.h" |
| 9 |
#include "analyze-fdstore.h" |
| 10 |
#include "bus-error.h" |
| 11 |
#include "bus-locator.h" |
| 12 |
#include "bus-util.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "format-table.h" |
| 15 |
#include "runtime-scope.h" |
| 16 |
#include "strv.h" |
| 17 |
#include "unit-name.h" |
| 18 |
|
| 19 |
static int dump_fdstore(sd_bus *bus, const char *arg) { |
| 20 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 21 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 22 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 23 |
_cleanup_free_ char *unit = NULL; |
| 24 |
int r; |
| 25 |
|
| 26 |
assert(bus); |
| 27 |
assert(arg); |
| 28 |
|
| 29 |
r = unit_name_mangle_with_suffix(arg, NULL, UNIT_NAME_MANGLE_GLOB, ".service", &unit); |
| 30 |
if (r < 0) |
| 31 |
return log_error_errno(r, "Failed to mangle name '%s': %m", arg); |
| 32 |
|
| 33 |
r = bus_call_method( |
| 34 |
bus, |
| 35 |
bus_systemd_mgr, |
| 36 |
"DumpUnitFileDescriptorStore", |
| 37 |
&error, |
| 38 |
&reply, |
| 39 |
"s", unit); |
| 40 |
if (r < 0) |
| 41 |
return log_error_errno(r, "Failed to call DumpUnitFileDescriptorStore: %s", |
| 42 |
bus_error_message(&error, r)); |
| 43 |
|
| 44 |
r = sd_bus_message_enter_container(reply, 'a', "(suuutuusu)"); |
| 45 |
if (r < 0) |
| 46 |
return bus_log_parse_error(r); |
| 47 |
|
| 48 |
table = table_new("fdname", "type", "devno", "inode", "rdevno", "path", "flags"); |
| 49 |
if (!table) |
| 50 |
return log_oom(); |
| 51 |
|
| 52 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 53 |
|
| 54 |
(void) table_set_align_percent(table, TABLE_HEADER_CELL(3), 100); |
| 55 |
|
| 56 |
for (;;) { |
| 57 |
uint32_t mode, major, minor, rmajor, rminor, flags; |
| 58 |
const char *fdname, *path; |
| 59 |
uint64_t inode; |
| 60 |
|
| 61 |
r = sd_bus_message_read( |
| 62 |
reply, |
| 63 |
"(suuutuusu)", |
| 64 |
&fdname, |
| 65 |
&mode, |
| 66 |
&major, &minor, |
| 67 |
&inode, |
| 68 |
&rmajor, &rminor, |
| 69 |
&path, |
| 70 |
&flags); |
| 71 |
if (r < 0) |
| 72 |
return bus_log_parse_error(r); |
| 73 |
if (r == 0) |
| 74 |
break; |
| 75 |
|
| 76 |
r = table_add_many( |
| 77 |
table, |
| 78 |
TABLE_STRING, fdname, |
| 79 |
TABLE_MODE_INODE_TYPE, mode, |
| 80 |
TABLE_DEVNUM, makedev(major, minor), |
| 81 |
TABLE_UINT64, inode, |
| 82 |
TABLE_DEVNUM, makedev(rmajor, rminor), |
| 83 |
TABLE_PATH, path, |
| 84 |
TABLE_STRING, accmode_to_string(flags)); |
| 85 |
if (r < 0) |
| 86 |
return table_log_add_error(r); |
| 87 |
} |
| 88 |
|
| 89 |
r = sd_bus_message_exit_container(reply); |
| 90 |
if (r < 0) |
| 91 |
return r; |
| 92 |
|
| 93 |
if (table_isempty(table) && !sd_json_format_enabled(arg_json_format_flags)) |
| 94 |
log_info("No file descriptors in fdstore of '%s'.", unit); |
| 95 |
else { |
| 96 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ true); |
| 97 |
if (r < 0) |
| 98 |
return r; |
| 99 |
} |
| 100 |
|
| 101 |
return EXIT_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
int verb_fdstore(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 105 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 106 |
int r; |
| 107 |
|
| 108 |
r = acquire_bus(&bus, NULL); |
| 109 |
if (r < 0) |
| 110 |
return bus_log_connect_error(r, arg_transport, arg_runtime_scope); |
| 111 |
|
| 112 |
STRV_FOREACH(arg, strv_skip(argv, 1)) { |
| 113 |
r = dump_fdstore(bus, *arg); |
| 114 |
if (r < 0) |
| 115 |
return r; |
| 116 |
} |
| 117 |
|
| 118 |
return EXIT_SUCCESS; |
| 119 |
} |