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