mirror of
https://github.com/systemd/systemd.git
synced 2024-11-12 15:21:19 +03:00
Merge pull request #412 from fsateler/sysv-invalid-names-v2
sysv-generator: detect invalid names and escape them V2
This commit is contained in:
commit
8914ea0e90
@ -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");
|
||||
r = endswith(c, ".sh");
|
||||
if (r) {
|
||||
*r = '\0';
|
||||
}
|
||||
|
||||
if (unit_name_mangle(c, UNIT_NAME_NOGLOB, &r) >= 0)
|
||||
return r;
|
||||
else
|
||||
/* Normal init script name */
|
||||
strcpy(stpcpy(r, name), ".service");
|
||||
|
||||
return r;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
|
||||
@ -340,6 +342,7 @@ static int handle_provides(SysvStub *s, unsigned line, const char *full_text, co
|
||||
|
||||
FOREACH_WORD_QUOTED(word, z, text, state_) {
|
||||
_cleanup_free_ char *n = NULL, *m = NULL;
|
||||
UnitType t;
|
||||
|
||||
n = strndup(word, z);
|
||||
if (!n)
|
||||
@ -351,12 +354,13 @@ static int handle_provides(SysvStub *s, unsigned line, const char *full_text, co
|
||||
if (r == 0)
|
||||
continue;
|
||||
|
||||
if (unit_name_to_type(m) == UNIT_SERVICE) {
|
||||
t = unit_name_to_type(m);
|
||||
if (t == UNIT_SERVICE) {
|
||||
log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
|
||||
r = add_alias(s->name, m);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m);
|
||||
} else {
|
||||
} else if (t == UNIT_TARGET) {
|
||||
/* NB: SysV targets which are provided by a
|
||||
* service are pulled in by the services, as
|
||||
* an indication that the generic service is
|
||||
@ -374,6 +378,10 @@ static int handle_provides(SysvStub *s, unsigned line, const char *full_text, co
|
||||
return log_oom();
|
||||
}
|
||||
}
|
||||
else if (t == _UNIT_TYPE_INVALID)
|
||||
log_warning("Unit name '%s' is invalid", m);
|
||||
else
|
||||
log_warning("Unknown unit type for unit '%s'", m);
|
||||
}
|
||||
if (!isempty(state_))
|
||||
log_error("[%s:%u] Trailing garbage in Provides, ignoring.", s->path, line);
|
||||
|
@ -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'''
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user