chiark / gitweb /
84a9859254eebcd263072b7efe193fd92cc4ce94
[elogind.git] / src / path-lookup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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
33 int user_config_home(char **config_home) {
34         const char *e;
35
36         if ((e = getenv("XDG_CONFIG_HOME"))) {
37                 if (asprintf(config_home, "%s/systemd/user", e) < 0)
38                         return -ENOMEM;
39
40                 return 1;
41         } else {
42                 const char *home;
43
44                 if ((home = getenv("HOME"))) {
45                         if (asprintf(config_home, "%s/.config/systemd/user", home) < 0)
46                                 return -ENOMEM;
47
48                         return 1;
49                 }
50         }
51
52         return 0;
53 }
54
55 static char** user_dirs(void) {
56         const char *home, *e;
57         char *config_home = NULL, *data_home = NULL;
58         char **config_dirs = NULL, **data_dirs = NULL;
59         char **r = NULL, **t;
60
61         /* Implement the mechanisms defined in
62          *
63          * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
64          *
65          * We look in both the config and the data dirs because we
66          * want to encourage that distributors ship their unit files
67          * as data, and allow overriding as configuration.
68          */
69
70         if (user_config_home(&config_home) < 0)
71                 goto fail;
72
73         home = getenv("HOME");
74
75         if ((e = getenv("XDG_CONFIG_DIRS")))
76                 if (!(config_dirs = strv_split(e, ":")))
77                         goto fail;
78
79         /* We don't treat /etc/xdg/systemd here as the spec
80          * suggests because we assume that that is a link to
81          * /etc/systemd/ anyway. */
82
83         if ((e = getenv("XDG_DATA_HOME"))) {
84                 if (asprintf(&data_home, "%s/systemd/user", e) < 0)
85                         goto fail;
86
87         } else if (home) {
88                 if (asprintf(&data_home, "%s/.local/share/systemd/user", home) < 0)
89                         goto fail;
90
91                 /* There is really no need for two unit dirs in $HOME,
92                  * except to be fully compliant with the XDG spec. We
93                  * now try to link the two dirs, so that we can
94                  * minimize disk seeks a little. Further down we'll
95                  * then filter out this link, if it is actually is
96                  * one. */
97
98                 mkdir_parents(data_home, 0777);
99                 (void) symlink("../../../.config/systemd/user", data_home);
100         }
101
102         if ((e = getenv("XDG_DATA_DIRS")))
103                 data_dirs = strv_split(e, ":");
104         else
105                 data_dirs = strv_new("/usr/local/share",
106                                      "/usr/local/lib",
107                                      "/usr/share",
108                                      "/usr/lib",
109                                      NULL);
110
111         if (!data_dirs)
112                 goto fail;
113
114         /* Now merge everything we found. */
115         if (config_home) {
116                 if (!(t = strv_append(r, config_home)))
117                         goto fail;
118                 strv_free(r);
119                 r = t;
120         }
121
122         if (!(t = strv_merge_concat(r, config_dirs, "/systemd/user")))
123                 goto finish;
124         strv_free(r);
125         r = t;
126
127         if (!(t = strv_append(r, USER_CONFIG_UNIT_PATH)))
128                 goto fail;
129         strv_free(r);
130         r = t;
131
132         if (data_home) {
133                 if (!(t = strv_append(r, data_home)))
134                         goto fail;
135                 strv_free(r);
136                 r = t;
137         }
138
139         if (!(t = strv_merge_concat(r, data_dirs, "/systemd/user")))
140                 goto fail;
141         strv_free(r);
142         r = t;
143
144         if (!(t = strv_append(r, USER_DATA_UNIT_PATH)))
145                 goto fail;
146         strv_free(r);
147         r = t;
148
149         if (!strv_path_make_absolute_cwd(r))
150             goto fail;
151
152 finish:
153         free(config_home);
154         strv_free(config_dirs);
155         free(data_home);
156         strv_free(data_dirs);
157
158         return r;
159
160 fail:
161         strv_free(r);
162         r = NULL;
163         goto finish;
164 }
165
166 int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal) {
167         const char *e;
168         char *t;
169
170         assert(p);
171
172         /* First priority is whatever has been passed to us via env
173          * vars */
174         if ((e = getenv("SYSTEMD_UNIT_PATH")))
175                 if (!(p->unit_path = split_path_and_make_absolute(e)))
176                         return -ENOMEM;
177
178         if (strv_isempty(p->unit_path)) {
179
180                 /* Nothing is set, so let's figure something out. */
181                 strv_free(p->unit_path);
182
183                 if (running_as == MANAGER_USER) {
184
185                         if (personal)
186                                 p->unit_path = user_dirs();
187                         else
188                                 p->unit_path = strv_new(
189                                                 /* If you modify this you also want to modify
190                                                  * systemduserunitpath= in systemd.pc.in, and
191                                                  * the arrays in user_dirs() above! */
192                                                 "/run/systemd/user",
193                                                 USER_CONFIG_UNIT_PATH,
194                                                 "/etc/systemd/system",
195                                                 "/usr/local/lib/systemd/user",
196                                                 "/usr/local/share/systemd/user",
197                                                 USER_DATA_UNIT_PATH,
198                                                 "/usr/lib/systemd/user",
199                                                 "/usr/share/systemd/user",
200                                                 NULL);
201
202                         if (!p->unit_path)
203                                 return -ENOMEM;
204
205                 } else
206                         if (!(p->unit_path = strv_new(
207                                               /* If you modify this you also want to modify
208                                                * systemdsystemunitpath= in systemd.pc.in! */
209                                               "/run/systemd/system",
210                                               SYSTEM_CONFIG_UNIT_PATH,
211                                               "/etc/systemd/system",
212                                               "/usr/local/lib/systemd/system",
213                                               "/usr/lib/systemd/system",
214                                               SYSTEM_DATA_UNIT_PATH,
215                                               "/lib/systemd/system",
216                                               NULL)))
217                                 return -ENOMEM;
218         }
219
220         if (p->unit_path)
221                 if (!strv_path_canonicalize(p->unit_path))
222                         return -ENOMEM;
223
224         strv_uniq(p->unit_path);
225         strv_path_remove_empty(p->unit_path);
226
227         if (!strv_isempty(p->unit_path)) {
228
229                 if (!(t = strv_join(p->unit_path, "\n\t")))
230                         return -ENOMEM;
231                 log_debug("Looking for unit files in:\n\t%s", t);
232                 free(t);
233         } else {
234                 log_debug("Ignoring unit files.");
235                 strv_free(p->unit_path);
236                 p->unit_path = NULL;
237         }
238
239         if (running_as == MANAGER_SYSTEM) {
240 #ifdef HAVE_SYSV_COMPAT
241                 /* /etc/init.d/ compatibility does not matter to users */
242
243                 if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
244                         if (!(p->sysvinit_path = split_path_and_make_absolute(e)))
245                                 return -ENOMEM;
246
247                 if (strv_isempty(p->sysvinit_path)) {
248                         strv_free(p->sysvinit_path);
249
250                         if (!(p->sysvinit_path = strv_new(
251                                               SYSTEM_SYSVINIT_PATH,     /* /etc/init.d/ */
252                                               NULL)))
253                                 return -ENOMEM;
254                 }
255
256                 if ((e = getenv("SYSTEMD_SYSVRCND_PATH")))
257                         if (!(p->sysvrcnd_path = split_path_and_make_absolute(e)))
258                                 return -ENOMEM;
259
260                 if (strv_isempty(p->sysvrcnd_path)) {
261                         strv_free(p->sysvrcnd_path);
262
263                         if (!(p->sysvrcnd_path = strv_new(
264                                               SYSTEM_SYSVRCND_PATH,     /* /etc/rcN.d/ */
265                                               NULL)))
266                                 return -ENOMEM;
267                 }
268
269                 if (p->sysvinit_path)
270                         if (!strv_path_canonicalize(p->sysvinit_path))
271                                 return -ENOMEM;
272
273                 if (p->sysvrcnd_path)
274                         if (!strv_path_canonicalize(p->sysvrcnd_path))
275                                 return -ENOMEM;
276
277                 strv_uniq(p->sysvinit_path);
278                 strv_uniq(p->sysvrcnd_path);
279
280                 strv_path_remove_empty(p->sysvinit_path);
281                 strv_path_remove_empty(p->sysvrcnd_path);
282
283                 if (!strv_isempty(p->sysvinit_path)) {
284
285                         if (!(t = strv_join(p->sysvinit_path, "\n\t")))
286                                 return -ENOMEM;
287
288                         log_debug("Looking for SysV init scripts in:\n\t%s", t);
289                         free(t);
290                 } else {
291                         log_debug("Ignoring SysV init scripts.");
292                         strv_free(p->sysvinit_path);
293                         p->sysvinit_path = NULL;
294                 }
295
296                 if (!strv_isempty(p->sysvrcnd_path)) {
297
298                         if (!(t = strv_join(p->sysvrcnd_path, "\n\t")))
299                                 return -ENOMEM;
300
301                         log_debug("Looking for SysV rcN.d links in:\n\t%s", t);
302                         free(t);
303                 } else {
304                         log_debug("Ignoring SysV rcN.d links.");
305                         strv_free(p->sysvrcnd_path);
306                         p->sysvrcnd_path = NULL;
307                 }
308 #else
309                 log_debug("Disabled SysV init scripts and rcN.d links support");
310 #endif
311         }
312
313         return 0;
314 }
315
316 void lookup_paths_free(LookupPaths *p) {
317         assert(p);
318
319         strv_free(p->unit_path);
320         p->unit_path = NULL;
321
322 #ifdef HAVE_SYSV_COMPAT
323         strv_free(p->sysvinit_path);
324         strv_free(p->sysvrcnd_path);
325         p->sysvinit_path = p->sysvrcnd_path = NULL;
326 #endif
327 }