Blob Blame History Raw
From 6d590cc99d696e9b0bf5b6edf7582b824f5177ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Sat, 24 Sep 2016 20:58:04 -0400
Subject: [PATCH] systemctl: suppress errors with "show" for nonexistent units
 and properties

Show is documented to be program-parseable, and printing the warning about
about a non-existent unit, while useful for humans, broke a lot of scripts.
Restore previous behaviour of returning success and printing empty or useless
stuff for units which do not exist, and printing empty values for properties
which do not exists.

With SYSTEMD_LOG_LEVEL=debug, hints are printed, but the return value is
still 0.

This undoes parts of e33a06a and 3dced37b7 and fixes #3856.

We might consider adding an explicit switch to fail on missing units/properties
(e.g. --ensure-exists or similar), and make -P foobar equivalent to
--ensure-exists --property=foobar.

Cherry-picked from: bd5b9f0a12dd9c1947b11534e99c395ddf44caa9
Resolves: #1380259
---
 src/systemctl/systemctl.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 0644784a5..a578897d9 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -4272,12 +4272,14 @@ static int show_one(
                         return log_error_errno(r, "Failed to map properties: %s", bus_error_message(&error, r));
 
                 if (streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) {
-                        log_error("Unit %s could not be found.", unit);
+                        log_full(streq(verb, "status") ? LOG_ERR : LOG_DEBUG,
+                                 "Unit %s could not be found.", unit);
 
                         if (streq(verb, "status"))
                                 return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN;
 
-                        return -ENOENT;
+                        if (!streq(verb, "show"))
+                                return -ENOENT;
                 }
 
                 r = sd_bus_message_rewind(reply, true);
@@ -4343,10 +4345,11 @@ static int show_one(
 
         if (show_properties) {
                 char **pp;
+                int not_found_level = streq(verb, "show") ? LOG_DEBUG : LOG_WARNING;
 
                 STRV_FOREACH(pp, arg_properties) {
                         if (!set_contains(found_properties, *pp)) {
-                                log_warning("Property %s does not exist.", *pp);
+                                log_full(not_found_level, "Property %s does not exist.", *pp);
                                 r = -ENXIO;
                         }
                 }