]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/environment-d-generator/environment-d-generator.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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
12 static int environment_dirs(char ***ret) {
13 _cleanup_strv_free_ char **dirs = NULL;
14 _cleanup_free_ char *c = NULL;
15 int r;
16
17 dirs = strv_new(CONF_PATHS_USR("environment.d"), NULL);
18 if (!dirs)
19 return -ENOMEM;
20
21 /* ~/.config/systemd/environment.d */
22 r = sd_path_home(SD_PATH_USER_CONFIGURATION, "environment.d", &c);
23 if (r < 0)
24 return r;
25
26 r = strv_extend_front(&dirs, c);
27 if (r < 0)
28 return r;
29
30 *ret = TAKE_PTR(dirs);
31 return 0;
32 }
33
34 static int load_and_print(void) {
35 _cleanup_strv_free_ char **dirs = NULL, **files = NULL, **env = NULL;
36 char **i;
37 int r;
38
39 r = environment_dirs(&dirs);
40 if (r < 0)
41 return r;
42
43 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs);
44 if (r < 0)
45 return r;
46
47 /* This will mutate the existing environment, based on the presumption
48 * that in case of failure, a partial update is better than none. */
49
50 STRV_FOREACH(i, files) {
51 r = merge_env_file(&env, NULL, *i);
52 if (r == -ENOMEM)
53 return r;
54 }
55
56 STRV_FOREACH(i, env) {
57 char *t;
58 _cleanup_free_ char *q = NULL;
59
60 t = strchr(*i, '=');
61 assert(t);
62
63 q = shell_maybe_quote(t + 1, ESCAPE_BACKSLASH);
64 if (!q)
65 return log_oom();
66
67 printf("%.*s=%s\n", (int) (t - *i), *i, q);
68 }
69
70 return 0;
71 }
72
73 int main(int argc, char *argv[]) {
74 int r;
75
76 log_parse_environment();
77 log_open();
78
79 if (argc > 1) {
80 log_error("This program takes no arguments.");
81 return EXIT_FAILURE;
82 }
83
84 r = load_and_print();
85 if (r < 0)
86 log_error_errno(r, "Failed to load environment.d: %m");
87
88 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
89 }