9fc0f6
From 9e42758bf5dcaff2120856556ef1c3cafff7d581 Mon Sep 17 00:00:00 2001
9fc0f6
From: Lukas Nykryn <lnykryn@redhat.com>
9fc0f6
Date: Tue, 19 Aug 2014 20:53:29 +0200
9fc0f6
Subject: [PATCH] systemctl: fail in the case that no unit files were found
9fc0f6
9fc0f6
Previously systemctl died with message
9fc0f6
9fc0f6
-bash-4.2# systemctl --root /rawhi list-unit-files
9fc0f6
(src/systemctl/systemctl.c:868) Out of memory.
9fc0f6
9fc0f6
in the case that no unit files were found in the --root
9fc0f6
or the directory did not exist.
9fc0f6
9fc0f6
So lets return ENOENT in the case that --root does not exist
9fc0f6
and empty list in the case that there are no unit files.
9fc0f6
9fc0f6
(cherry picked from commit fdbdf6ec29bda40763d7d3e7bb2a63e2f5d60c4c)
9fc0f6
9fc0f6
Related: #1111199
9fc0f6
---
9fc0f6
 src/shared/install.c      | 6 ++++++
9fc0f6
 src/systemctl/systemctl.c | 4 ++++
9fc0f6
 2 files changed, 10 insertions(+)
9fc0f6
9fc0f6
diff --git a/src/shared/install.c b/src/shared/install.c
9fc0f6
index 955a9bc..598323a 100644
9fc0f6
--- a/src/shared/install.c
9fc0f6
+++ b/src/shared/install.c
9fc0f6
@@ -1933,6 +1933,12 @@ int unit_file_get_list(
9fc0f6
         if (root_dir && scope != UNIT_FILE_SYSTEM)
9fc0f6
                 return -EINVAL;
9fc0f6
 
9fc0f6
+        if (root_dir) {
9fc0f6
+                r = access(root_dir, F_OK);
9fc0f6
+                if (r < 0)
9fc0f6
+                        return -errno;
9fc0f6
+        }
9fc0f6
+
9fc0f6
         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
9fc0f6
         if (r < 0)
9fc0f6
                 return r;
9fc0f6
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
9fc0f6
index 1cbcd20..90a80bd 100644
9fc0f6
--- a/src/systemctl/systemctl.c
9fc0f6
+++ b/src/systemctl/systemctl.c
9fc0f6
@@ -862,6 +862,10 @@ static int list_unit_files(DBusConnection *bus, char **args) {
9fc0f6
                 }
9fc0f6
 
9fc0f6
                 n_units = hashmap_size(h);
9fc0f6
+
9fc0f6
+                if (n_units == 0)
9fc0f6
+                        return 0;
9fc0f6
+
9fc0f6
                 units = new(UnitFileList, n_units);
9fc0f6
                 if (!units) {
9fc0f6
                         unit_file_list_free(h);