sysv-generator: split out two nested blocks into functions
No functional change intended. Just splitting this out to make it easier to edit in the future.
This commit is contained in:
parent
dd168445f8
commit
1e2fee5f75
|
@ -320,6 +320,110 @@ finish:
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int handle_provides(SysvStub *s, unsigned line, const char *full_text, const char *text) {
|
||||
const char *word, *state_;
|
||||
size_t z;
|
||||
int r;
|
||||
|
||||
FOREACH_WORD_QUOTED(word, z, text, state_) {
|
||||
_cleanup_free_ char *n = NULL, *m = NULL;
|
||||
|
||||
n = strndup(word, z);
|
||||
if (!n)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sysv_translate_facility(n, basename(s->path), &m);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
continue;
|
||||
|
||||
if (unit_name_to_type(m) == UNIT_SERVICE) {
|
||||
log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
|
||||
r = add_alias(s->name, m);
|
||||
} else {
|
||||
/* NB: SysV targets which are provided by a
|
||||
* service are pulled in by the services, as
|
||||
* an indication that the generic service is
|
||||
* now available. This is strictly one-way.
|
||||
* The targets do NOT pull in SysV services! */
|
||||
r = strv_extend(&s->before, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
r = strv_extend(&s->wants, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET)) {
|
||||
r = strv_extend(&s->before, SPECIAL_NETWORK_TARGET);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
|
||||
s->path, line, m, strerror(-r));
|
||||
}
|
||||
if (!isempty(state_))
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Trailing garbage in Provides, ignoring.",
|
||||
s->path, line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text, const char *text) {
|
||||
const char *word, *state_;
|
||||
size_t z;
|
||||
int r;
|
||||
|
||||
FOREACH_WORD_QUOTED(word, z, text, state_) {
|
||||
_cleanup_free_ char *n = NULL, *m = NULL;
|
||||
bool is_before;
|
||||
|
||||
n = strndup(word, z);
|
||||
if (!n)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sysv_translate_facility(n, basename(s->path), &m);
|
||||
if (r < 0) {
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
|
||||
s->path, line, n, strerror(-r));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (r == 0)
|
||||
continue;
|
||||
|
||||
is_before = startswith_no_case(full_text, "X-Start-Before:");
|
||||
|
||||
if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before) {
|
||||
/* the network-online target is special, as it needs to be actively pulled in */
|
||||
r = strv_extend(&s->after, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
r = strv_extend(&s->wants, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
} else {
|
||||
r = strv_extend(is_before ? &s->before : &s->after, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Failed to add dependency on %s, ignoring: %s",
|
||||
s->path, line, m, strerror(-r));
|
||||
}
|
||||
if (!isempty(state_))
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Trailing garbage in %*s, ignoring.",
|
||||
s->path, line, (int)(strchr(full_text, ':') - full_text), full_text);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_sysv(SysvStub *s) {
|
||||
_cleanup_fclose_ FILE *f;
|
||||
unsigned line = 0;
|
||||
|
@ -471,126 +575,22 @@ static int load_sysv(SysvStub *s) {
|
|||
} else if (state == LSB || state == LSB_DESCRIPTION) {
|
||||
|
||||
if (startswith_no_case(t, "Provides:")) {
|
||||
const char *word, *state_;
|
||||
size_t z;
|
||||
|
||||
state = LSB;
|
||||
|
||||
FOREACH_WORD_QUOTED(word, z, t+9, state_) {
|
||||
_cleanup_free_ char *n = NULL, *m = NULL;
|
||||
|
||||
n = strndup(word, z);
|
||||
if (!n)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sysv_translate_facility(n, basename(s->path), &m);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
continue;
|
||||
|
||||
if (unit_name_to_type(m) == UNIT_SERVICE) {
|
||||
log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
|
||||
r = add_alias(s->name, m);
|
||||
} else {
|
||||
/* NB: SysV targets
|
||||
* which are provided
|
||||
* by a service are
|
||||
* pulled in by the
|
||||
* services, as an
|
||||
* indication that the
|
||||
* generic service is
|
||||
* now available. This
|
||||
* is strictly
|
||||
* one-way. The
|
||||
* targets do NOT pull
|
||||
* in the SysV
|
||||
* services! */
|
||||
r = strv_extend(&s->before, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
r = strv_extend(&s->wants, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET)) {
|
||||
r = strv_extend(&s->before, SPECIAL_NETWORK_TARGET);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
|
||||
s->path, line, m, strerror(-r));
|
||||
}
|
||||
if (!isempty(state_))
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Trailing garbage in Provides, ignoring.",
|
||||
s->path, line);
|
||||
|
||||
r = handle_provides(s, line, t, t + 9);
|
||||
if (r < 0)
|
||||
return r;
|
||||
} else if (startswith_no_case(t, "Required-Start:") ||
|
||||
startswith_no_case(t, "Should-Start:") ||
|
||||
startswith_no_case(t, "X-Start-Before:") ||
|
||||
startswith_no_case(t, "X-Start-After:")) {
|
||||
const char *word, *state_;
|
||||
size_t z;
|
||||
|
||||
state = LSB;
|
||||
|
||||
FOREACH_WORD_QUOTED(word, z, strchr(t, ':')+1, state_) {
|
||||
_cleanup_free_ char *n = NULL, *m = NULL;
|
||||
bool is_before;
|
||||
r = handle_dependencies(s, line, t, strchr(t, ':') + 1);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
n = strndup(word, z);
|
||||
if (!n)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sysv_translate_facility(n, basename(s->path), &m);
|
||||
if (r < 0) {
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
|
||||
s->path, line, n, strerror(-r));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (r == 0)
|
||||
continue;
|
||||
|
||||
is_before = startswith_no_case(t, "X-Start-Before:");
|
||||
|
||||
if (streq(m, SPECIAL_NETWORK_ONLINE_TARGET) && !is_before) {
|
||||
/* the network-online target is special, as it needs to be actively pulled in */
|
||||
r = strv_extend(&s->after, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
r = strv_extend(&s->wants, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
else {
|
||||
if (is_before) {
|
||||
r = strv_extend(&s->before, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
else {
|
||||
r = strv_extend(&s->after, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Failed to add dependency on %s, ignoring: %s",
|
||||
s->path, line, m, strerror(-r));
|
||||
}
|
||||
if (!isempty(state_))
|
||||
log_unit_error(s->name,
|
||||
"[%s:%u] Trailing garbage in %*s, ignoring.",
|
||||
s->path, line,
|
||||
(int)(strchr(t, ':') - t), t);
|
||||
|
||||
} else if (startswith_no_case(t, "Description:")) {
|
||||
char *d, *j;
|
||||
|
|
Loading…
Reference in a new issue