Merge pull request #34743 from yuwata/bus-message-dump-fd
bus-message-util: introduce bus_message_dump_fd() and _string()
This commit is contained in:
+21
-35
@@ -6,13 +6,12 @@
|
||||
#include "analyze.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-locator.h"
|
||||
#include "bus-message-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "copy.h"
|
||||
|
||||
static int dump_fallback(sd_bus *bus) {
|
||||
static int dump_string(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;
|
||||
const char *text;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
@@ -21,36 +20,34 @@ static int dump_fallback(sd_bus *bus) {
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to call Dump: %s", bus_error_message(&error, r));
|
||||
|
||||
r = sd_bus_message_read(reply, "s", &text);
|
||||
if (r < 0)
|
||||
return bus_log_parse_error(r);
|
||||
|
||||
fputs(text, stdout);
|
||||
return 0;
|
||||
return bus_message_dump_string(reply);
|
||||
}
|
||||
|
||||
static int dump(sd_bus *bus) {
|
||||
static int dump_fd(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;
|
||||
int r;
|
||||
|
||||
r = bus_call_method(bus, bus_systemd_mgr, "DumpByFileDescriptor", &error, &reply, NULL);
|
||||
if (IN_SET(r, -EACCES, -EBADR))
|
||||
return 0; /* Fall back to non-fd method. We need to do this even if the bus supports sending
|
||||
* fds to cater to very old managers which didn't have the fd-based method. */
|
||||
/* Fall back to non-fd method. We need to do this even if the bus supports sending
|
||||
* fds to cater to very old managers which didn't have the fd-based method. */
|
||||
return dump_string(bus);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to call DumpByFileDescriptor: %s",
|
||||
bus_error_message(&error, r));
|
||||
|
||||
return dump_fd_reply(reply);
|
||||
return bus_message_dump_fd(reply);
|
||||
}
|
||||
|
||||
static int dump_patterns_fallback(sd_bus *bus, char **patterns) {
|
||||
static int dump_patterns_string(sd_bus *bus, char **patterns) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
|
||||
const char *text;
|
||||
int r;
|
||||
|
||||
if (strv_isempty(patterns))
|
||||
return dump_string(bus);
|
||||
|
||||
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "DumpUnitsMatchingPatterns");
|
||||
if (r < 0)
|
||||
return bus_log_create_error(r);
|
||||
@@ -64,19 +61,17 @@ static int dump_patterns_fallback(sd_bus *bus, char **patterns) {
|
||||
return log_error_errno(r, "Failed to call DumpUnitsMatchingPatterns: %s",
|
||||
bus_error_message(&error, r));
|
||||
|
||||
r = sd_bus_message_read(reply, "s", &text);
|
||||
if (r < 0)
|
||||
return bus_log_parse_error(r);
|
||||
|
||||
fputs(text, stdout);
|
||||
return 0;
|
||||
return bus_message_dump_string(reply);
|
||||
}
|
||||
|
||||
static int dump_patterns(sd_bus *bus, char **patterns) {
|
||||
static int dump_patterns_fd(sd_bus *bus, char **patterns) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
|
||||
int r;
|
||||
|
||||
if (strv_isempty(patterns))
|
||||
return dump_fd(bus);
|
||||
|
||||
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "DumpUnitsMatchingPatternsByFileDescriptor");
|
||||
if (r < 0)
|
||||
return bus_log_create_error(r);
|
||||
@@ -90,7 +85,7 @@ static int dump_patterns(sd_bus *bus, char **patterns) {
|
||||
return log_error_errno(r, "Failed to call DumpUnitsMatchingPatternsByFileDescriptor: %s",
|
||||
bus_error_message(&error, r));
|
||||
|
||||
return dump_fd_reply(reply);
|
||||
return bus_message_dump_fd(reply);
|
||||
}
|
||||
|
||||
static int mangle_patterns(char **args, char ***ret) {
|
||||
@@ -109,9 +104,6 @@ static int mangle_patterns(char **args, char ***ret) {
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
if (strv_isempty(mangled))
|
||||
mangled = strv_free(mangled);
|
||||
|
||||
*ret = TAKE_PTR(mangled);
|
||||
return 0;
|
||||
}
|
||||
@@ -125,8 +117,6 @@ int verb_dump(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
|
||||
|
||||
pager_open(arg_pager_flags);
|
||||
|
||||
r = mangle_patterns(strv_skip(argv, 1), &patterns);
|
||||
if (r < 0)
|
||||
return r;
|
||||
@@ -134,12 +124,8 @@ int verb_dump(int argc, char *argv[], void *userdata) {
|
||||
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)
|
||||
r = patterns ? dump_patterns(bus, patterns) : dump(bus);
|
||||
if (r == 0) /* wasn't supported */
|
||||
r = patterns ? dump_patterns_fallback(bus, patterns) : dump_fallback(bus);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
pager_open(arg_pager_flags);
|
||||
|
||||
return r > 0 ? dump_patterns_fd(bus, patterns) : dump_patterns_string(bus, patterns);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "analyze.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-internal.h"
|
||||
#include "bus-message-util.h"
|
||||
|
||||
static int dump_malloc_info(sd_bus *bus, char *service) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
@@ -26,7 +27,7 @@ static int dump_malloc_info(sd_bus *bus, char *service) {
|
||||
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);
|
||||
return bus_message_dump_fd(reply);
|
||||
}
|
||||
|
||||
int verb_malloc(int argc, char *argv[], void *userdata) {
|
||||
|
||||
@@ -179,23 +179,6 @@ 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;
|
||||
|
||||
@@ -57,5 +57,3 @@ 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);
|
||||
|
||||
+2
-8
@@ -6,7 +6,7 @@
|
||||
#include "build.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-locator.h"
|
||||
#include "copy.h"
|
||||
#include "bus-message-util.h"
|
||||
#include "main-func.h"
|
||||
#include "pretty-print.h"
|
||||
#include "terminal-util.h"
|
||||
@@ -47,7 +47,6 @@ static int dump_state(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
int fd = -EBADF;
|
||||
int r;
|
||||
|
||||
r = sd_bus_open_system(&bus);
|
||||
@@ -60,12 +59,7 @@ static int dump_state(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to dump context: %s", bus_error_message(&error, r));
|
||||
|
||||
r = sd_bus_message_read(reply, "h", &fd);
|
||||
if (r < 0)
|
||||
return bus_log_parse_error(r);
|
||||
|
||||
fflush(stdout);
|
||||
return copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
|
||||
return bus_message_dump_fd(reply);
|
||||
}
|
||||
|
||||
static int parse_argv(int argc, char *argv[]) {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "bus-message-util.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#include "bus-message-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "copy.h"
|
||||
#include "resolve-util.h"
|
||||
|
||||
int bus_message_read_ifindex(sd_bus_message *message, sd_bus_error *error, int *ret) {
|
||||
@@ -183,3 +186,34 @@ clear:
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int bus_message_dump_string(sd_bus_message *message) {
|
||||
const char *s;
|
||||
int r;
|
||||
|
||||
assert(message);
|
||||
|
||||
r = sd_bus_message_read(message, "s", &s);
|
||||
if (r < 0)
|
||||
return bus_log_parse_error(r);
|
||||
|
||||
fputs(s, stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bus_message_dump_fd(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 log_error_errno(r, "Failed to dump contents in received file descriptor: %m");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -16,3 +16,6 @@ int bus_message_read_dns_servers(
|
||||
bool extended,
|
||||
struct in_addr_full ***ret_dns,
|
||||
size_t *ret_n_dns);
|
||||
|
||||
int bus_message_dump_string(sd_bus_message *message);
|
||||
int bus_message_dump_fd(sd_bus_message *message);
|
||||
|
||||
Reference in New Issue
Block a user