Blob Blame History Raw
From 9e42758bf5dcaff2120856556ef1c3cafff7d581 Mon Sep 17 00:00:00 2001
From: Lukas Nykryn <lnykryn@redhat.com>
Date: Tue, 19 Aug 2014 20:53:29 +0200
Subject: [PATCH] systemctl: fail in the case that no unit files were found

Previously systemctl died with message

-bash-4.2# systemctl --root /rawhi list-unit-files
(src/systemctl/systemctl.c:868) Out of memory.

in the case that no unit files were found in the --root
or the directory did not exist.

So lets return ENOENT in the case that --root does not exist
and empty list in the case that there are no unit files.

(cherry picked from commit fdbdf6ec29bda40763d7d3e7bb2a63e2f5d60c4c)

Related: #1111199
---
 src/shared/install.c      | 6 ++++++
 src/systemctl/systemctl.c | 4 ++++
 2 files changed, 10 insertions(+)

diff --git a/src/shared/install.c b/src/shared/install.c
index 955a9bc..598323a 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1933,6 +1933,12 @@ int unit_file_get_list(
         if (root_dir && scope != UNIT_FILE_SYSTEM)
                 return -EINVAL;
 
+        if (root_dir) {
+                r = access(root_dir, F_OK);
+                if (r < 0)
+                        return -errno;
+        }
+
         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
         if (r < 0)
                 return r;
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 1cbcd20..90a80bd 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -862,6 +862,10 @@ static int list_unit_files(DBusConnection *bus, char **args) {
                 }
 
                 n_units = hashmap_size(h);
+
+                if (n_units == 0)
+                        return 0;
+
                 units = new(UnitFileList, n_units);
                 if (!units) {
                         unit_file_list_free(h);