chiark / gitweb /
521eb0821aea9c84128003b3bb4444f20c18e4ca
[elogind.git] / load-dropin.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <dirent.h>
4 #include <errno.h>
5
6 #include "unit.h"
7 #include "load-dropin.h"
8 #include "log.h"
9
10 int unit_load_dropin(Unit *u) {
11         Iterator i;
12         int r;
13         char *t;
14
15         assert(u);
16
17         /* Load dependencies from supplementary drop-in directories */
18
19         SET_FOREACH(t, u->meta.names, i) {
20                 char *path;
21                 DIR *d;
22                 struct dirent *de;
23
24                 if (asprintf(&path, "%s/%s.wants", unit_path(), t) < 0)
25                         return -ENOMEM;
26
27                 if (!(d = opendir(path))) {
28                         r = -errno;
29                         free(path);
30
31                         if (r == -ENOENT)
32                                 continue;
33
34                         return r;
35                 }
36
37                 free(path);
38
39                 while ((de = readdir(d))) {
40                         if (de->d_name[0] == '.')
41                                 continue;
42
43                         assert(de->d_name[0]);
44
45                         if (de->d_name[strlen(de->d_name)-1] == '~')
46                                 continue;
47
48                         if (asprintf(&path, "%s/%s.wants/%s", unit_path(), t, de->d_name) < 0) {
49                                 closedir(d);
50                                 return -ENOMEM;
51                         }
52
53                         if (!unit_name_is_valid(de->d_name)) {
54                                 log_info("Name of %s is not a valid unit name. Ignoring.", path);
55                                 free(path);
56                                 continue;
57                         }
58
59                         r = unit_add_dependency_by_name(u, UNIT_WANTS, path);
60                         free(path);
61
62                         if (r < 0) {
63                                 closedir(d);
64                                 return r;
65                         }
66                 }
67
68                 closedir(d);
69         }
70
71         return 0;
72 }