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) {
57 char *config_home = NULL, *data_home = NULL;
58 char **config_dirs = NULL, **data_dirs = NULL;
61 /* Implement the mechanisms defined in
63 * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
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.
70 if (user_config_home(&config_home) < 0)
73 home = getenv("HOME");
75 if ((e = getenv("XDG_CONFIG_DIRS")))
76 if (!(config_dirs = strv_split(e, ":")))
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. */
83 if ((e = getenv("XDG_DATA_HOME"))) {
84 if (asprintf(&data_home, "%s/systemd/user", e) < 0)
88 if (asprintf(&data_home, "%s/.local/share/systemd/user", home) < 0)
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
98 mkdir_parents(data_home, 0777);
99 (void) symlink("../../../.config/systemd/user", data_home);
102 if ((e = getenv("XDG_DATA_DIRS")))
103 data_dirs = strv_split(e, ":");
105 data_dirs = strv_new("/usr/local/share",
114 /* Now merge everything we found. */
116 if (!(t = strv_append(r, config_home)))
122 if (!(t = strv_merge_concat(r, config_dirs, "/systemd/user")))
127 if (!(t = strv_append(r, USER_CONFIG_UNIT_PATH)))
133 if (!(t = strv_append(r, data_home)))
139 if (!(t = strv_merge_concat(r, data_dirs, "/systemd/user")))
144 if (!(t = strv_append(r, USER_DATA_UNIT_PATH)))
149 if (!strv_path_make_absolute_cwd(r))
154 strv_free(config_dirs);
156 strv_free(data_dirs);
166 int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as) {
172 /* First priority is whatever has been passed to us via env
174 if ((e = getenv("SYSTEMD_UNIT_PATH")))
175 if (!(p->unit_path = split_path_and_make_absolute(e)))
178 if (strv_isempty(p->unit_path)) {
180 /* Nothing is set, so let's figure something out. */
181 strv_free(p->unit_path);
183 if (running_as == MANAGER_USER) {
184 if (!(p->unit_path = user_dirs()))
187 if (!(p->unit_path = strv_new(
188 /* If you modify this you also want to modify
189 * systemdsystemunitpath= in systemd.pc.in! */
190 "/run/systemd/system",
191 SYSTEM_CONFIG_UNIT_PATH,
192 "/etc/systemd/system",
193 "/usr/local/share/systemd/system",
194 "/usr/local/lib/systemd/system",
195 "/usr/share/systemd/system",
196 "/usr/lib/systemd/system",
197 "/lib/systemd/system",
198 SYSTEM_DATA_UNIT_PATH,
204 if (!strv_path_canonicalize(p->unit_path))
207 strv_uniq(p->unit_path);
208 strv_path_remove_empty(p->unit_path);
210 if (!strv_isempty(p->unit_path)) {
212 if (!(t = strv_join(p->unit_path, "\n\t")))
214 log_debug("Looking for unit files in:\n\t%s", t);
217 log_debug("Ignoring unit files.");
218 strv_free(p->unit_path);
222 if (running_as == MANAGER_SYSTEM) {
223 #ifdef HAVE_SYSV_COMPAT
224 /* /etc/init.d/ compatibility does not matter to users */
226 if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
227 if (!(p->sysvinit_path = split_path_and_make_absolute(e)))
230 if (strv_isempty(p->sysvinit_path)) {
231 strv_free(p->sysvinit_path);
233 if (!(p->sysvinit_path = strv_new(
234 SYSTEM_SYSVINIT_PATH, /* /etc/init.d/ */
239 if ((e = getenv("SYSTEMD_SYSVRCND_PATH")))
240 if (!(p->sysvrcnd_path = split_path_and_make_absolute(e)))
243 if (strv_isempty(p->sysvrcnd_path)) {
244 strv_free(p->sysvrcnd_path);
246 if (!(p->sysvrcnd_path = strv_new(
247 SYSTEM_SYSVRCND_PATH, /* /etc/rcN.d/ */
252 if (p->sysvinit_path)
253 if (!strv_path_canonicalize(p->sysvinit_path))
256 if (p->sysvrcnd_path)
257 if (!strv_path_canonicalize(p->sysvrcnd_path))
260 strv_uniq(p->sysvinit_path);
261 strv_uniq(p->sysvrcnd_path);
263 strv_path_remove_empty(p->sysvinit_path);
264 strv_path_remove_empty(p->sysvrcnd_path);
266 if (!strv_isempty(p->sysvinit_path)) {
268 if (!(t = strv_join(p->sysvinit_path, "\n\t")))
271 log_debug("Looking for SysV init scripts in:\n\t%s", t);
274 log_debug("Ignoring SysV init scripts.");
275 strv_free(p->sysvinit_path);
276 p->sysvinit_path = NULL;
279 if (!strv_isempty(p->sysvrcnd_path)) {
281 if (!(t = strv_join(p->sysvrcnd_path, "\n\t")))
284 log_debug("Looking for SysV rcN.d links in:\n\t%s", t);
287 log_debug("Ignoring SysV rcN.d links.");
288 strv_free(p->sysvrcnd_path);
289 p->sysvrcnd_path = NULL;
292 log_debug("Disabled SysV init scripts and rcN.d links support");
299 void lookup_paths_free(LookupPaths *p) {
302 strv_free(p->unit_path);
305 #ifdef HAVE_SYSV_COMPAT
306 strv_free(p->sysvinit_path);
307 strv_free(p->sysvrcnd_path);
308 p->sysvinit_path = p->sysvrcnd_path = NULL;