1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-10 05:18:17 +03:00

Merge pull request #31886 from DaanDeMeyer/logind

logind: Add fallback for when the PIDFDs= property is not available
This commit is contained in:
Mike Yuan 2024-03-24 19:19:12 +08:00 committed by GitHub
commit 8e1a1caa9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 43 additions and 32 deletions

View File

@ -4230,6 +4230,7 @@ int manager_start_scope(
Manager *manager,
const char *scope,
const PidRef *pidref,
bool allow_pidfd,
const char *slice,
const char *description,
const char * const *requires,
@ -4299,7 +4300,7 @@ int manager_start_scope(
if (r < 0)
return r;
r = bus_append_scope_pidref(m, pidref);
r = bus_append_scope_pidref(m, pidref, allow_pidfd);
if (r < 0)
return r;
@ -4330,8 +4331,27 @@ int manager_start_scope(
return r;
r = sd_bus_call(manager->bus, m, 0, error, &reply);
if (r < 0)
if (r < 0) {
/* If this failed with a property we couldn't write, this is quite likely because the server
* doesn't support PIDFDs yet, let's try without. */
if (allow_pidfd &&
sd_bus_error_has_names(error, SD_BUS_ERROR_UNKNOWN_PROPERTY, SD_BUS_ERROR_PROPERTY_READ_ONLY))
return manager_start_scope(
manager,
scope,
pidref,
/* allow_pidfd = */ false,
slice,
description,
requires,
extra_after,
requires_mounts_for,
more_properties,
error,
ret_job);
return r;
}
return strdup_job(reply, ret_job);
}

View File

@ -28,6 +28,7 @@ int manager_start_scope(
Manager *manager,
const char *scope,
const PidRef *pidref,
bool allow_pidfd,
const char *slice,
const char *description,
const char * const *requires,

View File

@ -742,6 +742,7 @@ static int session_start_scope(Session *s, sd_bus_message *properties, sd_bus_er
s->manager,
scope,
&s->leader,
/* allow_pidfd = */ true,
s->user->slice,
description,
/* These should have been pulled in explicitly in user_start(). Just to be sure. */

View File

@ -385,7 +385,7 @@ static int machine_start_scope(
if (r < 0)
return r;
r = bus_append_scope_pidref(m, &machine->leader);
r = bus_append_scope_pidref(m, &machine->leader, /* allow_pidfd = */ true);
if (r < 0)
return r;

View File

@ -297,15 +297,12 @@ int allocate_scope(
description = strjoina("Container ", machine_name);
if (allow_pidfd) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_set_pid(&pidref, pid);
if (r < 0)
return log_error_errno(r, "Failed to allocate PID reference: %m");
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_set_pid(&pidref, pid);
if (r < 0)
return log_error_errno(r, "Failed to allocate PID reference: %m");
r = bus_append_scope_pidref(m, &pidref);
} else
r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) pid);
r = bus_append_scope_pidref(m, &pidref, allow_pidfd);
if (r < 0)
return bus_log_create_error(r);

View File

@ -1265,18 +1265,13 @@ static int transient_scope_set_properties(sd_bus_message *m, bool allow_pidfd) {
if (r < 0)
return r;
if (allow_pidfd) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_set_self(&pidref);
if (r < 0)
return r;
r = pidref_set_self(&pidref);
if (r < 0)
return r;
r = bus_append_scope_pidref(m, &pidref);
} else
r = sd_bus_message_append(
m, "(sv)",
"PIDs", "au", 1, getpid_cached());
r = bus_append_scope_pidref(m, &pidref, allow_pidfd);
if (r < 0)
return bus_log_create_error(r);

View File

@ -2821,13 +2821,13 @@ int bus_append_unit_property_assignment_many(sd_bus_message *m, UnitType t, char
return 0;
}
int bus_append_scope_pidref(sd_bus_message *m, const PidRef *pidref) {
int bus_append_scope_pidref(sd_bus_message *m, const PidRef *pidref, bool allow_pidfd) {
assert(m);
if (!pidref_is_set(pidref))
return -ESRCH;
if (pidref->fd >= 0)
if (pidref->fd >= 0 && allow_pidfd)
return sd_bus_message_append(
m, "(sv)",
"PIDFDs", "ah", 1, pidref->fd);

View File

@ -26,7 +26,7 @@ int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u);
int bus_append_unit_property_assignment(sd_bus_message *m, UnitType t, const char *assignment);
int bus_append_unit_property_assignment_many(sd_bus_message *m, UnitType t, char **l);
int bus_append_scope_pidref(sd_bus_message *m, const PidRef *pidref);
int bus_append_scope_pidref(sd_bus_message *m, const PidRef *pidref, bool allow_pidfd);
int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet);

View File

@ -61,15 +61,12 @@ int start_transient_scope(sd_bus *bus, const char *machine_name, bool allow_pidf
"AddRef", "b", 1,
"CollectMode", "s", "inactive-or-failed");
if (allow_pidfd) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_set_pid(&pidref, getpid_cached());
if (r < 0)
return log_error_errno(r, "Failed to allocate PID reference: %m");
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_set_self(&pidref);
if (r < 0)
return log_error_errno(r, "Failed to allocate PID reference: %m");
r = bus_append_scope_pidref(m, &pidref);
} else
r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, getpid_cached());
r = bus_append_scope_pidref(m, &pidref, allow_pidfd);
if (r < 0)
return bus_log_create_error(r);