naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
923a60
From 72b3ff75e786efa2c9b2fdfb50e46597434c5420 Mon Sep 17 00:00:00 2001
923a60
From: Lukas Nykryn <lnykryn@redhat.com>
923a60
Date: Wed, 20 Jan 2016 15:16:32 +0100
923a60
Subject: [PATCH] sysv-generator: do not join dependencies on one line, split
923a60
 them
923a60
923a60
If there is a lot of initscripts and dependencies between them we might
923a60
end generating After= (and similar) lines which are longer then LINE_MAX
923a60
and thus rejected by parser in systemd.
923a60
923a60
Fixes #2099
923a60
923a60
Cherry-picked from: c584ffc0b75d4b9e9229bf1d8edb7d89562be3c1
923a60
Resolves: #1288600
923a60
---
923a60
 src/sysv-generator/sysv-generator.c | 44 ++++++++---------------------
923a60
 test/sysv-generator-test.py         | 18 ++++++++++--
923a60
 2 files changed, 28 insertions(+), 34 deletions(-)
923a60
923a60
diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c
923a60
index 0a8a528bdc..d60e75a06b 100644
923a60
--- a/src/sysv-generator/sysv-generator.c
923a60
+++ b/src/sysv-generator/sysv-generator.c
923a60
@@ -134,34 +134,14 @@ static int add_alias(const char *service, const char *alias) {
923a60
 }
923a60
 
923a60
 static int generate_unit_file(SysvStub *s) {
923a60
-        char **p;
923a60
         _cleanup_fclose_ FILE *f = NULL;
923a60
-        _cleanup_free_ char *unit = NULL;
923a60
-        _cleanup_free_ char *before = NULL;
923a60
-        _cleanup_free_ char *after = NULL;
923a60
-        _cleanup_free_ char *wants = NULL;
923a60
-        _cleanup_free_ char *conflicts = NULL;
923a60
+        const char *unit;
923a60
+        char **p;
923a60
         int r;
923a60
 
923a60
-        before = strv_join(s->before, " ");
923a60
-        if (!before)
923a60
-                return log_oom();
923a60
-
923a60
-        after = strv_join(s->after, " ");
923a60
-        if (!after)
923a60
-                return log_oom();
923a60
-
923a60
-        wants = strv_join(s->wants, " ");
923a60
-        if (!wants)
923a60
-                return log_oom();
923a60
-
923a60
-        conflicts = strv_join(s->conflicts, " ");
923a60
-        if (!conflicts)
923a60
-                return log_oom();
923a60
+        assert(s);
923a60
 
923a60
-        unit = strjoin(arg_dest, "/", s->name, NULL);
923a60
-        if (!unit)
923a60
-                return log_oom();
923a60
+        unit = strjoina(arg_dest, "/", s->name);
923a60
 
923a60
         /* We might already have a symlink with the same name from a Provides:,
923a60
          * or from backup files like /etc/init.d/foo.bak. Real scripts always win,
923a60
@@ -183,14 +163,14 @@ static int generate_unit_file(SysvStub *s) {
923a60
                 "Description=%s\n",
923a60
                 s->path, s->description);
923a60
 
923a60
-        if (!isempty(before))
923a60
-                fprintf(f, "Before=%s\n", before);
923a60
-        if (!isempty(after))
923a60
-                fprintf(f, "After=%s\n", after);
923a60
-        if (!isempty(wants))
923a60
-                fprintf(f, "Wants=%s\n", wants);
923a60
-        if (!isempty(conflicts))
923a60
-                fprintf(f, "Conflicts=%s\n", conflicts);
923a60
+        STRV_FOREACH(p, s->before)
923a60
+                fprintf(f, "Before=%s\n", *p);
923a60
+        STRV_FOREACH(p, s->after)
923a60
+                fprintf(f, "After=%s\n", *p);
923a60
+        STRV_FOREACH(p, s->wants)
923a60
+                fprintf(f, "Wants=%s\n", *p);
923a60
+        STRV_FOREACH(p, s->conflicts)
923a60
+                fprintf(f, "Conflicts=%s\n", *p);
923a60
 
923a60
         fprintf(f,
923a60
                 "\n[Service]\n"
923a60
diff --git a/test/sysv-generator-test.py b/test/sysv-generator-test.py
923a60
index 2060ad754e..25a35da47f 100644
923a60
--- a/test/sysv-generator-test.py
923a60
+++ b/test/sysv-generator-test.py
923a60
@@ -23,6 +23,7 @@ import subprocess
923a60
 import tempfile
923a60
 import shutil
923a60
 from glob import glob
923a60
+import collections
923a60
 
923a60
 try:
923a60
     from configparser import RawConfigParser
923a60
@@ -32,6 +33,12 @@ except ImportError:
923a60
 
923a60
 sysv_generator = os.path.join(os.environ.get('builddir', '.'), 'systemd-sysv-generator')
923a60
 
923a60
+class MultiDict(collections.OrderedDict):
923a60
+    def __setitem__(self, key, value):
923a60
+        if isinstance(value, list) and key in self:
923a60
+            self[key].extend(value)
923a60
+        else:
923a60
+            super(MultiDict, self).__setitem__(key, value)
923a60
 
923a60
 class SysvGeneratorTest(unittest.TestCase):
923a60
     def setUp(self):
923a60
@@ -77,7 +84,14 @@ class SysvGeneratorTest(unittest.TestCase):
923a60
         for service in glob(self.out_dir + '/*.service'):
923a60
             if os.path.islink(service):
923a60
                 continue
923a60
-            cp = RawConfigParser()
923a60
+            try:
923a60
+                # for python3 we need here strict=False to parse multiple
923a60
+                # lines with the same key
923a60
+                cp = RawConfigParser(dict_type=MultiDict, strict=False)
923a60
+            except TypeError:
923a60
+                # RawConfigParser in python2 does not have the strict option
923a60
+                # but it allows multiple lines with the same key by default
923a60
+                cp = RawConfigParser(dict_type=MultiDict)
923a60
             cp.optionxform = lambda o: o  # don't lower-case option names
923a60
             with open(service) as f:
923a60
                 cp.readfp(f)
923a60
@@ -215,7 +229,7 @@ class SysvGeneratorTest(unittest.TestCase):
923a60
         s = self.run_generator()[1]['foo.service']
923a60
         self.assertEqual(set(s.options('Unit')),
923a60
                          set(['Documentation', 'SourcePath', 'Description', 'After']))
923a60
-        self.assertEqual(s.get('Unit', 'After'), 'nss-lookup.target rpcbind.target')
923a60
+        self.assertEqual(s.get('Unit', 'After').split(), ['nss-lookup.target', 'rpcbind.target'])
923a60
 
923a60
     def test_lsb_deps(self):
923a60
         '''LSB header dependencies to other services'''