1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "sd-bus.h"
4 
5 #include "analyze-dump.h"
6 #include "analyze.h"
7 #include "bus-error.h"
8 #include "bus-locator.h"
9 #include "bus-util.h"
10 #include "copy.h"
11 
dump_fallback(sd_bus * bus)12 static int dump_fallback(sd_bus *bus) {
13         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
14         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
15         const char *text = NULL;
16         int r;
17 
18         assert(bus);
19 
20         r = bus_call_method(bus, bus_systemd_mgr, "Dump", &error, &reply, NULL);
21         if (r < 0)
22                 return log_error_errno(r, "Failed to issue method call Dump: %s", bus_error_message(&error, r));
23 
24         r = sd_bus_message_read(reply, "s", &text);
25         if (r < 0)
26                 return bus_log_parse_error(r);
27 
28         fputs(text, stdout);
29         return 0;
30 }
31 
verb_dump(int argc,char * argv[],void * userdata)32 int verb_dump(int argc, char *argv[], void *userdata) {
33         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
34         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
35         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
36         int fd = -1;
37         int r;
38 
39         r = acquire_bus(&bus, NULL);
40         if (r < 0)
41                 return bus_log_connect_error(r, arg_transport);
42 
43         pager_open(arg_pager_flags);
44 
45         if (!sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD))
46                 return dump_fallback(bus);
47 
48         r = bus_call_method(bus, bus_systemd_mgr, "DumpByFileDescriptor", &error, &reply, NULL);
49         if (r < 0) {
50                 /* fall back to Dump if DumpByFileDescriptor is not supported */
51                 if (!IN_SET(r, -EACCES, -EBADR))
52                         return log_error_errno(r, "Failed to issue method call DumpByFileDescriptor: %s",
53                                                bus_error_message(&error, r));
54 
55                 return dump_fallback(bus);
56         }
57 
58         r = sd_bus_message_read(reply, "h", &fd);
59         if (r < 0)
60                 return bus_log_parse_error(r);
61 
62         fflush(stdout);
63         return copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
64 }
65