]>
Commit | Line | Data |
---|---|---|
29686440 ZJS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Zbigniew Jędrzejewski-Szmek | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
a8fbdf54 TA |
20 | #include <errno.h> |
21 | #include <stdarg.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | ||
b5efdb8a | 25 | #include "alloc-util.h" |
1a7f1b38 | 26 | #include "conf-files.h" |
8fb3f009 | 27 | #include "dirent-util.h" |
3ffd4af2 | 28 | #include "dropin.h" |
4f5dd394 | 29 | #include "escape.h" |
3ffd4af2 | 30 | #include "fd-util.h" |
4f5dd394 | 31 | #include "fileio-label.h" |
17e78d18 | 32 | #include "fs-util.h" |
a8fbdf54 TA |
33 | #include "hashmap.h" |
34 | #include "log.h" | |
35 | #include "macro.h" | |
4f5dd394 | 36 | #include "mkdir.h" |
bb15fafe | 37 | #include "path-util.h" |
a8fbdf54 | 38 | #include "set.h" |
07630cea | 39 | #include "string-util.h" |
4f5dd394 | 40 | #include "strv.h" |
a8fbdf54 | 41 | #include "unit-name.h" |
29686440 | 42 | |
8eea8687 | 43 | int drop_in_file(const char *dir, const char *unit, unsigned level, |
29686440 ZJS |
44 | const char *name, char **_p, char **_q) { |
45 | ||
09c62487 | 46 | char prefix[DECIMAL_STR_MAX(unsigned)]; |
29686440 ZJS |
47 | _cleanup_free_ char *b = NULL; |
48 | char *p, *q; | |
49 | ||
50 | assert(unit); | |
51 | assert(name); | |
52 | assert(_p); | |
53 | assert(_q); | |
54 | ||
8eea8687 ZJS |
55 | sprintf(prefix, "%u", level); |
56 | ||
29686440 ZJS |
57 | b = xescape(name, "/."); |
58 | if (!b) | |
59 | return -ENOMEM; | |
60 | ||
ae6c3cc0 | 61 | if (!filename_is_valid(b)) |
29686440 ZJS |
62 | return -EINVAL; |
63 | ||
605405c6 | 64 | p = strjoin(dir, "/", unit, ".d"); |
29686440 ZJS |
65 | if (!p) |
66 | return -ENOMEM; | |
67 | ||
605405c6 | 68 | q = strjoin(p, "/", prefix, "-", b, ".conf"); |
29686440 ZJS |
69 | if (!q) { |
70 | free(p); | |
71 | return -ENOMEM; | |
72 | } | |
73 | ||
74 | *_p = p; | |
75 | *_q = q; | |
76 | return 0; | |
77 | } | |
78 | ||
8eea8687 | 79 | int write_drop_in(const char *dir, const char *unit, unsigned level, |
29686440 ZJS |
80 | const char *name, const char *data) { |
81 | ||
82 | _cleanup_free_ char *p = NULL, *q = NULL; | |
83 | int r; | |
84 | ||
85 | assert(dir); | |
86 | assert(unit); | |
87 | assert(name); | |
88 | assert(data); | |
89 | ||
8eea8687 | 90 | r = drop_in_file(dir, unit, level, name, &p, &q); |
29686440 ZJS |
91 | if (r < 0) |
92 | return r; | |
93 | ||
45519fd6 | 94 | (void) mkdir_p(p, 0755); |
29686440 ZJS |
95 | return write_string_file_atomic_label(q, data); |
96 | } | |
97 | ||
8eea8687 | 98 | int write_drop_in_format(const char *dir, const char *unit, unsigned level, |
29686440 ZJS |
99 | const char *name, const char *format, ...) { |
100 | _cleanup_free_ char *p = NULL; | |
101 | va_list ap; | |
102 | int r; | |
103 | ||
104 | assert(dir); | |
105 | assert(unit); | |
106 | assert(name); | |
107 | assert(format); | |
108 | ||
109 | va_start(ap, format); | |
110 | r = vasprintf(&p, format, ap); | |
111 | va_end(ap); | |
112 | ||
113 | if (r < 0) | |
114 | return -ENOMEM; | |
115 | ||
8eea8687 | 116 | return write_drop_in(dir, unit, level, name, p); |
29686440 | 117 | } |
1a7f1b38 | 118 | |
dcc4f30e | 119 | static int unit_file_find_dir( |
17e78d18 | 120 | const char *original_root, |
dcc4f30e ZJS |
121 | const char *path, |
122 | char ***dirs) { | |
1a7f1b38 | 123 | |
17e78d18 | 124 | _cleanup_free_ char *chased = NULL; |
1a7f1b38 ZJS |
125 | int r; |
126 | ||
127 | assert(path); | |
128 | ||
17e78d18 | 129 | r = chase_symlinks(path, original_root, 0, &chased); |
09c62487 LP |
130 | if (r == -ENOENT) /* Ignore -ENOENT, after all most units won't have a drop-in dir */ |
131 | return 0; | |
17e78d18 | 132 | if (r < 0) |
09c62487 | 133 | return log_full_errno(LOG_WARNING, r, "Failed to canonicalize path %s: %m", path); |
17e78d18 | 134 | |
dcc4f30e ZJS |
135 | r = strv_push(dirs, chased); |
136 | if (r < 0) | |
137 | return log_oom(); | |
1a7f1b38 | 138 | |
dcc4f30e | 139 | chased = NULL; |
1a7f1b38 ZJS |
140 | return 0; |
141 | } | |
142 | ||
dcc4f30e | 143 | static int unit_file_find_dirs( |
17e78d18 | 144 | const char *original_root, |
7410616c | 145 | Set *unit_path_cache, |
1a7f1b38 ZJS |
146 | const char *unit_path, |
147 | const char *name, | |
148 | const char *suffix, | |
dcc4f30e | 149 | char ***dirs) { |
1a7f1b38 | 150 | |
96bb2fd8 | 151 | char *path; |
7410616c | 152 | int r; |
1a7f1b38 ZJS |
153 | |
154 | assert(unit_path); | |
155 | assert(name); | |
156 | assert(suffix); | |
157 | ||
96bb2fd8 | 158 | path = strjoina(unit_path, "/", name, suffix); |
1a7f1b38 | 159 | |
dcc4f30e ZJS |
160 | if (!unit_path_cache || set_get(unit_path_cache, path)) { |
161 | r = unit_file_find_dir(original_root, path, dirs); | |
162 | if (r < 0) | |
163 | return r; | |
164 | } | |
1a7f1b38 | 165 | |
7410616c | 166 | if (unit_name_is_valid(name, UNIT_NAME_INSTANCE)) { |
1a7f1b38 ZJS |
167 | /* Also try the template dir */ |
168 | ||
a09d3eaf LP |
169 | _cleanup_free_ char *template = NULL; |
170 | ||
7410616c LP |
171 | r = unit_name_template(name, &template); |
172 | if (r < 0) | |
173 | return log_error_errno(r, "Failed to generate template from unit name: %m"); | |
1a7f1b38 | 174 | |
a09d3eaf | 175 | return unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs); |
1a7f1b38 ZJS |
176 | } |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | int unit_file_find_dropin_paths( | |
17e78d18 | 182 | const char *original_root, |
1a7f1b38 ZJS |
183 | char **lookup_path, |
184 | Set *unit_path_cache, | |
95778782 ZJS |
185 | const char *dir_suffix, |
186 | const char *file_suffix, | |
1a7f1b38 | 187 | Set *names, |
058db925 | 188 | char ***ret) { |
1a7f1b38 | 189 | |
3f6de63b | 190 | _cleanup_strv_free_ char **dirs = NULL; |
1a7f1b38 | 191 | Iterator i; |
dcc4f30e | 192 | char *t, **p; |
1a7f1b38 ZJS |
193 | int r; |
194 | ||
058db925 | 195 | assert(ret); |
1a7f1b38 | 196 | |
dcc4f30e | 197 | SET_FOREACH(t, names, i) |
1a7f1b38 | 198 | STRV_FOREACH(p, lookup_path) |
dcc4f30e | 199 | unit_file_find_dirs(original_root, unit_path_cache, *p, t, dir_suffix, &dirs); |
1a7f1b38 | 200 | |
058db925 LP |
201 | if (strv_isempty(dirs)) { |
202 | *ret = NULL; | |
1a7f1b38 | 203 | return 0; |
058db925 | 204 | } |
1a7f1b38 | 205 | |
3f6de63b | 206 | r = conf_files_list_strv(ret, file_suffix, NULL, (const char**) dirs); |
1a7f1b38 | 207 | if (r < 0) |
3f6de63b | 208 | return log_warning_errno(r, "Failed to create the list of configuration files: %m"); |
058db925 | 209 | |
1a7f1b38 ZJS |
210 | return 1; |
211 | } |