1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 static char** session_dirs(void) {
35 char *config_home = NULL, *data_home = NULL;
36 char **config_dirs = NULL, **data_dirs = NULL;
39 /* Implement the mechanisms defined in
41 * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
43 * We look in both the config and the data dirs because we
44 * want to encourage that distributors ship their unit files
45 * as data, and allow overriding as configuration.
48 home = getenv("HOME");
50 if ((e = getenv("XDG_CONFIG_HOME"))) {
51 if (asprintf(&config_home, "%s/systemd/session", e) < 0)
55 if (asprintf(&config_home, "%s/.config/systemd/session", home) < 0)
59 if ((e = getenv("XDG_CONFIG_DIRS")))
60 if (!(config_dirs = strv_split(e, ":")))
63 /* We don't treat /etc/xdg/systemd here as the spec
64 * suggests because we assume that that is a link to
65 * /etc/systemd/ anyway. */
67 if ((e = getenv("XDG_DATA_HOME"))) {
68 if (asprintf(&data_home, "%s/systemd/session", e) < 0)
72 if (asprintf(&data_home, "%s/.local/share/systemd/session", home) < 0)
75 /* There is really no need for two unit dirs in $HOME,
76 * except to be fully compliant with the XDG spec. We
77 * now try to link the two dirs, so that we can
78 * minimize disk seeks a little. Further down we'll
79 * then filter out this link, if it is actually is
82 mkdir_parents(data_home, 0777);
83 symlink("../../../.config/systemd/session", data_home);
86 if ((e = getenv("XDG_DATA_DIRS")))
87 data_dirs = strv_split(e, ":");
89 data_dirs = strv_new("/usr/local/share", "/usr/share", NULL);
94 /* Now merge everything we found. */
96 if (!(t = strv_append(r, config_home)))
102 if (!(t = strv_merge_concat(r, config_dirs, "/systemd/session")))
107 if (!(t = strv_append(r, SESSION_CONFIG_UNIT_PATH)))
113 if (!(t = strv_append(r, data_home)))
119 if (!(t = strv_merge_concat(r, data_dirs, "/systemd/session")))
124 if (!(t = strv_append(r, SESSION_DATA_UNIT_PATH)))
129 if (!strv_path_make_absolute_cwd(r))
134 strv_free(config_dirs);
136 strv_free(data_dirs);
146 int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as) {
152 /* First priority is whatever has been passed to us via env
154 if ((e = getenv("SYSTEMD_UNIT_PATH")))
155 if (!(p->unit_path = split_path_and_make_absolute(e)))
158 if (strv_isempty(p->unit_path)) {
160 /* Nothing is set, so let's figure something out. */
161 strv_free(p->unit_path);
163 if (running_as == MANAGER_SESSION) {
164 if (!(p->unit_path = session_dirs()))
167 if (!(p->unit_path = strv_new(
168 SYSTEM_CONFIG_UNIT_PATH, /* /etc/systemd/system/ */
169 SYSTEM_DATA_UNIT_PATH, /* /lib/systemd/system/ */
174 if (running_as == MANAGER_INIT) {
175 /* /etc/init.d/ compatibility does not matter to users */
177 if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
178 if (!(p->sysvinit_path = split_path_and_make_absolute(e)))
181 if (strv_isempty(p->sysvinit_path)) {
182 strv_free(p->sysvinit_path);
184 if (!(p->sysvinit_path = strv_new(
185 SYSTEM_SYSVINIT_PATH, /* /etc/init.d/ */
190 if ((e = getenv("SYSTEMD_SYSVRCND_PATH")))
191 if (!(p->sysvrcnd_path = split_path_and_make_absolute(e)))
194 if (strv_isempty(p->sysvrcnd_path)) {
195 strv_free(p->sysvrcnd_path);
197 if (!(p->sysvrcnd_path = strv_new(
198 SYSTEM_SYSVRCND_PATH, /* /etc/rcN.d/ */
205 if (!strv_path_canonicalize(p->unit_path))
208 if (p->sysvinit_path)
209 if (!strv_path_canonicalize(p->sysvinit_path))
212 if (p->sysvrcnd_path)
213 if (!strv_path_canonicalize(p->sysvrcnd_path))
216 strv_uniq(p->unit_path);
217 strv_uniq(p->sysvinit_path);
218 strv_uniq(p->sysvrcnd_path);
220 if (!strv_isempty(p->unit_path)) {
222 if (!(t = strv_join(p->unit_path, "\n\t")))
224 log_debug("Looking for unit files in:\n\t%s", t);
227 log_debug("Ignoring unit files.");
228 strv_free(p->unit_path);
232 if (!strv_isempty(p->sysvinit_path)) {
234 if (!(t = strv_join(p->sysvinit_path, "\n\t")))
237 log_debug("Looking for SysV init scripts in:\n\t%s", t);
240 log_debug("Ignoring SysV init scripts.");
241 strv_free(p->sysvinit_path);
242 p->sysvinit_path = NULL;
245 if (!strv_isempty(p->sysvrcnd_path)) {
247 if (!(t = strv_join(p->sysvrcnd_path, "\n\t")))
250 log_debug("Looking for SysV rcN.d links in:\n\t%s", t);
253 log_debug("Ignoring SysV rcN.d links.");
254 strv_free(p->sysvrcnd_path);
255 p->sysvrcnd_path = NULL;
261 void lookup_paths_free(LookupPaths *p) {
264 strv_free(p->unit_path);
265 strv_free(p->sysvinit_path);
266 strv_free(p->sysvrcnd_path);
268 p->unit_path = p->sysvinit_path = p->sysvrcnd_path = NULL;