2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2017-02-11 17:32:17 +01:00
|
|
|
|
|
|
|
#include "sd-path.h"
|
|
|
|
|
|
|
|
#include "conf-files.h"
|
|
|
|
#include "def.h"
|
2018-11-30 22:08:41 +01:00
|
|
|
#include "env-file.h"
|
2017-02-11 17:32:17 +01:00
|
|
|
#include "escape.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "path-lookup.h"
|
2019-07-04 15:46:16 +02:00
|
|
|
#include "strv.h"
|
2017-02-11 17:32:17 +01:00
|
|
|
|
|
|
|
static int environment_dirs(char ***ret) {
|
|
|
|
_cleanup_strv_free_ char **dirs = NULL;
|
|
|
|
_cleanup_free_ char *c = NULL;
|
|
|
|
int r;
|
|
|
|
|
2019-01-02 17:34:55 +01:00
|
|
|
dirs = strv_new(CONF_PATHS_USR("environment.d"), NULL);
|
2017-02-11 17:32:17 +01:00
|
|
|
if (!dirs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* ~/.config/systemd/environment.d */
|
2020-03-23 19:50:59 +01:00
|
|
|
r = sd_path_lookup(SD_PATH_USER_CONFIGURATION, "environment.d", &c);
|
2017-02-11 17:32:17 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = strv_extend_front(&dirs, c);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-08-26 22:53:01 +02:00
|
|
|
if (DEBUG_LOGGING) {
|
|
|
|
_cleanup_free_ char *t;
|
|
|
|
|
|
|
|
t = strv_join(dirs, "\n\t");
|
|
|
|
log_debug("Looking for environment.d files in (higher priority first):\n\t%s", strna(t));
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*ret = TAKE_PTR(dirs);
|
2017-02-11 17:32:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_and_print(void) {
|
|
|
|
_cleanup_strv_free_ char **dirs = NULL, **files = NULL, **env = NULL;
|
|
|
|
char **i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = environment_dirs(&dirs);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs);
|
2017-02-11 17:32:17 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* This will mutate the existing environment, based on the presumption
|
|
|
|
* that in case of failure, a partial update is better than none. */
|
|
|
|
|
|
|
|
STRV_FOREACH(i, files) {
|
2019-08-26 22:53:01 +02:00
|
|
|
log_debug("Reading %s…", *i);
|
|
|
|
|
2017-02-11 17:32:17 +01:00
|
|
|
r = merge_env_file(&env, NULL, *i);
|
|
|
|
if (r == -ENOMEM)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
STRV_FOREACH(i, env) {
|
|
|
|
char *t;
|
|
|
|
_cleanup_free_ char *q = NULL;
|
|
|
|
|
|
|
|
t = strchr(*i, '=');
|
|
|
|
assert(t);
|
|
|
|
|
2017-06-11 21:24:07 +02:00
|
|
|
q = shell_maybe_quote(t + 1, ESCAPE_BACKSLASH);
|
2017-02-11 17:32:17 +01:00
|
|
|
if (!q)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
printf("%.*s=%s\n", (int) (t - *i), *i, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
log_error("This program takes no arguments.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = load_and_print();
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to load environment.d: %m");
|
|
|
|
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|