]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/path-lookup.c
drop support for MANAGER_SESSION, introduce MANAGER_USER instead
[thirdparty/systemd.git] / src / path-lookup.c
CommitLineData
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 33int 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 55static char** user_dirs(void) {
84e3543e
LP
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
af2d49f7 70 if (user_config_home(&config_home) < 0)
10e87ee7 71 goto fail;
84e3543e 72
10e87ee7 73 home = getenv("HOME");
84e3543e
LP
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"))) {
af2d49f7 84 if (asprintf(&data_home, "%s/systemd/user", e) < 0)
84e3543e
LP
85 goto fail;
86
87 } else if (home) {
af2d49f7 88 if (asprintf(&data_home, "%s/.local/share/systemd/user", home) < 0)
84e3543e
LP
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);
af2d49f7 99 (void) symlink("../../../.config/systemd/user", data_home);
84e3543e
LP
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", "/usr/share", NULL);
106
107 if (!data_dirs)
108 goto fail;
109
110 /* Now merge everything we found. */
111 if (config_home) {
112 if (!(t = strv_append(r, config_home)))
113 goto fail;
114 strv_free(r);
115 r = t;
116 }
117
af2d49f7 118 if (!(t = strv_merge_concat(r, config_dirs, "/systemd/user")))
84e3543e
LP
119 goto finish;
120 strv_free(r);
121 r = t;
122
af2d49f7 123 if (!(t = strv_append(r, USER_CONFIG_UNIT_PATH)))
84e3543e
LP
124 goto fail;
125 strv_free(r);
126 r = t;
127
128 if (data_home) {
129 if (!(t = strv_append(r, data_home)))
130 goto fail;
131 strv_free(r);
132 r = t;
133 }
134
af2d49f7 135 if (!(t = strv_merge_concat(r, data_dirs, "/systemd/user")))
84e3543e
LP
136 goto fail;
137 strv_free(r);
138 r = t;
139
af2d49f7 140 if (!(t = strv_append(r, USER_DATA_UNIT_PATH)))
84e3543e
LP
141 goto fail;
142 strv_free(r);
143 r = t;
144
145 if (!strv_path_make_absolute_cwd(r))
146 goto fail;
147
148finish:
149 free(config_home);
150 strv_free(config_dirs);
151 free(data_home);
152 strv_free(data_dirs);
153
154 return r;
155
156fail:
157 strv_free(r);
158 r = NULL;
159 goto finish;
160}
161
162int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as) {
163 const char *e;
164 char *t;
165
166 assert(p);
167
168 /* First priority is whatever has been passed to us via env
169 * vars */
170 if ((e = getenv("SYSTEMD_UNIT_PATH")))
171 if (!(p->unit_path = split_path_and_make_absolute(e)))
172 return -ENOMEM;
173
174 if (strv_isempty(p->unit_path)) {
175
176 /* Nothing is set, so let's figure something out. */
177 strv_free(p->unit_path);
178
af2d49f7
LP
179 if (running_as == MANAGER_USER) {
180 if (!(p->unit_path = user_dirs()))
84e3543e
LP
181 return -ENOMEM;
182 } else
183 if (!(p->unit_path = strv_new(
37072578 184 "/dev/.systemd/system",
c0115b1f
LP
185 SYSTEM_CONFIG_UNIT_PATH,
186 "/etc/systemd/system",
bf9a6e8b
LP
187 "/usr/local/share/systemd/system",
188 "/usr/share/systemd/system",
c0115b1f
LP
189 "/lib/systemd/system",
190 SYSTEM_DATA_UNIT_PATH,
84e3543e
LP
191 NULL)))
192 return -ENOMEM;
193 }
194
07459bb6
FF
195 if (p->unit_path)
196 if (!strv_path_canonicalize(p->unit_path))
197 return -ENOMEM;
198
199 strv_uniq(p->unit_path);
200
201 if (!strv_isempty(p->unit_path)) {
202
203 if (!(t = strv_join(p->unit_path, "\n\t")))
204 return -ENOMEM;
205 log_debug("Looking for unit files in:\n\t%s", t);
206 free(t);
207 } else {
208 log_debug("Ignoring unit files.");
209 strv_free(p->unit_path);
210 p->unit_path = NULL;
211 }
212
a3d4e06d 213 if (running_as == MANAGER_SYSTEM) {
07459bb6 214#ifdef HAVE_SYSV_COMPAT
84e3543e
LP
215 /* /etc/init.d/ compatibility does not matter to users */
216
217 if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
218 if (!(p->sysvinit_path = split_path_and_make_absolute(e)))
219 return -ENOMEM;
220
221 if (strv_isempty(p->sysvinit_path)) {
222 strv_free(p->sysvinit_path);
223
224 if (!(p->sysvinit_path = strv_new(
225 SYSTEM_SYSVINIT_PATH, /* /etc/init.d/ */
226 NULL)))
227 return -ENOMEM;
228 }
229
230 if ((e = getenv("SYSTEMD_SYSVRCND_PATH")))
231 if (!(p->sysvrcnd_path = split_path_and_make_absolute(e)))
232 return -ENOMEM;
233
234 if (strv_isempty(p->sysvrcnd_path)) {
235 strv_free(p->sysvrcnd_path);
236
237 if (!(p->sysvrcnd_path = strv_new(
238 SYSTEM_SYSVRCND_PATH, /* /etc/rcN.d/ */
239 NULL)))
240 return -ENOMEM;
241 }
84e3543e 242
07459bb6
FF
243 if (p->sysvinit_path)
244 if (!strv_path_canonicalize(p->sysvinit_path))
245 return -ENOMEM;
84e3543e 246
07459bb6
FF
247 if (p->sysvrcnd_path)
248 if (!strv_path_canonicalize(p->sysvrcnd_path))
249 return -ENOMEM;
84e3543e 250
07459bb6
FF
251 strv_uniq(p->sysvinit_path);
252 strv_uniq(p->sysvrcnd_path);
84e3543e 253
07459bb6 254 if (!strv_isempty(p->sysvinit_path)) {
84e3543e 255
07459bb6
FF
256 if (!(t = strv_join(p->sysvinit_path, "\n\t")))
257 return -ENOMEM;
84e3543e 258
07459bb6
FF
259 log_debug("Looking for SysV init scripts in:\n\t%s", t);
260 free(t);
261 } else {
262 log_debug("Ignoring SysV init scripts.");
263 strv_free(p->sysvinit_path);
264 p->sysvinit_path = NULL;
265 }
84e3543e 266
07459bb6 267 if (!strv_isempty(p->sysvrcnd_path)) {
84e3543e 268
07459bb6
FF
269 if (!(t = strv_join(p->sysvrcnd_path, "\n\t")))
270 return -ENOMEM;
84e3543e 271
07459bb6
FF
272 log_debug("Looking for SysV rcN.d links in:\n\t%s", t);
273 free(t);
274 } else {
275 log_debug("Ignoring SysV rcN.d links.");
276 strv_free(p->sysvrcnd_path);
277 p->sysvrcnd_path = NULL;
278 }
279#else
280 log_debug("Disabled SysV init scripts and rcN.d links support");
281#endif
84e3543e
LP
282 }
283
284 return 0;
285}
286
287void lookup_paths_free(LookupPaths *p) {
288 assert(p);
289
290 strv_free(p->unit_path);
07459bb6
FF
291 p->unit_path = NULL;
292
293#ifdef HAVE_SYSV_COMPAT
84e3543e
LP
294 strv_free(p->sysvinit_path);
295 strv_free(p->sysvrcnd_path);
07459bb6
FF
296 p->sysvinit_path = p->sysvrcnd_path = NULL;
297#endif
84e3543e 298}