[systemd-devel] [PATCH 2/2] systemd: enabling/disabling template units
Michal Sekletar
msekleta at redhat.com
Tue Jul 3 03:04:40 PDT 2012
Makes possible to enable/disable instance of units and should resolve
BZ 752774.
---
src/shared/install.c | 81 +++++++++++++++++++++++++++++++++++++++++-------
src/shared/unit-name.c | 11 +++++++
src/shared/unit-name.h | 1 +
3 files changed, 81 insertions(+), 12 deletions(-)
diff --git a/src/shared/install.c b/src/shared/install.c
index 13ae9a9..dd27272 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -196,7 +196,8 @@ static int remove_marked_symlinks_fd(
const char *config_path,
bool *deleted,
UnitFileChange **changes,
- unsigned *n_changes) {
+ unsigned *n_changes,
+ char** files) {
int r = 0;
DIR *d;
@@ -255,7 +256,7 @@ static int remove_marked_symlinks_fd(
}
/* 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);
+ q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, deleted, changes, n_changes, files);
free(p);
if (r == 0)
@@ -284,9 +285,16 @@ static int remove_marked_symlinks_fd(
continue;
}
- found =
- set_get(remove_symlinks_to, dest) ||
- set_get(remove_symlinks_to, path_get_file_name(dest));
+ if (unit_name_is_instance(p)) {
+ char *template = path_get_file_name(p);
+
+ found = (strv_contains(files, template)) &&
+ (set_get(remove_symlinks_to, dest) ||
+ set_get(remove_symlinks_to, path_get_file_name(dest)));
+ } else
+ found = set_get(remove_symlinks_to, dest) ||
+ set_get(remove_symlinks_to, path_get_file_name(dest));
+
if (found) {
@@ -326,7 +334,8 @@ static int remove_marked_symlinks(
Set *remove_symlinks_to,
const char *config_path,
UnitFileChange **changes,
- unsigned *n_changes) {
+ unsigned *n_changes,
+ char** files) {
int fd, r = 0;
bool deleted;
@@ -351,7 +360,7 @@ static int remove_marked_symlinks(
}
/* 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);
+ 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);
@@ -717,7 +726,7 @@ int unit_file_unmask(
finish:
- q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes);
+ q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
if (r == 0)
r = q;
@@ -1090,6 +1099,43 @@ static int unit_file_search(
if (!path)
return -ENOMEM;
+ r = access(path, R_OK);
+ if (r < 0 && errno == ENOENT && unit_name_is_instance(info->name)) {
+ char *template = NULL,
+ *template_path = NULL,
+ *template_dir = NULL;
+
+ template = unit_name_template(info->name);
+ if (!template)
+ return -ENOMEM;
+
+ if (isempty(root_dir))
+ asprintf(&template_dir, "%s/", *p);
+ else
+ asprintf(&template_dir, "%s/%s/", root_dir, *p);
+
+ if (!template_dir) {
+ free(template);
+ return -ENOMEM;
+ }
+
+ template_path = join(template_dir, template, NULL);
+ if (!template_path) {
+ free(template);
+ free(template_dir);
+ return -ENOMEM;
+ }
+
+ if (!access(template_path, R_OK)) {
+ free(path);
+ path = template_path;
+ } else
+ free(template_path);
+
+ free(template);
+ free(template_dir);
+ }
+
r = unit_file_load(c, info, path, allow_symlink);
if (r >= 0)
@@ -1419,7 +1465,18 @@ static int install_context_mark_for_removal(
} else if (r >= 0)
r += q;
- q = mark_symlink_for_removal(remove_symlinks_to, i->name);
+ if (unit_name_is_instance(i->name)) {
+ char *template = NULL;
+
+ template = unit_name_template(i->name);
+ if (!template)
+ return -ENOMEM;
+
+ q = mark_symlink_for_removal(remove_symlinks_to, template);
+ free(template);
+ } else
+ q = mark_symlink_for_removal(remove_symlinks_to, i->name);
+
if (r >= 0 && q < 0)
r = q;
}
@@ -1511,7 +1568,7 @@ int unit_file_disable(
r = install_context_mark_for_removal(&c, &paths, &remove_symlinks_to, config_path, root_dir);
- q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes);
+ q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
if (r == 0)
r = q;
@@ -1563,7 +1620,7 @@ int unit_file_reenable(
goto finish;
}
- r = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes);
+ r = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
/* Returns number of symlinks that where supposed to be installed. */
q = install_context_apply(&c, &paths, config_path, root_dir, force, changes, n_changes);
@@ -1813,7 +1870,7 @@ int unit_file_preset(
r = install_context_mark_for_removal(&minus, &paths, &remove_symlinks_to, config_path, root_dir);
- q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes);
+ q = remove_marked_symlinks(remove_symlinks_to, config_path, changes, n_changes, files);
if (r == 0)
r = q;
diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c
index 4b52f7b..cb2d9e3 100644
--- a/src/shared/unit-name.c
+++ b/src/shared/unit-name.c
@@ -313,6 +313,17 @@ bool unit_name_is_template(const char *n) {
return p[1] == '.';
}
+bool unit_name_is_instance(const char *n) {
+ const char *p;
+
+ assert(n);
+
+ if (!(p = strchr(n, '@')))
+ return false;
+
+ return p[1] != '.';
+}
+
char *unit_name_replace_instance(const char *f, const char *i) {
const char *p, *e;
char *r, *k;
diff --git a/src/shared/unit-name.h b/src/shared/unit-name.h
index 13665c5..840f820 100644
--- a/src/shared/unit-name.h
+++ b/src/shared/unit-name.h
@@ -44,6 +44,7 @@ char *unit_name_path_escape(const char *f);
char *unit_name_path_unescape(const char *f);
bool unit_name_is_template(const char *n);
+bool unit_name_is_instance(const char *n);
char *unit_name_replace_instance(const char *f, const char *i);
--
1.7.10.4
More information about the systemd-devel
mailing list