dcavalca / rpms / systemd

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