linux: up-backend: A safeguard for the risky CriticalPowerAction

If AllowRiskyCriticalPowerAction is true and the CriticalPowerAction
is "Suspend", The "Suspend" can be the CriticalPowerAction.
Otherwise, "HybridSleep" will be the CriticalPowerAction.
This commit is contained in:
Kate Hsuan 2024-03-22 20:01:43 +08:00
parent 469346b6bf
commit 69436647c5
2 changed files with 14 additions and 4 deletions

View file

@ -97,7 +97,7 @@ AllowRiskyCriticalPowerAction=false
# HybridSleep
# Suspend (AllowRiskyCriticalPowerAction should be true to use this option but risky)
#
# If Suspend isn't available or AllowRiskyCriticalPowerAction=false, Poweroff will be used
# If Suspend isn't available or AllowRiskyCriticalPowerAction=false, HybridSleep will be used
# If HybridSleep isn't available, Hibernate will be used
# If Hibernate isn't available, PowerOff will be used
CriticalPowerAction=HybridSleep

View file

@ -558,20 +558,30 @@ up_backend_get_critical_action (UpBackend *backend)
{ "Hibernate", "CanHibernate" },
{ "PowerOff", NULL },
};
g_autofree gchar *action = NULL;
gboolean can_risky = FALSE;
guint i = 1;
char *action;
g_return_val_if_fail (backend->priv->logind_proxy != NULL, NULL);
/* Find the configured action first */
can_risky = up_config_get_boolean (backend->priv->config,
"AllowRiskyCriticalPowerAction");
/* find the configured action first */
action = up_config_get_string (backend->priv->config, "CriticalPowerAction");
/* safeguard for the risky actions */
if (!can_risky && !g_strcmp0 (action, "Suspend")) {
g_free (action);
action = g_strdup_printf ("HybridSleep");
}
if (action != NULL) {
for (i = 0; i < G_N_ELEMENTS (actions); i++)
if (g_str_equal (actions[i].method, action))
break;
if (i >= G_N_ELEMENTS (actions))
i = 1;
g_free (action);
}
for (; i < G_N_ELEMENTS (actions); i++) {