summaryrefslogtreecommitdiff
path: root/src/shared/install.c
diff options
context:
space:
mode:
authorVáclav Pavlín <vpavlin@redhat.com>2013-05-28 11:05:48 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-05-30 20:44:41 -0400
commit99504dd4c13af7516a976fffc0f68e6f26d3faac (patch)
treeb0650f8436617bb48701353f28310ac7f9a62de9 /src/shared/install.c
parent9749cd77bc6121a304a7f1eb0f03f26e620dc9da (diff)
systemctl: add commands set-default and get-default
systemctl set-default NAME links the default.target to the given unit, get-default prints out the path to the currently set default target.
Diffstat (limited to 'src/shared/install.c')
-rw-r--r--src/shared/install.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/shared/install.c b/src/shared/install.c
index 8f27c6d479..954dcb1e71 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1570,6 +1570,92 @@ int unit_file_reenable(
return r;
}
+int unit_file_set_default(
+ UnitFileScope scope,
+ const char *root_dir,
+ char *file,
+ UnitFileChange **changes,
+ unsigned *n_changes) {
+
+ _cleanup_lookup_paths_free_ LookupPaths paths = {};
+ _cleanup_install_context_done_ InstallContext c = {};
+ _cleanup_free_ char *config_path = NULL;
+ char *path;
+ int r;
+ InstallInfo *i = NULL;
+
+ assert(scope >= 0);
+ assert(scope < _UNIT_FILE_SCOPE_MAX);
+
+ if (unit_name_to_type(file) != UNIT_TARGET)
+ return -EINVAL;
+
+ r = lookup_paths_init_from_scope(&paths, scope);
+ if (r < 0)
+ return r;
+
+ r = get_config_path(scope, false, root_dir, &config_path);
+ if (r < 0)
+ return r;
+
+ r = install_info_add_auto(&c, file);
+ if (r < 0)
+ return r;
+
+ i = (InstallInfo*)hashmap_first(c.will_install);
+
+ r = unit_file_search(&c, i, &paths, root_dir, false);
+ if (r < 0)
+ return r;
+
+ path = strappenda(config_path, "/default.target");
+ r = create_symlink(i->path, path, true, changes, n_changes);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+int unit_file_get_default(
+ UnitFileScope scope,
+ const char *root_dir,
+ char **name) {
+
+ _cleanup_lookup_paths_free_ LookupPaths paths = {};
+ char **p;
+ int r;
+
+ r = lookup_paths_init_from_scope(&paths, scope);
+ if (r < 0)
+ return r;
+
+ STRV_FOREACH(p, paths.unit_path) {
+ _cleanup_free_ char *path = NULL, *tmp = NULL;
+
+ if (isempty(root_dir))
+ path = strappend(*p, "/default.target");
+ else
+ path = strjoin(root_dir, "/", *p, "/default.target", NULL);
+
+ if (!path)
+ return -ENOMEM;
+
+ r = readlink_malloc(path, &tmp);
+ if (r == -ENOENT)
+ continue;
+ else if (r < 0)
+ return r;
+
+ *name = strdup(path_get_file_name(tmp));
+ if (!*name)
+ return -ENOMEM;
+
+ return 0;
+ }
+
+ return -ENOENT;
+}
+
UnitFileState unit_file_get_state(
UnitFileScope scope,
const char *root_dir,