chiark / gitweb /
service: add options RestartPreventExitStatus and SuccessExitStatus
[elogind.git] / src / shared / install.c
index d6644e580fbf16cc904bfe92ec4837a7a347d389..ef1c3f584d3a4d28c9c76eb4326d37070a31cdc8 100644 (file)
@@ -60,7 +60,8 @@ static int lookup_paths_init_from_scope(LookupPaths *paths, UnitFileScope scope)
 
         return lookup_paths_init(paths,
                                  scope == UNIT_FILE_SYSTEM ? MANAGER_SYSTEM : MANAGER_USER,
-                                 scope == UNIT_FILE_USER);
+                                 scope == UNIT_FILE_USER,
+                                 NULL, NULL, NULL);
 }
 
 static int get_config_path(UnitFileScope scope, bool runtime, const char *root_dir, char **ret) {
@@ -195,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;
@@ -254,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)
@@ -287,6 +289,9 @@ static int remove_marked_symlinks_fd(
                                 set_get(remove_symlinks_to, dest) ||
                                 set_get(remove_symlinks_to, path_get_file_name(dest));
 
+                        if (unit_name_is_instance(p))
+                                found = found && strv_contains(files, path_get_file_name(p));
+
                         if (found) {
 
                                 if (unlink(p) < 0 && errno != ENOENT) {
@@ -325,7 +330,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;
@@ -350,7 +356,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);
@@ -607,7 +613,7 @@ int unit_file_mask(
         STRV_FOREACH(i, files) {
                 char *path;
 
-                if (!unit_name_is_valid_no_type(*i, true)) {
+                if (!unit_name_is_valid(*i, true)) {
                         if (r == 0)
                                 r = -EINVAL;
                         continue;
@@ -683,7 +689,7 @@ int unit_file_unmask(
         STRV_FOREACH(i, files) {
                 char *path;
 
-                if (!unit_name_is_valid_no_type(*i, true)) {
+                if (!unit_name_is_valid(*i, true)) {
                         if (r == 0)
                                 r = -EINVAL;
                         continue;
@@ -716,7 +722,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;
 
@@ -759,7 +765,7 @@ int unit_file_link(
                 fn = path_get_file_name(*i);
 
                 if (!path_is_absolute(*i) ||
-                    !unit_name_is_valid_no_type(fn, true)) {
+                    !unit_name_is_valid(fn, true)) {
                         if (r == 0)
                                 r = -EINVAL;
                         continue;
@@ -922,7 +928,7 @@ static int install_info_add(
         if (!name)
                 name = path_get_file_name(path);
 
-        if (!unit_name_is_valid_no_type(name, true))
+        if (!unit_name_is_valid(name, true))
                 return -EINVAL;
 
         if (hashmap_get(c->have_installed, name) ||
@@ -1093,8 +1099,48 @@ static int unit_file_search(
 
                 if (r >= 0)
                         info->path = path;
-                else
+                else {
+                        if (r == -ENOENT && unit_name_is_instance(info->name)) {
+                                /* unit file doesn't exist, however instance enablement was request */
+                                /* we will check if it is possible to load template unit file */
+                                char *template = NULL,
+                                     *template_path = NULL,
+                                     *template_dir = NULL;
+
+                                template = unit_name_template(info->name);
+                                if (!template) {
+                                        free(path);
+                                        return -ENOMEM;
+                                }
+
+                                /* we will reuse path variable since we don't need it anymore */
+                                template_dir = path;
+                                *(strrchr(path, '/') + 1) = '\0';
+
+                                template_path = strjoin(template_dir, template, NULL);
+                                if (!template_path) {
+                                        free(path);
+                                        free(template);
+                                        return -ENOMEM;
+                                }
+
+                                /* let's try to load template unit */
+                                r = unit_file_load(c, info, template_path, allow_symlink);
+                                if (r >= 0) {
+                                        info->path = strdup(template_path);
+                                        if (!info->path) {
+                                                free(path);
+                                                free(template);
+                                                free(template_path);
+                                                return -ENOMEM;
+                                        }
+                                }
+
+                                free(template);
+                                free(template_path);
+                        }
                         free(path);
+                }
 
                 if (r != -ENOENT && r != -ELOOP)
                         return r;
@@ -1150,7 +1196,7 @@ static int create_symlink(
         assert(old_path);
         assert(new_path);
 
-        mkdir_parents(new_path, 0755);
+        mkdir_parents_label(new_path, 0755);
 
         if (symlink(old_path, new_path) >= 0) {
                 add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
@@ -1232,7 +1278,7 @@ static int install_info_symlink_wants(
         STRV_FOREACH(s, i->wanted_by) {
                 char *path;
 
-                if (!unit_name_is_valid_no_type(*s, true)) {
+                if (!unit_name_is_valid(*s, true)) {
                         r = -EINVAL;
                         continue;
                 }
@@ -1266,7 +1312,7 @@ static int install_info_symlink_requires(
         STRV_FOREACH(s, i->required_by) {
                 char *path;
 
-                if (!unit_name_is_valid_no_type(*s, true)) {
+                if (!unit_name_is_valid(*s, true)) {
                         r = -EINVAL;
                         continue;
                 }
@@ -1418,7 +1464,20 @@ 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 *unit_file = NULL;
+
+                        unit_file = path_get_file_name(i->path);
+
+                        if (unit_name_is_instance(unit_file))
+                                /* unit file named as instance exists, thus all symlinks pointing to it, will be removed */
+                                q = mark_symlink_for_removal(remove_symlinks_to, i->name);
+                        else
+                                /* does not exist, thus we will mark for removal symlinks to template unit file */
+                                q = mark_symlink_for_removal(remove_symlinks_to, unit_file);
+                } else
+                        q = mark_symlink_for_removal(remove_symlinks_to, i->name);
+
                 if (r >= 0 && q < 0)
                         r = q;
         }
@@ -1510,7 +1569,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;
 
@@ -1562,7 +1621,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);
@@ -1597,7 +1656,7 @@ UnitFileState unit_file_get_state(
         if (root_dir && scope != UNIT_FILE_SYSTEM)
                 return -EINVAL;
 
-        if (!unit_name_is_valid_no_type(name, true))
+        if (!unit_name_is_valid(name, true))
                 return -EINVAL;
 
         r = lookup_paths_init_from_scope(&paths, scope);
@@ -1682,16 +1741,18 @@ int unit_file_query_preset(UnitFileScope scope, const char *name) {
 
         if (scope == UNIT_FILE_SYSTEM)
                 r = conf_files_list(&files, ".preset",
-                                    "/etc/systemd/system.preset",
-                                    "/usr/local/lib/systemd/system.preset",
-                                    "/usr/lib/systemd/system.preset",
-                                    "/lib/systemd/system.preset",
+                                    "/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)
                 r = conf_files_list(&files, ".preset",
-                                    "/etc/systemd/user.preset",
-                                    "/usr/local/lib/systemd/user.preset",
-                                    "/usr/lib/systemd/user.preset",
+                                    "/etc/systemd/user-preset",
+                                    "/usr/local/lib/systemd/user-preset",
+                                    "/usr/lib/systemd/user-preset",
                                     NULL);
         else
                 return 1;
@@ -1790,7 +1851,7 @@ int unit_file_preset(
 
         STRV_FOREACH(i, files) {
 
-                if (!unit_name_is_valid_no_type(*i, true)) {
+                if (!unit_name_is_valid(*i, true)) {
                         r = -EINVAL;
                         goto finish;
                 }
@@ -1810,7 +1871,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;
 
@@ -1895,7 +1956,7 @@ int unit_file_get_list(
                         if (ignore_file(de->d_name))
                                 continue;
 
-                        if (!unit_name_is_valid_no_type(de->d_name, true))
+                        if (!unit_name_is_valid(de->d_name, true))
                                 continue;
 
                         if (hashmap_get(h, de->d_name))