Debian Codegraph @

src/systemctl/systemctl-add-dependency.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 "bus-locator.h"
8 #include "bus-unit-util.h"
9 #include "bus-util.h"
10 #include "install.h"
11 #include "log.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "systemctl.h"
15 #include "systemctl-add-dependency.h"
16 #include "systemctl-daemon-reload.h"
17 #include "systemctl-util.h"
18 #include "unit-name.h"
19
20 int verb_add_dependency(int argc, char *argv[], uintptr_t _data, void *userdata) {
21 _cleanup_strv_free_ char **names = NULL;
22 _cleanup_free_ char *target = NULL;
23 const char *verb = argv[0];
24 UnitDependency dep;
25 int r;
26
27 if (!argv[1])
28 return 0;
29
30 r = unit_name_mangle_with_suffix(argv[1], "as target",
31 arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
32 ".target", &target);
33 if (r < 0)
34 return log_error_errno(r, "Failed to mangle unit name: %m");
35
36 r = mangle_names("as dependency", strv_skip(argv, 2), &names);
37 if (r < 0)
38 return r;
39
40 if (streq(verb, "add-wants"))
41 dep = UNIT_WANTS;
42 else if (streq(verb, "add-requires"))
43 dep = UNIT_REQUIRES;
44 else
45 assert_not_reached();
46
47 if (install_client_side() != INSTALL_CLIENT_SIDE_NO) {
48 InstallChange *changes = NULL;
49 size_t n_changes = 0;
50
51 CLEANUP_ARRAY(changes, n_changes, install_changes_free);
52
53 r = unit_file_add_dependency(arg_runtime_scope, unit_file_flags_from_args(), arg_root, names, target, dep, &changes, &n_changes);
54 r = install_changes_dump(r, "add dependency on", changes, n_changes, arg_quiet);
55 if (r < 0)
56 return r;
57 } else {
58 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
59 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
60 sd_bus *bus;
61
62 r = acquire_bus(BUS_MANAGER, &bus);
63 if (r < 0)
64 return r;
65
66 polkit_agent_open_maybe();
67
68 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "AddDependencyUnitFiles");
69 if (r < 0)
70 return bus_log_create_error(r);
71
72 r = sd_bus_message_append_strv(m, names);
73 if (r < 0)
74 return bus_log_create_error(r);
75
76 r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force);
77 if (r < 0)
78 return bus_log_create_error(r);
79
80 r = sd_bus_call(bus, m, 0, &error, &reply);
81 if (r < 0)
82 return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r));
83
84 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
85 if (r < 0)
86 return r;
87
88 if (!arg_no_reload) {
89 r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
90 if (r < 0)
91 return r;
92 }
93 }
94
95 return 0;
96 }