#include <string.h>
#include "alloc-util.h"
+#include "fileio.h"
+#include "fs-util.h"
#include "install.h"
#include "log.h"
#include "macro.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
+#include "user-util.h"
#include "util.h"
static int user_runtime_dir(char **ret, const char *suffix) {
static int user_config_dir(char **ret, const char *suffix) {
const char *e;
char *j;
+ int r;
assert(ret);
if (e)
j = strappend(e, suffix);
else {
- const char *home;
+ _cleanup_free_ char *home = NULL;
- home = getenv("HOME");
- if (!home)
- return -ENXIO;
+ r = get_home_dir(&home);
+ if (r < 0)
+ return r;
- j = strjoin(home, "/.config", suffix, NULL);
+ j = strjoin(home, "/.config", suffix);
}
if (!j)
static int user_data_dir(char **ret, const char *suffix) {
const char *e;
char *j;
+ int r;
assert(ret);
assert(suffix);
/* We don't treat /etc/xdg/systemd here as the spec
- * suggests because we assume that that is a link to
+ * suggests because we assume that is a link to
* /etc/systemd/ anyway. */
e = getenv("XDG_DATA_HOME");
if (e)
j = strappend(e, suffix);
else {
- const char *home;
-
- home = getenv("HOME");
- if (!home)
- return -ENXIO;
+ _cleanup_free_ char *home = NULL;
+ r = get_home_dir(&home);
+ if (r < 0)
+ return r;
- j = strjoin(home, "/.local/share", suffix, NULL);
+ j = strjoin(home, "/.local/share", suffix);
}
if (!j)
return -ENOMEM;
return 1;
}
+static const char* const user_data_unit_paths[] = {
+ "/usr/local/lib/systemd/user",
+ "/usr/local/share/systemd/user",
+ USER_DATA_UNIT_PATH,
+ "/usr/lib/systemd/user",
+ "/usr/share/systemd/user",
+ NULL
+};
+
+static const char* const user_config_unit_paths[] = {
+ USER_CONFIG_UNIT_PATH,
+ "/etc/systemd/user",
+ NULL
+};
+
static char** user_dirs(
const char *persistent_config,
const char *runtime_config,
const char *persistent_control,
const char *runtime_control) {
- const char * const config_unit_paths[] = {
- USER_CONFIG_UNIT_PATH,
- "/etc/systemd/user",
- NULL
- };
-
- const char * const data_unit_paths[] = {
- "/usr/local/lib/systemd/user",
- "/usr/local/share/systemd/user",
- USER_DATA_UNIT_PATH,
- "/usr/lib/systemd/user",
- "/usr/share/systemd/user",
- NULL
- };
-
- const char *e;
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
_cleanup_free_ char *data_home = NULL;
- _cleanup_free_ char **res = NULL;
+ _cleanup_strv_free_ char **res = NULL;
+ const char *e;
char **tmp;
int r;
if (strv_extend(&res, generator_early) < 0)
return NULL;
- if (!strv_isempty(config_dirs))
- if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
- return NULL;
+ if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
+ return NULL;
if (strv_extend(&res, persistent_config) < 0)
return NULL;
- if (strv_extend_strv(&res, (char**) config_unit_paths, false) < 0)
+ if (strv_extend_strv(&res, (char**) user_config_unit_paths, false) < 0)
return NULL;
if (strv_extend(&res, runtime_config) < 0)
if (strv_extend(&res, data_home) < 0)
return NULL;
- if (!strv_isempty(data_dirs))
- if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
- return NULL;
+ if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
+ return NULL;
- if (strv_extend_strv(&res, (char**) data_unit_paths, false) < 0)
+ if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0)
return NULL;
if (strv_extend(&res, generator_late) < 0)
tmp = res;
res = NULL;
+
return tmp;
}
+bool path_is_user_data_dir(const char *path) {
+ assert(path);
+
+ return strv_contains((char**) user_data_unit_paths, path);
+}
+
+bool path_is_user_config_dir(const char *path) {
+ assert(path);
+
+ return strv_contains((char**) user_config_unit_paths, path);
+}
+
static int acquire_generator_dirs(
UnitFileScope scope,
+ const char *tempdir,
char **generator,
char **generator_early,
char **generator_late) {
+ _cleanup_(rmdir_and_freep) char *t = NULL;
_cleanup_free_ char *x = NULL, *y = NULL, *z = NULL;
const char *prefix;
assert(generator);
assert(generator_early);
assert(generator_late);
+ assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL));
- switch (scope) {
+ if (scope == UNIT_FILE_GLOBAL)
+ return -EOPNOTSUPP;
- case UNIT_FILE_SYSTEM:
- prefix = "/run/systemd/";
- break;
+ if (tempdir)
+ prefix = tempdir;
- case UNIT_FILE_USER: {
+ else if (scope == UNIT_FILE_SYSTEM)
+ prefix = "/run/systemd";
+
+ else if (scope == UNIT_FILE_USER) {
const char *e;
e = getenv("XDG_RUNTIME_DIR");
if (!e)
return -ENXIO;
- prefix = strjoina(e, "/systemd/");
- break;
- }
-
- case UNIT_FILE_GLOBAL:
- return -EOPNOTSUPP;
-
- default:
- assert_not_reached("Hmm, unexpected scope value.");
+ prefix = strjoina(e, "/systemd");
}
- x = strappend(prefix, "generator");
+ x = strappend(prefix, "/generator");
if (!x)
return -ENOMEM;
- y = strappend(prefix, "generator.early");
+ y = strappend(prefix, "/generator.early");
if (!y)
return -ENOMEM;
- z = strappend(prefix, "generator.late");
+ z = strappend(prefix, "/generator.late");
if (!z)
return -ENOMEM;
return 0;
}
-static int acquire_transient_dir(UnitFileScope scope, char **ret) {
- assert(ret);
-
- switch (scope) {
+static int acquire_transient_dir(
+ UnitFileScope scope,
+ const char *tempdir,
+ char **ret) {
- case UNIT_FILE_SYSTEM: {
- char *transient;
+ char *transient;
- transient = strdup("/run/systemd/transient");
- if (!transient)
- return -ENOMEM;
+ assert(ret);
+ assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL));
- *ret = transient;
- return 0;
- }
+ if (scope == UNIT_FILE_GLOBAL)
+ return -EOPNOTSUPP;
- case UNIT_FILE_USER:
+ if (tempdir)
+ transient = strjoin(tempdir, "/transient");
+ else if (scope == UNIT_FILE_SYSTEM)
+ transient = strdup("/run/systemd/transient");
+ else
return user_runtime_dir(ret, "/systemd/transient");
- case UNIT_FILE_GLOBAL:
- return -EOPNOTSUPP;
-
- default:
- assert_not_reached("Hmm, unexpected scope value.");
- }
+ if (!transient)
+ return -ENOMEM;
+ *ret = transient;
+ return 0;
}
static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **runtime) {
case UNIT_FILE_USER:
r = user_config_dir(&a, "/systemd/user");
- if (r < 0)
+ if (r < 0 && r != -ENXIO)
return r;
r = user_runtime_dir(runtime, "/systemd/user");
- if (r < 0)
- return r;
+ if (r < 0) {
+ if (r != -ENXIO)
+ return r;
+
+ /* If XDG_RUNTIME_DIR is not set, don't consider that fatal, simply initialize the runtime
+ * directory to NULL */
+ *runtime = NULL;
+ }
*persistent = a;
a = NULL;
case UNIT_FILE_USER:
r = user_config_dir(&a, "/systemd/system.control");
- if (r < 0)
+ if (r < 0 && r != -ENXIO)
return r;
r = user_runtime_dir(runtime, "/systemd/system.control");
- if (r < 0)
- return r;
+ if (r < 0) {
+ if (r != -ENXIO)
+ return r;
+
+ /* If XDG_RUNTIME_DIR is not set, don't consider this fatal, simply initialize the directory to
+ * NULL */
+ *runtime = NULL;
+ }
break;
LookupPathsFlags flags,
const char *root_dir) {
+ _cleanup_(rmdir_and_freep) char *tempdir = NULL;
_cleanup_free_ char
*root = NULL,
*persistent_config = NULL, *runtime_config = NULL,
return -ENOMEM;
}
+ if (flags & LOOKUP_PATHS_TEMPORARY_GENERATED) {
+ r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir);
+ if (r < 0)
+ return log_error_errno(r, "Failed to create temporary directory: %m");
+ }
+
+ /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */
r = acquire_config_dirs(scope, &persistent_config, &runtime_config);
- if (r < 0 && r != -ENXIO)
+ if (r < 0)
return r;
if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) {
- r = acquire_generator_dirs(scope, &generator, &generator_early, &generator_late);
+ /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
+ r = acquire_generator_dirs(scope, tempdir,
+ &generator, &generator_early, &generator_late);
if (r < 0 && r != -EOPNOTSUPP && r != -ENXIO)
return r;
}
- r = acquire_transient_dir(scope, &transient);
+ /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
+ r = acquire_transient_dir(scope, tempdir, &transient);
if (r < 0 && r != -EOPNOTSUPP && r != -ENXIO)
return r;
+ /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_control to NULL */
r = acquire_control_dirs(scope, &persistent_control, &runtime_control);
- if (r < 0 && r != -EOPNOTSUPP && r != -ENXIO)
+ if (r < 0 && r != -EOPNOTSUPP)
return r;
/* First priority is whatever has been passed to us via env vars */
append = true;
}
- /* FIXME: empty components in other places should be
- * rejected. */
+ /* FIXME: empty components in other places should be rejected. */
r = path_split_and_make_absolute(e, &paths);
if (r < 0)
"/usr/local/lib/systemd/system",
SYSTEM_DATA_UNIT_PATH,
"/usr/lib/systemd/system",
-#ifdef HAVE_SPLIT_USR
+#if HAVE_SPLIT_USR
"/lib/systemd/system",
#endif
STRV_IFNOTNULL(generator_late),
p->root_dir = root;
root = NULL;
+ p->temporary_dir = tempdir;
+ tempdir = NULL;
+
return 0;
}
p->runtime_control = mfree(p->runtime_control);
p->root_dir = mfree(p->root_dir);
+ p->temporary_dir = mfree(p->temporary_dir);
}
int lookup_paths_reduce(LookupPaths *p) {
(void) rm_rf(p->generator_early, REMOVE_ROOT);
if (p->generator_late)
(void) rm_rf(p->generator_late, REMOVE_ROOT);
+
+ if (p->temporary_dir)
+ (void) rm_rf(p->temporary_dir, REMOVE_ROOT);
}
char **generator_binary_paths(UnitFileScope scope) {