1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-06 16:59:03 +03:00

Merge pull request #8934 from yuwata/fix-8913

bus-util: print a friendly message when PID1 is not systemd
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-05-09 10:31:24 +02:00 committed by GitHub
commit 4aa524a13c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 32 deletions

View File

@ -118,23 +118,19 @@ struct host_info {
char *architecture; char *architecture;
}; };
static int acquire_systemd_bus(sd_bus **bus) { static int acquire_bus(sd_bus **bus, bool *use_full_bus) {
bool user = arg_scope != UNIT_FILE_SYSTEM; bool user = arg_scope != UNIT_FILE_SYSTEM;
int r;
return bus_connect_transport_systemd(arg_transport, arg_host, user, bus); if (use_full_bus && *use_full_bus) {
} r = bus_connect_transport(arg_transport, arg_host, user, bus);
if (IN_SET(r, 0, -EHOSTDOWN))
static int acquire_full_bus(bool *use_full_bus, sd_bus **bus) { return r;
bool user = arg_scope != UNIT_FILE_SYSTEM;
if (*use_full_bus) {
if (bus_connect_transport(arg_transport, arg_host, user, bus) == 0)
return 0;
*use_full_bus = false; *use_full_bus = false;
} }
return acquire_systemd_bus(bus); return bus_connect_transport_systemd(arg_transport, arg_host, user, bus);
} }
static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) { static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) {
@ -606,7 +602,7 @@ static int analyze_plot(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *pretty_times = NULL; _cleanup_free_ char *pretty_times = NULL;
struct unit_times *u; struct unit_times *u;
r = acquire_full_bus(&use_full_bus, &bus); r = acquire_bus(&bus, &use_full_bus);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1015,7 +1011,7 @@ static int analyze_critical_chain(int argc, char *argv[], void *userdata) {
Hashmap *h; Hashmap *h;
int n, r; int n, r;
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1057,7 +1053,7 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
unsigned i; unsigned i;
int n, r; int n, r;
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1085,7 +1081,7 @@ static int analyze_time(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *buf = NULL; _cleanup_free_ char *buf = NULL;
int r; int r;
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1219,7 +1215,7 @@ static int dot(int argc, char *argv[], void *userdata) {
int r; int r;
UnitInfo u; UnitInfo u;
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1286,7 +1282,7 @@ static int dump(int argc, char *argv[], void *userdata) {
const char *text = NULL; const char *text = NULL;
int r; int r;
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1320,7 +1316,7 @@ static int set_log_level(int argc, char *argv[], void *userdata) {
assert(argc == 2); assert(argc == 2);
assert(argv); assert(argv);
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1345,7 +1341,7 @@ static int get_log_level(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *level = NULL; _cleanup_free_ char *level = NULL;
int r; int r;
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1376,7 +1372,7 @@ static int set_log_target(int argc, char *argv[], void *userdata) {
assert(argc == 2); assert(argc == 2);
assert(argv); assert(argv);
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1401,7 +1397,7 @@ static int get_log_target(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *target = NULL; _cleanup_free_ char *target = NULL;
int r; int r;
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");
@ -1563,7 +1559,7 @@ static int service_watchdogs(int argc, char *argv[], void *userdata) {
assert(IN_SET(argc, 1, 2)); assert(IN_SET(argc, 1, 2));
assert(argv); assert(argv);
r = acquire_systemd_bus(&bus); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m"); return log_error_errno(r, "Failed to create bus connection: %m");

View File

@ -1300,9 +1300,15 @@ int bus_connect_transport(BusTransport transport, const char *host, bool user, s
case BUS_TRANSPORT_LOCAL: case BUS_TRANSPORT_LOCAL:
if (user) if (user)
r = sd_bus_default_user(&bus); r = sd_bus_default_user(&bus);
else else {
r = sd_bus_default_system(&bus); if (sd_booted() <= 0) {
/* Print a friendly message when the local system is actually not running systemd as PID 1. */
log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
return -EHOSTDOWN;
}
r = sd_bus_default_system(&bus);
}
break; break;
case BUS_TRANSPORT_REMOTE: case BUS_TRANSPORT_REMOTE:
@ -1343,9 +1349,15 @@ int bus_connect_transport_systemd(BusTransport transport, const char *host, bool
case BUS_TRANSPORT_LOCAL: case BUS_TRANSPORT_LOCAL:
if (user) if (user)
r = bus_connect_user_systemd(bus); r = bus_connect_user_systemd(bus);
else else {
r = bus_connect_system_systemd(bus); if (sd_booted() <= 0) {
/* Print a friendly message when the local system is actually not running systemd as PID 1. */
log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
return -EHOSTDOWN;
}
r = bus_connect_system_systemd(bus);
}
break; break;
case BUS_TRANSPORT_REMOTE: case BUS_TRANSPORT_REMOTE:

View File

@ -211,12 +211,6 @@ static int acquire_bus(BusFocus focus, sd_bus **ret) {
user = arg_scope != UNIT_FILE_SYSTEM; user = arg_scope != UNIT_FILE_SYSTEM;
if (!user && sd_booted() <= 0) {
/* Print a friendly message when the local system is actually not running systemd as PID 1. */
log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
return -EHOSTDOWN;
}
if (focus == BUS_MANAGER) if (focus == BUS_MANAGER)
r = bus_connect_transport_systemd(arg_transport, arg_host, user, &busses[focus]); r = bus_connect_transport_systemd(arg_transport, arg_host, user, &busses[focus]);
else else