chiark / gitweb /
sysv-generator: Handle .sh suffixes when translating Provides:
authorMartin Pitt <martin.pitt@ubuntu.com>
Tue, 20 Jan 2015 15:41:31 +0000 (16:41 +0100)
committerMartin Pitt <martin.pitt@ubuntu.com>
Wed, 21 Jan 2015 13:53:38 +0000 (14:53 +0100)
When deciding whether the provided name equals the file name in
sysv_translate_facility(), also consider them equal if the file name has a
".sh" suffix.

This was uncovered by commit b7e7184 which then created a symlink
"<name>.service" to itself for ".sh" suffixed init.d scripts.

For additional robustness, refuse to create symlinks to itself in add_alias().

Add test case which reproduces the bug.

https://bugs.debian.org/775889

src/sysv-generator/sysv-generator.c
test/sysv-generator-test.py

index 4774981016d85728ce2d3082b33befc3e54fba5e..a47b0722e0aa50a5afe894dfff2c7e669696dadc 100644 (file)
@@ -119,6 +119,11 @@ static int add_alias(const char *service, const char *alias) {
         assert(service);
         assert(alias);
 
+        if (streq(service, alias)) {
+                log_error("Ignoring creation of an alias %s for itself", service);
+                return 0;
+        }
+
         link = strjoin(arg_dest, "/", alias, NULL);
         if (!link)
                 return log_oom();
@@ -263,6 +268,7 @@ static int sysv_translate_facility(const char *name, const char *filename, char
         unsigned i;
         char *r;
         const char *n;
+        _cleanup_free_ char *filename_no_sh = NULL;
 
         assert(name);
         assert(_r);
@@ -284,6 +290,13 @@ static int sysv_translate_facility(const char *name, const char *filename, char
                 goto finish;
         }
 
+        /* strip ".sh" suffix from file name for comparison */
+        filename_no_sh = strdup(filename);
+        if (!filename_no_sh)
+                return -ENOMEM;
+        if (endswith(filename, ".sh"))
+                filename_no_sh[strlen(filename)-3] = '\0';
+
         /* If we don't know this name, fallback heuristics to figure
          * out whether something is a target or a service alias. */
 
@@ -293,7 +306,7 @@ static int sysv_translate_facility(const char *name, const char *filename, char
 
                 /* Facilities starting with $ are most likely targets */
                 r = unit_name_build(n, NULL, ".target");
-        } else if (filename && streq(name, filename))
+        } else if (filename && streq(name, filename_no_sh))
                 /* Names equaling the file name of the services are redundant */
                 return 0;
         else
index 2a84f9fcaf432e17f3cb361d983624cbf13c4d4a..a3daa9fc6bda2cc548d72cacb5a6106d5598e0f7 100644 (file)
@@ -278,6 +278,42 @@ class SysvGeneratorTest(unittest.TestCase):
         err, results = self.run_generator()
         self.assertEqual(results, {})
 
+    def test_sh_suffix(self):
+        '''init.d script with .sh suffix'''
+
+        self.add_sysv('foo.sh', {}, enable=True)
+        err, results = self.run_generator()
+        s = results['foo.service']
+
+        self.assertEqual(s.sections(), ['Unit', 'Service'])
+        # should not have a .sh
+        self.assertEqual(s.get('Unit', 'Description'), 'LSB: test foo service')
+
+        # calls correct script with .sh
+        init_script = os.path.join(self.init_d_dir, 'foo.sh')
+        self.assertEqual(s.get('Service', 'ExecStart'),
+                         '%s start' % init_script)
+        self.assertEqual(s.get('Service', 'ExecStop'),
+                         '%s stop' % init_script)
+
+        self.assert_enabled('foo.service', [2, 3, 4, 5])
+
+    def test_sh_suffix_with_provides(self):
+        '''init.d script with .sh suffix and Provides:'''
+
+        self.add_sysv('foo.sh', {'Provides': 'foo bar'})
+        err, results = self.run_generator()
+        # ensure we don't try to create a symlink to itself
+        self.assertNotIn(err, 'itself')
+        self.assertEqual(list(results), ['foo.service'])
+        self.assertEqual(results['foo.service'].get('Unit', 'Description'),
+                         'LSB: test foo service')
+
+        # should create symlink for the alternative name
+        self.assertEqual(os.readlink(os.path.join(self.out_dir, 'bar.service')),
+                         'foo.service')
+
+
 
 if __name__ == '__main__':
     unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))