chiark / gitweb /
properly terminate strings with NUL byte
[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
9 int unit_load_dropin(Unit *u) {
10         Iterator i;
11         int r;
12         char *t;
13
14         assert(u);
15
16         /* Load dependencies from supplementary drop-in directories */
17
18         SET_FOREACH(t, u->meta.names, i) {
19                 char *path;
20                 DIR *d;
21                 struct dirent *de;
22
23                 if (asprintf(&path, "%s/%s.wants", unit_path(), t) < 0)
24                         return -ENOMEM;
25
26                 if (!(d = opendir(path))) {
27                         r = -errno;
28                         free(path);
29
30                         if (r == -ENOENT)
31                                 continue;
32
33                         return r;
34                 }
35
36                 free(path);
37
38                 while ((de = readdir(d))) {
39                         Unit *other;
40
41                         if (de->d_name[0] == '.')
42                                 continue;
43
44                         assert(de->d_name[0]);
45
46                         if (de->d_name[strlen(de->d_name)-1] == '~')
47                                 continue;
48
49                         if (asprintf(&path, "%s/%s.wants/%s", unit_path(), t, de->d_name) < 0) {
50                                 closedir(d);
51                                 return -ENOMEM;
52                         }
53
54                         r = manager_load_unit(u->meta.manager, path, &other);
55                         free(path);
56
57                         if (r < 0) {
58                                 closedir(d);
59                                 return r;
60                         }
61
62                         if ((r = unit_add_dependency(u, UNIT_WANTS, other)) < 0) {
63                                 closedir(d);
64                                 return r;
65                         }
66                 }
67
68                 closedir(d);
69         }
70
71         return 0;
72 }