Debian Codegraph @

src/systemctl/systemctl-log-setting.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-error.h"
7 #include "log.h"
8 #include "pretty-print.h"
9 #include "string-util.h"
10 #include "systemctl.h"
11 #include "systemctl-log-setting.h"
12 #include "systemctl-util.h"
13 #include "unit-def.h"
14 #include "unit-name.h"
15 #include "verb-log-control.h"
16
17 static void give_log_control1_hint(const char *name) {
18 _cleanup_free_ char *link = NULL;
19
20 if (arg_quiet)
21 return;
22
23 (void) terminal_urlify_man("org.freedesktop.LogControl1", "5", &link);
24
25 log_notice("Hint: the service must declare BusName= and implement the appropriate D-Bus interface.\n"
26 " See the %s for details.", link ?: "org.freedesktop.LogControl1(5) man page");
27 }
28
29 int verb_log_setting(int argc, char *argv[], uintptr_t _data, void *userdata) {
30 sd_bus *bus;
31 int r;
32
33 assert(argc >= 1 && argc <= 2);
34
35 r = acquire_bus(BUS_MANAGER, &bus);
36 if (r < 0)
37 return r;
38
39 return verb_log_control_common(bus, "org.freedesktop.systemd1", argv[0], argv[1]);
40 }
41
42 static int service_name_to_dbus(sd_bus *bus, const char *name, char **ret_dbus_name) {
43 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
44 _cleanup_free_ char *bus_name = NULL;
45 int r;
46
47 assert(ret_dbus_name);
48
49 /* First, look for the BusName= property */
50 _cleanup_free_ char *dbus_path = unit_dbus_path_from_name(name);
51 if (!dbus_path)
52 return log_oom();
53
54 r = sd_bus_get_property_string(
55 bus,
56 "org.freedesktop.systemd1",
57 dbus_path,
58 "org.freedesktop.systemd1.Service",
59 "BusName",
60 &error,
61 &bus_name);
62 if (r < 0)
63 return log_error_errno(r, "Failed to obtain BusName= property of %s: %s",
64 name, bus_error_message(&error, r));
65
66 if (isempty(bus_name)) {
67 log_error("Unit %s doesn't declare BusName=.", name);
68 give_log_control1_hint(name);
69 return -ENOLINK;
70 }
71
72 *ret_dbus_name = TAKE_PTR(bus_name);
73 return 0;
74 }
75
76 int verb_service_log_setting(int argc, char *argv[], uintptr_t _data, void *userdata) {
77 sd_bus *bus;
78 _cleanup_free_ char *unit = NULL, *dbus_name = NULL;
79 int r;
80
81 assert(argc >= 2 && argc <= 3);
82
83 r = acquire_bus(BUS_FULL, &bus);
84 if (r < 0)
85 return r;
86
87 r = unit_name_mangle_with_suffix(argv[1], argv[0],
88 arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
89 ".service", &unit);
90 if (r < 0)
91 return log_error_errno(r, "Failed to mangle unit name: %m");
92
93 r = service_name_to_dbus(bus, unit, &dbus_name);
94 if (r < 0)
95 return r;
96
97 r = verb_log_control_common(bus, dbus_name, argv[0], argv[2]);
98
99 if (r == -EBADR)
100 give_log_control1_hint(dbus_name);
101
102 return r;
103 }