systemctl: move check whether a service exists as native unit file to install.c

Move the search path check from the SysV service compat support into install.c
so that we can reuse the usual algorithm instead of rolling a private loop for
this.
This commit is contained in:
Lennart Poettering 2016-04-07 17:40:11 +02:00
parent 27c06cb516
commit e735decc38
3 changed files with 45 additions and 27 deletions

View file

@ -2052,6 +2052,25 @@ int unit_file_get_state(
return unit_file_lookup_state(scope, &paths, name, ret);
}
int unit_file_exists(UnitFileScope scope, const LookupPaths *paths, const char *name) {
_cleanup_(install_context_done) InstallContext c = {};
int r;
assert(paths);
assert(name);
if (!unit_name_is_valid(name, UNIT_NAME_ANY))
return -EINVAL;
r = install_info_discover(scope, &c, paths, name, 0, NULL);
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
return 1;
}
int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
_cleanup_strv_free_ char **files = NULL;
char **p;

View file

@ -139,6 +139,7 @@ int unit_file_add_dependency(UnitFileScope scope, bool runtime, const char *root
int unit_file_lookup_state(UnitFileScope scope, const LookupPaths *paths, const char *name, UnitFileState *ret);
int unit_file_get_state(UnitFileScope scope, const char *root_dir, const char *filename, UnitFileState *ret);
int unit_file_exists(UnitFileScope scope, const LookupPaths *paths, const char *name);
int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h);
Hashmap* unit_file_list_free(Hashmap *h);

View file

@ -5223,8 +5223,10 @@ static int enable_sysv_units(const char *verb, char **args) {
int r = 0;
#if defined(HAVE_SYSV_COMPAT)
unsigned f = 0;
_cleanup_lookup_paths_free_ LookupPaths paths = {};
unsigned f = 0;
/* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */
if (arg_scope != UNIT_FILE_SYSTEM)
return 0;
@ -5238,24 +5240,28 @@ static int enable_sysv_units(const char *verb, char **args) {
"is-enabled"))
return 0;
/* Processes all SysV units, and reshuffles the array so that
* afterwards only the native units remain */
r = lookup_paths_init(&paths, arg_scope, arg_root);
if (r < 0)
return r;
r = 0;
while (args[f]) {
const char *name;
const char *argv[] = {
ROOTLIBEXECDIR "/systemd-sysv-install",
NULL,
NULL,
NULL,
NULL,
};
_cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
bool found_native = false, found_sysv;
unsigned c = 1;
const char *argv[6] = { ROOTLIBEXECDIR "/systemd-sysv-install", NULL, NULL, NULL, NULL };
char **k;
int j;
pid_t pid;
siginfo_t status;
const char *name;
unsigned c = 1;
pid_t pid;
int j;
name = args[f++];
@ -5265,21 +5271,13 @@ static int enable_sysv_units(const char *verb, char **args) {
if (path_is_absolute(name))
continue;
STRV_FOREACH(k, paths.search_path) {
_cleanup_free_ char *path = NULL;
j = unit_file_exists(arg_scope, &paths, name);
if (j < 0)
return log_error_errno(j, "Failed to lookup unit file state: %m");
found_native = j > 0;
path = path_join(arg_root, *k, name);
if (!path)
return log_oom();
found_native = access(path, F_OK) >= 0;
if (found_native)
break;
}
/* If we have both a native unit and a SysV script,
* enable/disable them both (below); for is-enabled, prefer the
* native unit */
/* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled,
* prefer the native unit */
if (found_native && streq(verb, "is-enabled"))
continue;
@ -5293,9 +5291,9 @@ static int enable_sysv_units(const char *verb, char **args) {
continue;
if (found_native)
log_info("Synchronizing state of %s with SysV init with %s...", name, argv[0]);
log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]);
else
log_info("%s is not a native service, redirecting to systemd-sysv-install", name);
log_info("%s is not a native service, redirecting to systemd-sysv-install.", name);
if (!isempty(arg_root))
argv[c++] = q = strappend("--root=", arg_root);
@ -5308,7 +5306,7 @@ static int enable_sysv_units(const char *verb, char **args) {
if (!l)
return log_oom();
log_info("Executing %s", l);
log_info("Executing: %s", l);
pid = fork();
if (pid < 0)