1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
31 #include "path-lookup.h"
33 int user_config_home(char **config_home
) {
36 if ((e
= getenv("XDG_CONFIG_HOME"))) {
37 if (asprintf(config_home
, "%s/systemd/user", e
) < 0)
44 if ((home
= getenv("HOME"))) {
45 if (asprintf(config_home
, "%s/.config/systemd/user", home
) < 0)
55 static char** user_dirs(void) {
56 const char * const config_unit_paths
[] = {
58 USER_CONFIG_UNIT_PATH
,
63 const char * const data_unit_paths
[] = {
64 "/usr/local/lib/systemd/user",
65 "/usr/local/share/systemd/user",
67 "/usr/lib/systemd/user",
68 "/usr/share/systemd/user",
73 char *config_home
= NULL
, *data_home
= NULL
;
74 char **config_dirs
= NULL
, **data_dirs
= NULL
;
77 /* Implement the mechanisms defined in
79 * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
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.
86 if (user_config_home(&config_home
) < 0)
89 home
= getenv("HOME");
91 if ((e
= getenv("XDG_CONFIG_DIRS")))
92 if (!(config_dirs
= strv_split(e
, ":")))
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. */
99 if ((e
= getenv("XDG_DATA_HOME"))) {
100 if (asprintf(&data_home
, "%s/systemd/user", e
) < 0)
104 if (asprintf(&data_home
, "%s/.local/share/systemd/user", home
) < 0)
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
114 mkdir_parents(data_home
, 0777);
115 (void) symlink("../../../.config/systemd/user", data_home
);
118 if ((e
= getenv("XDG_DATA_DIRS")))
119 data_dirs
= strv_split(e
, ":");
121 data_dirs
= strv_new("/usr/local/share",
128 /* Now merge everything we found. */
130 if (!(t
= strv_append(r
, config_home
)))
136 if (!strv_isempty(config_dirs
)) {
137 if (!(t
= strv_merge_concat(r
, config_dirs
, "/systemd/user")))
143 if (!(t
= strv_merge(r
, (char**) config_unit_paths
)))
149 if (!(t
= strv_append(r
, data_home
)))
155 if (!strv_isempty(data_dirs
)) {
156 if (!(t
= strv_merge_concat(r
, data_dirs
, "/systemd/user")))
162 if (!(t
= strv_merge(r
, (char**) data_unit_paths
)))
167 if (!strv_path_make_absolute_cwd(r
))
172 strv_free(config_dirs
);
174 strv_free(data_dirs
);
184 int lookup_paths_init(LookupPaths
*p
, ManagerRunningAs running_as
, bool personal
) {
190 /* First priority is whatever has been passed to us via env
192 if ((e
= getenv("SYSTEMD_UNIT_PATH")))
193 if (!(p
->unit_path
= split_path_and_make_absolute(e
)))
196 if (strv_isempty(p
->unit_path
)) {
198 /* Nothing is set, so let's figure something out. */
199 strv_free(p
->unit_path
);
201 if (running_as
== MANAGER_USER
) {
204 p
->unit_path
= user_dirs();
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! */
211 USER_CONFIG_UNIT_PATH
,
213 "/usr/local/lib/systemd/user",
214 "/usr/local/share/systemd/user",
216 "/usr/lib/systemd/user",
217 "/usr/share/systemd/user",
224 if (!(p
->unit_path
= strv_new(
225 /* If you modify this you also want to modify
226 * systemdsystemunitpath= in systemd.pc.in! */
227 "/run/systemd/system",
228 SYSTEM_CONFIG_UNIT_PATH
,
229 "/etc/systemd/system",
230 "/usr/local/lib/systemd/system",
231 "/usr/lib/systemd/system",
232 SYSTEM_DATA_UNIT_PATH
,
233 "/lib/systemd/system",
239 if (!strv_path_canonicalize(p
->unit_path
))
242 strv_uniq(p
->unit_path
);
243 strv_path_remove_empty(p
->unit_path
);
245 if (!strv_isempty(p
->unit_path
)) {
247 if (!(t
= strv_join(p
->unit_path
, "\n\t")))
249 log_debug("Looking for unit files in:\n\t%s", t
);
252 log_debug("Ignoring unit files.");
253 strv_free(p
->unit_path
);
257 if (running_as
== MANAGER_SYSTEM
) {
258 #ifdef HAVE_SYSV_COMPAT
259 /* /etc/init.d/ compatibility does not matter to users */
261 if ((e
= getenv("SYSTEMD_SYSVINIT_PATH")))
262 if (!(p
->sysvinit_path
= split_path_and_make_absolute(e
)))
265 if (strv_isempty(p
->sysvinit_path
)) {
266 strv_free(p
->sysvinit_path
);
268 if (!(p
->sysvinit_path
= strv_new(
269 SYSTEM_SYSVINIT_PATH
, /* /etc/init.d/ */
274 if ((e
= getenv("SYSTEMD_SYSVRCND_PATH")))
275 if (!(p
->sysvrcnd_path
= split_path_and_make_absolute(e
)))
278 if (strv_isempty(p
->sysvrcnd_path
)) {
279 strv_free(p
->sysvrcnd_path
);
281 if (!(p
->sysvrcnd_path
= strv_new(
282 SYSTEM_SYSVRCND_PATH
, /* /etc/rcN.d/ */
287 if (p
->sysvinit_path
)
288 if (!strv_path_canonicalize(p
->sysvinit_path
))
291 if (p
->sysvrcnd_path
)
292 if (!strv_path_canonicalize(p
->sysvrcnd_path
))
295 strv_uniq(p
->sysvinit_path
);
296 strv_uniq(p
->sysvrcnd_path
);
298 strv_path_remove_empty(p
->sysvinit_path
);
299 strv_path_remove_empty(p
->sysvrcnd_path
);
301 if (!strv_isempty(p
->sysvinit_path
)) {
303 if (!(t
= strv_join(p
->sysvinit_path
, "\n\t")))
306 log_debug("Looking for SysV init scripts in:\n\t%s", t
);
309 log_debug("Ignoring SysV init scripts.");
310 strv_free(p
->sysvinit_path
);
311 p
->sysvinit_path
= NULL
;
314 if (!strv_isempty(p
->sysvrcnd_path
)) {
316 if (!(t
= strv_join(p
->sysvrcnd_path
, "\n\t")))
319 log_debug("Looking for SysV rcN.d links in:\n\t%s", t
);
322 log_debug("Ignoring SysV rcN.d links.");
323 strv_free(p
->sysvrcnd_path
);
324 p
->sysvrcnd_path
= NULL
;
327 log_debug("Disabled SysV init scripts and rcN.d links support");
334 void lookup_paths_free(LookupPaths
*p
) {
337 strv_free(p
->unit_path
);
340 #ifdef HAVE_SYSV_COMPAT
341 strv_free(p
->sysvinit_path
);
342 strv_free(p
->sysvrcnd_path
);
343 p
->sysvinit_path
= p
->sysvrcnd_path
= NULL
;