]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/environment-d-generator/environment-d-generator.c
environment-d-generator: output logs in debug mode
[thirdparty/systemd.git] / src / environment-d-generator / environment-d-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-path.h"
4
5 #include "conf-files.h"
6 #include "def.h"
7 #include "env-file.h"
8 #include "escape.h"
9 #include "log.h"
10 #include "path-lookup.h"
11 #include "strv.h"
12
13 static int environment_dirs(char ***ret) {
14 _cleanup_strv_free_ char **dirs = NULL;
15 _cleanup_free_ char *c = NULL;
16 int r;
17
18 dirs = strv_new(CONF_PATHS_USR("environment.d"), NULL);
19 if (!dirs)
20 return -ENOMEM;
21
22 /* ~/.config/systemd/environment.d */
23 r = sd_path_home(SD_PATH_USER_CONFIGURATION, "environment.d", &c);
24 if (r < 0)
25 return r;
26
27 r = strv_extend_front(&dirs, c);
28 if (r < 0)
29 return r;
30
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
38 *ret = TAKE_PTR(dirs);
39 return 0;
40 }
41
42 static 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
51 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs);
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) {
59 log_debug("Reading %s…", *i);
60
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
73 q = shell_maybe_quote(t + 1, ESCAPE_BACKSLASH);
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
83 int 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 }