+static int config_parse_default_instance(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ UnitFileInstallInfo *i = data;
+ char *printed;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+
+ r = install_full_printf(i, rvalue, &printed);
+ if (r < 0)
+ return r;
+
+ if (!unit_instance_is_valid(printed)) {
+ free(printed);
+ return -EINVAL;
+ }
+
+ free(i->default_instance);
+ i->default_instance = printed;
+
+ return 0;
+}
+
+static int unit_file_load(
+ InstallContext *c,
+ UnitFileInstallInfo *info,
+ const char *path,
+ const char *root_dir,
+ bool allow_symlink,
+ bool load,
+ bool *also) {
+
+ const ConfigTableItem items[] = {
+ { "Install", "Alias", config_parse_strv, 0, &info->aliases },
+ { "Install", "WantedBy", config_parse_strv, 0, &info->wanted_by },
+ { "Install", "RequiredBy", config_parse_strv, 0, &info->required_by },
+ { "Install", "DefaultInstance", config_parse_default_instance, 0, info },
+ { "Install", "Also", config_parse_also, 0, c },
+ { "Exec", "User", config_parse_user, 0, info },
+ {}
+ };
+
+ _cleanup_fclose_ FILE *f = NULL;
+ int fd, r;
+
+ assert(c);
+ assert(info);
+ assert(path);
+
+ if (!isempty(root_dir))
+ path = strjoina(root_dir, "/", path);
+
+ if (!load) {
+ r = access(path, F_OK) ? -errno : 0;
+ return r;
+ }
+
+ fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
+ if (fd < 0)
+ return -errno;
+
+ f = fdopen(fd, "re");
+ if (!f) {
+ safe_close(fd);
+ return -ENOMEM;
+ }
+
+ r = config_parse(NULL, path, f,
+ NULL,
+ config_item_table_lookup, items,
+ true, true, false, info);
+ if (r < 0)
+ return r;
+
+ if (also)
+ *also = !strv_isempty(info->also);
+
+ return
+ (int) strv_length(info->aliases) +
+ (int) strv_length(info->wanted_by) +
+ (int) strv_length(info->required_by);
+}
+
+static int unit_file_search(
+ InstallContext *c,
+ UnitFileInstallInfo *info,
+ const LookupPaths *paths,
+ const char *root_dir,
+ bool allow_symlink,
+ bool load,
+ bool *also) {
+
+ char **p;