]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/environment-d-generator/environment-d-generator.c
service: add new RootImageOptions feature
[thirdparty/systemd.git] / src / environment-d-generator / environment-d-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f63c4aab
ZJS
2
3#include "sd-path.h"
4
5#include "conf-files.h"
6#include "def.h"
686d13b9 7#include "env-file.h"
f63c4aab 8#include "escape.h"
f63c4aab
ZJS
9#include "log.h"
10#include "path-lookup.h"
5cfa33e0 11#include "strv.h"
f63c4aab
ZJS
12
13static int environment_dirs(char ***ret) {
14 _cleanup_strv_free_ char **dirs = NULL;
15 _cleanup_free_ char *c = NULL;
16 int r;
17
3dd84d46 18 dirs = strv_new(CONF_PATHS_USR("environment.d"), NULL);
f63c4aab
ZJS
19 if (!dirs)
20 return -ENOMEM;
21
22 /* ~/.config/systemd/environment.d */
51327bcc 23 r = sd_path_lookup(SD_PATH_USER_CONFIGURATION, "environment.d", &c);
f63c4aab
ZJS
24 if (r < 0)
25 return r;
26
27 r = strv_extend_front(&dirs, c);
28 if (r < 0)
29 return r;
30
f6301bdc
ZJS
31 if (DEBUG_LOGGING) {
32 _cleanup_free_ char *t;
33
34 t = strv_join(dirs, "\n\t");
35 log_debug("Looking for environment.d files in (higher priority first):\n\t%s", strna(t));
36 }
37
ae2a15bc 38 *ret = TAKE_PTR(dirs);
f63c4aab
ZJS
39 return 0;
40}
41
42static int load_and_print(void) {
43 _cleanup_strv_free_ char **dirs = NULL, **files = NULL, **env = NULL;
44 char **i;
45 int r;
46
47 r = environment_dirs(&dirs);
48 if (r < 0)
49 return r;
50
b5084605 51 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs);
f63c4aab
ZJS
52 if (r < 0)
53 return r;
54
55 /* This will mutate the existing environment, based on the presumption
56 * that in case of failure, a partial update is better than none. */
57
58 STRV_FOREACH(i, files) {
f6301bdc
ZJS
59 log_debug("Reading %s…", *i);
60
f63c4aab
ZJS
61 r = merge_env_file(&env, NULL, *i);
62 if (r == -ENOMEM)
63 return r;
64 }
65
66 STRV_FOREACH(i, env) {
67 char *t;
68 _cleanup_free_ char *q = NULL;
69
70 t = strchr(*i, '=');
71 assert(t);
72
804ee07c 73 q = shell_maybe_quote(t + 1, ESCAPE_BACKSLASH);
f63c4aab
ZJS
74 if (!q)
75 return log_oom();
76
77 printf("%.*s=%s\n", (int) (t - *i), *i, q);
78 }
79
80 return 0;
81}
82
83int main(int argc, char *argv[]) {
84 int r;
85
86 log_parse_environment();
87 log_open();
88
89 if (argc > 1) {
90 log_error("This program takes no arguments.");
91 return EXIT_FAILURE;
92 }
93
94 r = load_and_print();
95 if (r < 0)
96 log_error_errno(r, "Failed to load environment.d: %m");
97
98 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
99}