1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "bus-error.h"
4 #include "bus-locator.h"
5 #include "locale-util.h"
6 #include "path-util.h"
7 #include "systemctl-daemon-reload.h"
8 #include "systemctl-enable.h"
9 #include "systemctl-start-unit.h"
10 #include "systemctl-sysv-compat.h"
11 #include "systemctl-util.h"
12 #include "systemctl.h"
13 
normalize_filenames(char ** names)14 static int normalize_filenames(char **names) {
15         int r;
16 
17         STRV_FOREACH(u, names)
18                 if (!path_is_absolute(*u)) {
19                         char* normalized_path;
20 
21                         if (!isempty(arg_root))
22                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
23                                                        "Non-absolute paths are not allowed when --root is used: %s",
24                                                        *u);
25 
26                         if (!strchr(*u,'/'))
27                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
28                                                        "Link argument does contain at least one directory separator: %s",
29                                                        *u);
30 
31                         r = path_make_absolute_cwd(*u, &normalized_path);
32                         if (r < 0)
33                                 return r;
34 
35                         free_and_replace(*u, normalized_path);
36                 }
37 
38         return 0;
39 }
40 
normalize_names(char ** names)41 static int normalize_names(char **names) {
42         bool was_path = false;
43 
44         STRV_FOREACH(u, names) {
45                 int r;
46 
47                 if (!is_path(*u))
48                         continue;
49 
50                 r = free_and_strdup(u, basename(*u));
51                 if (r < 0)
52                         return log_error_errno(r, "Failed to normalize unit file path: %m");
53 
54                 was_path = true;
55         }
56 
57         if (was_path)
58                 log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name.");
59 
60         return 0;
61 }
62 
verb_enable(int argc,char * argv[],void * userdata)63 int verb_enable(int argc, char *argv[], void *userdata) {
64         _cleanup_strv_free_ char **names = NULL;
65         const char *verb = argv[0];
66         UnitFileChange *changes = NULL;
67         size_t n_changes = 0;
68         int carries_install_info = -1;
69         bool ignore_carries_install_info = arg_quiet;
70         int r;
71 
72         if (!argv[1])
73                 return 0;
74 
75         r = mangle_names("to enable", strv_skip(argv, 1), &names);
76         if (r < 0)
77                 return r;
78 
79         r = enable_sysv_units(verb, names);
80         if (r < 0)
81                 return r;
82 
83         /* If the operation was fully executed by the SysV compat, let's finish early */
84         if (strv_isempty(names)) {
85                 if (arg_no_reload || install_client_side())
86                         return 0;
87 
88                 r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
89                 return r > 0 ? 0 : r;
90         }
91 
92         if (streq(verb, "disable")) {
93                 r = normalize_names(names);
94                 if (r < 0)
95                         return r;
96         }
97 
98         if (streq(verb, "link")) {
99                 r = normalize_filenames(names);
100                 if (r < 0)
101                         return r;
102         }
103 
104         if (install_client_side()) {
105                 UnitFileFlags flags;
106 
107                 flags = unit_file_flags_from_args();
108                 if (streq(verb, "enable")) {
109                         r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes);
110                         carries_install_info = r;
111                 } else if (streq(verb, "disable"))
112                         r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes);
113                 else if (streq(verb, "reenable")) {
114                         r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes);
115                         carries_install_info = r;
116                 } else if (streq(verb, "link"))
117                         r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes);
118                 else if (streq(verb, "preset"))
119                         r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes);
120                 else if (streq(verb, "mask"))
121                         r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes);
122                 else if (streq(verb, "unmask"))
123                         r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes);
124                 else if (streq(verb, "revert"))
125                         r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes);
126                 else
127                         assert_not_reached();
128 
129                 unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet);
130                 if (r < 0)
131                         goto finish;
132                 r = 0;
133         } else {
134                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
135                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
136                 bool expect_carries_install_info = false;
137                 bool send_runtime = true, send_force = true, send_preset_mode = false;
138                 const char *method;
139                 sd_bus *bus;
140 
141                 if (STR_IN_SET(verb, "mask", "unmask")) {
142                         _cleanup_(lookup_paths_free) LookupPaths lp = {};
143 
144                         r = lookup_paths_init_or_warn(&lp, arg_scope, 0, arg_root);
145                         if (r < 0)
146                                 return r;
147 
148                         STRV_FOREACH(name, names) {
149                                 r = unit_exists(&lp, *name);
150                                 if (r < 0)
151                                         return r;
152                                 if (r == 0)
153                                         log_notice("Unit %s does not exist, proceeding anyway.", *name);
154                         }
155                 }
156 
157                 r = acquire_bus(BUS_MANAGER, &bus);
158                 if (r < 0)
159                         return r;
160 
161                 polkit_agent_open_maybe();
162 
163                 if (streq(verb, "enable")) {
164                         method = "EnableUnitFiles";
165                         expect_carries_install_info = true;
166                 } else if (streq(verb, "disable")) {
167                         method = "DisableUnitFiles";
168                         send_force = false;
169                 } else if (streq(verb, "reenable")) {
170                         method = "ReenableUnitFiles";
171                         expect_carries_install_info = true;
172                 } else if (streq(verb, "link"))
173                         method = "LinkUnitFiles";
174                 else if (streq(verb, "preset")) {
175 
176                         if (arg_preset_mode != UNIT_FILE_PRESET_FULL) {
177                                 method = "PresetUnitFilesWithMode";
178                                 send_preset_mode = true;
179                         } else
180                                 method = "PresetUnitFiles";
181 
182                         expect_carries_install_info = true;
183                         ignore_carries_install_info = true;
184                 } else if (streq(verb, "mask"))
185                         method = "MaskUnitFiles";
186                 else if (streq(verb, "unmask")) {
187                         method = "UnmaskUnitFiles";
188                         send_force = false;
189                 } else if (streq(verb, "revert")) {
190                         method = "RevertUnitFiles";
191                         send_runtime = send_force = false;
192                 } else
193                         assert_not_reached();
194 
195                 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method);
196                 if (r < 0)
197                         return bus_log_create_error(r);
198 
199                 r = sd_bus_message_append_strv(m, names);
200                 if (r < 0)
201                         return bus_log_create_error(r);
202 
203                 if (send_preset_mode) {
204                         r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode));
205                         if (r < 0)
206                                 return bus_log_create_error(r);
207                 }
208 
209                 if (send_runtime) {
210                         r = sd_bus_message_append(m, "b", arg_runtime);
211                         if (r < 0)
212                                 return bus_log_create_error(r);
213                 }
214 
215                 if (send_force) {
216                         r = sd_bus_message_append(m, "b", arg_force);
217                         if (r < 0)
218                                 return bus_log_create_error(r);
219                 }
220 
221                 r = sd_bus_call(bus, m, 0, &error, &reply);
222                 if (r < 0)
223                         return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r));
224 
225                 if (expect_carries_install_info) {
226                         r = sd_bus_message_read(reply, "b", &carries_install_info);
227                         if (r < 0)
228                                 return bus_log_parse_error(r);
229                 }
230 
231                 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
232                 if (r < 0)
233                         goto finish;
234 
235                 /* Try to reload if enabled */
236                 if (!arg_no_reload) {
237                         r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
238                         if (r > 0)
239                                 r = 0;
240                 } else
241                         r = 0;
242         }
243 
244         if (carries_install_info == 0 && !ignore_carries_install_info)
245                 log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n"
246                            "Alias= settings in the [Install] section, and DefaultInstance= for template\n"
247                            "units). This means they are not meant to be enabled using systemctl.\n"
248                            " \n" /* trick: the space is needed so that the line does not get stripped from output */
249                            "Possible reasons for having this kind of units are:\n"
250                            "%1$s A unit may be statically enabled by being symlinked from another unit's\n"
251                            "  .wants/ or .requires/ directory.\n"
252                            "%1$s A unit's purpose may be to act as a helper for some other unit which has\n"
253                            "  a requirement dependency on it.\n"
254                            "%1$s A unit may be started when needed via activation (socket, path, timer,\n"
255                            "  D-Bus, udev, scripted systemctl call, ...).\n"
256                            "%1$s In case of template units, the unit is meant to be enabled with some\n"
257                            "  instance name specified.",
258                            special_glyph(SPECIAL_GLYPH_BULLET));
259 
260         if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) {
261                 sd_bus *bus;
262                 size_t len, i;
263 
264                 r = acquire_bus(BUS_MANAGER, &bus);
265                 if (r < 0)
266                         goto finish;
267 
268                 len = strv_length(names);
269                 {
270                         char *new_args[len + 2];
271 
272                         new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop");
273                         for (i = 0; i < len; i++)
274                                 new_args[i + 1] = basename(names[i]);
275                         new_args[i + 1] = NULL;
276 
277                         r = verb_start(len + 1, new_args, userdata);
278                 }
279         }
280 
281 finish:
282         unit_file_changes_free(changes, n_changes);
283 
284         return r;
285 }
286