[systemd-devel] [PATCH] Added globbing support to EnvironmentFile

Pekka Lundstrom pekka.lundstrom at jollamobile.com
Wed Jan 2 03:41:52 PST 2013


This patch allows globbing to be used with EnvironmentFile option.
Example:
EnvironmentFile=/etc/foo.d/*.conf

t. Pekka
---
Signed-off-by: Pekka Lundstrom <pekka.lundstrom at jollamobile.com>

---
 man/systemd.exec.xml |    2 +-
 src/core/execute.c   |   51 ++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
index 6ca7405..302ac43 100644
--- a/man/systemd.exec.xml
+++ b/man/systemd.exec.xml
@@ -291,7 +291,7 @@
                                 double quotes (").
                                 The
                                 argument passed should be an absolute
-                                file name, optionally prefixed with
+                                file name or wildcard expression, optionally prefixed with
                                 "-", which indicates that if the file
                                 does not exist it won't be read and no
                                 error or warning message is
diff --git a/src/core/execute.c b/src/core/execute.c
index 7628470..bf0458e 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -39,6 +39,7 @@
 #include <linux/oom.h>
 #include <sys/poll.h>
 #include <linux/seccomp-bpf.h>
+#include <glob.h>
 
 #ifdef HAVE_PAM
 #include <security/pam_appl.h>
@@ -1657,6 +1658,8 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                 int k;
                 bool ignore = false;
                 char **p;
+                glob_t pglob;
+                int count, n;
 
                 fn = *i;
 
@@ -1674,29 +1677,53 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                         return -EINVAL;
                 }
 
-                if ((k = load_env_file(fn, &p)) < 0) {
+                /* Filename supports globbing, take all matching files */
+                pglob.gl_pathc = 0;
+                pglob.gl_pathv = NULL;
+                if (glob(fn, 0, NULL, &pglob) != 0) {
+                        globfree(&pglob);
+                        if (ignore)
+                                continue;
 
+                        strv_free(r);
+                        return -EINVAL;
+                }
+                if ((count = pglob.gl_pathc) == 0) {
+                        globfree(&pglob);
                         if (ignore)
                                 continue;
 
                         strv_free(r);
-                        return k;
+                        return -EINVAL;
                 }
+                for (n = 0; n < count; n++) {
+                        if ((k = load_env_file(pglob.gl_pathv[n], &p)) < 0) {
+                                if (ignore)
+                                        continue;
 
-                if (r == NULL)
-                        r = p;
-                else {
-                        char **m;
+                                strv_free(r);
+                                globfree(&pglob);
+                                return k;
+                         }
 
-                        m = strv_env_merge(2, r, p);
-                        strv_free(r);
-                        strv_free(p);
+                        if (r == NULL)
+                                r = p;
+                        else {
+                                char **m;
 
-                        if (!m)
-                                return -ENOMEM;
+                                m = strv_env_merge(2, r, p);
+                                strv_free(r);
+                                strv_free(p);
 
-                        r = m;
+                                if (!m) {
+                                        globfree(&pglob);
+                                        return -ENOMEM;
+                                }
+
+                                r = m;
+                        }
                 }
+                globfree(&pglob);
         }
 
         *l = r;
-- 
1.7.9.5



More information about the systemd-devel mailing list