1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "bus-error.h"
4 #include "bus-locator.h"
5 #include "systemctl-daemon-reload.h"
6 #include "systemctl-preset-all.h"
7 #include "systemctl-util.h"
8 #include "systemctl.h"
9 
verb_preset_all(int argc,char * argv[],void * userdata)10 int verb_preset_all(int argc, char *argv[], void *userdata) {
11         UnitFileChange *changes = NULL;
12         size_t n_changes = 0;
13         int r;
14 
15         if (install_client_side()) {
16                 r = unit_file_preset_all(arg_scope, unit_file_flags_from_args(), arg_root, arg_preset_mode, &changes, &n_changes);
17                 unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet);
18 
19                 if (r > 0)
20                         r = 0;
21         } else {
22                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
23                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
24                 sd_bus *bus;
25 
26                 r = acquire_bus(BUS_MANAGER, &bus);
27                 if (r < 0)
28                         return r;
29 
30                 polkit_agent_open_maybe();
31 
32                 r = bus_call_method(
33                                 bus,
34                                 bus_systemd_mgr,
35                                 "PresetAllUnitFiles",
36                                 &error,
37                                 &reply,
38                                 "sbb",
39                                 unit_file_preset_mode_to_string(arg_preset_mode),
40                                 arg_runtime,
41                                 arg_force);
42                 if (r < 0)
43                         return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r));
44 
45                 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
46                 if (r < 0)
47                         goto finish;
48 
49                 if (arg_no_reload) {
50                         r = 0;
51                         goto finish;
52                 }
53 
54                 r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
55                 if (r > 0)
56                         r = 0;
57         }
58 
59 finish:
60         unit_file_changes_free(changes, n_changes);
61 
62         return r;
63 }
64