install.c revision 3f0b2f0f452e94444e4fb7b62030ea05738bb1b6
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty <of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <fnmatch.h>
#include "util.h"
#include "mkdir.h"
#include "hashmap.h"
#include "set.h"
#include "path-util.h"
#include "path-lookup.h"
#include "strv.h"
#include "unit-name.h"
#include "install.h"
#include "conf-parser.h"
#include "conf-files.h"
#include "specifier.h"
#include "install-printf.h"
typedef struct {
#define _cleanup_lookup_paths_free_ \
#define _cleanup_install_context_done_ \
return lookup_paths_init(paths,
scope == UNIT_FILE_USER,
}
char *p = NULL;
int r;
switch (scope) {
case UNIT_FILE_SYSTEM:
else if (runtime)
else if (root_dir)
else
break;
case UNIT_FILE_GLOBAL:
if (root_dir)
return -EINVAL;
if (runtime)
else
p = strdup(USER_CONFIG_UNIT_PATH);
break;
case UNIT_FILE_USER:
return -EINVAL;
r = user_config_home(&p);
if (r <= 0)
return r < 0 ? r : -ENOENT;
break;
default:
assert_not_reached("Bad scope");
}
if (!p)
return -ENOMEM;
*ret = p;
return 0;
}
static int add_file_change(
unsigned *n_changes,
const char *path,
const char *source) {
UnitFileChange *c;
unsigned i;
if (!changes)
return 0;
if (!c)
return -ENOMEM;
*changes = c;
i = *n_changes;
if (!c[i].path)
return -ENOMEM;
if (source) {
if (!c[i].source) {
return -ENOMEM;
}
} else
*n_changes = i+1;
return 0;
}
static int mark_symlink_for_removal(
const char *p) {
char *n;
int r;
assert(p);
if (r < 0)
return r;
n = strdup(p);
if (!n)
return -ENOMEM;
r = set_put(*remove_symlinks_to, n);
if (r < 0) {
free(n);
return r == -EEXIST ? 0 : r;
}
return 0;
}
static int remove_marked_symlinks_fd(
int fd,
const char *path,
const char *config_path,
bool *deleted,
unsigned *n_changes,
char** files) {
int r = 0;
if (!d) {
return -errno;
}
rewinddir(d);
for (;;) {
union dirent_storage buf;
int k;
if (k != 0) {
r = -errno;
break;
}
if (!de)
break;
continue;
dirent_ensure_type(d, de);
int nfd, q;
char _cleanup_free_ *p = NULL;
if (nfd < 0) {
continue;
if (r == 0)
r = -errno;
continue;
}
if (!p) {
return -ENOMEM;
}
/* This will close nfd, regardless whether it succeeds or not */
q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, deleted, changes, n_changes, files);
if (r == 0)
r = q;
int q;
bool found;
if (!p)
return -ENOMEM;
q = readlink_and_canonicalize(p, &dest);
if (q < 0) {
if (q == -ENOENT)
continue;
if (r == 0)
r = q;
continue;
}
found =
if (unit_name_is_instance(p))
if (found) {
if (r == 0)
r = -errno;
} else {
rmdir_parents(p, config_path);
if (!set_get(remove_symlinks_to, p)) {
q = mark_symlink_for_removal(&remove_symlinks_to, p);
if (q < 0) {
if (r == 0)
r = q;
} else
*deleted = true;
}
}
}
}
}
return r;
}
static int remove_marked_symlinks(
const char *config_path,
unsigned *n_changes,
char** files) {
int fd, r = 0;
bool deleted;
if (set_size(remove_symlinks_to) <= 0)
return 0;
if (fd < 0)
return -errno;
do {
int q, cfd;
deleted = false;
if (cfd < 0) {
r = -errno;
break;
}
/* This takes possession of cfd and closes it */
q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, &deleted, changes, n_changes, files);
if (r == 0)
r = q;
} while (deleted);
return r;
}
static int find_symlinks_fd(
const char *name,
int fd,
const char *path,
const char *config_path,
bool *same_name_link) {
int r = 0;
if (!d) {
return -errno;
}
for (;;) {
int k;
union dirent_storage buf;
if (k != 0)
return -errno;
if (!de)
return r;
continue;
dirent_ensure_type(d, de);
int nfd, q;
char _cleanup_free_ *p = NULL;
if (nfd < 0) {
continue;
if (r == 0)
r = -errno;
continue;
}
if (!p) {
return -ENOMEM;
}
/* This will close nfd, regardless whether it succeeds or not */
if (q > 0)
return 1;
if (r == 0)
r = q;
bool found_path, found_dest, b = false;
int q;
/* Acquire symlink name */
if (!p)
return -ENOMEM;
/* Acquire symlink destination */
q = readlink_and_canonicalize(p, &dest);
if (q < 0) {
if (q == -ENOENT)
continue;
if (r == 0)
r = q;
continue;
}
/* Check if the symlink itself matches what we
* are looking for */
if (path_is_absolute(name))
else
/* Check if what the symlink points to
* matches what we are looking for */
if (path_is_absolute(name))
else
if (found_path && found_dest) {
char _cleanup_free_ *t = NULL;
/* Filter out same name links in the main
* config path */
if (!t)
return -ENOMEM;
b = path_equal(t, p);
}
if (b)
*same_name_link = true;
else if (found_path || found_dest)
return 1;
}
}
return r;
}
static int find_symlinks(
const char *name,
const char *config_path,
bool *same_name_link) {
int fd;
if (fd < 0) {
return 0;
return -errno;
}
/* This takes possession of fd and closes it */
}
static int find_symlinks_in_scope(
const char *root_dir,
const char *name,
UnitFileState *state) {
int r;
bool same_name_link_runtime = false, same_name_link = false;
/* First look in runtime config path */
if (r < 0)
return r;
if (r < 0)
return r;
else if (r > 0) {
return r;
}
}
/* Then look in the normal config path */
if (r < 0)
return r;
if (r < 0)
return r;
else if (r > 0) {
return r;
}
/* Hmm, we didn't find it, but maybe we found the same name
* link? */
if (same_name_link_runtime) {
return 1;
} else if (same_name_link) {
return 1;
}
return 0;
}
int unit_file_mask(
bool runtime,
const char *root_dir,
char *files[],
bool force,
unsigned *n_changes) {
char **i;
char _cleanup_free_ *prefix;
int r;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
if (!unit_name_is_valid(*i, true)) {
if (r == 0)
r = -EINVAL;
continue;
}
if (!path) {
r = -ENOMEM;
break;
}
continue;
}
if (null_or_empty_path(path) > 0)
continue;
if (force) {
continue;
}
}
if (r == 0)
r = -EEXIST;
} else {
if (r == 0)
r = -errno;
}
}
return r;
}
int unit_file_unmask(
bool runtime,
const char *root_dir,
char *files[],
unsigned *n_changes) {
char **i, *config_path = NULL;
int r, q;
if (r < 0)
goto finish;
STRV_FOREACH(i, files) {
char *path;
if (!unit_name_is_valid(*i, true)) {
if (r == 0)
r = -EINVAL;
continue;
}
if (!path) {
r = -ENOMEM;
break;
}
q = null_or_empty_path(path);
if (q > 0) {
continue;
}
q = -errno;
}
if (q != -ENOENT && r == 0)
r = q;
}
if (r == 0)
r = q;
return r;
}
int unit_file_link(
bool runtime,
const char *root_dir,
char *files[],
bool force,
unsigned *n_changes) {
char **i;
int r, q;
if (r < 0)
return r;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
char *fn;
fn = path_get_file_name(*i);
if (!path_is_absolute(*i) ||
!unit_name_is_valid(fn, true)) {
if (r == 0)
r = -EINVAL;
continue;
}
if (r == 0)
r = -errno;
continue;
}
r = -ENOENT;
continue;
}
if (q < 0)
return q;
if (q > 0)
continue;
if (!path)
return -ENOMEM;
continue;
}
if (r == 0)
r = q;
continue;
}
if (q >= 0 && path_equal(dest, *i))
continue;
if (force) {
continue;
}
}
if (r == 0)
r = -EEXIST;
} else {
if (r == 0)
r = -errno;
}
}
return r;
}
void unit_file_list_free(Hashmap *h) {
UnitFileList *i;
while ((i = hashmap_steal_first(h))) {
free(i);
}
hashmap_free(h);
}
unsigned i;
if (!changes)
return;
for (i = 0; i < n_changes; i++) {
}
}
static void install_info_free(InstallInfo *i) {
assert(i);
strv_free(i->required_by);
free(i);
}
static void install_info_hashmap_free(Hashmap *m) {
InstallInfo *i;
if (!m)
return;
while ((i = hashmap_steal_first(m)))
hashmap_free(m);
}
static void install_context_done(InstallContext *c) {
assert(c);
}
static int install_info_add(
InstallContext *c,
const char *name,
const char *path) {
InstallInfo *i = NULL;
int r;
assert(c);
if (!name)
if (!unit_name_is_valid(name, true))
return -EINVAL;
return 0;
if (r < 0)
return r;
if (!i)
return -ENOMEM;
if (!i->name) {
r = -ENOMEM;
goto fail;
}
if (path) {
if (!i->path) {
r = -ENOMEM;
goto fail;
}
}
if (r < 0)
goto fail;
return 0;
fail:
if (i)
return r;
}
static int install_info_add_auto(
InstallContext *c,
const char *name_or_path) {
assert(c);
if (path_is_absolute(name_or_path))
else
}
static int config_parse_also(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char *w;
size_t l;
char *state;
InstallContext *c = data;
char _cleanup_free_ *n;
int r;
n = strndup(w, l);
if (!n)
return -ENOMEM;
r = install_info_add(c, n, NULL);
if (r < 0)
return r;
}
return 0;
}
static int config_parse_user(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
InstallInfo *i = data;
char* printed;
if (!printed)
return -ENOMEM;
return 0;
}
static int unit_file_load(
InstallContext *c,
const char *path,
bool allow_symlink) {
const ConfigTableItem items[] = {
};
int fd;
int r;
assert(c);
if (fd < 0)
return -errno;
if (!f) {
return -ENOMEM;
}
if (r < 0)
return r;
return
}
static int unit_file_search(
InstallContext *c,
const char *root_dir,
bool allow_symlink) {
char **p;
int r;
assert(c);
else
if (!path)
return -ENOMEM;
if (r >= 0)
else {
/* unit file doesn't exist, however instance enablement was request */
/* we will check if it is possible to load template unit file */
*template_path = NULL,
*template_dir = NULL;
if (!template) {
return -ENOMEM;
}
/* we will reuse path variable since we don't need it anymore */
template_dir = path;
if (!template_path) {
return -ENOMEM;
}
/* let's try to load template unit */
if (r >= 0) {
return -ENOMEM;
}
}
}
}
return r;
}
return -ENOENT;
}
static int unit_file_can_install(
const char *root_dir,
const char *name,
bool allow_symlink) {
InstallInfo *i;
int r;
r = install_info_add_auto(&c, name);
if (r < 0)
return r;
if (r >= 0)
r =
strv_length(i->aliases) +
strv_length(i->wanted_by) +
strv_length(i->required_by);
return r;
}
static int create_symlink(
const char *old_path,
const char *new_path,
bool force,
unsigned *n_changes) {
int r;
return 0;
}
return -errno;
if (r < 0)
return r;
return 0;
if (!force)
return -EEXIST;
return 0;
}
return -errno;
}
static int install_info_symlink_alias(
InstallInfo *i,
const char *config_path,
bool force,
unsigned *n_changes) {
char **s;
int r = 0, q;
assert(i);
STRV_FOREACH(s, i->aliases) {
dst = install_full_printf(i, *s);
if (!dst)
return -ENOMEM;
if (!alias_path)
return -ENOMEM;
if (r == 0)
r = q;
}
return r;
}
static int install_info_symlink_wants(
InstallInfo *i,
const char *config_path,
bool force,
unsigned *n_changes) {
char **s;
int r = 0, q;
assert(i);
STRV_FOREACH(s, i->wanted_by) {
dst = install_full_printf(i, *s);
if (!dst)
return -ENOMEM;
if (!unit_name_is_valid(dst, true)) {
r = -EINVAL;
continue;
}
return -ENOMEM;
if (r == 0)
r = q;
}
return r;
}
static int install_info_symlink_requires(
InstallInfo *i,
const char *config_path,
bool force,
unsigned *n_changes) {
char **s;
int r = 0, q;
assert(i);
STRV_FOREACH(s, i->required_by) {
dst = install_full_printf(i, *s);
if (!dst)
return -ENOMEM;
if (!unit_name_is_valid(dst, true)) {
r = -EINVAL;
continue;
}
return -ENOMEM;
if (r == 0)
r = q;
}
return r;
}
static int install_info_symlink_link(
InstallInfo *i,
const char *config_path,
bool force,
unsigned *n_changes) {
int r;
assert(i);
if (r != 0)
return r;
return -ENOMEM;
return r;
}
static int install_info_apply(
InstallInfo *i,
const char *config_path,
bool force,
unsigned *n_changes) {
int r, q;
assert(i);
if (r == 0)
r = q;
if (r == 0)
r = q;
if (r == 0)
r = q;
return r;
}
static int install_context_apply(
InstallContext *c,
const char *config_path,
const char *root_dir,
bool force,
unsigned *n_changes) {
InstallInfo *i;
int r = 0, q;
assert(c);
while ((i = hashmap_first(c->will_install))) {
if (q < 0)
return q;
if (q < 0) {
if (r >= 0)
r = q;
return r;
} else if (r >= 0)
r += q;
if (r >= 0 && q < 0)
r = q;
}
return r;
}
static int install_context_mark_for_removal(
InstallContext *c,
const char *config_path,
const char *root_dir) {
InstallInfo *i;
int r = 0, q;
assert(c);
/* Marks all items for removal */
while ((i = hashmap_first(c->will_install))) {
if (q < 0)
return q;
if (q < 0) {
if (r >= 0)
r = q;
return r;
} else if (r >= 0)
r += q;
if (unit_name_is_instance(i->name)) {
/* unit file named as instance exists, thus all symlinks pointing to it, will be removed */
else
/* does not exist, thus we will mark for removal symlinks to template unit file */
} else
if (r >= 0 && q < 0)
r = q;
}
return r;
}
int unit_file_enable(
bool runtime,
const char *root_dir,
char *files[],
bool force,
unsigned *n_changes) {
char **i;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
r = install_info_add_auto(&c, *i);
if (r < 0)
return r;
}
/* This will return the number of symlink rules that were
supposed to be created, not the ones actually created. This is
useful to determine whether the passed files had any
installation data at all. */
return r;
}
int unit_file_disable(
bool runtime,
const char *root_dir,
char *files[],
unsigned *n_changes) {
char **i;
int r, q;
if (r < 0)
return r;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
r = install_info_add_auto(&c, *i);
if (r < 0)
return r;
}
if (r == 0)
r = q;
return r;
}
int unit_file_reenable(
bool runtime,
const char *root_dir,
char *files[],
bool force,
unsigned *n_changes) {
char **i;
int r, q;
if (r < 0)
return r;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
r = mark_symlink_for_removal(&remove_symlinks_to, *i);
if (r < 0)
return r;
r = install_info_add_auto(&c, *i);
if (r < 0)
return r;
}
/* Returns number of symlinks that where supposed to be installed. */
if (r == 0)
r = q;
return r;
}
const char *root_dir,
const char *name) {
char **i;
int r;
return -EINVAL;
if (!unit_name_is_valid(name, true))
return -EINVAL;
if (r < 0)
return r;
if (root_dir)
else
if (!path)
return -ENOMEM;
r = -errno;
continue;
return -errno;
}
return -ENOENT;
r = null_or_empty_path(path);
if (r < 0 && r != -ENOENT)
return r;
else if (r > 0) {
return state;
}
if (r < 0)
return r;
else if (r > 0)
return state;
return r;
else if (r > 0)
return UNIT_FILE_DISABLED;
else if (r == 0)
return UNIT_FILE_STATIC;
}
return r < 0 ? r : state;
}
char **i;
int r;
if (scope == UNIT_FILE_SYSTEM)
"/etc/systemd/system-preset",
"/usr/local/lib/systemd/system-preset",
"/usr/lib/systemd/system-preset",
#ifdef HAVE_SPLIT_USR
"/lib/systemd/system-preset",
#endif
NULL);
else if (scope == UNIT_FILE_GLOBAL)
"/etc/systemd/user-preset",
"/usr/local/lib/systemd/user-preset",
"/usr/lib/systemd/user-preset",
NULL);
else
return 1;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
FILE _cleanup_fclose_ *f;
f = fopen(*i, "re");
if (!f) {
continue;
return -errno;
}
for (;;) {
break;
if (!*l)
continue;
continue;
if (first_word(l, "enable")) {
l += 6;
l += strspn(l, WHITESPACE);
return 1;
} else if (first_word(l, "disable")) {
l += 7;
l += strspn(l, WHITESPACE);
return 0;
} else
log_debug("Couldn't parse line '%s'", l);
}
}
/* Default is "enable" */
return 1;
}
int unit_file_preset(
bool runtime,
const char *root_dir,
char *files[],
bool force,
unsigned *n_changes) {
char **i;
int r, q;
if (r < 0)
return r;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
if (!unit_name_is_valid(*i, true))
return -EINVAL;
r = unit_file_query_preset(scope, *i);
if (r < 0)
return r;
if (r)
r = install_info_add_auto(&plus, *i);
else
r = install_info_add_auto(&minus, *i);
if (r < 0)
return r;
}
if (r == 0)
r = q;
/* Returns number of symlinks that where supposed to be installed. */
if (r == 0)
r = q;
return r;
}
static void unitfilelist_free(UnitFileList **f) {
if (!*f)
return;
free(*f);
}
int unit_file_get_list(
const char *root_dir,
Hashmap *h) {
char **i;
int r;
assert(h);
return -EINVAL;
if (r < 0)
return r;
const char *units_dir;
if (root_dir) {
return -ENOMEM;
} else
units_dir = *i;
if (d)
closedir(d);
if (!d) {
continue;
return -errno;
}
for (;;) {
union dirent_storage buffer;
*f = NULL;
if (r != 0)
return -r;
if (!de)
break;
continue;
continue;
continue;
r = dirent_ensure_type(d, de);
if (r < 0) {
if (r == -ENOENT)
continue;
return r;
}
continue;
if (!f)
return -ENOMEM;
if (!f->path)
return -ENOMEM;
r = null_or_empty_path(f->path);
if (r < 0 && r != -ENOENT)
return r;
else if (r > 0) {
f->state =
path_startswith(*i, "/run") ?
goto found;
}
if (r < 0)
return r;
else if (r > 0) {
f->state = UNIT_FILE_ENABLED;
goto found;
}
if (r == -EINVAL || /* Invalid setting? */
r == -EBADMSG || /* Invalid format? */
r == -ENOENT /* Included file not found? */)
f->state = UNIT_FILE_INVALID;
else if (r < 0)
return r;
else if (r > 0)
f->state = UNIT_FILE_DISABLED;
else
f->state = UNIT_FILE_STATIC;
if (r < 0)
return r;
f = NULL; /* prevent cleanup */
}
}
return r;
}
static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
[UNIT_FILE_ENABLED] = "enabled",
[UNIT_FILE_ENABLED_RUNTIME] = "enabled-runtime",
[UNIT_FILE_LINKED] = "linked",
[UNIT_FILE_LINKED_RUNTIME] = "linked-runtime",
[UNIT_FILE_MASKED] = "masked",
[UNIT_FILE_MASKED_RUNTIME] = "masked-runtime",
[UNIT_FILE_STATIC] = "static",
[UNIT_FILE_DISABLED] = "disabled",
[UNIT_FILE_INVALID] = "invalid",
};
static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX] = {
[UNIT_FILE_SYMLINK] = "symlink",
[UNIT_FILE_UNLINK] = "unlink",
};