#include "unit.h"
#include "load-dropin.h"
#include "log.h"
+#include "strv.h"
+#include "unit-name.h"
+
+static int iterate_dir(Unit *u, const char *path) {
+ DIR *d;
+ struct dirent *de;
+ int r;
+
+ if (!(d = opendir(path))) {
+
+ if (errno == ENOENT)
+ return 0;
+
+ return -errno;
+ }
+
+ while ((de = readdir(d))) {
+ char *f;
+
+ if (ignore_file(de->d_name))
+ continue;
+
+ if (asprintf(&f, "%s/%s", path, de->d_name) < 0) {
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ r = unit_add_dependency_by_name(u, UNIT_WANTS, de->d_name, f);
+ free(f);
+
+ if (r < 0)
+ goto finish;
+ }
+
+ r = 0;
+
+finish:
+ closedir(d);
+ return r;
+}
int unit_load_dropin(Unit *u) {
Iterator i;
SET_FOREACH(t, u->meta.names, i) {
char *path;
- DIR *d;
- struct dirent *de;
+ char **p;
- if (asprintf(&path, "%s/%s.wants", unit_path(), t) < 0)
- return -ENOMEM;
+ STRV_FOREACH(p, u->meta.manager->unit_path) {
- if (!(d = opendir(path))) {
- r = -errno;
- free(path);
+ if (asprintf(&path, "%s/%s.wants", *p, t) < 0)
+ return -ENOMEM;
- if (r == -ENOENT)
- continue;
+ r = iterate_dir(u, path);
+ free(path);
- return r;
- }
+ if (r < 0)
+ return r;
- free(path);
+ if (u->meta.instance) {
+ char *template;
+ /* Also try the template dir */
- while ((de = readdir(d))) {
- if (de->d_name[0] == '.')
- continue;
+ if (!(template = unit_name_template(t)))
+ return -ENOMEM;
- assert(de->d_name[0]);
+ r = asprintf(&path, "%s/%s.wants", *p, template);
+ free(template);
- if (de->d_name[strlen(de->d_name)-1] == '~')
- continue;
+ if (r < 0)
+ return -ENOMEM;
- if (asprintf(&path, "%s/%s.wants/%s", unit_path(), t, de->d_name) < 0) {
- closedir(d);
- return -ENOMEM;
- }
-
- if (!unit_name_is_valid(de->d_name)) {
- log_info("Name of %s is not a valid unit name. Ignoring.", path);
+ r = iterate_dir(u, path);
free(path);
- continue;
- }
-
- r = unit_add_dependency_by_name(u, UNIT_WANTS, path);
- free(path);
- if (r < 0) {
- closedir(d);
- return r;
+ if (r < 0)
+ return r;
}
- }
- closedir(d);
+ }
}
return 0;