naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
803fb7
From 6d590cc99d696e9b0bf5b6edf7582b824f5177ab Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
803fb7
Date: Sat, 24 Sep 2016 20:58:04 -0400
803fb7
Subject: [PATCH] systemctl: suppress errors with "show" for nonexistent units
803fb7
 and properties
803fb7
803fb7
Show is documented to be program-parseable, and printing the warning about
803fb7
about a non-existent unit, while useful for humans, broke a lot of scripts.
803fb7
Restore previous behaviour of returning success and printing empty or useless
803fb7
stuff for units which do not exist, and printing empty values for properties
803fb7
which do not exists.
803fb7
803fb7
With SYSTEMD_LOG_LEVEL=debug, hints are printed, but the return value is
803fb7
still 0.
803fb7
803fb7
This undoes parts of e33a06a and 3dced37b7 and fixes #3856.
803fb7
803fb7
We might consider adding an explicit switch to fail on missing units/properties
803fb7
(e.g. --ensure-exists or similar), and make -P foobar equivalent to
803fb7
--ensure-exists --property=foobar.
803fb7
803fb7
Cherry-picked from: bd5b9f0a12dd9c1947b11534e99c395ddf44caa9
803fb7
Resolves: #1380259
803fb7
---
803fb7
 src/systemctl/systemctl.c | 9 ++++++---
803fb7
 1 file changed, 6 insertions(+), 3 deletions(-)
803fb7
803fb7
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
803fb7
index 0644784a5..a578897d9 100644
803fb7
--- a/src/systemctl/systemctl.c
803fb7
+++ b/src/systemctl/systemctl.c
803fb7
@@ -4272,12 +4272,14 @@ static int show_one(
803fb7
                         return log_error_errno(r, "Failed to map properties: %s", bus_error_message(&error, r));
803fb7
 
803fb7
                 if (streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) {
803fb7
-                        log_error("Unit %s could not be found.", unit);
803fb7
+                        log_full(streq(verb, "status") ? LOG_ERR : LOG_DEBUG,
803fb7
+                                 "Unit %s could not be found.", unit);
803fb7
 
803fb7
                         if (streq(verb, "status"))
803fb7
                                 return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN;
803fb7
 
803fb7
-                        return -ENOENT;
803fb7
+                        if (!streq(verb, "show"))
803fb7
+                                return -ENOENT;
803fb7
                 }
803fb7
 
803fb7
                 r = sd_bus_message_rewind(reply, true);
803fb7
@@ -4343,10 +4345,11 @@ static int show_one(
803fb7
 
803fb7
         if (show_properties) {
803fb7
                 char **pp;
803fb7
+                int not_found_level = streq(verb, "show") ? LOG_DEBUG : LOG_WARNING;
803fb7
 
803fb7
                 STRV_FOREACH(pp, arg_properties) {
803fb7
                         if (!set_contains(found_properties, *pp)) {
803fb7
-                                log_warning("Property %s does not exist.", *pp);
803fb7
+                                log_full(not_found_level, "Property %s does not exist.", *pp);
803fb7
                                 r = -ENXIO;
803fb7
                         }
803fb7
                 }