diff --git a/man/systemd-analyze.xml b/man/systemd-analyze.xml index f81dcc324a..8ceeccb37a 100644 --- a/man/systemd-analyze.xml +++ b/man/systemd-analyze.xml @@ -139,6 +139,12 @@ security UNIT + + systemd-analyze + OPTIONS + malloc + D-BUS SERVICE + @@ -271,6 +277,16 @@ Timestamp units-load-finish: Thu 2019-03-14 23:28:07 CET + + <command>systemd-analyze malloc [<replaceable>D-Bus service</replaceable>…]</command> + + This command can be used to request the output of the internal memory state (as returned by + malloc_info3) of + a D-Bus service implementing this pattern. If no service is specified, the command will be sent to + org.freedesktop.systemd1 (the system or user service manager). The output format + is subject to change without notice and should not be parsed by applications. + + <command>systemd-analyze plot</command> diff --git a/src/analyze/analyze-dump.c b/src/analyze/analyze-dump.c index 2e838c906f..2642582903 100644 --- a/src/analyze/analyze-dump.c +++ b/src/analyze/analyze-dump.c @@ -29,21 +29,6 @@ static int dump_fallback(sd_bus *bus) { return 0; } -static int dump_fd_reply(sd_bus_message *message) { - int fd, r; - - r = sd_bus_message_read(message, "h", &fd); - if (r < 0) - return bus_log_parse_error(r); - - fflush(stdout); - r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0); - if (r < 0) - return r; - - return 1; /* Success */ -} - static int dump(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; diff --git a/src/analyze/analyze-malloc.c b/src/analyze/analyze-malloc.c new file mode 100644 index 0000000000..5e6ff5bb96 --- /dev/null +++ b/src/analyze/analyze-malloc.c @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "sd-bus.h" + +#include "analyze-malloc.h" +#include "analyze.h" +#include "bus-error.h" +#include "bus-internal.h" + +static int dump_malloc_info(sd_bus *bus, char *service) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + int r; + + assert(bus); + assert(service); + + r = sd_bus_call_method(bus, + service, + "/org/freedesktop/MemoryAllocation1", + "org.freedesktop.MemoryAllocation1", + "GetMallocInfo", + &error, + &reply, + NULL); + if (r < 0) + return log_error_errno(r, "Failed to call GetMallocInfo on '%s': %s", service, bus_error_message(&error, r)); + + return dump_fd_reply(reply); +} + +int verb_malloc(int argc, char *argv[], void *userdata) { + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; + char **services = STRV_MAKE("org.freedesktop.systemd1"); + int r; + + if (!strv_isempty(strv_skip(argv, 1))) { + services = strv_skip(argv, 1); + STRV_FOREACH(service, services) + if (!service_name_is_valid(*service)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "D-Bus service name '%s' is not valid.", *service); + } + + r = acquire_bus(&bus, NULL); + if (r < 0) + return bus_log_connect_error(r, arg_transport); + + r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD); + if (r < 0) + return log_error_errno(r, "Unable to determine if bus connection supports fd passing: %m"); + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unable to receive FDs over D-Bus."); + + pager_open(arg_pager_flags); + + STRV_FOREACH(service, services) { + r = dump_malloc_info(bus, *service); + if (r < 0) + return r; + } + + return EXIT_SUCCESS; +} diff --git a/src/analyze/analyze-malloc.h b/src/analyze/analyze-malloc.h new file mode 100644 index 0000000000..d3feabd757 --- /dev/null +++ b/src/analyze/analyze-malloc.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#pragma once + +int verb_malloc(int argc, char *argv[], void *userdata); diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index aaa7e4b706..d7688c1a85 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -25,6 +25,7 @@ #include "analyze-filesystems.h" #include "analyze-inspect-elf.h" #include "analyze-log-control.h" +#include "analyze-malloc.h" #include "analyze-plot.h" #include "analyze-security.h" #include "analyze-service-watchdogs.h" @@ -166,6 +167,23 @@ void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timesp "Use 'systemd-analyze timespan \"%s\"' instead?", p); } +int dump_fd_reply(sd_bus_message *message) { + int fd, r; + + assert(message); + + r = sd_bus_message_read(message, "h", &fd); + if (r < 0) + return bus_log_parse_error(r); + + fflush(stdout); + r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0); + if (r < 0) + return r; + + return 1; /* Success */ +} + static int help(int argc, char *argv[], void *userdata) { _cleanup_free_ char *link = NULL, *dot_link = NULL; int r; @@ -211,6 +229,7 @@ static int help(int argc, char *argv[], void *userdata) { " timespan SPAN... Validate a time span\n" " security [UNIT...] Analyze security of unit\n" " inspect-elf FILE... Parse and print ELF package metadata\n" + " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n" "\nOptions:\n" " --recursive-errors=MODE Control which units are verified\n" " --offline=BOOL Perform a security review on unit file(s)\n" @@ -601,6 +620,7 @@ static int run(int argc, char *argv[]) { { "timespan", 2, VERB_ANY, 0, verb_timespan }, { "security", VERB_ANY, VERB_ANY, 0, verb_security }, { "inspect-elf", 2, VERB_ANY, 0, verb_elf_inspection }, + { "malloc", VERB_ANY, VERB_ANY, 0, verb_malloc }, {} }; diff --git a/src/analyze/analyze.h b/src/analyze/analyze.h index e4af7b47e0..35b1cddb38 100644 --- a/src/analyze/analyze.h +++ b/src/analyze/analyze.h @@ -44,3 +44,5 @@ int acquire_bus(sd_bus **bus, bool *use_full_bus); int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv); void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan); + +int dump_fd_reply(sd_bus_message *message); diff --git a/src/analyze/meson.build b/src/analyze/meson.build index 6493cb2e99..ac40600a6d 100644 --- a/src/analyze/meson.build +++ b/src/analyze/meson.build @@ -14,6 +14,7 @@ systemd_analyze_sources = files( 'analyze-filesystems.c', 'analyze-inspect-elf.c', 'analyze-log-control.c', + 'analyze-malloc.c', 'analyze-plot.c', 'analyze-security.c', 'analyze-service-watchdogs.c', diff --git a/src/core/dbus.c b/src/core/dbus.c index c295e1fbd4..9ce9ddc8f2 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -737,6 +737,10 @@ static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void return 0; } + r = bus_register_malloc_status(bus, "org.freedesktop.systemd1"); + if (r < 0) + log_warning_errno(r, "Failed to register MemoryAllocation1, ignoring: %m"); + r = set_ensure_put(&m->private_buses, NULL, bus); if (r == -ENOMEM) { log_oom(); @@ -798,6 +802,10 @@ static int bus_setup_api(Manager *m, sd_bus *bus) { if (r < 0) return log_error_errno(r, "Failed to request name: %m"); + r = bus_register_malloc_status(bus, "org.freedesktop.systemd1"); + if (r < 0) + log_warning_errno(r, "Failed to register MemoryAllocation1, ignoring: %m"); + log_debug("Successfully connected to API bus."); return 0; diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index d09ec5148d..61ec6661e0 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -18,6 +18,8 @@ #include "bus-internal.h" #include "bus-label.h" #include "bus-util.h" +#include "data-fd-util.h" +#include "fd-util.h" #include "path-util.h" #include "socket-util.h" #include "stdio-util.h" @@ -578,6 +580,62 @@ int bus_reply_pair_array(sd_bus_message *m, char **l) { return sd_bus_send(NULL, reply, NULL); } +static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { + _cleanup_free_ char *dump = NULL; /* keep this above dump_file, so that it's freed after */ + _cleanup_fclose_ FILE *dump_file = NULL; + _cleanup_close_ int fd = -EBADF; + size_t dump_size; + int r; + + assert(message); + + dump_file = open_memstream(&dump, &dump_size); + if (!dump_file) + return -ENOMEM; + + r = RET_NERRNO(malloc_info(/* options= */ 0, dump_file)); + if (r < 0) + return r; + + dump_file = safe_fclose(dump_file); + + fd = acquire_data_fd(dump, dump_size, 0); + if (fd < 0) + return fd; + + r = sd_bus_reply_method_return(message, "h", fd); + if (r < 0) + return r; + + return 1; /* Stop further processing */ +} + +/* The default install callback will fail and disconnect the bus if it cannot register the match, but this + * is only a debug method, we definitely don't want to fail in case there's some permission issue. */ +static int dummy_install_callback(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { + return 1; +} + +int bus_register_malloc_status(sd_bus *bus, const char *destination) { + const char *match; + int r; + + assert(bus); + assert(!isempty(destination)); + + match = strjoina("type='method_call'," + "interface='org.freedesktop.MemoryAllocation1'," + "path='/org/freedesktop/MemoryAllocation1'," + "destination='", destination, "',", + "member='GetMallocInfo'"); + + r = sd_bus_add_match_async(bus, NULL, match, method_dump_memory_state_by_fd, dummy_install_callback, NULL); + if (r < 0) + return log_debug_errno(r, "Failed to subscribe to GetMallocInfo() calls on MemoryAllocation1 interface: %m"); + + return 0; +} + static void bus_message_unref_wrapper(void *m) { sd_bus_message_unref(m); } diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index ab553c866c..6489bfc756 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -63,4 +63,7 @@ static inline int bus_open_system_watch_bind(sd_bus **ret) { int bus_reply_pair_array(sd_bus_message *m, char **l); +/* Listen to GetMallocInfo() calls to 'destination' and return malloc_info() via FD */ +int bus_register_malloc_status(sd_bus *bus, const char *destination); + extern const struct hash_ops bus_message_hash_ops; diff --git a/test/units/testsuite-65.sh b/test/units/testsuite-65.sh index 4093c5a2a7..edaf667107 100755 --- a/test/units/testsuite-65.sh +++ b/test/units/testsuite-65.sh @@ -56,6 +56,7 @@ systemd-analyze dump "*" >/dev/null systemd-analyze dump "*.socket" >/dev/null systemd-analyze dump "*.socket" "*.service" aaaaaaa ... >/dev/null systemd-analyze dump systemd-journald.service >/dev/null +systemd-analyze malloc >/dev/null (! systemd-analyze dump "") # unit-files systemd-analyze unit-files >/dev/null