sysv-generator: modernize
- Make sure we log each error at least once, and at most once - Replace FOREACH_WORD loops by extract_first_word() loops - Use FOREACH_DIRENT() for directory loops - Use free_and_strdup() where appropriate - Do not operate on half-loaded SysV files - Always properly free all memory
This commit is contained in:
parent
a1a078eef2
commit
c279613f86
|
@ -26,7 +26,9 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "hashmap.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "install.h"
|
||||
|
@ -85,9 +87,13 @@ typedef struct SysvStub {
|
|||
char **conflicts;
|
||||
bool has_lsb;
|
||||
bool reload;
|
||||
bool loaded;
|
||||
} SysvStub;
|
||||
|
||||
static void free_sysvstub(SysvStub *s) {
|
||||
if (!s)
|
||||
return;
|
||||
|
||||
free(s->name);
|
||||
free(s->path);
|
||||
free(s->description);
|
||||
|
@ -112,19 +118,14 @@ static void free_sysvstub_hashmapp(Hashmap **h) {
|
|||
}
|
||||
|
||||
static int add_symlink(const char *service, const char *where) {
|
||||
_cleanup_free_ char *from = NULL, *to = NULL;
|
||||
const char *from, *to;
|
||||
int r;
|
||||
|
||||
assert(service);
|
||||
assert(where);
|
||||
|
||||
from = strjoin(arg_dest, "/", service, NULL);
|
||||
if (!from)
|
||||
return log_oom();
|
||||
|
||||
to = strjoin(arg_dest, "/", where, ".wants/", service, NULL);
|
||||
if (!to)
|
||||
return log_oom();
|
||||
from = strjoina(arg_dest, "/", service);
|
||||
to = strjoina(arg_dest, "/", where, ".wants/", service);
|
||||
|
||||
mkdir_parents_label(to, 0755);
|
||||
|
||||
|
@ -132,6 +133,7 @@ static int add_symlink(const char *service, const char *where) {
|
|||
if (r < 0) {
|
||||
if (errno == EEXIST)
|
||||
return 0;
|
||||
|
||||
return -errno;
|
||||
}
|
||||
|
||||
|
@ -139,20 +141,19 @@ static int add_symlink(const char *service, const char *where) {
|
|||
}
|
||||
|
||||
static int add_alias(const char *service, const char *alias) {
|
||||
_cleanup_free_ char *link = NULL;
|
||||
const char *link;
|
||||
int r;
|
||||
|
||||
assert(service);
|
||||
assert(alias);
|
||||
|
||||
link = strjoin(arg_dest, "/", alias, NULL);
|
||||
if (!link)
|
||||
return log_oom();
|
||||
link = strjoina(arg_dest, "/", alias);
|
||||
|
||||
r = symlink(service, link);
|
||||
if (r < 0) {
|
||||
if (errno == EEXIST)
|
||||
return 0;
|
||||
|
||||
return -errno;
|
||||
}
|
||||
|
||||
|
@ -160,26 +161,32 @@ static int add_alias(const char *service, const char *alias) {
|
|||
}
|
||||
|
||||
static int generate_unit_file(SysvStub *s) {
|
||||
char **p;
|
||||
_cleanup_free_ char *before = NULL, *after = NULL, *wants = NULL, *conflicts = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_free_ char *unit = NULL,
|
||||
*before = NULL, *after = NULL,
|
||||
*wants = NULL, *conflicts = NULL;
|
||||
const char *unit;
|
||||
char **p;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
|
||||
if (!s->loaded)
|
||||
return 0;
|
||||
|
||||
unit = strjoina(arg_dest, "/", s->name);
|
||||
|
||||
before = strv_join(s->before, " ");
|
||||
after = strv_join(s->after, " ");
|
||||
wants = strv_join(s->wants, " ");
|
||||
conflicts = strv_join(s->conflicts, " ");
|
||||
unit = strjoin(arg_dest, "/", s->name, NULL);
|
||||
if (!before || !after || !wants || !conflicts || !unit)
|
||||
|
||||
if (!before || !after || !wants || !conflicts)
|
||||
return log_oom();
|
||||
|
||||
/* We might already have a symlink with the same name from a Provides:,
|
||||
* or from backup files like /etc/init.d/foo.bak. Real scripts always win,
|
||||
* so remove an existing link */
|
||||
if (is_symlink(unit) > 0) {
|
||||
log_warning("Overwriting existing symlink %s with real service", unit);
|
||||
log_warning("Overwriting existing symlink %s with real service.", unit);
|
||||
(void) unlink(unit);
|
||||
}
|
||||
|
||||
|
@ -191,9 +198,11 @@ static int generate_unit_file(SysvStub *s) {
|
|||
"# Automatically generated by systemd-sysv-generator\n\n"
|
||||
"[Unit]\n"
|
||||
"Documentation=man:systemd-sysv-generator(8)\n"
|
||||
"SourcePath=%s\n"
|
||||
"Description=%s\n",
|
||||
s->path, s->description);
|
||||
"SourcePath=%s\n",
|
||||
s->path);
|
||||
|
||||
if (s->description)
|
||||
fprintf(f, "Description=%s\n", s->description);
|
||||
|
||||
if (!isempty(before))
|
||||
fprintf(f, "Before=%s\n", before);
|
||||
|
@ -226,13 +235,17 @@ static int generate_unit_file(SysvStub *s) {
|
|||
if (s->reload)
|
||||
fprintf(f, "ExecReload=%s reload\n", s->path);
|
||||
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to write unit %s: %m", unit);
|
||||
|
||||
STRV_FOREACH(p, s->wanted_by) {
|
||||
r = add_symlink(s->name, *p);
|
||||
if (r < 0)
|
||||
log_error_errno(r, "Failed to create 'Wants' symlink to %s: %m", *p);
|
||||
log_warning_errno(r, "Failed to create 'Wants' symlink to %s, ignoring: %m", *p);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool usage_contains_reload(const char *line) {
|
||||
|
@ -262,7 +275,7 @@ static char *sysv_translate_name(const char *name) {
|
|||
return res;
|
||||
}
|
||||
|
||||
static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
|
||||
static int sysv_translate_facility(const char *name, const char *filename, char **ret) {
|
||||
|
||||
/* We silently ignore the $ prefix here. According to the LSB
|
||||
* spec it simply indicates whether something is a
|
||||
|
@ -281,31 +294,45 @@ static int sysv_translate_facility(const char *name, const char *filename, char
|
|||
"time", SPECIAL_TIME_SYNC_TARGET,
|
||||
};
|
||||
|
||||
char *filename_no_sh, *e, *r;
|
||||
char *filename_no_sh, *e, *m;
|
||||
const char *n;
|
||||
unsigned i;
|
||||
int r;
|
||||
|
||||
assert(name);
|
||||
assert(_r);
|
||||
assert(filename);
|
||||
assert(ret);
|
||||
|
||||
n = *name == '$' ? name + 1 : name;
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(table); i += 2) {
|
||||
|
||||
if (!streq(table[i], n))
|
||||
continue;
|
||||
|
||||
if (!table[i+1])
|
||||
return 0;
|
||||
|
||||
r = strdup(table[i+1]);
|
||||
if (!r)
|
||||
m = strdup(table[i+1]);
|
||||
if (!m)
|
||||
return log_oom();
|
||||
|
||||
goto finish;
|
||||
*ret = m;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* strip ".sh" suffix from file name for comparison */
|
||||
/* If we don't know this name, fallback heuristics to figure
|
||||
* out whether something is a target or a service alias. */
|
||||
|
||||
/* Facilities starting with $ are most likely targets */
|
||||
if (*name == '$') {
|
||||
r = unit_name_build(n, NULL, ".target", ret);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to build name: %m");
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Strip ".sh" suffix from file name for comparison */
|
||||
filename_no_sh = strdupa(filename);
|
||||
e = endswith(filename_no_sh, ".sh");
|
||||
if (e) {
|
||||
|
@ -313,103 +340,103 @@ static int sysv_translate_facility(const char *name, const char *filename, char
|
|||
filename = filename_no_sh;
|
||||
}
|
||||
|
||||
/* If we don't know this name, fallback heuristics to figure
|
||||
* out whether something is a target or a service alias. */
|
||||
|
||||
if (*name == '$') {
|
||||
int k;
|
||||
|
||||
/* Facilities starting with $ are most likely targets */
|
||||
k = unit_name_build(n, NULL, ".target", &r);
|
||||
if (k < 0)
|
||||
return k;
|
||||
|
||||
} else if (streq_ptr(n, filename))
|
||||
/* Names equaling the file name of the services are redundant */
|
||||
/* Names equaling the file name of the services are redundant */
|
||||
if (streq_ptr(n, filename))
|
||||
return 0;
|
||||
else
|
||||
/* Everything else we assume to be normal service names */
|
||||
r = sysv_translate_name(n);
|
||||
if (!r)
|
||||
return -ENOMEM;
|
||||
|
||||
finish:
|
||||
*_r = r;
|
||||
/* Everything else we assume to be normal service names */
|
||||
m = sysv_translate_name(n);
|
||||
if (!m)
|
||||
return log_oom();
|
||||
|
||||
*ret = m;
|
||||
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;
|
||||
UnitType t;
|
||||
assert(s);
|
||||
assert(full_text);
|
||||
assert(text);
|
||||
|
||||
n = strndup(word, z);
|
||||
if (!n)
|
||||
return log_oom();
|
||||
for (;;) {
|
||||
_cleanup_free_ char *word = NULL, *m = NULL;
|
||||
|
||||
r = sysv_translate_facility(n, basename(s->path), &m);
|
||||
r = extract_first_word(&text, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
|
||||
if (r < 0)
|
||||
return r;
|
||||
return log_error_errno(r, "Failed to parse word from provides string: %m");
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
r = sysv_translate_facility(word, basename(s->path), &m);
|
||||
if (r <= 0) /* continue on error */
|
||||
continue;
|
||||
|
||||
t = unit_name_to_type(m);
|
||||
if (t == UNIT_SERVICE) {
|
||||
switch (unit_name_to_type(m)) {
|
||||
|
||||
case 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 if (t == UNIT_TARGET) {
|
||||
break;
|
||||
|
||||
case 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
|
||||
* 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();
|
||||
}
|
||||
} else if (t == _UNIT_TYPE_INVALID)
|
||||
|
||||
break;
|
||||
|
||||
case _UNIT_TYPE_INVALID:
|
||||
log_warning("Unit name '%s' is invalid", m);
|
||||
else
|
||||
break;
|
||||
|
||||
default:
|
||||
log_warning("Unknown unit type for unit '%s'", m);
|
||||
}
|
||||
}
|
||||
if (!isempty(state_))
|
||||
log_error("[%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;
|
||||
assert(s);
|
||||
assert(full_text);
|
||||
assert(text);
|
||||
|
||||
for (;;) {
|
||||
_cleanup_free_ char *word = NULL, *m = NULL;
|
||||
bool is_before;
|
||||
|
||||
n = strndup(word, z);
|
||||
if (!n)
|
||||
return log_oom();
|
||||
|
||||
r = sysv_translate_facility(n, basename(s->path), &m);
|
||||
if (r < 0) {
|
||||
log_warning_errno(r, "[%s:%u] Failed to translate LSB dependency %s, ignoring: %m", s->path, line, n);
|
||||
continue;
|
||||
}
|
||||
r = extract_first_word(&text, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse word from provides string: %m");
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
r = sysv_translate_facility(word, basename(s->path), &m);
|
||||
if (r <= 0) /* continue on error */
|
||||
continue;
|
||||
|
||||
is_before = startswith_no_case(full_text, "X-Start-Before:");
|
||||
|
@ -419,15 +446,14 @@ static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text
|
|||
r = strv_extend(&s->after, m);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
r = strv_extend(&s->wants, m);
|
||||
} else
|
||||
r = strv_extend(is_before ? &s->before : &s->after, m);
|
||||
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
if (!isempty(state_))
|
||||
log_warning("[%s:%u] Trailing garbage in %*s, ignoring.", s->path, line, (int)(strchr(full_text, ':') - full_text), full_text);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -445,24 +471,22 @@ static int load_sysv(SysvStub *s) {
|
|||
_cleanup_free_ char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL;
|
||||
char *description;
|
||||
bool supports_reload = false;
|
||||
char l[LINE_MAX];
|
||||
|
||||
assert(s);
|
||||
|
||||
f = fopen(s->path, "re");
|
||||
if (!f)
|
||||
return errno == ENOENT ? 0 : -errno;
|
||||
if (!f) {
|
||||
if (errno == ENOENT)
|
||||
return 0;
|
||||
|
||||
return log_error_errno(errno, "Failed to open %s: %m", s->path);
|
||||
}
|
||||
|
||||
log_debug("Loading SysV script %s", s->path);
|
||||
|
||||
while (!feof(f)) {
|
||||
char l[LINE_MAX], *t;
|
||||
|
||||
if (!fgets(l, sizeof(l), f)) {
|
||||
if (feof(f))
|
||||
break;
|
||||
|
||||
return log_error_errno(errno, "Failed to read configuration file '%s': %m", s->path);
|
||||
}
|
||||
FOREACH_LINE(l, f, goto fail) {
|
||||
char *t;
|
||||
|
||||
line++;
|
||||
|
||||
|
@ -505,29 +529,25 @@ static int load_sysv(SysvStub *s) {
|
|||
|
||||
if (startswith_no_case(t, "description:")) {
|
||||
|
||||
size_t k = strlen(t);
|
||||
char *d;
|
||||
const char *j;
|
||||
size_t k;
|
||||
const const char *j;
|
||||
|
||||
if (t[k-1] == '\\') {
|
||||
k = strlen(t);
|
||||
if (k > 0 && t[k-1] == '\\') {
|
||||
state = DESCRIPTION;
|
||||
t[k-1] = 0;
|
||||
}
|
||||
|
||||
j = strstrip(t+12);
|
||||
if (j && *j) {
|
||||
d = strdup(j);
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
} else
|
||||
d = NULL;
|
||||
if (isempty(j))
|
||||
j = NULL;
|
||||
|
||||
free(chkconfig_description);
|
||||
chkconfig_description = d;
|
||||
r = free_and_strdup(&chkconfig_description, j);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
} else if (startswith_no_case(t, "pidfile:")) {
|
||||
|
||||
char *fn;
|
||||
const char *fn;
|
||||
|
||||
state = NORMAL;
|
||||
|
||||
|
@ -537,12 +557,9 @@ static int load_sysv(SysvStub *s) {
|
|||
continue;
|
||||
}
|
||||
|
||||
fn = strdup(fn);
|
||||
if (!fn)
|
||||
return -ENOMEM;
|
||||
|
||||
free(s->pid_file);
|
||||
s->pid_file = fn;
|
||||
r = free_and_strdup(&s->pid_file, fn);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
} else if (state == DESCRIPTION) {
|
||||
|
@ -550,25 +567,25 @@ static int load_sysv(SysvStub *s) {
|
|||
/* Try to parse Red Hat style description
|
||||
* continuation */
|
||||
|
||||
size_t k = strlen(t);
|
||||
size_t k;
|
||||
char *j;
|
||||
|
||||
if (t[k-1] == '\\')
|
||||
k = strlen(t);
|
||||
if (k > 0 && t[k-1] == '\\')
|
||||
t[k-1] = 0;
|
||||
else
|
||||
state = NORMAL;
|
||||
|
||||
j = strstrip(t);
|
||||
if (j && *j) {
|
||||
if (!isempty(j)) {
|
||||
char *d = NULL;
|
||||
|
||||
if (chkconfig_description)
|
||||
d = strjoin(chkconfig_description, " ", j, NULL);
|
||||
else
|
||||
d = strdup(j);
|
||||
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
return log_oom();
|
||||
|
||||
free(chkconfig_description);
|
||||
chkconfig_description = d;
|
||||
|
@ -582,6 +599,7 @@ static int load_sysv(SysvStub *s) {
|
|||
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:") ||
|
||||
|
@ -593,55 +611,47 @@ static int load_sysv(SysvStub *s) {
|
|||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
} else if (startswith_no_case(t, "Description:")) {
|
||||
char *d, *j;
|
||||
const char *j;
|
||||
|
||||
state = LSB_DESCRIPTION;
|
||||
|
||||
j = strstrip(t+12);
|
||||
if (j && *j) {
|
||||
d = strdup(j);
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
} else
|
||||
d = NULL;
|
||||
if (isempty(j))
|
||||
j = NULL;
|
||||
|
||||
free(long_description);
|
||||
long_description = d;
|
||||
r = free_and_strdup(&long_description, j);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
} else if (startswith_no_case(t, "Short-Description:")) {
|
||||
char *d, *j;
|
||||
const char *j;
|
||||
|
||||
state = LSB;
|
||||
|
||||
j = strstrip(t+18);
|
||||
if (j && *j) {
|
||||
d = strdup(j);
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
} else
|
||||
d = NULL;
|
||||
if (isempty(j))
|
||||
j = NULL;
|
||||
|
||||
free(short_description);
|
||||
short_description = d;
|
||||
r = free_and_strdup(&short_description, j);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
} else if (state == LSB_DESCRIPTION) {
|
||||
|
||||
if (startswith(l, "#\t") || startswith(l, "# ")) {
|
||||
char *j;
|
||||
const char *j;
|
||||
|
||||
j = strstrip(t);
|
||||
if (j && *j) {
|
||||
if (!isempty(j)) {
|
||||
char *d = NULL;
|
||||
|
||||
if (long_description)
|
||||
d = strjoin(long_description, " ", t, NULL);
|
||||
else
|
||||
d = strdup(j);
|
||||
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
return log_oom();
|
||||
|
||||
free(long_description);
|
||||
long_description = d;
|
||||
|
@ -672,12 +682,16 @@ static int load_sysv(SysvStub *s) {
|
|||
|
||||
d = strappend(s->has_lsb ? "LSB: " : "SYSV: ", description);
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
return log_oom();
|
||||
|
||||
s->description = d;
|
||||
}
|
||||
|
||||
s->loaded = true;
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
return log_error_errno(errno, "Failed to read configuration file '%s': %m", s->path);
|
||||
}
|
||||
|
||||
static int fix_order(SysvStub *s, Hashmap *all_services) {
|
||||
|
@ -687,6 +701,9 @@ static int fix_order(SysvStub *s, Hashmap *all_services) {
|
|||
|
||||
assert(s);
|
||||
|
||||
if (!s->loaded)
|
||||
return 0;
|
||||
|
||||
if (s->sysv_start_priority < 0)
|
||||
return 0;
|
||||
|
||||
|
@ -694,6 +711,9 @@ static int fix_order(SysvStub *s, Hashmap *all_services) {
|
|||
if (s == other)
|
||||
continue;
|
||||
|
||||
if (!other->loaded)
|
||||
continue;
|
||||
|
||||
if (other->sysv_start_priority < 0)
|
||||
continue;
|
||||
|
||||
|
@ -706,13 +726,12 @@ static int fix_order(SysvStub *s, Hashmap *all_services) {
|
|||
r = strv_extend(&s->after, other->name);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
else if (other->sysv_start_priority > s->sysv_start_priority) {
|
||||
|
||||
} else if (other->sysv_start_priority > s->sysv_start_priority) {
|
||||
r = strv_extend(&s->before, other->name);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
else
|
||||
} else
|
||||
continue;
|
||||
|
||||
/* FIXME: Maybe we should compare the name here lexicographically? */
|
||||
|
@ -724,6 +743,9 @@ static int fix_order(SysvStub *s, Hashmap *all_services) {
|
|||
static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
|
||||
char **path;
|
||||
|
||||
assert(lp);
|
||||
assert(all_services);
|
||||
|
||||
STRV_FOREACH(path, lp->sysvinit_path) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
struct dirent *de;
|
||||
|
@ -731,21 +753,18 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
|
|||
d = opendir(*path);
|
||||
if (!d) {
|
||||
if (errno != ENOENT)
|
||||
log_warning_errno(errno, "opendir(%s) failed: %m", *path);
|
||||
log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path);
|
||||
continue;
|
||||
}
|
||||
|
||||
while ((de = readdir(d))) {
|
||||
FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
|
||||
_cleanup_free_ char *fpath = NULL, *name = NULL;
|
||||
_cleanup_(free_sysvstubp) SysvStub *service = NULL;
|
||||
struct stat st;
|
||||
int r;
|
||||
|
||||
if (hidden_file(de->d_name))
|
||||
continue;
|
||||
|
||||
if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
|
||||
log_warning_errno(errno, "stat() failed on %s/%s: %m", *path, de->d_name);
|
||||
log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -762,15 +781,15 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
|
|||
if (hashmap_contains(all_services, name))
|
||||
continue;
|
||||
|
||||
fpath = strjoin(*path, "/", de->d_name, NULL);
|
||||
if (!fpath)
|
||||
return log_oom();
|
||||
|
||||
if (unit_file_lookup_state(UNIT_FILE_SYSTEM, NULL, lp, name) >= 0) {
|
||||
log_debug("Native unit for %s already exists, skipping", name);
|
||||
continue;
|
||||
}
|
||||
|
||||
fpath = strjoin(*path, "/", de->d_name, NULL);
|
||||
if (!fpath)
|
||||
return log_oom();
|
||||
|
||||
service = new0(SysvStub, 1);
|
||||
if (!service)
|
||||
return log_oom();
|
||||
|
@ -778,12 +797,12 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
|
|||
service->sysv_start_priority = -1;
|
||||
service->name = name;
|
||||
service->path = fpath;
|
||||
name = fpath = NULL;
|
||||
|
||||
r = hashmap_put(all_services, service->name, service);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
name = fpath = NULL;
|
||||
service = NULL;
|
||||
}
|
||||
}
|
||||
|
@ -792,43 +811,41 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
|
|||
}
|
||||
|
||||
static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) {
|
||||
char **p;
|
||||
unsigned i;
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
_cleanup_free_ char *path = NULL, *fpath = NULL;
|
||||
SysvStub *service;
|
||||
Iterator j;
|
||||
Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {};
|
||||
_cleanup_set_free_ Set *shutdown_services = NULL;
|
||||
int r = 0;
|
||||
SysvStub *service;
|
||||
unsigned i;
|
||||
Iterator j;
|
||||
char **p;
|
||||
int r;
|
||||
|
||||
STRV_FOREACH(p, lp->sysvrcnd_path)
|
||||
assert(lp);
|
||||
|
||||
STRV_FOREACH(p, lp->sysvrcnd_path) {
|
||||
for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
|
||||
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
struct dirent *de;
|
||||
|
||||
free(path);
|
||||
path = strjoin(*p, "/", rcnd_table[i].path, NULL);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
safe_closedir(d);
|
||||
if (!path) {
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
|
||||
d = opendir(path);
|
||||
if (!d) {
|
||||
if (errno != ENOENT)
|
||||
log_warning_errno(errno, "opendir(%s) failed: %m", path);
|
||||
log_warning_errno(errno, "Opening %s failed, ignoring: %m", path);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
while ((de = readdir(d))) {
|
||||
_cleanup_free_ char *name = NULL;
|
||||
|
||||
FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", path)) {
|
||||
_cleanup_free_ char *name = NULL, *fpath = NULL;
|
||||
int a, b;
|
||||
|
||||
if (hidden_file(de->d_name))
|
||||
continue;
|
||||
|
||||
if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
|
||||
continue;
|
||||
|
||||
|
@ -841,10 +858,9 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic
|
|||
if (a < 0 || b < 0)
|
||||
continue;
|
||||
|
||||
free(fpath);
|
||||
fpath = strjoin(*p, "/", de->d_name, NULL);
|
||||
if (!fpath) {
|
||||
r = -ENOMEM;
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
|
||||
|
@ -856,64 +872,77 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic
|
|||
|
||||
service = hashmap_get(all_services, name);
|
||||
if (!service){
|
||||
log_debug("Ignoring %s symlink in %s, not generating %s.",
|
||||
de->d_name, rcnd_table[i].path, name);
|
||||
log_debug("Ignoring %s symlink in %s, not generating %s.", de->d_name, rcnd_table[i].path, name);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (de->d_name[0] == 'S') {
|
||||
|
||||
if (rcnd_table[i].type == RUNLEVEL_UP) {
|
||||
service->sysv_start_priority =
|
||||
MAX(a*10 + b, service->sysv_start_priority);
|
||||
}
|
||||
if (rcnd_table[i].type == RUNLEVEL_UP)
|
||||
service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority);
|
||||
|
||||
r = set_ensure_allocated(&runlevel_services[i], NULL);
|
||||
if (r < 0)
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = set_put(runlevel_services[i], service);
|
||||
if (r < 0)
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
|
||||
} else if (de->d_name[0] == 'K' &&
|
||||
(rcnd_table[i].type == RUNLEVEL_DOWN)) {
|
||||
|
||||
r = set_ensure_allocated(&shutdown_services, NULL);
|
||||
if (r < 0)
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = set_put(shutdown_services, service);
|
||||
if (r < 0)
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
|
||||
SET_FOREACH(service, runlevel_services[i], j) {
|
||||
r = strv_extend(&service->before, rcnd_table[i].target);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
r = strv_extend(&service->wanted_by, rcnd_table[i].target);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
}
|
||||
|
||||
SET_FOREACH(service, shutdown_services, j) {
|
||||
r = strv_extend(&service->before, SPECIAL_SHUTDOWN_TARGET);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
r = strv_extend(&service->conflicts, SPECIAL_SHUTDOWN_TARGET);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
}
|
||||
|
||||
r = 0;
|
||||
|
||||
finish:
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
|
||||
set_free(runlevel_services[i]);
|
||||
|
||||
|
@ -921,11 +950,11 @@ finish:
|
|||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int r, q;
|
||||
_cleanup_lookup_paths_free_ LookupPaths lp = {};
|
||||
_cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
|
||||
_cleanup_lookup_paths_free_ LookupPaths lp = {};
|
||||
SysvStub *service;
|
||||
Iterator j;
|
||||
int r;
|
||||
|
||||
if (argc > 1 && argc != 4) {
|
||||
log_error("This program takes three or no arguments.");
|
||||
|
@ -943,43 +972,34 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
r = lookup_paths_init(&lp, MANAGER_SYSTEM, true, NULL, NULL, NULL, NULL);
|
||||
if (r < 0) {
|
||||
log_error("Failed to find lookup paths.");
|
||||
return EXIT_FAILURE;
|
||||
log_error_errno(r, "Failed to find lookup paths: %m");
|
||||
goto finish;
|
||||
}
|
||||
|
||||
all_services = hashmap_new(&string_hash_ops);
|
||||
if (!all_services) {
|
||||
log_oom();
|
||||
return EXIT_FAILURE;
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = enumerate_sysv(&lp, all_services);
|
||||
if (r < 0) {
|
||||
log_error("Failed to generate units for all init scripts.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
r = set_dependencies_from_rcnd(&lp, all_services);
|
||||
if (r < 0) {
|
||||
log_error("Failed to read runlevels from rcnd links.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
HASHMAP_FOREACH(service, all_services, j)
|
||||
(void) load_sysv(service);
|
||||
|
||||
HASHMAP_FOREACH(service, all_services, j) {
|
||||
q = load_sysv(service);
|
||||
if (q < 0)
|
||||
continue;
|
||||
(void) fix_order(service, all_services);
|
||||
(void) generate_unit_file(service);
|
||||
}
|
||||
|
||||
HASHMAP_FOREACH(service, all_services, j) {
|
||||
q = fix_order(service, all_services);
|
||||
if (q < 0)
|
||||
continue;
|
||||
r = 0;
|
||||
|
||||
q = generate_unit_file(service);
|
||||
if (q < 0)
|
||||
continue;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
finish:
|
||||
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue