Compare commits

...

3 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek 583e509c4c
Merge 0e54562082 into 0e44a351ea 2024-11-25 00:14:26 +00:00
Zbigniew Jędrzejewski-Szmek 0e54562082 logind: define flags enum for manager_is_inhibited()
The most common case of block=true, ignore_inactive=false is mapped to flags=0.

For https://github.com/systemd/systemd/issues/34091.
2024-11-20 13:23:12 +01:00
Zbigniew Jędrzejewski-Szmek 03af199aaf logind: drop one duplicate param in manager_is_inhibited()
In the review in https://github.com/systemd/systemd/pull/30307#pullrequestreview-2255002732
removal of the excessive boolean parameters was requested. We don't need
a separate boolean param here, since we always pass true with a uid and
false otherwise.
2024-11-20 09:44:01 +01:00
5 changed files with 35 additions and 18 deletions

View File

@ -234,7 +234,7 @@ static int handle_action_execute(
/* If the actual operation is inhibited, warn and fail */ /* If the actual operation is inhibited, warn and fail */
if (inhibit_what_is_valid(inhibit_operation) && if (inhibit_what_is_valid(inhibit_operation) &&
!ignore_inhibited && !ignore_inhibited &&
manager_is_inhibited(m, inhibit_operation, /* block= */ true, NULL, false, false, 0, &offending)) { manager_is_inhibited(m, inhibit_operation, NULL, /* flags= */ 0, UID_INVALID, &offending)) {
_cleanup_free_ char *comm = NULL, *u = NULL; _cleanup_free_ char *comm = NULL, *u = NULL;
(void) pidref_get_comm(&offending->pid, &comm); (void) pidref_get_comm(&offending->pid, &comm);
@ -372,7 +372,7 @@ int manager_handle_action(
/* If the key handling is inhibited, don't do anything */ /* If the key handling is inhibited, don't do anything */
if (inhibit_key > 0) { if (inhibit_key > 0) {
if (manager_is_inhibited(m, inhibit_key, /* block= */ true, NULL, true, false, 0, NULL)) { if (manager_is_inhibited(m, inhibit_key, NULL, MANAGER_IS_INHIBITED_IGNORE_INACTIVE, UID_INVALID, NULL)) {
log_debug("Refusing %s operation, %s is inhibited.", log_debug("Refusing %s operation, %s is inhibited.",
handle_action_to_string(handle), handle_action_to_string(handle),
inhibit_what_to_string(inhibit_key)); inhibit_what_to_string(inhibit_key));

View File

@ -411,7 +411,7 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
assert(m); assert(m);
idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, /* block= */ true, t, false, false, 0, NULL); idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, t, /* flags= */ 0, UID_INVALID, NULL);
HASHMAP_FOREACH(s, m->sessions) { HASHMAP_FOREACH(s, m->sessions) {
dual_timestamp k; dual_timestamp k;

View File

@ -1931,7 +1931,12 @@ int manager_dispatch_delayed(Manager *manager, bool timeout) {
if (!manager->delayed_action || manager->action_job) if (!manager->delayed_action || manager->action_job)
return 0; return 0;
if (manager_is_inhibited(manager, manager->delayed_action->inhibit_what, /* block= */ false, NULL, false, false, 0, &offending)) { if (manager_is_inhibited(manager,
manager->delayed_action->inhibit_what,
NULL,
MANAGER_IS_INHIBITED_CHECK_DELAY,
UID_INVALID,
&offending)) {
_cleanup_free_ char *comm = NULL, *u = NULL; _cleanup_free_ char *comm = NULL, *u = NULL;
if (!timeout) if (!timeout)
@ -2033,7 +2038,7 @@ int bus_manager_shutdown_or_sleep_now_or_later(
delayed = delayed =
m->inhibit_delay_max > 0 && m->inhibit_delay_max > 0 &&
manager_is_inhibited(m, a->inhibit_what, /* block= */ false, NULL, false, false, 0, NULL); manager_is_inhibited(m, a->inhibit_what, NULL, MANAGER_IS_INHIBITED_CHECK_DELAY, UID_INVALID, NULL);
if (delayed) if (delayed)
/* Shutdown is delayed, keep in mind what we /* Shutdown is delayed, keep in mind what we
@ -2077,7 +2082,7 @@ static int verify_shutdown_creds(
return r; return r;
multiple_sessions = r > 0; multiple_sessions = r > 0;
blocked = manager_is_inhibited(m, a->inhibit_what, /* block= */ true, NULL, false, true, uid, &offending); blocked = manager_is_inhibited(m, a->inhibit_what, NULL, /* flags= */ 0, uid, &offending);
interactive = flags & SD_LOGIND_INTERACTIVE; interactive = flags & SD_LOGIND_INTERACTIVE;
if (multiple_sessions) { if (multiple_sessions) {
@ -2820,7 +2825,7 @@ static int method_can_shutdown_or_sleep(
return r; return r;
multiple_sessions = r > 0; multiple_sessions = r > 0;
blocked = manager_is_inhibited(m, a->inhibit_what, /* block= */ true, NULL, false, true, uid, NULL); blocked = manager_is_inhibited(m, a->inhibit_what, NULL, /* flags= */ 0, uid, NULL);
if (check_unit_state && a->target) { if (check_unit_state && a->target) {
_cleanup_free_ char *load_state = NULL; _cleanup_free_ char *load_state = NULL;

View File

@ -399,11 +399,9 @@ static int pidref_is_active_session(Manager *m, const PidRef *pid) {
bool manager_is_inhibited( bool manager_is_inhibited(
Manager *m, Manager *m,
InhibitWhat w, InhibitWhat w,
bool block,
dual_timestamp *since, dual_timestamp *since,
bool ignore_inactive, ManagerIsInhibitedFlags flags,
bool ignore_uid, uid_t uid_to_ignore,
uid_t uid,
Inhibitor **ret_offending) { Inhibitor **ret_offending) {
Inhibitor *i, *offending = NULL; Inhibitor *i, *offending = NULL;
@ -421,18 +419,19 @@ bool manager_is_inhibited(
if (!(i->what & w)) if (!(i->what & w))
continue; continue;
if ((block && !IN_SET(i->mode, INHIBIT_BLOCK, INHIBIT_BLOCK_WEAK)) || if ((flags & MANAGER_IS_INHIBITED_CHECK_DELAY) != (i->mode == INHIBIT_DELAY))
(!block && i->mode != INHIBIT_DELAY))
continue; continue;
if (ignore_inactive && pidref_is_active_session(m, &i->pid) <= 0) if ((flags & MANAGER_IS_INHIBITED_IGNORE_INACTIVE) &&
pidref_is_active_session(m, &i->pid) <= 0)
continue; continue;
if (i->mode == INHIBIT_BLOCK_WEAK && ignore_uid && i->uid == uid) if (i->mode == INHIBIT_BLOCK_WEAK &&
uid_is_valid(uid_to_ignore) &&
uid_to_ignore == i->uid)
continue; continue;
if (!inhibited || if (!inhibited || i->since.monotonic < ts.monotonic)
i->since.monotonic < ts.monotonic)
ts = i->since; ts = i->since;
inhibited = true; inhibited = true;

View File

@ -67,7 +67,20 @@ int inhibitor_create_fifo(Inhibitor *i);
bool inhibitor_is_orphan(Inhibitor *i); bool inhibitor_is_orphan(Inhibitor *i);
InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mode); InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mode);
bool manager_is_inhibited(Manager *m, InhibitWhat w, bool block, dual_timestamp *since, bool ignore_inactive, bool ignore_uid, uid_t uid, Inhibitor **offending);
typedef enum ManagerIsInhibitedFlags {
MANAGER_IS_INHIBITED_CHECK_DELAY = 1 << 0, /* When set, we only check delay inhibitors.
* Otherwise, we only check block inhibitors. */
MANAGER_IS_INHIBITED_IGNORE_INACTIVE = 1 << 1, /* When set, ignore inactive sessions. */
} ManagerIsInhibitedFlags;
bool manager_is_inhibited(
Manager *m,
InhibitWhat w,
dual_timestamp *since,
ManagerIsInhibitedFlags flags,
uid_t uid_to_ignore,
Inhibitor **ret_offending);
static inline bool inhibit_what_is_valid(InhibitWhat w) { static inline bool inhibit_what_is_valid(InhibitWhat w) {
return w > 0 && w < _INHIBIT_WHAT_MAX; return w > 0 && w < _INHIBIT_WHAT_MAX;