install.c revision 034a2a52ac0ec83e0229941d635d310b23eb04df
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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 "hashmap.h"
#include "set.h"
#include "path-lookup.h"
#include "strv.h"
#include "unit-name.h"
#include "install.h"
#include "conf-parser.h"
typedef struct {
char *name;
char *path;
char **aliases;
char **wanted_by;
} InstallInfo;
typedef struct {
return lookup_paths_init(paths,
scope == UNIT_FILE_USER);
}
char *p = NULL;
int r;
switch (scope) {
case UNIT_FILE_SYSTEM:
return -EINVAL;
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) {
int r = 0;
DIR *d;
if (!d) {
return -errno;
}
rewinddir(d);
for (;;) {
int k;
if (k != 0) {
r = -errno;
break;
}
if (!de)
break;
continue;
dirent_ensure_type(d, de);
int nfd, q;
char *p;
if (nfd < 0) {
continue;
if (r == 0)
r = -errno;
continue;
}
if (!p) {
r = -ENOMEM;
break;
}
/* 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);
free(p);
if (r == 0)
r = q;
char *p, *dest;
int q;
bool found;
if (!p) {
r = -ENOMEM;
break;
}
q = readlink_and_canonicalize(p, &dest);
if (q < 0) {
free(p);
if (q == -ENOENT)
continue;
if (r == 0)
r = q;
continue;
}
found =
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;
}
}
}
free(p);
}
}
closedir(d);
return r;
}
static int remove_marked_symlinks(
const char *config_path,
unsigned *n_changes) {
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);
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;
DIR *d;
if (!d) {
return -errno;
}
for (;;) {
int k;
if (k != 0) {
r = -errno;
break;
}
if (!de)
break;
continue;
dirent_ensure_type(d, de);
int nfd, q;
char *p;
if (nfd < 0) {
continue;
if (r == 0)
r = -errno;
continue;
}
if (!p) {
r = -ENOMEM;
break;
}
/* This will close nfd, regardless whether it succeeds or not */
free(p);
if (q > 0) {
r = 1;
break;
}
if (r == 0)
r = q;
char *p, *dest;
bool found_path, found_dest, b = false;
int q;
/* Acquire symlink name */
if (!p) {
r = -ENOMEM;
break;
}
/* Acquire symlink destination */
q = readlink_and_canonicalize(p, &dest);
if (q < 0) {
free(p);
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 *t;
/* Filter out same name links in the main
* config path */
if (!t) {
free(p);
r = -ENOMEM;
break;
}
b = path_equal(t, p);
free(t);
}
free(p);
if (b)
*same_name_link = true;
else if (found_path || found_dest) {
r = 1;
break;
}
}
}
closedir(d);
return r;
}
static int find_symlinks(
const char *name,
const char *config_path,
bool *same_name_link) {
int fd;
if (fd < 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;
char *path;
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, *prefix;
int r;
if (r < 0)
return r;
STRV_FOREACH(i, files) {
char *path;
if (!unit_name_is_valid_no_type(*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_no_type(*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, *config_path = NULL;
int r, q;
if (r < 0)
return r;
if (r < 0)
goto finish;
STRV_FOREACH(i, files) {
fn = file_name_from_path(*i);
if (!path_is_absolute(*i) ||
!unit_name_is_valid_no_type(fn, true)) {
if (r == 0)
r = -EINVAL;
continue;
}
if (r == 0)
r = -errno;
continue;
}
r = -ENOENT;
continue;
}
if (q < 0) {
r = q;
break;
}
if (q > 0)
continue;
if (!path) {
r = -ENOMEM;
break;
}
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);
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_no_type(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 *n;
int r;
n = strndup(w, l);
if (!n)
return -ENOMEM;
r = install_info_add(c, n, NULL);
if (r < 0) {
free(n);
return r;
}
free(n);
}
return 0;
}
static int unit_file_load(
InstallContext *c,
const char *path,
bool allow_symlink) {
const ConfigItem items[] = {
};
int fd;
FILE *f;
int r;
assert(c);
if (fd < 0)
return -errno;
if (!f) {
return -ENOMEM;
}
fclose(f);
if (r < 0)
return r;
}
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
return r;
}
return -ENOENT;
}
static int unit_file_can_install(
const char *root_dir,
const char *name,
bool allow_symlink) {
InstallInfo *i;
int r;
zero(c);
r = install_info_add_auto(&c, name);
if (r < 0)
return r;
if (r >= 0)
install_context_done(&c);
return r;
}
static int create_symlink(
const char *old_path,
const char *new_path,
bool force,
unsigned *n_changes) {
char *dest;
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) {
char *alias_path;
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) {
char *path;
if (!unit_name_is_valid_no_type(*s, 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;
char *path;
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;
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 (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, *config_path = NULL;
int r;
zero(c);
if (r < 0)
return r;
if (r < 0)
goto finish;
STRV_FOREACH(i, files) {
r = install_info_add_auto(&c, *i);
if (r < 0)
goto finish;
}
install_context_done(&c);
return r;
}
int unit_file_disable(
bool runtime,
const char *root_dir,
char *files[],
unsigned *n_changes) {
char **i, *config_path = NULL;
int r, q;
zero(c);
if (r < 0)
return r;
if (r < 0)
goto finish;
STRV_FOREACH(i, files) {
r = install_info_add_auto(&c, *i);
if (r < 0)
goto finish;
}
if (r == 0)
r = q;
install_context_done(&c);
return r;
}
int unit_file_reenable(
bool runtime,
const char *root_dir,
char *files[],
bool force,
unsigned *n_changes) {
char **i, *config_path = NULL;
int r, q;
zero(c);
if (r < 0)
return r;
if (r < 0)
goto finish;
STRV_FOREACH(i, files) {
r = mark_symlink_for_removal(&remove_symlinks_to, *i);
if (r < 0)
goto finish;
r = install_info_add_auto(&c, *i);
if (r < 0)
goto finish;
}
if (r == 0)
r = q;
install_context_done(&c);
return r;
}
const char *root_dir,
const char *name) {
int r;
return -EINVAL;
if (!unit_name_is_valid_no_type(name, true))
return -EINVAL;
if (r < 0)
return r;
if (root_dir)
else
if (!path) {
r = -ENOMEM;
goto finish;
}
continue;
r = -errno;
goto finish;
}
r = -ENOENT;
goto finish;
}
r = null_or_empty_path(path);
if (r < 0 && r != -ENOENT)
goto finish;
else if (r > 0) {
r = 0;
goto finish;
}
if (r < 0) {
goto finish;
} else if (r > 0) {
r = 0;
goto finish;
}
goto finish;
else if (r > 0) {
r = 0;
goto finish;
} else if (r == 0) {
r = 0;
goto finish;
}
}
return r < 0 ? r : state;
}
char **files, **i;
int r;
if (scope == UNIT_FILE_SYSTEM)
"/etc/systemd/system.preset",
"/usr/local/lib/systemd/system.preset",
"/usr/lib/systemd/system.preset",
"/lib/systemd/system.preset",
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 *f;
f = fopen(*i, "re");
if (!f) {
continue;
r = -errno;
goto finish;
}
for (;;) {
break;
if (!*l)
continue;
continue;
if (first_word(l, "enable")) {
l += 6;
l += strspn(l, WHITESPACE);
r = 1;
fclose(f);
goto finish;
}
} else if (first_word(l, "disable")) {
l += 7;
l += strspn(l, WHITESPACE);
r = 0;
fclose(f);
goto finish;
}
} else
log_debug("Couldn't parse line '%s'", l);
}
fclose(f);
}
/* Default is "enable" */
r = 1;
return r;
}
int unit_file_preset(
bool runtime,
const char *root_dir,
char *files[],
bool force,
unsigned *n_changes) {
char **i, *config_path = NULL;
int r, q;
if (r < 0)
return r;
if (r < 0)
goto finish;
STRV_FOREACH(i, files) {
if (!unit_name_is_valid_no_type(*i, true)) {
r = -EINVAL;
goto finish;
}
r = unit_file_query_preset(scope, *i);
if (r < 0)
goto finish;
if (r)
r = install_info_add_auto(&plus, *i);
else
r = install_info_add_auto(&minus, *i);
if (r < 0)
goto finish;
}
if (r == 0)
r = q;
if (r == 0)
r = q;
return r;
}
int unit_file_get_list(
const char *root_dir,
Hashmap *h) {
int r;
assert(h);
return -EINVAL;
if (r < 0)
return r;
const char *units_dir;
if (root_dir) {
r = -ENOMEM;
goto finish;
}
} else
units_dir = *i;
if (d)
closedir(d);
if (!d) {
continue;
r = -errno;
goto finish;
}
for (;;) {
UnitFileList *f;
if (r != 0) {
r = -r;
goto finish;
}
if (!de)
break;
continue;
continue;
continue;
r = dirent_ensure_type(d, de);
if (r < 0) {
continue;
goto finish;
}
continue;
if (!f) {
r = -ENOMEM;
goto finish;
}
if (!f->path) {
free(f);
r = -ENOMEM;
goto finish;
}
r = null_or_empty_path(f->path);
if (r < 0) {
free(f);
goto finish;
} else if (r > 0) {
f->state =
path_startswith(*i, "/run") ?
goto found;
}
if (r < 0) {
free(f);
goto finish;
} else if (r > 0)
goto found;
if (r < 0) {
free(f);
goto finish;
} else if (r > 0) {
f->state = UNIT_FILE_DISABLED;
goto found;
} else if (r == 0) {
f->state = UNIT_FILE_STATIC;
goto found;
}
free(f);
continue;
if (r < 0) {
free(f);
goto finish;
}
}
}
if (d)
closedir(d);
return r;
}
static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {
[UNIT_FILE_ENABLED] = "enabled",
[UNIT_FILE_ENABLED_RUNTIME] = "enabled-runtie",
[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"
};