summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Sateler <fsateler@debian.org>2015-06-27 21:00:32 -0300
committerFelipe Sateler <fsateler@debian.org>2015-06-29 16:07:49 -0300
commit264581a2f1599a27de577549dc75fccefef6a579 (patch)
tree164ac6c5d432d99cbcd753b39a43cd6c8dc76c02
parent2c09a745eba5f463e12b498a2f62a5036253c55c (diff)
downloadsystemd-264581a2f1599a27de577549dc75fccefef6a579.tar.gz
sysv-generator: escape names when translating from sysv name
While the LSB suggests only [A-Za-z0-9], that doesn't prevent admins from doing the wrong thing. Lets not generate invalid names in that case.
-rw-r--r--src/sysv-generator/sysv-generator.c22
-rw-r--r--test/sysv-generator-test.py19
2 files changed, 31 insertions, 10 deletions
diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c
index c9dd3b6302..0d246b1835 100644
--- a/src/sysv-generator/sysv-generator.c
+++ b/src/sysv-generator/sysv-generator.c
@@ -241,19 +241,21 @@ static bool usage_contains_reload(const char *line) {
static char *sysv_translate_name(const char *name) {
char *r;
+ _cleanup_free_ char *c;
- r = new(char, strlen(name) + strlen(".service") + 1);
- if (!r)
- return NULL;
+ c = strdup(name);
+ if (!c)
+ return NULL;
- if (endswith(name, ".sh"))
- /* Drop .sh suffix */
- strcpy(stpcpy(r, name) - 3, ".service");
- else
- /* Normal init script name */
- strcpy(stpcpy(r, name), ".service");
+ r = endswith(c, ".sh");
+ if (r) {
+ *r = '\0';
+ }
- return r;
+ if (unit_name_mangle(c, UNIT_NAME_NOGLOB, &r) >= 0)
+ return r;
+ else
+ return NULL;
}
static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
diff --git a/test/sysv-generator-test.py b/test/sysv-generator-test.py
index af0493b9a8..23d6646bba 100644
--- a/test/sysv-generator-test.py
+++ b/test/sysv-generator-test.py
@@ -190,6 +190,15 @@ class SysvGeneratorTest(unittest.TestCase):
self.assert_enabled('foo.service', ['multi-user', 'graphical'])
self.assertNotIn('Overwriting', err)
+ def test_simple_escaped(self):
+ '''simple service without dependencies, that requires escaping the name'''
+
+ self.add_sysv('foo+', {})
+ self.add_sysv('foo-admin', {})
+ err, results = self.run_generator()
+ self.assertEqual(list(results), ['foo-admin.service', 'foo\\x2b.service'])
+ self.assertNotIn('Overwriting', err)
+
def test_simple_enabled_some(self):
'''simple service without dependencies, enabled in some runlevels'''
@@ -276,6 +285,16 @@ class SysvGeneratorTest(unittest.TestCase):
'foo.service')
self.assertNotIn('Overwriting', err)
+ def test_provides_escaped(self):
+ '''a script that Provides: a name that requires escaping'''
+
+ self.add_sysv('foo', {'Provides': 'foo foo+'})
+ err, results = self.run_generator()
+ self.assertEqual(list(results), ['foo.service'])
+ self.assertEqual(os.readlink(os.path.join(self.out_dir, 'foo\\x2b.service')),
+ 'foo.service')
+ self.assertNotIn('Overwriting', err)
+
def test_same_provides_in_multiple_scripts(self):
'''multiple init.d scripts provide the same name'''