naccyde / rpms / systemd

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