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