| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
#include "sd-daemon.h" |
| 5 |
#include "sd-event.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "bus-error.h" |
| 9 |
#include "bus-locator.h" |
| 10 |
#include "bus-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "strv.h" |
| 13 |
#include "systemctl.h" |
| 14 |
#include "systemctl-is-system-running.h" |
| 15 |
#include "systemctl-util.h" |
| 16 |
#include "virt.h" |
| 17 |
|
| 18 |
static int match_startup_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 19 |
char **state = ASSERT_PTR(userdata); |
| 20 |
int r; |
| 21 |
|
| 22 |
r = bus_get_property_string(sd_bus_message_get_bus(m), bus_systemd_mgr, "SystemState", NULL, state); |
| 23 |
|
| 24 |
sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), r); |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
int verb_is_system_running(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 29 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 30 |
_cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_startup_finished = NULL; |
| 31 |
_cleanup_(sd_event_unrefp) sd_event* event = NULL; |
| 32 |
_cleanup_free_ char *state = NULL; |
| 33 |
sd_bus *bus; |
| 34 |
int r; |
| 35 |
|
| 36 |
if (!isempty(arg_root) || running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
| 37 |
if (!arg_quiet) |
| 38 |
puts("offline"); |
| 39 |
return EXIT_FAILURE; |
| 40 |
} |
| 41 |
|
| 42 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 43 |
if (r < 0) |
| 44 |
return r; |
| 45 |
|
| 46 |
if (arg_wait) { |
| 47 |
r = sd_event_default(&event); |
| 48 |
if (r >= 0) |
| 49 |
r = sd_bus_attach_event(bus, event, 0); |
| 50 |
if (r >= 0) |
| 51 |
r = bus_match_signal_async( |
| 52 |
bus, |
| 53 |
&slot_startup_finished, |
| 54 |
bus_systemd_mgr, |
| 55 |
"StartupFinished", |
| 56 |
match_startup_finished, NULL, &state); |
| 57 |
if (r < 0) { |
| 58 |
log_warning_errno(r, "Failed to request match for StartupFinished: %m"); |
| 59 |
arg_wait = false; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
r = bus_get_property_string(bus, bus_systemd_mgr, "SystemState", &error, &state); |
| 64 |
if (r < 0) { |
| 65 |
log_warning_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); |
| 66 |
|
| 67 |
if (!arg_quiet) |
| 68 |
puts("unknown"); |
| 69 |
return EXIT_FAILURE; |
| 70 |
} |
| 71 |
|
| 72 |
if (arg_wait && STR_IN_SET(state, "initializing", "starting")) { |
| 73 |
/* The signal handler will allocate memory and assign to 'state', hence need to free previous |
| 74 |
* one before entering the event loop. */ |
| 75 |
state = mfree(state); |
| 76 |
|
| 77 |
r = sd_event_loop(event); |
| 78 |
if (r < 0) { |
| 79 |
log_warning_errno(r, "Failed to get property from event loop: %m"); |
| 80 |
if (!arg_quiet) |
| 81 |
puts("unknown"); |
| 82 |
return EXIT_FAILURE; |
| 83 |
} |
| 84 |
|
| 85 |
assert(state); |
| 86 |
} |
| 87 |
|
| 88 |
if (!arg_quiet) |
| 89 |
puts(state); |
| 90 |
|
| 91 |
return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; |
| 92 |
} |