chiark / gitweb /
don't choke on invalid dropin file names
[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                         Unit *other;
41
42                         if (de->d_name[0] == '.')
43                                 continue;
44
45                         assert(de->d_name[0]);
46
47                         if (de->d_name[strlen(de->d_name)-1] == '~')
48                                 continue;
49
50                         if (asprintf(&path, "%s/%s.wants/%s", unit_path(), t, de->d_name) < 0) {
51                                 closedir(d);
52                                 return -ENOMEM;
53                         }
54
55                         if (!unit_name_is_valid(de->d_name)) {
56                                 log_info("Name of %s is not a valid unit name. Ignoring.", path);
57                                 free(path);
58                                 continue;
59                         }
60
61                         r = manager_load_unit(u->meta.manager, path, &other);
62                         free(path);
63
64                         if (r < 0) {
65                                 closedir(d);
66                                 return r;
67                         }
68
69                         if ((r = unit_add_dependency(u, UNIT_WANTS, other)) < 0) {
70                                 closedir(d);
71                                 return r;
72                         }
73                 }
74
75                 closedir(d);
76         }
77
78         return 0;
79 }