From f8bfa318561c375377ac4143ca19aa9cb35d8fa6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 8 Jan 2019 02:39:31 +0900 Subject: [PATCH 1/4] login: do not use cast to bool for enum --- src/login/logind-action.c | 2 +- src/login/logind-dbus.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/login/logind-action.c b/src/login/logind-action.c index e4e6c90191..6c9366761d 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -121,7 +121,7 @@ int manager_handle_action( return -EOPNOTSUPP; } - if (m->action_what) { + if (m->action_what > 0) { log_debug("Action already in progress, ignoring."); return -EALREADY; } diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index bd9f5ac4d6..4ab0dcb595 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1773,7 +1773,7 @@ static int method_do_shutdown_or_sleep( return r; /* Don't allow multiple jobs being executed at the same time */ - if (m->action_what) + if (m->action_what > 0) return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress"); if (sleep_verb) { @@ -2012,7 +2012,7 @@ static int manager_scheduled_shutdown_handler( assert_not_reached("unexpected shutdown type"); /* Don't allow multiple jobs being executed at the same time */ - if (m->action_what) { + if (m->action_what > 0) { r = -EALREADY; log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target); goto error; From 543680f41549076ba2abd2bcd99e0829ca15eef1 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 8 Jan 2019 02:40:33 +0900 Subject: [PATCH 2/4] login: adjust range of InhibitWhat in assertions --- src/login/logind-dbus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 4ab0dcb595..2392435f21 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1656,7 +1656,7 @@ int bus_manager_shutdown_or_sleep_now_or_later( assert(m); assert(unit_name); assert(w > 0); - assert(w <= _INHIBIT_WHAT_MAX); + assert(w < _INHIBIT_WHAT_MAX); assert(!m->action_job); r = unit_load_state(m->bus, unit_name, &load_state); From 6b9f8b71351cf715f2bfe491f0769a3f4df0a7ce Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 8 Jan 2019 02:41:15 +0900 Subject: [PATCH 3/4] login: use free_and_strdup() at one more place --- src/login/logind-dbus.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 2392435f21..bb3f6b8451 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1502,7 +1502,6 @@ static int execute_shutdown_or_sleep( sd_bus_error *error) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - char *c = NULL; const char *p; int r; @@ -1530,15 +1529,11 @@ static int execute_shutdown_or_sleep( if (r < 0) goto error; - c = strdup(p); - if (!c) { - r = -ENOMEM; + r = free_and_strdup(&m->action_job, p); + if (r < 0) goto error; - } m->action_unit = unit_name; - free(m->action_job); - m->action_job = c; m->action_what = w; /* Make sure the lid switch is ignored for a while */ From 06c2f0a816a1c67a14688107009a6199c96c4524 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 8 Jan 2019 02:43:00 +0900 Subject: [PATCH 4/4] login: simplify the condtion in assertion and slightly decrease binary size --- src/login/logind-dbus.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index bb3f6b8451..8ab498fdc2 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1474,23 +1474,15 @@ int manager_set_lid_switch_ignore(Manager *m, usec_t until) { } static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) { - - static const char * const signal_name[_INHIBIT_WHAT_MAX] = { - [INHIBIT_SHUTDOWN] = "PrepareForShutdown", - [INHIBIT_SLEEP] = "PrepareForSleep" - }; - int active = _active; assert(m); - assert(w >= 0); - assert(w < _INHIBIT_WHAT_MAX); - assert(signal_name[w]); + assert(IN_SET(w, INHIBIT_SHUTDOWN, INHIBIT_SLEEP)); return sd_bus_emit_signal(m->bus, "/org/freedesktop/login1", "org.freedesktop.login1.Manager", - signal_name[w], + w == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep", "b", active); }