| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "bus-error.h" |
| 6 |
#include "bus-locator.h" |
| 7 |
#include "bus-util.h" |
| 8 |
#include "bus-wait-for-units.h" |
| 9 |
#include "log.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "strv.h" |
| 12 |
#include "systemctl.h" |
| 13 |
#include "systemctl-clean-or-freeze.h" |
| 14 |
#include "systemctl-util.h" |
| 15 |
|
| 16 |
int verb_clean_or_freeze(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 17 |
_cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL; |
| 18 |
_cleanup_strv_free_ char **names = NULL; |
| 19 |
int r, ret = EXIT_SUCCESS; |
| 20 |
const char *method; |
| 21 |
sd_bus *bus; |
| 22 |
|
| 23 |
r = acquire_bus(BUS_FULL, &bus); |
| 24 |
if (r < 0) |
| 25 |
return r; |
| 26 |
|
| 27 |
polkit_agent_open_maybe(); |
| 28 |
|
| 29 |
if (!arg_clean_what) { |
| 30 |
arg_clean_what = strv_new("cache", "runtime", "fdstore"); |
| 31 |
if (!arg_clean_what) |
| 32 |
return log_oom(); |
| 33 |
} |
| 34 |
|
| 35 |
r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
| 36 |
if (r < 0) |
| 37 |
return log_error_errno(r, "Failed to expand names: %m"); |
| 38 |
|
| 39 |
if (!arg_no_block) { |
| 40 |
r = bus_wait_for_units_new(bus, &w); |
| 41 |
if (r < 0) |
| 42 |
return log_error_errno(r, "Failed to allocate unit waiter: %m"); |
| 43 |
} |
| 44 |
|
| 45 |
if (streq(argv[0], "clean")) |
| 46 |
method = "CleanUnit"; |
| 47 |
else if (streq(argv[0], "freeze")) |
| 48 |
method = "FreezeUnit"; |
| 49 |
else if (streq(argv[0], "thaw")) |
| 50 |
method = "ThawUnit"; |
| 51 |
else |
| 52 |
assert_not_reached(); |
| 53 |
|
| 54 |
STRV_FOREACH(name, names) { |
| 55 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 56 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 57 |
|
| 58 |
if (w) { |
| 59 |
/* If we shall wait for the cleaning to complete, let's add a ref on the unit first */ |
| 60 |
r = bus_call_method(bus, bus_systemd_mgr, "RefUnit", &error, NULL, "s", *name); |
| 61 |
if (r < 0) { |
| 62 |
log_error_errno(r, "Failed to add reference to unit %s: %s", *name, bus_error_message(&error, r)); |
| 63 |
if (ret == EXIT_SUCCESS) |
| 64 |
ret = r; |
| 65 |
continue; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
| 70 |
if (r < 0) |
| 71 |
return bus_log_create_error(r); |
| 72 |
|
| 73 |
r = sd_bus_message_append(m, "s", *name); |
| 74 |
if (r < 0) |
| 75 |
return bus_log_create_error(r); |
| 76 |
|
| 77 |
if (streq(method, "CleanUnit")) { |
| 78 |
r = sd_bus_message_append_strv(m, arg_clean_what); |
| 79 |
if (r < 0) |
| 80 |
return bus_log_create_error(r); |
| 81 |
} |
| 82 |
|
| 83 |
r = sd_bus_call(bus, m, 0, &error, NULL); |
| 84 |
if (r < 0) { |
| 85 |
log_error_errno(r, "Failed to %s unit %s: %s", argv[0], *name, bus_error_message(&error, r)); |
| 86 |
if (ret == EXIT_SUCCESS) { |
| 87 |
ret = r; |
| 88 |
continue; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (w) { |
| 93 |
r = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_REFFED|BUS_WAIT_FOR_MAINTENANCE_END, NULL, NULL); |
| 94 |
if (r < 0) |
| 95 |
return log_error_errno(r, "Failed to watch unit %s: %m", *name); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
r = bus_wait_for_units_run(w); |
| 100 |
if (r < 0) |
| 101 |
return log_error_errno(r, "Failed to wait for units: %m"); |
| 102 |
if (r == BUS_WAIT_FAILURE) |
| 103 |
ret = EXIT_FAILURE; |
| 104 |
|
| 105 |
return ret; |
| 106 |
} |