]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
84e3543e LP |
2 | |
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <assert.h> | |
23 | #include <stdlib.h> | |
24 | #include <stdio.h> | |
25 | #include <unistd.h> | |
26 | #include <errno.h> | |
27 | ||
28 | #include "util.h" | |
29 | #include "strv.h" | |
30 | ||
31 | #include "path-lookup.h" | |
32 | ||
af2d49f7 | 33 | int user_config_home(char **config_home) { |
10e87ee7 LP |
34 | const char *e; |
35 | ||
36 | if ((e = getenv("XDG_CONFIG_HOME"))) { | |
af2d49f7 | 37 | if (asprintf(config_home, "%s/systemd/user", e) < 0) |
10e87ee7 LP |
38 | return -ENOMEM; |
39 | ||
40 | return 1; | |
41 | } else { | |
42 | const char *home; | |
43 | ||
44 | if ((home = getenv("HOME"))) { | |
af2d49f7 | 45 | if (asprintf(config_home, "%s/.config/systemd/user", home) < 0) |
10e87ee7 LP |
46 | return -ENOMEM; |
47 | ||
48 | return 1; | |
49 | } | |
50 | } | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
af2d49f7 | 55 | static char** user_dirs(void) { |
f437d5d2 | 56 | const char * const config_unit_paths[] = { |
f437d5d2 | 57 | USER_CONFIG_UNIT_PATH, |
4bf2bbb6 | 58 | "/etc/systemd/user", |
fc1a2e06 | 59 | "/run/systemd/user", |
4bf2bbb6 | 60 | NULL |
f437d5d2 LP |
61 | }; |
62 | ||
63 | const char * const data_unit_paths[] = { | |
64 | "/usr/local/lib/systemd/user", | |
65 | "/usr/local/share/systemd/user", | |
66 | USER_DATA_UNIT_PATH, | |
67 | "/usr/lib/systemd/user", | |
4bf2bbb6 LP |
68 | "/usr/share/systemd/user", |
69 | NULL | |
f437d5d2 LP |
70 | }; |
71 | ||
84e3543e LP |
72 | const char *home, *e; |
73 | char *config_home = NULL, *data_home = NULL; | |
74 | char **config_dirs = NULL, **data_dirs = NULL; | |
75 | char **r = NULL, **t; | |
76 | ||
77 | /* Implement the mechanisms defined in | |
78 | * | |
79 | * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html | |
80 | * | |
81 | * We look in both the config and the data dirs because we | |
82 | * want to encourage that distributors ship their unit files | |
83 | * as data, and allow overriding as configuration. | |
84 | */ | |
85 | ||
af2d49f7 | 86 | if (user_config_home(&config_home) < 0) |
10e87ee7 | 87 | goto fail; |
84e3543e | 88 | |
10e87ee7 | 89 | home = getenv("HOME"); |
84e3543e LP |
90 | |
91 | if ((e = getenv("XDG_CONFIG_DIRS"))) | |
92 | if (!(config_dirs = strv_split(e, ":"))) | |
93 | goto fail; | |
94 | ||
95 | /* We don't treat /etc/xdg/systemd here as the spec | |
96 | * suggests because we assume that that is a link to | |
97 | * /etc/systemd/ anyway. */ | |
98 | ||
99 | if ((e = getenv("XDG_DATA_HOME"))) { | |
af2d49f7 | 100 | if (asprintf(&data_home, "%s/systemd/user", e) < 0) |
84e3543e LP |
101 | goto fail; |
102 | ||
103 | } else if (home) { | |
af2d49f7 | 104 | if (asprintf(&data_home, "%s/.local/share/systemd/user", home) < 0) |
84e3543e LP |
105 | goto fail; |
106 | ||
107 | /* There is really no need for two unit dirs in $HOME, | |
108 | * except to be fully compliant with the XDG spec. We | |
109 | * now try to link the two dirs, so that we can | |
110 | * minimize disk seeks a little. Further down we'll | |
111 | * then filter out this link, if it is actually is | |
112 | * one. */ | |
113 | ||
114 | mkdir_parents(data_home, 0777); | |
af2d49f7 | 115 | (void) symlink("../../../.config/systemd/user", data_home); |
84e3543e LP |
116 | } |
117 | ||
118 | if ((e = getenv("XDG_DATA_DIRS"))) | |
119 | data_dirs = strv_split(e, ":"); | |
120 | else | |
ef3102bf | 121 | data_dirs = strv_new("/usr/local/share", |
ef3102bf | 122 | "/usr/share", |
ef3102bf | 123 | NULL); |
84e3543e LP |
124 | |
125 | if (!data_dirs) | |
126 | goto fail; | |
127 | ||
128 | /* Now merge everything we found. */ | |
129 | if (config_home) { | |
130 | if (!(t = strv_append(r, config_home))) | |
131 | goto fail; | |
132 | strv_free(r); | |
133 | r = t; | |
134 | } | |
135 | ||
f437d5d2 LP |
136 | if (!strv_isempty(config_dirs)) { |
137 | if (!(t = strv_merge_concat(r, config_dirs, "/systemd/user"))) | |
138 | goto finish; | |
139 | strv_free(r); | |
140 | r = t; | |
141 | } | |
84e3543e | 142 | |
f437d5d2 | 143 | if (!(t = strv_merge(r, (char**) config_unit_paths))) |
84e3543e LP |
144 | goto fail; |
145 | strv_free(r); | |
146 | r = t; | |
147 | ||
148 | if (data_home) { | |
149 | if (!(t = strv_append(r, data_home))) | |
150 | goto fail; | |
151 | strv_free(r); | |
152 | r = t; | |
153 | } | |
154 | ||
f437d5d2 LP |
155 | if (!strv_isempty(data_dirs)) { |
156 | if (!(t = strv_merge_concat(r, data_dirs, "/systemd/user"))) | |
157 | goto fail; | |
158 | strv_free(r); | |
159 | r = t; | |
160 | } | |
84e3543e | 161 | |
f437d5d2 | 162 | if (!(t = strv_merge(r, (char**) data_unit_paths))) |
84e3543e LP |
163 | goto fail; |
164 | strv_free(r); | |
165 | r = t; | |
166 | ||
167 | if (!strv_path_make_absolute_cwd(r)) | |
168 | goto fail; | |
169 | ||
170 | finish: | |
171 | free(config_home); | |
172 | strv_free(config_dirs); | |
173 | free(data_home); | |
174 | strv_free(data_dirs); | |
175 | ||
176 | return r; | |
177 | ||
178 | fail: | |
179 | strv_free(r); | |
180 | r = NULL; | |
181 | goto finish; | |
182 | } | |
183 | ||
c800e483 | 184 | int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal) { |
84e3543e LP |
185 | const char *e; |
186 | char *t; | |
187 | ||
188 | assert(p); | |
189 | ||
190 | /* First priority is whatever has been passed to us via env | |
191 | * vars */ | |
192 | if ((e = getenv("SYSTEMD_UNIT_PATH"))) | |
193 | if (!(p->unit_path = split_path_and_make_absolute(e))) | |
194 | return -ENOMEM; | |
195 | ||
196 | if (strv_isempty(p->unit_path)) { | |
197 | ||
198 | /* Nothing is set, so let's figure something out. */ | |
199 | strv_free(p->unit_path); | |
200 | ||
af2d49f7 | 201 | if (running_as == MANAGER_USER) { |
c800e483 LP |
202 | |
203 | if (personal) | |
204 | p->unit_path = user_dirs(); | |
205 | else | |
206 | p->unit_path = strv_new( | |
207 | /* If you modify this you also want to modify | |
208 | * systemduserunitpath= in systemd.pc.in, and | |
209 | * the arrays in user_dirs() above! */ | |
c800e483 | 210 | USER_CONFIG_UNIT_PATH, |
a5afffa1 | 211 | "/etc/systemd/user", |
fc1a2e06 | 212 | "/run/systemd/user", |
c800e483 LP |
213 | "/usr/local/lib/systemd/user", |
214 | "/usr/local/share/systemd/user", | |
215 | USER_DATA_UNIT_PATH, | |
216 | "/usr/lib/systemd/user", | |
217 | "/usr/share/systemd/user", | |
218 | NULL); | |
219 | ||
220 | if (!p->unit_path) | |
84e3543e | 221 | return -ENOMEM; |
c800e483 | 222 | |
84e3543e LP |
223 | } else |
224 | if (!(p->unit_path = strv_new( | |
607df95b LP |
225 | /* If you modify this you also want to modify |
226 | * systemdsystemunitpath= in systemd.pc.in! */ | |
c0115b1f LP |
227 | SYSTEM_CONFIG_UNIT_PATH, |
228 | "/etc/systemd/system", | |
fc1a2e06 | 229 | "/run/systemd/system", |
ef3102bf | 230 | "/usr/local/lib/systemd/system", |
0b5b0ffb | 231 | SYSTEM_DATA_UNIT_PATH, |
b412ab3b | 232 | "/usr/lib/systemd/system", |
283b73b3 | 233 | #ifdef HAVE_SPLIT_USR |
c800e483 | 234 | "/lib/systemd/system", |
283b73b3 | 235 | #endif |
84e3543e LP |
236 | NULL))) |
237 | return -ENOMEM; | |
238 | } | |
239 | ||
07459bb6 FF |
240 | if (p->unit_path) |
241 | if (!strv_path_canonicalize(p->unit_path)) | |
242 | return -ENOMEM; | |
243 | ||
244 | strv_uniq(p->unit_path); | |
a9dd2082 | 245 | strv_path_remove_empty(p->unit_path); |
07459bb6 FF |
246 | |
247 | if (!strv_isempty(p->unit_path)) { | |
248 | ||
249 | if (!(t = strv_join(p->unit_path, "\n\t"))) | |
250 | return -ENOMEM; | |
251 | log_debug("Looking for unit files in:\n\t%s", t); | |
252 | free(t); | |
253 | } else { | |
254 | log_debug("Ignoring unit files."); | |
255 | strv_free(p->unit_path); | |
256 | p->unit_path = NULL; | |
257 | } | |
258 | ||
a3d4e06d | 259 | if (running_as == MANAGER_SYSTEM) { |
07459bb6 | 260 | #ifdef HAVE_SYSV_COMPAT |
84e3543e LP |
261 | /* /etc/init.d/ compatibility does not matter to users */ |
262 | ||
263 | if ((e = getenv("SYSTEMD_SYSVINIT_PATH"))) | |
264 | if (!(p->sysvinit_path = split_path_and_make_absolute(e))) | |
265 | return -ENOMEM; | |
266 | ||
267 | if (strv_isempty(p->sysvinit_path)) { | |
268 | strv_free(p->sysvinit_path); | |
269 | ||
270 | if (!(p->sysvinit_path = strv_new( | |
271 | SYSTEM_SYSVINIT_PATH, /* /etc/init.d/ */ | |
272 | NULL))) | |
273 | return -ENOMEM; | |
274 | } | |
275 | ||
276 | if ((e = getenv("SYSTEMD_SYSVRCND_PATH"))) | |
277 | if (!(p->sysvrcnd_path = split_path_and_make_absolute(e))) | |
278 | return -ENOMEM; | |
279 | ||
280 | if (strv_isempty(p->sysvrcnd_path)) { | |
281 | strv_free(p->sysvrcnd_path); | |
282 | ||
283 | if (!(p->sysvrcnd_path = strv_new( | |
284 | SYSTEM_SYSVRCND_PATH, /* /etc/rcN.d/ */ | |
285 | NULL))) | |
286 | return -ENOMEM; | |
287 | } | |
84e3543e | 288 | |
07459bb6 FF |
289 | if (p->sysvinit_path) |
290 | if (!strv_path_canonicalize(p->sysvinit_path)) | |
291 | return -ENOMEM; | |
84e3543e | 292 | |
07459bb6 FF |
293 | if (p->sysvrcnd_path) |
294 | if (!strv_path_canonicalize(p->sysvrcnd_path)) | |
295 | return -ENOMEM; | |
84e3543e | 296 | |
07459bb6 FF |
297 | strv_uniq(p->sysvinit_path); |
298 | strv_uniq(p->sysvrcnd_path); | |
84e3543e | 299 | |
a9dd2082 LP |
300 | strv_path_remove_empty(p->sysvinit_path); |
301 | strv_path_remove_empty(p->sysvrcnd_path); | |
302 | ||
07459bb6 | 303 | if (!strv_isempty(p->sysvinit_path)) { |
84e3543e | 304 | |
07459bb6 FF |
305 | if (!(t = strv_join(p->sysvinit_path, "\n\t"))) |
306 | return -ENOMEM; | |
84e3543e | 307 | |
07459bb6 FF |
308 | log_debug("Looking for SysV init scripts in:\n\t%s", t); |
309 | free(t); | |
310 | } else { | |
311 | log_debug("Ignoring SysV init scripts."); | |
312 | strv_free(p->sysvinit_path); | |
313 | p->sysvinit_path = NULL; | |
314 | } | |
84e3543e | 315 | |
07459bb6 | 316 | if (!strv_isempty(p->sysvrcnd_path)) { |
84e3543e | 317 | |
07459bb6 FF |
318 | if (!(t = strv_join(p->sysvrcnd_path, "\n\t"))) |
319 | return -ENOMEM; | |
84e3543e | 320 | |
07459bb6 FF |
321 | log_debug("Looking for SysV rcN.d links in:\n\t%s", t); |
322 | free(t); | |
323 | } else { | |
324 | log_debug("Ignoring SysV rcN.d links."); | |
325 | strv_free(p->sysvrcnd_path); | |
326 | p->sysvrcnd_path = NULL; | |
327 | } | |
328 | #else | |
329 | log_debug("Disabled SysV init scripts and rcN.d links support"); | |
330 | #endif | |
84e3543e LP |
331 | } |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
336 | void lookup_paths_free(LookupPaths *p) { | |
337 | assert(p); | |
338 | ||
339 | strv_free(p->unit_path); | |
07459bb6 FF |
340 | p->unit_path = NULL; |
341 | ||
342 | #ifdef HAVE_SYSV_COMPAT | |
84e3543e LP |
343 | strv_free(p->sysvinit_path); |
344 | strv_free(p->sysvrcnd_path); | |
07459bb6 FF |
345 | p->sysvinit_path = p->sysvrcnd_path = NULL; |
346 | #endif | |
84e3543e | 347 | } |